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


lowercase		:						Optional[Any]			=					namedtuple('ExampleNamedTuple', 'a b c')




class    _snake_case  (						unittest.TestCase       ):






							def 							__lowercase  (					self						:			Any    ):






												"""simple docstring"""



												__a:						str		    =					torch.randn(5						, 2    )
												__a:						Any		    =					torch.device("""cuda"""    ) if torch.cuda.is_available() else torch.device("""cpu"""    )

												__a:						str		    =					send_to_device(snake_case__						, snake_case__    )
												self.assertTrue(torch.equal(resulta.cpu()						, snake_case__    )    )

												__a:						Union[str, Any]		    =					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:						str		    =					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:						str		    =					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:						List[str]		    =					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 							__lowercase  (					self						:			Optional[int]    ):






												"""simple docstring"""



												with self.assertRaises(snake_case__    ) as cm:
																	__a:						Optional[Any]		    =					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 							__lowercase  (					self						:			Tuple    ):






												"""simple docstring"""



												__a:						Dict		    =					torch.tensor([1, 2, 3, 4, 5]    )
												self.assertEqual(listify(snake_case__    )						, [1, 2, 3, 4, 5]    )

												__a:						int		    =					torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]    )
												self.assertEqual(listify(snake_case__    )						, [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]    )

												__a:						Dict		    =					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(snake_case__    )						, [[[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 							__lowercase  (					self						:			Tuple    ):






												"""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 							__lowercase  (					self						:			Tuple    ):






												"""simple docstring"""



												__a:						Any		    =					RegressionModel()
												__a:						Union[str, Any]		    =					model.forward
												__a:						List[Any]		    =					convert_outputs_to_fpaa(model.forward    )
												__a:						Tuple		    =					extract_model_from_parallel(snake_case__						, keep_fpaa_wrapper=snake_case__    )
												__a:						Union[str, Any]		    =					pickle.dumps(snake_case__    )






							@require_cuda
							def 							__lowercase  (					self						:			Optional[Any]    ):






												"""simple docstring"""



												__a:						str		    =					RegressionModel()
												__a:						str		    =					model.forward
												__a:						Dict		    =					torch.cuda.amp.autocast(dtype=torch.floataa    )(model.forward    )
												__a:						str		    =					convert_outputs_to_fpaa(model.forward    )
												__a:						List[str]		    =					extract_model_from_parallel(snake_case__						, keep_fpaa_wrapper=snake_case__    )
												__a:						Optional[Any]		    =					pickle.dumps(snake_case__    )






							@require_cuda
							@require_torch_min_version(version="""2.0"""    )
							def 							__lowercase  (					self						:			Union[str, Any]    ):






												"""simple docstring"""



												__a:						Optional[Any]		    =					RegressionModel()
												__a:						Optional[int]		    =					model.forward
												__a:						Tuple		    =					torch.cuda.amp.autocast(dtype=torch.floataa    )(model.forward    )
												__a:						Any		    =					convert_outputs_to_fpaa(model.forward    )
												__a:						Dict		    =					torch.compile(model.forward						, backend="""inductor"""    )
												__a:						Union[str, Any]		    =					torch.randn(4						, 1_0    ).cuda()
												__a:						Dict		    =					model(snake_case__    )






							def 							__lowercase  (					self						:			Any    ):






												"""simple docstring"""



												__a:						Optional[int]		    =					RegressionModel()
												# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
												__a:						List[Any]		    =					torch.nn.parallel.DataParallel(snake_case__    )
												__a:						int		    =					extract_model_from_parallel(snake_case__    )

												self.assertEqual(snake_case__						, snake_case__    )






							@require_torch_min_version(version="""2.0"""    )
							def 							__lowercase  (					self						:			List[str]    ):






												"""simple docstring"""



												__a:						str		    =					RegressionModel()
												__a:						int		    =					torch.compile(snake_case__    )

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

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






							def 							__lowercase  (					self						:			Optional[int]    ):






												"""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"""
lowercase		:						str =					{
    '0000': '0',
    '0001': '1',
    '0010': '2',
    '0011': '3',
    '0100': '4',
    '0101': '5',
    '0110': '6',
    '0111': '7',
    '1000': '8',
    '1001': '9',
    '1010': 'a',
    '1011': 'b',
    '1100': 'c',
    '1101': 'd',
    '1110': 'e',
    '1111': 'f',
}


def     lowerCamelCase_    (	lowerCamelCase_ )     ->			str:


					'''simple docstring'''
					__a:						Optional[int]		    =					str(lowerCamelCase_ ).strip()

					# Exceptions
					if not binary_str:
										raise ValueError("""Empty string was passed to the function""" )
					__a:						List[Any]		    =					binary_str[0] == """-"""
					__a:						List[str]		    =					binary_str[1:] if is_negative else binary_str
					if not all(char in """01""" for char in binary_str ):
										raise ValueError("""Non-binary value was passed to the function""" )

					__a:						Any		    =					(
					    """0""" * (4 * (divmod(len(lowerCamelCase_ )	,				4 )[0] + 1) - len(lowerCamelCase_ )) + binary_str
					)

					__a:						int		    =					[]
					for x in range(0	,				len(lowerCamelCase_ )	,				4 ):
										hexadecimal.append(BITS_TO_HEX[binary_str[x : x + 4]] )
					__a:						Tuple		    =					"""0x""" + """""".join(lowerCamelCase_ )

					return "-" + hexadecimal_str if is_negative else hexadecimal_str


if __name__ == "__main__":
			from doctest import testmod

			testmod()
