




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,
)


__a  :		List[Any]		     =   namedtuple('''ExampleNamedTuple''', '''a b c''')







class 				UpperCAmelCase(					unittest.TestCase					):



	"""simple docstring"""






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





								"""simple docstring"""


								lowercase__  :	Union[str, Any]        =							torch.randn(5	,				2    )
								lowercase__  :	Tuple        =							torch.device("cuda"    ) if torch.cuda.is_available() else torch.device("cpu"    )

								lowercase__  :	str        =							send_to_device(lowercase_	,				lowercase_    )
								self.assertTrue(torch.equal(resulta.cpu()	,				lowercase_    )    )

								lowercase__  :	Any        =							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    )

								lowercase__  :	Tuple        =							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    )

								lowercase__  :	List[str]        =							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    )

								lowercase__  :	Optional[Any]        =							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[str]:





								"""simple docstring"""


								with self.assertRaises(lowercase_    ) as cm:
															lowercase__  :	Dict        =							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    ) ->      List[str]:





								"""simple docstring"""


								lowercase__  :	Any        =							torch.tensor([1, 2, 3, 4, 5]    )
								self.assertEqual(listify(lowercase_    )	,				[1, 2, 3, 4, 5]    )

								lowercase__  :	Tuple        =							torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]    )
								self.assertEqual(listify(lowercase_    )	,				[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]    )

								lowercase__  :	Optional[int]        =							torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]    )
								self.assertEqual(
								    listify(lowercase_    )	,				[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]    )

	def    __a     (					self    ) ->      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]:





								"""simple docstring"""


								lowercase__  :	List[Any]        =							RegressionModel()
								lowercase__  :	Tuple        =							model.forward
								lowercase__  :	List[Any]        =							convert_outputs_to_fpaa(model.forward    )
								lowercase__  :	List[Any]        =							extract_model_from_parallel(lowercase_	,				keep_fpaa_wrapper=lowercase_    )
								lowercase__  :	Optional[int]        =							pickle.dumps(lowercase_    )

	@require_cuda
	def    __a     (					self    ) ->      List[Any]:





								"""simple docstring"""


								lowercase__  :	str        =							RegressionModel()
								lowercase__  :	Optional[Any]        =							model.forward
								lowercase__  :	Dict        =							torch.cuda.amp.autocast(dtype=torch.floataa    )(model.forward    )
								lowercase__  :	Optional[Any]        =							convert_outputs_to_fpaa(model.forward    )
								lowercase__  :	Dict        =							extract_model_from_parallel(lowercase_	,				keep_fpaa_wrapper=lowercase_    )
								lowercase__  :	List[str]        =							pickle.dumps(lowercase_    )

	@require_cuda
	@require_torch_min_version(version="2.0"    )
	def    __a     (					self    ) ->      str:





								"""simple docstring"""


								lowercase__  :	Tuple        =							RegressionModel()
								lowercase__  :	Any        =							model.forward
								lowercase__  :	Any        =							torch.cuda.amp.autocast(dtype=torch.floataa    )(model.forward    )
								lowercase__  :	Union[str, Any]        =							convert_outputs_to_fpaa(model.forward    )
								lowercase__  :	int        =							torch.compile(model.forward	,				backend="inductor"    )
								lowercase__  :	int        =							torch.randn(4	,				10    ).cuda()
								lowercase__  :	Union[str, Any]        =							model(lowercase_    )

	def    __a     (					self    ) ->      List[str]:





								"""simple docstring"""


								lowercase__  :	str        =							RegressionModel()
								# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
								lowercase__  :	Dict        =							torch.nn.parallel.DataParallel(lowercase_    )
								lowercase__  :	Optional[Any]        =							extract_model_from_parallel(lowercase_    )

								self.assertEqual(lowercase_	,				lowercase_    )

	@require_torch_min_version(version="2.0"    )
	def    __a     (					self    ) ->      Optional[int]:





								"""simple docstring"""


								lowercase__  :	Dict        =							RegressionModel()
								lowercase__  :	Optional[int]        =							torch.compile(lowercase_    )

								# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
								lowercase__  :	Tuple        =							torch.nn.parallel.DataParallel(lowercase_    )
								lowercase__  :	Optional[int]        =							torch.compile(lowercase_    )
								lowercase__  :	Tuple        =							extract_model_from_parallel(lowercase_    )

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







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





								"""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"]    )    )












from collections import OrderedDict
from typing import Mapping

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


__a  :		Any		     =   logging.get_logger(__name__)

__a  :		Union[str, Any]		     =   {
    '''squeezebert/squeezebert-uncased''': (
        '''https://huggingface.co/squeezebert/squeezebert-uncased/resolve/main/config.json'''
    ),
    '''squeezebert/squeezebert-mnli''': '''https://huggingface.co/squeezebert/squeezebert-mnli/resolve/main/config.json''',
    '''squeezebert/squeezebert-mnli-headless''': (
        '''https://huggingface.co/squeezebert/squeezebert-mnli-headless/resolve/main/config.json'''
    ),
}







class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






	a     : str	  =					SQUEEZEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
	a     : int	  =					"""squeezebert"""

	def __init__(					self	,				lowerCamelCase=30522	,				lowerCamelCase=768	,				lowerCamelCase=12	,				lowerCamelCase=12	,				lowerCamelCase=3072	,				lowerCamelCase="gelu"	,				lowerCamelCase=0.1	,				lowerCamelCase=0.1	,				lowerCamelCase=512	,				lowerCamelCase=2	,				lowerCamelCase=0.02	,				lowerCamelCase=1E-12	,				lowerCamelCase=0	,				lowerCamelCase=768	,				lowerCamelCase=4	,				lowerCamelCase=4	,				lowerCamelCase=4	,				lowerCamelCase=1	,				lowerCamelCase=4	,				lowerCamelCase=4	,				**lowerCamelCase	,				) ->      str:





								"""simple docstring"""


								super().__init__(pad_token_id=lowerCamelCase	,				**lowerCamelCase    )

								lowercase__  :	Any        =							vocab_size
								lowercase__  :	Tuple        =							hidden_size
								lowercase__  :	str        =							num_hidden_layers
								lowercase__  :	Optional[int]        =							num_attention_heads
								lowercase__  :	List[Any]        =							hidden_act
								lowercase__  :	Union[str, Any]        =							intermediate_size
								lowercase__  :	int        =							hidden_dropout_prob
								lowercase__  :	List[Any]        =							attention_probs_dropout_prob
								lowercase__  :	Any        =							max_position_embeddings
								lowercase__  :	List[Any]        =							type_vocab_size
								lowercase__  :	Any        =							initializer_range
								lowercase__  :	Optional[Any]        =							layer_norm_eps
								lowercase__  :	Any        =							embedding_size
								lowercase__  :	Optional[Any]        =							q_groups
								lowercase__  :	List[str]        =							k_groups
								lowercase__  :	List[Any]        =							v_groups
								lowercase__  :	str        =							post_attention_groups
								lowercase__  :	Optional[int]        =							intermediate_groups
								lowercase__  :	Optional[int]        =							output_groups







class 				UpperCAmelCase(					snake_case_					):



	"""simple docstring"""






	@property
	def    __a     (					self    ) ->      Mapping[str, Mapping[int, str]]:





								"""simple docstring"""


								if self.task == "multiple-choice":
															lowercase__  :	Union[str, Any]        =							{0: "batch", 1: "choice", 2: "sequence"}
								else:
															lowercase__  :	Union[str, Any]        =							{0: "batch", 1: "sequence"}
								return OrderedDict(
								    [
								        ("input_ids", dynamic_axis),
								        ("attention_mask", dynamic_axis),
								        ("token_type_ids", dynamic_axis),
								    ]    )