



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


A     =      namedtuple('ExampleNamedTuple', 'a b c')






class   UpperCAmelCase__		(   unittest.TestCase  ):



		def        A_		(			self	:     Union[str, Any]					)      ->    Tuple:






									'''simple docstring'''

									A    =       torch.randn(5   ,				2					)
									A    =       torch.device('cuda'					) if torch.cuda.is_available() else torch.device('cpu'					)

									A    =       send_to_device(snake_case   ,				snake_case					)
									self.assertTrue(torch.equal(resulta.cpu()   ,				snake_case					)					)

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

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

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

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



		def        A_		(			self	:     Union[str, Any]					)      ->    List[str]:






									'''simple docstring'''

									with self.assertRaises(snake_case					) as cm:
																A    =       recursively_apply(torch.tensor   ,				(torch.tensor(1					), 1)   ,				error_on_other_type=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	:     str					)      ->    Dict:






									'''simple docstring'''

									A    =       torch.tensor([1, 2, 3, 4, 5]					)
									self.assertEqual(listify(snake_case					)   ,				[1, 2, 3, 4, 5]					)

									A    =       torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]					)
									self.assertEqual(listify(snake_case					)   ,				[[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(snake_case					)   ,				[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]					)



		def        A_		(			self	:     Optional[int]					)      ->    List[str]:






									'''simple docstring'''

									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	:     Tuple					)      ->    List[Any]:






									'''simple docstring'''

									A    =       RegressionModel()
									A    =       model.forward
									A    =       convert_outputs_to_fpaa(model.forward					)
									A    =       extract_model_from_parallel(snake_case   ,				keep_fpaa_wrapper=snake_case					)
									A    =       pickle.dumps(snake_case					)



		@require_cuda
		def        A_		(			self	:     List[Any]					)      ->    Any:






									'''simple docstring'''

									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(snake_case   ,				keep_fpaa_wrapper=snake_case					)
									A    =       pickle.dumps(snake_case					)



		@require_cuda
		@require_torch_min_version(version='2.0'					)
		def        A_		(			self	:     Optional[Any]					)      ->    str:






									'''simple docstring'''

									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(snake_case					)



		def        A_		(			self	:     List[Any]					)      ->    Union[str, Any]:






									'''simple docstring'''

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

									self.assertEqual(snake_case   ,				snake_case					)



		@require_torch_min_version(version='2.0'					)
		def        A_		(			self	:     Tuple					)      ->    Tuple:






									'''simple docstring'''

									A    =       RegressionModel()
									A    =       torch.compile(snake_case					)

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

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



		def        A_		(			self	:     List[str]					)      ->    Any:






									'''simple docstring'''

									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 collections import namedtuple

A     =      namedtuple('from_to', 'from_ to')

A     =      {
    'millimeter': 'mm',
    'centimeter': 'cm',
    'meter': 'm',
    'kilometer': 'km',
    'inch': 'in',
    'inche': 'in',  # Trailing 's' has been stripped off
    'feet': 'ft',
    'foot': 'ft',
    'yard': 'yd',
    'mile': 'mi',
}

A     =      {
    'mm': from_to(0.001, 1_0_0_0),
    'cm': from_to(0.01, 1_0_0),
    'm': from_to(1, 1),
    'km': from_to(1_0_0_0, 0.001),
    'in': from_to(0.0254, 39.3701),
    'ft': from_to(0.3048, 3.2_8084),
    'yd': from_to(0.9144, 1.0_9361),
    'mi': from_to(1609.34, 0.0_0062_1371),
}

def 	lowerCAmelCase__      (  lowerCamelCase__					,   lowerCamelCase__					,   lowerCamelCase__      )			->				float:
							A    =       from_type.lower().rstrip('s'      )
							A    =       TYPE_CONVERSION.get(lowerCamelCase__					,   lowerCamelCase__      )
							A    =       to_type.lower().rstrip('s'      )
							A    =       TYPE_CONVERSION.get(lowerCamelCase__					,   lowerCamelCase__      )
							if new_from not in METRIC_CONVERSION:
														A    =       (
														    f"""Invalid 'from_type' value: {from_type!r}.\n"""
														    f"""Conversion abbreviations are: {', '.join(lowerCamelCase__      )}"""
														)
														raise ValueError(lowerCamelCase__      )
							if new_to not in METRIC_CONVERSION:
														A    =       (
														    f"""Invalid 'to_type' value: {to_type!r}.\n"""
														    f"""Conversion abbreviations are: {', '.join(lowerCamelCase__      )}"""
														)
														raise ValueError(lowerCamelCase__      )
							return value * METRIC_CONVERSION[new_from].from_ * METRIC_CONVERSION[new_to].to


if __name__ == "__main__":
							import doctest

							doctest.testmod()



