'''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 		SCREAMING_SNAKE_CASE_					(unittest.TestCase      ):



			"""simple docstring"""






			def   lowerCamelCase (							self					):
									snake_case :       Optional[Any]				=    torch.randn(5				,			2					)
									snake_case :       Optional[int]				=    torch.device("cuda"					) if torch.cuda.is_available() else torch.device("cpu"					)

									snake_case :       Optional[Any]				=    send_to_device(snake_case_				,			snake_case_					)
									self.assertTrue(torch.equal(resulta.cpu()				,			snake_case_					)					)

									snake_case :       str				=    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					)

									snake_case :       List[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					)

									snake_case :       Tuple				=    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					)

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

									snake_case :       str				=    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]]					)

									snake_case :       int				=    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   lowerCamelCase (							self					):
									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   lowerCamelCase (							self					):
									snake_case :       List[str]				=    RegressionModel()
									snake_case :       str				=    model.forward
									snake_case :       Optional[int]				=    convert_outputs_to_fpaa(model.forward					)
									snake_case :       int				=    extract_model_from_parallel(snake_case_				,			keep_fpaa_wrapper=snake_case_					)
									snake_case :       str				=    pickle.dumps(snake_case_					)



			@require_cuda
			def   lowerCamelCase (							self					):
									snake_case :       str				=    RegressionModel()
									snake_case :       Dict				=    model.forward
									snake_case :       Dict				=    torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
									snake_case :       int				=    convert_outputs_to_fpaa(model.forward					)
									snake_case :       Tuple				=    extract_model_from_parallel(snake_case_				,			keep_fpaa_wrapper=snake_case_					)
									snake_case :       Optional[Any]				=    pickle.dumps(snake_case_					)



			@require_cuda
			@require_torch_min_version(version="2.0"					)
			def   lowerCamelCase (							self					):
									snake_case :       Tuple				=    RegressionModel()
									snake_case :       int				=    model.forward
									snake_case :       int				=    torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
									snake_case :       str				=    convert_outputs_to_fpaa(model.forward					)
									snake_case :       List[str]				=    torch.compile(model.forward				,			backend="inductor"					)
									snake_case :       int				=    torch.randn(4				,			10					).cuda()
									snake_case :       List[str]				=    model(snake_case_					)



			def   lowerCamelCase (							self					):
									snake_case :       List[str]				=    RegressionModel()
									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									snake_case :       List[str]				=    torch.nn.parallel.DataParallel(snake_case_					)
									snake_case :       int				=    extract_model_from_parallel(snake_case_					)

									self.assertEqual(snake_case_				,			snake_case_					)



			@require_torch_min_version(version="2.0"					)
			def   lowerCamelCase (							self					):
									snake_case :       Tuple				=    RegressionModel()
									snake_case :       Union[str, Any]				=    torch.compile(snake_case_					)

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

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






			def   lowerCamelCase (							self					):
									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 __future__ import annotations


def        UpperCAmelCase__   (				_lowerCAmelCase    :			list[int | float])    ->		int | float:



						"""simple docstring"""

						if len(_lowerCAmelCase) == 0:
												raise ValueError("find_max() arg is an empty sequence")
						snake_case :       Any				=    nums[0]
						for x in nums:
												if x > max_num:
																		snake_case :       int				=    x
						return max_num


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)





