




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


__SCREAMING_SNAKE_CASE			:      List[Any]      			=namedtuple("""ExampleNamedTuple""", """a b c""")






class 	A	(						unittest.TestCase						):
     """simple docstring"""

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

           lowerCamelCase__					     = send_to_device(A__		,     A__	)
           self.assertTrue(torch.equal(resulta.cpu()		,     A__	)	)

           lowerCamelCase__					     = send_to_device((tensor, [tensor, tensor], 1)		,     A__	)
           self.assertIsInstance(A__		,     A__	)
           self.assertTrue(torch.equal(resulta[0].cpu()		,     A__	)	)
           self.assertIsInstance(resulta[1]		,     A__	)
           self.assertTrue(torch.equal(resulta[1][0].cpu()		,     A__	)	)
           self.assertTrue(torch.equal(resulta[1][1].cpu()		,     A__	)	)
           self.assertEqual(resulta[2]		,     1	)

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

           lowerCamelCase__					     = send_to_device(ExampleNamedTuple(a=A__		,     b=[tensor, tensor]		,     c=1	)		,     A__	)
           self.assertIsInstance(A__		,     A__	)
           self.assertTrue(torch.equal(resulta.a.cpu()		,     A__	)	)
           self.assertIsInstance(resulta.b		,     A__	)
           self.assertTrue(torch.equal(resulta.b[0].cpu()		,     A__	)	)
           self.assertTrue(torch.equal(resulta.b[1].cpu()		,     A__	)	)
           self.assertEqual(resulta.c		,     1	)

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




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

           lowerCamelCase__					     = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]	)
           self.assertEqual(listify(A__	)		,     [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]	)

           lowerCamelCase__					     = 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(A__	)		,     [[[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        __snake_case      (			self     :      Optional[Any]	) ->				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        __snake_case      (			self     :      Optional[Any]	) ->				int:
           lowerCamelCase__					     = RegressionModel()
           lowerCamelCase__					     = model.forward
           lowerCamelCase__					     = convert_outputs_to_fpaa(model.forward	)
           lowerCamelCase__					     = extract_model_from_parallel(A__		,     keep_fpaa_wrapper=A__	)
           lowerCamelCase__					     = pickle.dumps(A__	)




     @require_cuda
     def        __snake_case      (			self     :      Any	) ->				int:
           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(A__		,     keep_fpaa_wrapper=A__	)
           lowerCamelCase__					     = pickle.dumps(A__	)




     @require_cuda
     @require_torch_min_version(version='2.0'	)
     def        __snake_case      (			self     :      Tuple	) ->				Dict:
           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		,     1_0	).cuda()
           lowerCamelCase__					     = model(A__	)




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

           self.assertEqual(A__		,     A__	)




     @require_torch_min_version(version='2.0'	)
     def        __snake_case      (			self     :      Tuple	) ->				Tuple:
           lowerCamelCase__					     = RegressionModel()
           lowerCamelCase__					     = torch.compile(A__	)

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

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




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







import unittest
from timeit import timeit






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	first_num if first_num >= second_num else second_num
       UpperCAmelCase					=	max_num
       while (common_mult % first_num > 0) or (common_mult % second_num > 0):
              common_mult += max_num
       return common_mult






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       return b if a == 0 else greatest_common_divisor(b % a		,					lowerCAmelCase						)






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       return first_num // greatest_common_divisor(lowerCAmelCase		,					lowerCAmelCase						) * second_num






def 					_lowerCAmelCase				(					):







       '''simple docstring'''
       UpperCAmelCase					=	(
           """from __main__ import least_common_multiple_slow, least_common_multiple_fast"""
       )
       print(
           """least_common_multiple_slow():"""		,					timeit("""least_common_multiple_slow(1000, 999)"""		,					setup=lowerCAmelCase						)		,					)
       print(
           """least_common_multiple_fast():"""		,					timeit("""least_common_multiple_fast(1000, 999)"""		,					setup=lowerCAmelCase						)		,					)





class      UpperCamelCase_       (				unittest.TestCase     ):
  _A				:    List[str] =		(
      (10, 20),
      (13, 15),
      (4, 31),
      (10, 42),
      (43, 34),
      (5, 12),
      (12, 25),
      (10, 25),
      (6, 9),
  )
  _A				:    int =		(20, 195, 124, 210, 1462, 60, 300, 50, 18)



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         for i, (first_num, second_num) in enumerate(self.test_inputs ):
                UpperCAmelCase					=	least_common_multiple_slow(snake_case__       ,						snake_case__ )
                UpperCAmelCase					=	least_common_multiple_fast(snake_case__       ,						snake_case__ )
                with self.subTest(i=snake_case__ ):
                       self.assertEqual(snake_case__       ,						self.expected_results[i] )
                       self.assertEqual(snake_case__       ,						self.expected_results[i] )


if __name__ == "__main__":
 benchmark()
 unittest.main()
