






'''simple docstring'''


from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


lowerCamelCase_					:			Optional[int]     						=				logging.get_logger(__name__)
class  _SCREAMING_SNAKE_CASE				(		SCREAMING_SNAKE_CASE						):


			'''simple docstring'''





			__a				:				List[Any]         =       "mt5"
			__a				:				Tuple         =       ["past_key_values"]






			def __init__( self				:    Any			,   lowercase				:    Tuple=2_5_0_1_1_2			,   lowercase				:    Any=5_1_2			,   lowercase				:    Optional[Any]=6_4			,   lowercase				:    Optional[int]=1_0_2_4			,   lowercase				:    int=8			,   lowercase				:    Any=None			,   lowercase				:    List[Any]=6			,   lowercase				:    Tuple=3_2			,   lowercase				:    Union[str, Any]=1_2_8			,   lowercase				:    List[str]=0.1			,   lowercase				:    Dict=1e-6			,   lowercase				:    Any=1.0			,   lowercase				:    List[Any]="gated-gelu"			,   lowercase				:    Any=True			,   lowercase				:    Optional[int]=True			,   lowercase				:    Optional[Any]="T5Tokenizer"			,   lowercase				:    Dict=False			,   lowercase				:    Union[str, Any]=0			,   lowercase				:    str=1			,   lowercase				:    Optional[int]=0			,   **lowercase				:    List[str]			,   )				->     Dict:

									'''simple docstring'''







									super().__init__(
									    is_encoder_decoder=lowercase			,   tokenizer_class=lowercase			,   tie_word_embeddings=lowercase			,   pad_token_id=lowercase			,   eos_token_id=lowercase			,   decoder_start_token_id=lowercase			,   **lowercase			,   )
									UpperCamelCase__       =			vocab_size
									UpperCamelCase__       =			d_model
									UpperCamelCase__       =			d_kv
									UpperCamelCase__       =			d_ff
									UpperCamelCase__       =			num_layers
									UpperCamelCase__       =			(
									    num_decoder_layers if num_decoder_layers is not None else self.num_layers
									)  # default = symmetry
									UpperCamelCase__       =			num_heads
									UpperCamelCase__       =			relative_attention_num_buckets
									UpperCamelCase__       =			relative_attention_max_distance
									UpperCamelCase__       =			dropout_rate
									UpperCamelCase__       =			layer_norm_epsilon
									UpperCamelCase__       =			initializer_factor
									UpperCamelCase__       =			feed_forward_proj
									UpperCamelCase__       =			use_cache

									UpperCamelCase__       =			self.feed_forward_proj.split("""-"""      )
									UpperCamelCase__       =			act_info[-1]
									UpperCamelCase__       =			act_info[0] == """gated"""

									if len(lowercase      ) > 1 and act_info[0] != "gated" or len(lowercase      ) > 2:
															raise ValueError(
															    f"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."
															    """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """
															    """'gated-gelu' or 'relu'"""      )

									# for backwards compatibility
									if feed_forward_proj == "gated-gelu":
															UpperCamelCase__       =			"""gelu_new"""






			@property
			def 					A   ( self				:    Dict      )				->     List[Any]:

									'''simple docstring'''







									return self.d_model






			@property
			def 					A   ( self				:    List[str]      )				->     Union[str, Any]:

									'''simple docstring'''







									return self.num_heads







			@property
			def 					A   ( self				:    Optional[Any]      )				->     List[str]:

									'''simple docstring'''







									return self.num_layers
class  _SCREAMING_SNAKE_CASE				(		SCREAMING_SNAKE_CASE						):


			'''simple docstring'''





			@property
			# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
			def 					A   ( self				:    Any      )				->     Mapping[str, Mapping[int, str]]:

									'''simple docstring'''







									UpperCamelCase__       =			{
									    """input_ids""": {0: """batch""", 1: """encoder_sequence"""},
									    """attention_mask""": {0: """batch""", 1: """encoder_sequence"""},
									}
									if self.use_past:
															UpperCamelCase__       =			"""past_encoder_sequence + sequence"""
															UpperCamelCase__       =			{0: """batch"""}
															UpperCamelCase__       =			{0: """batch""", 1: """past_decoder_sequence + sequence"""}
									else:
															UpperCamelCase__       =			{0: """batch""", 1: """decoder_sequence"""}
															UpperCamelCase__       =			{0: """batch""", 1: """decoder_sequence"""}

									if self.use_past:
															self.fill_with_past_key_values_(lowercase			,   direction="""inputs"""      )

									return common_inputs






			@property
			# Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
			def 					A   ( self				:    Optional[Any]      )				->     int:

									'''simple docstring'''







									return 1_3







			@property
			def 					A   ( self				:    Tuple      )				->     float:

									'''simple docstring'''







									return 5e-4








'''simple docstring'''


import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


lowerCamelCase_					:			List[Any]     						=				namedtuple('''ExampleNamedTuple''', '''a b c''')
class  _SCREAMING_SNAKE_CASE				(		unittest.TestCase						):


			'''simple docstring'''





			def 					A   ( self				:    List[Any]      )				->     str:

									'''simple docstring'''







									UpperCamelCase__       =			torch.randn(5			,   2      )
									UpperCamelCase__       =			torch.device("""cuda"""      ) if torch.cuda.is_available() else torch.device("""cpu"""      )

									UpperCamelCase__       =			send_to_device(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta.cpu()			,   lowercase      )      )

									UpperCamelCase__       =			send_to_device((tensor, [tensor, tensor], 1)			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta[0].cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta[1]			,   lowercase      )
									self.assertTrue(torch.equal(resulta[1][0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta[1][1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta[2]			,   1      )

									UpperCamelCase__       =			send_to_device({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""a"""].cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta["""b"""]			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""b"""][0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta["""b"""][1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta["""c"""]			,   1      )

									UpperCamelCase__       =			send_to_device(ExampleNamedTuple(a=lowercase			,   b=[tensor, tensor]			,   c=1      )			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta.a.cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta.b			,   lowercase      )
									self.assertTrue(torch.equal(resulta.b[0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta.b[1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta.c			,   1      )

									UpperCamelCase__       =			send_to_device(UserDict({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}      )			,   lowercase      )
									self.assertIsInstance(lowercase			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""a"""].cpu()			,   lowercase      )      )
									self.assertIsInstance(resulta["""b"""]			,   lowercase      )
									self.assertTrue(torch.equal(resulta["""b"""][0].cpu()			,   lowercase      )      )
									self.assertTrue(torch.equal(resulta["""b"""][1].cpu()			,   lowercase      )      )
									self.assertEqual(resulta["""c"""]			,   1      )






			def 					A   ( self				:    List[Any]      )				->     List[str]:

									'''simple docstring'''







									with self.assertRaises(lowercase      ) as cm:
															UpperCamelCase__       =			recursively_apply(torch.tensor			,   (torch.tensor(1      ), 1)			,   error_on_other_type=lowercase      )
									self.assertEqual(
									    str(cm.exception      )			,   """Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed."""			,   )






			def 					A   ( self				:    Tuple      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			torch.tensor([1, 2, 3, 4, 5]      )
									self.assertEqual(listify(lowercase      )			,   [1, 2, 3, 4, 5]      )

									UpperCamelCase__       =			torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]      )
									self.assertEqual(listify(lowercase      )			,   [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]      )

									UpperCamelCase__       =			torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]      )
									self.assertEqual(
									    listify(lowercase      )			,   [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]      )






			def 					A   ( self				:    Union[str, Any]      )				->     List[str]:

									'''simple docstring'''







									with patch_environment(aa=1			,   BB=2      ):
															self.assertEqual(os.environ.get("""AA"""      )			,   """1"""      )
															self.assertEqual(os.environ.get("""BB"""      )			,   """2"""      )

									self.assertNotIn("""AA"""			,   os.environ      )
									self.assertNotIn("""BB"""			,   os.environ      )






			def 					A   ( self				:    Optional[int]      )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			model.forward
									UpperCamelCase__       =			convert_outputs_to_fpaa(model.forward      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase			,   keep_fpaa_wrapper=lowercase      )
									UpperCamelCase__       =			pickle.dumps(lowercase      )






			@require_cuda
			def 					A   ( self				:    int      )				->     Optional[int]:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			model.forward
									UpperCamelCase__       =			torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
									UpperCamelCase__       =			convert_outputs_to_fpaa(model.forward      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase			,   keep_fpaa_wrapper=lowercase      )
									UpperCamelCase__       =			pickle.dumps(lowercase      )






			@require_cuda
			@require_torch_min_version(version="""2.0"""      )
			def 					A   ( self				:    Any      )				->     int:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			model.forward
									UpperCamelCase__       =			torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
									UpperCamelCase__       =			convert_outputs_to_fpaa(model.forward      )
									UpperCamelCase__       =			torch.compile(model.forward			,   backend="""inductor"""      )
									UpperCamelCase__       =			torch.randn(4			,   1_0      ).cuda()
									UpperCamelCase__       =			model(lowercase      )






			def 					A   ( self				:    Union[str, Any]      )				->     Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									UpperCamelCase__       =			torch.nn.parallel.DataParallel(lowercase      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase      )

									self.assertEqual(lowercase			,   lowercase      )






			@require_torch_min_version(version="""2.0"""      )
			def 					A   ( self				:    Tuple      )				->     Tuple:

									'''simple docstring'''







									UpperCamelCase__       =			RegressionModel()
									UpperCamelCase__       =			torch.compile(lowercase      )

									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									UpperCamelCase__       =			torch.nn.parallel.DataParallel(lowercase      )
									UpperCamelCase__       =			torch.compile(lowercase      )
									UpperCamelCase__       =			extract_model_from_parallel(lowercase      )

									self.assertEqual(compiled_model._orig_mod			,   compiled_model_unwrapped._orig_mod      )







			def 					A   ( self				:    Union[str, Any]      )				->     Tuple:

									'''simple docstring'''







									self.assertEqual(find_device([1, """a""", torch.tensor([1, 2, 3]      )]      )			,   torch.device("""cpu"""      )      )
									self.assertEqual(find_device({"""a""": 1, """b""": torch.tensor([1, 2, 3]      )}      )			,   torch.device("""cpu"""      )      )
									self.assertIsNone(find_device([1, """a"""]      )      )
