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


UpperCamelCase     					=     namedtuple('ExampleNamedTuple', 'a b c')
class   UpperCAmelCase__				(    unittest.TestCase			):


	"""simple docstring"""







	def       A    (			self )    ->	int:
							a_							:			Dict     		=					torch.randn(5       ,     2 )
							a_							:			List[Any]     		=					torch.device("cuda" ) if torch.cuda.is_available() else torch.device("cpu" )

							a_							:			Optional[int]     		=					send_to_device(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )
							self.assertTrue(torch.equal(resulta.cpu()       ,     _SCREAMING_SNAKE_CASE ) )

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

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

							a_							:			Dict     		=					send_to_device(ExampleNamedTuple(a=_SCREAMING_SNAKE_CASE       ,     b=[tensor, tensor]       ,     c=1 )       ,     _SCREAMING_SNAKE_CASE )
							self.assertIsInstance(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )
							self.assertTrue(torch.equal(resulta.a.cpu()       ,     _SCREAMING_SNAKE_CASE ) )
							self.assertIsInstance(resulta.b       ,     _SCREAMING_SNAKE_CASE )
							self.assertTrue(torch.equal(resulta.b[0].cpu()       ,     _SCREAMING_SNAKE_CASE ) )
							self.assertTrue(torch.equal(resulta.b[1].cpu()       ,     _SCREAMING_SNAKE_CASE ) )
							self.assertEqual(resulta.c       ,     1 )

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







	def       A    (			self )    ->	List[Any]:
							with self.assertRaises(_SCREAMING_SNAKE_CASE ) as cm:
													a_							:			Optional[int]     		=					recursively_apply(torch.tensor       ,     (torch.tensor(1 ), 1)       ,     error_on_other_type=_SCREAMING_SNAKE_CASE )
							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:
							a_							:			Optional[Any]     		=					torch.tensor([1, 2, 3, 4, 5] )
							self.assertEqual(listify(_SCREAMING_SNAKE_CASE )       ,     [1, 2, 3, 4, 5] )

							a_							:			Any     		=					torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]] )
							self.assertEqual(listify(_SCREAMING_SNAKE_CASE )       ,     [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]] )

							a_							:			Dict     		=					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(_SCREAMING_SNAKE_CASE )       ,     [[[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 )    ->	Optional[Any]:
							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 )    ->	str:
							a_							:			List[str]     		=					RegressionModel()
							a_							:			Optional[int]     		=					model.forward
							a_							:			Union[str, Any]     		=					convert_outputs_to_fpaa(model.forward )
							a_							:			str     		=					extract_model_from_parallel(_SCREAMING_SNAKE_CASE       ,     keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE )
							a_							:			int     		=					pickle.dumps(_SCREAMING_SNAKE_CASE )







	@require_cuda
	def       A    (			self )    ->	Any:
							a_							:			Any     		=					RegressionModel()
							a_							:			Dict     		=					model.forward
							a_							:			List[str]     		=					torch.cuda.amp.autocast(dtype=torch.floataa )(model.forward )
							a_							:			List[Any]     		=					convert_outputs_to_fpaa(model.forward )
							a_							:			Dict     		=					extract_model_from_parallel(_SCREAMING_SNAKE_CASE       ,     keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE )
							a_							:			Dict     		=					pickle.dumps(_SCREAMING_SNAKE_CASE )







	@require_cuda
	@require_torch_min_version(version="2.0" )
	def       A    (			self )    ->	int:
							a_							:			Any     		=					RegressionModel()
							a_							:			Optional[int]     		=					model.forward
							a_							:			Any     		=					torch.cuda.amp.autocast(dtype=torch.floataa )(model.forward )
							a_							:			Optional[int]     		=					convert_outputs_to_fpaa(model.forward )
							a_							:			Tuple     		=					torch.compile(model.forward       ,     backend="inductor" )
							a_							:			Tuple     		=					torch.randn(4       ,     1_0 ).cuda()
							a_							:			List[Any]     		=					model(_SCREAMING_SNAKE_CASE )







	def       A    (			self )    ->	str:
							a_							:			int     		=					RegressionModel()
							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							a_							:			Tuple     		=					torch.nn.parallel.DataParallel(_SCREAMING_SNAKE_CASE )
							a_							:			Union[str, Any]     		=					extract_model_from_parallel(_SCREAMING_SNAKE_CASE )

							self.assertEqual(_SCREAMING_SNAKE_CASE       ,     _SCREAMING_SNAKE_CASE )







	@require_torch_min_version(version="2.0" )
	def       A    (			self )    ->	Any:
							a_							:			Any     		=					RegressionModel()
							a_							:			Optional[int]     		=					torch.compile(_SCREAMING_SNAKE_CASE )

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

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







	def       A    (			self )    ->	Dict:
							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"] ) )





"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCamelCase     					=     {'configuration_levit': ['LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'LevitConfig', 'LevitOnnxConfig']}

try:
				if not is_vision_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     ['LevitFeatureExtractor']
				UpperCamelCase     					=     ['LevitImageProcessor']

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase     					=     [
				    'LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'LevitForImageClassification',
				    'LevitForImageClassificationWithTeacher',
				    'LevitModel',
				    'LevitPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_levit import LEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, LevitConfig, LevitOnnxConfig

				try:
								if not is_vision_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .feature_extraction_levit import LevitFeatureExtractor
								from .image_processing_levit import LevitImageProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_levit import (
								    LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    LevitForImageClassification,
								    LevitForImageClassificationWithTeacher,
								    LevitModel,
								    LevitPreTrainedModel,
								)
else:
				import sys

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




