
"""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 							snake_case       (     unittest.TestCase		):







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

      a_						   =     send_to_device(__UpperCAmelCase       ,							__UpperCAmelCase)
      self.assertTrue(torch.equal(resulta.cpu()       ,							__UpperCAmelCase))

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

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

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

      a_						   =     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)   ->Any:
      with self.assertRaises(__UpperCAmelCase) as cm:
         a_						   =     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)   ->Any:
      a_						   =     torch.tensor([1, 2, 3, 4, 5])
      self.assertEqual(listify(__UpperCAmelCase)       ,							[1, 2, 3, 4, 5])

      a_						   =     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]])

      a_						   =     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)   ->str:
      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)   ->Tuple:
      a_						   =     RegressionModel()
      a_						   =     model.forward
      a_						   =     convert_outputs_to_fpaa(model.forward)
      a_						   =     extract_model_from_parallel(__UpperCAmelCase       ,							keep_fpaa_wrapper=__UpperCAmelCase)
      a_						   =     pickle.dumps(__UpperCAmelCase)







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







   @require_cuda
   @require_torch_min_version(version="2.0")
   def    UpperCAmelCase__	( self)   ->str:
      a_						   =     RegressionModel()
      a_						   =     model.forward
      a_						   =     torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
      a_						   =     convert_outputs_to_fpaa(model.forward)
      a_						   =     torch.compile(model.forward       ,							backend="inductor")
      a_						   =     torch.randn(4       ,							10).cuda()
      a_						   =     model(__UpperCAmelCase)







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

      self.assertEqual(__UpperCAmelCase       ,							__UpperCAmelCase)







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

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

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







   def    UpperCAmelCase__	( self)   ->List[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"]))

"""simple docstring"""






from typing import TYPE_CHECKING

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


UpperCamelCase_						   =   {
    'configuration_mobilevitv2': [
        'MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileViTV2Config',
        'MobileViTV2OnnxConfig',
    ],
}


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase_						   =   [
          '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

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