



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


class 	a__		(    unittest.TestCase  ):


   def 							lowercase					(				self   :				Any      )     ->							int:
         lowercase	:   Union[str, Any]											= torch.randn(5,  2      )
         lowercase	:   Any											= torch.device('cuda'      ) if torch.cuda.is_available() else torch.device('cpu'      )

         lowercase	:   Tuple											= send_to_device(_lowerCAmelCase,  _lowerCAmelCase      )
         self.assertTrue(torch.equal(resulta.cpu(),  _lowerCAmelCase      )      )

         lowercase	:   Union[str, Any]											= send_to_device((tensor, [tensor, tensor], 1),  _lowerCAmelCase      )
         self.assertIsInstance(_lowerCAmelCase,  _lowerCAmelCase      )
         self.assertTrue(torch.equal(resulta[0].cpu(),  _lowerCAmelCase      )      )
         self.assertIsInstance(resulta[1],  _lowerCAmelCase      )
         self.assertTrue(torch.equal(resulta[1][0].cpu(),  _lowerCAmelCase      )      )
         self.assertTrue(torch.equal(resulta[1][1].cpu(),  _lowerCAmelCase      )      )
         self.assertEqual(resulta[2],  1      )

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

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

         lowercase	:   Dict											= send_to_device(UserDict({'a': tensor, 'b': [tensor, tensor], 'c': 1}      ),  _lowerCAmelCase      )
         self.assertIsInstance(_lowerCAmelCase,  _lowerCAmelCase      )
         self.assertTrue(torch.equal(resulta['a'].cpu(),  _lowerCAmelCase      )      )
         self.assertIsInstance(resulta['b'],  _lowerCAmelCase      )
         self.assertTrue(torch.equal(resulta['b'][0].cpu(),  _lowerCAmelCase      )      )
         self.assertTrue(torch.equal(resulta['b'][1].cpu(),  _lowerCAmelCase      )      )
         self.assertEqual(resulta['c'],  1      )


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

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

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


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


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


   @require_cuda
   @require_torch_min_version(version='2.0'      )
   def 							lowercase					(				self   :				str      )     ->							int:
         lowercase	:   Dict											= RegressionModel()
         lowercase	:   List[Any]											= model.forward
         lowercase	:   Tuple											= torch.cuda.amp.autocast(dtype=torch.floataa      )(model.forward      )
         lowercase	:   Any											= convert_outputs_to_fpaa(model.forward      )
         lowercase	:   Tuple											= torch.compile(model.forward,  backend='inductor'      )
         lowercase	:   Union[str, Any]											= torch.randn(4,  10      ).cuda()
         lowercase	:   List[Any]											= model(_lowerCAmelCase      )


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

         self.assertEqual(_lowerCAmelCase,  _lowerCAmelCase      )


   @require_torch_min_version(version='2.0'      )
   def 							lowercase					(				self   :				Optional[int]      )     ->							Dict:
         lowercase	:   Optional[int]											= RegressionModel()
         lowercase	:   int											= torch.compile(_lowerCAmelCase      )

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

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






   def 							lowercase					(				self   :				Dict      )     ->							int:
         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']      )      )







def  __UpperCamelCase   (     a = 998001)			->int:

					# fetches the next number
					for number in range(n - 1,  9999,  -1):
										lowerCamelCase__      =							str(a)

										# checks whether 'str_number' is a palindrome.
										if str_number == str_number[::-1]:
															lowerCamelCase__      =							999

															# if 'number' is a product of two 3-digit numbers
															# then number is the answer otherwise fetch next number.
															while divisor != 99:
																				if (number % divisor == 0) and (len(str(number // divisor)) == 3.0):
																									return number
																				divisor -= 1
					raise ValueError("That number is larger than our acceptable range.")


if __name__ == "__main__":
						print(f"""{solution() = }""")

