

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	:   Any	=					namedtuple('ExampleNamedTuple', 'a b c')


class 			_SCREAMING_SNAKE_CASE      (  unittest.TestCase):







						def        _snake_case				(			self							)->							int:
													lowerCamelCase_								=torch.randn(5     ,						2							)
													lowerCamelCase_								=torch.device("""cuda"""							) if torch.cuda.is_available() else torch.device("""cpu"""							)

													lowerCamelCase_								=send_to_device(_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)
													self.assertTrue(torch.equal(resulta.cpu()     ,						_SCREAMING_SNAKE_CASE							)							)

													lowerCamelCase_								=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							)

													lowerCamelCase_								=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							)

													lowerCamelCase_								=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							)

													lowerCamelCase_								=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        _snake_case				(			self							)->							Dict:
													with self.assertRaises(_SCREAMING_SNAKE_CASE							) as cm:
																				lowerCamelCase_								=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        _snake_case				(			self							)->							Tuple:
													lowerCamelCase_								=torch.tensor([1, 2, 3, 4, 5]							)
													self.assertEqual(listify(_SCREAMING_SNAKE_CASE							)     ,						[1, 2, 3, 4, 5]							)

													lowerCamelCase_								=torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]							)
													self.assertEqual(listify(_SCREAMING_SNAKE_CASE							)     ,						[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]							)

													lowerCamelCase_								=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(_SCREAMING_SNAKE_CASE							)     ,						[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]							)







						def        _snake_case				(			self							)->							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        _snake_case				(			self							)->							Dict:
													lowerCamelCase_								=RegressionModel()
													lowerCamelCase_								=model.forward
													lowerCamelCase_								=convert_outputs_to_fpaa(model.forward							)
													lowerCamelCase_								=extract_model_from_parallel(_SCREAMING_SNAKE_CASE     ,						keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=pickle.dumps(_SCREAMING_SNAKE_CASE							)







						@require_cuda
						def        _snake_case				(			self							)->							Optional[Any]:
													lowerCamelCase_								=RegressionModel()
													lowerCamelCase_								=model.forward
													lowerCamelCase_								=torch.cuda.amp.autocast(dtype=torch.floataa							)(model.forward							)
													lowerCamelCase_								=convert_outputs_to_fpaa(model.forward							)
													lowerCamelCase_								=extract_model_from_parallel(_SCREAMING_SNAKE_CASE     ,						keep_fpaa_wrapper=_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=pickle.dumps(_SCREAMING_SNAKE_CASE							)







						@require_cuda
						@require_torch_min_version(version="""2.0"""							)
						def        _snake_case				(			self							)->							List[str]:
													lowerCamelCase_								=RegressionModel()
													lowerCamelCase_								=model.forward
													lowerCamelCase_								=torch.cuda.amp.autocast(dtype=torch.floataa							)(model.forward							)
													lowerCamelCase_								=convert_outputs_to_fpaa(model.forward							)
													lowerCamelCase_								=torch.compile(model.forward     ,						backend="""inductor"""							)
													lowerCamelCase_								=torch.randn(4     ,						10							).cuda()
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Any:
													lowerCamelCase_								=RegressionModel()
													# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
													lowerCamelCase_								=torch.nn.parallel.DataParallel(_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=extract_model_from_parallel(_SCREAMING_SNAKE_CASE							)

													self.assertEqual(_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)







						@require_torch_min_version(version="""2.0"""							)
						def        _snake_case				(			self							)->							List[Any]:
													lowerCamelCase_								=RegressionModel()
													lowerCamelCase_								=torch.compile(_SCREAMING_SNAKE_CASE							)

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

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







						def        _snake_case				(			self							)->							Any:
													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,
    is_vision_available,
)


__A	:   Optional[int]	=					{
    'configuration_mobilevitv2': [
        'MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileViTV2Config',
        'MobileViTV2OnnxConfig',
    ],
}


try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__A	:   Tuple	=					[
	    'MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'MobileViTV2ForImageClassification',
	    'MobileViTV2ForSemanticSegmentation',
	    'MobileViTV2Model',
	    'MobileViTV2PreTrainedModel',
	]

if TYPE_CHECKING:
	from .configuration_mobilevitva import (
	    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    MobileViTVaConfig,
	    MobileViTVaOnnxConfig,
	)

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_mobilevitva import (
		    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
		    MobileViTVaForImageClassification,
		    MobileViTVaForSemanticSegmentation,
		    MobileViTVaModel,
		    MobileViTVaPreTrainedModel,
		)

else:
	import sys

	__A	:   List[str]	=					_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
