


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


snake_case__  				=			namedtuple('ExampleNamedTuple', 'a b c')



class      snake_case__	(   unittest.TestCase	):
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Optional[int]				=   torch.randn(5  ,   2   )
										__magic_name__					:				Union[str, Any]				=   torch.device('cuda'   ) if torch.cuda.is_available() else torch.device('cpu'   )

										__magic_name__					:				Any				=   send_to_device(_UpperCAmelCase  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta.cpu()  ,   _UpperCAmelCase   )   )

										__magic_name__					:				Any				=   send_to_device((tensor, [tensor, tensor], 1)  ,   _UpperCAmelCase   )
										self.assertIsInstance(_UpperCAmelCase  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta[0].cpu()  ,   _UpperCAmelCase   )   )
										self.assertIsInstance(resulta[1]  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta[1][0].cpu()  ,   _UpperCAmelCase   )   )
										self.assertTrue(torch.equal(resulta[1][1].cpu()  ,   _UpperCAmelCase   )   )
										self.assertEqual(resulta[2]  ,   1   )

										__magic_name__					:				Tuple				=   send_to_device({'a': tensor, 'b': [tensor, tensor], 'c': 1}  ,   _UpperCAmelCase   )
										self.assertIsInstance(_UpperCAmelCase  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta['a'].cpu()  ,   _UpperCAmelCase   )   )
										self.assertIsInstance(resulta['b']  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta['b'][0].cpu()  ,   _UpperCAmelCase   )   )
										self.assertTrue(torch.equal(resulta['b'][1].cpu()  ,   _UpperCAmelCase   )   )
										self.assertEqual(resulta['c']  ,   1   )

										__magic_name__					:				Optional[int]				=   send_to_device(ExampleNamedTuple(a=_UpperCAmelCase  ,   b=[tensor, tensor]  ,   c=1   )  ,   _UpperCAmelCase   )
										self.assertIsInstance(_UpperCAmelCase  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta.a.cpu()  ,   _UpperCAmelCase   )   )
										self.assertIsInstance(resulta.b  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta.b[0].cpu()  ,   _UpperCAmelCase   )   )
										self.assertTrue(torch.equal(resulta.b[1].cpu()  ,   _UpperCAmelCase   )   )
										self.assertEqual(resulta.c  ,   1   )

										__magic_name__					:				Optional[int]				=   send_to_device(UserDict({'a': tensor, 'b': [tensor, tensor], 'c': 1}   )  ,   _UpperCAmelCase   )
										self.assertIsInstance(_UpperCAmelCase  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta['a'].cpu()  ,   _UpperCAmelCase   )   )
										self.assertIsInstance(resulta['b']  ,   _UpperCAmelCase   )
										self.assertTrue(torch.equal(resulta['b'][0].cpu()  ,   _UpperCAmelCase   )   )
										self.assertTrue(torch.equal(resulta['b'][1].cpu()  ,   _UpperCAmelCase   )   )
										self.assertEqual(resulta['c']  ,   1   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										with self.assertRaises(_UpperCAmelCase   ) as cm:
																__magic_name__					:				Union[str, Any]				=   recursively_apply(torch.tensor  ,   (torch.tensor(1   ), 1)  ,   error_on_other_type=_UpperCAmelCase   )
										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 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Optional[Any]				=   torch.tensor([1, 2, 3, 4, 5]   )
										self.assertEqual(listify(_UpperCAmelCase   )  ,   [1, 2, 3, 4, 5]   )

										__magic_name__					:				Any				=   torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )
										self.assertEqual(listify(_UpperCAmelCase   )  ,   [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )

										__magic_name__					:				Union[str, Any]				=   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(_UpperCAmelCase   )  ,   [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]   )
				def 			UpperCamelCase_    (	self   ):




										"""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 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Optional[Any]				=   RegressionModel()
										__magic_name__					:				Optional[int]				=   model.forward
										__magic_name__					:				Any				=   convert_outputs_to_fpaa(model.forward   )
										__magic_name__					:				int				=   extract_model_from_parallel(_UpperCAmelCase  ,   keep_fpaa_wrapper=_UpperCAmelCase   )
										__magic_name__					:				Optional[Any]				=   pickle.dumps(_UpperCAmelCase   )
				@require_cuda
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				List[str]				=   RegressionModel()
										__magic_name__					:				Optional[Any]				=   model.forward
										__magic_name__					:				Dict				=   torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
										__magic_name__					:				Optional[int]				=   convert_outputs_to_fpaa(model.forward   )
										__magic_name__					:				Optional[int]				=   extract_model_from_parallel(_UpperCAmelCase  ,   keep_fpaa_wrapper=_UpperCAmelCase   )
										__magic_name__					:				Optional[int]				=   pickle.dumps(_UpperCAmelCase   )
				@require_cuda
				@require_torch_min_version(version='2.0'   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Optional[int]				=   RegressionModel()
										__magic_name__					:				List[Any]				=   model.forward
										__magic_name__					:				Dict				=   torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
										__magic_name__					:				List[Any]				=   convert_outputs_to_fpaa(model.forward   )
										__magic_name__					:				Union[str, Any]				=   torch.compile(model.forward  ,   backend='inductor'   )
										__magic_name__					:				Tuple				=   torch.randn(4  ,   10   ).cuda()
										__magic_name__					:				Any				=   model(_UpperCAmelCase   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




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

										self.assertEqual(_UpperCAmelCase  ,   _UpperCAmelCase   )
				@require_torch_min_version(version='2.0'   )
				def 			UpperCamelCase_    (	self   ):




										"""simple docstring"""




										__magic_name__					:				Union[str, Any]				=   RegressionModel()
										__magic_name__					:				Dict				=   torch.compile(_UpperCAmelCase   )

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

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





				def 			UpperCamelCase_    (	self   ):




										"""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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case__  				=			{'configuration_qdqbert': ['QDQBERT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'QDQBertConfig']}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			snake_case__  				=			[
			    'QDQBERT_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'QDQBertForMaskedLM',
			    'QDQBertForMultipleChoice',
			    'QDQBertForNextSentencePrediction',
			    'QDQBertForQuestionAnswering',
			    'QDQBertForSequenceClassification',
			    'QDQBertForTokenClassification',
			    'QDQBertLayer',
			    'QDQBertLMHeadModel',
			    'QDQBertModel',
			    'QDQBertPreTrainedModel',
			    'load_tf_weights_in_qdqbert',
			]


if TYPE_CHECKING:
			from .configuration_qdqbert import QDQBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, QDQBertConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_qdqbert import (
						    QDQBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    QDQBertForMaskedLM,
						    QDQBertForMultipleChoice,
						    QDQBertForNextSentencePrediction,
						    QDQBertForQuestionAnswering,
						    QDQBertForSequenceClassification,
						    QDQBertForTokenClassification,
						    QDQBertLayer,
						    QDQBertLMHeadModel,
						    QDQBertModel,
						    QDQBertPreTrainedModel,
						    load_tf_weights_in_qdqbert,
						)


else:
			import sys

			snake_case__  				=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)


