




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






class        __lowerCAmelCase					(			unittest.TestCase					):







			def UpperCamelCase     (			self       :     Union[str, Any] ):




									"""simple docstring"""


									_UpperCAmelCase						=	torch.randn(5       ,       2 )
									_UpperCAmelCase						=	torch.device("cuda" ) if torch.cuda.is_available() else torch.device("cpu" )

									_UpperCAmelCase						=	send_to_device(_snake_case       ,       _snake_case )
									self.assertTrue(torch.equal(resulta.cpu()       ,       _snake_case ) )

									_UpperCAmelCase						=	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 )

									_UpperCAmelCase						=	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 )

									_UpperCAmelCase						=	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 )

									_UpperCAmelCase						=	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 UpperCamelCase     (			self       :     Optional[Any] ):




									"""simple docstring"""


									with self.assertRaises(_snake_case ) as cm:
															_UpperCAmelCase						=	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 UpperCamelCase     (			self       :     Optional[int] ):




									"""simple docstring"""


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

									_UpperCAmelCase						=	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]] )

									_UpperCAmelCase						=	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 UpperCamelCase     (			self       :     List[Any] ):




									"""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 UpperCamelCase     (			self       :     Optional[int] ):




									"""simple docstring"""


									_UpperCAmelCase						=	RegressionModel()
									_UpperCAmelCase						=	model.forward
									_UpperCAmelCase						=	convert_outputs_to_fpaa(model.forward )
									_UpperCAmelCase						=	extract_model_from_parallel(_snake_case       ,       keep_fpaa_wrapper=_snake_case )
									_UpperCAmelCase						=	pickle.dumps(_snake_case )







			@require_cuda
			def UpperCamelCase     (			self       :     Dict ):




									"""simple docstring"""


									_UpperCAmelCase						=	RegressionModel()
									_UpperCAmelCase						=	model.forward
									_UpperCAmelCase						=	torch.cuda.amp.autocast(dtype=torch.floataa )(model.forward )
									_UpperCAmelCase						=	convert_outputs_to_fpaa(model.forward )
									_UpperCAmelCase						=	extract_model_from_parallel(_snake_case       ,       keep_fpaa_wrapper=_snake_case )
									_UpperCAmelCase						=	pickle.dumps(_snake_case )







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




									"""simple docstring"""


									_UpperCAmelCase						=	RegressionModel()
									_UpperCAmelCase						=	model.forward
									_UpperCAmelCase						=	torch.cuda.amp.autocast(dtype=torch.floataa )(model.forward )
									_UpperCAmelCase						=	convert_outputs_to_fpaa(model.forward )
									_UpperCAmelCase						=	torch.compile(model.forward       ,       backend="inductor" )
									_UpperCAmelCase						=	torch.randn(4       ,       10 ).cuda()
									_UpperCAmelCase						=	model(_snake_case )







			def UpperCamelCase     (			self       :     int ):




									"""simple docstring"""


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

									self.assertEqual(_snake_case       ,       _snake_case )







			@require_torch_min_version(version="2.0" )
			def UpperCamelCase     (			self       :     str ):




									"""simple docstring"""


									_UpperCAmelCase						=	RegressionModel()
									_UpperCAmelCase						=	torch.compile(_snake_case )

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

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







			def UpperCamelCase     (			self       :     Optional[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'''


import unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
						import torch



class        lowercase    (	unittest.TestCase,       a_			):


							def     _snake_case				(      self)				->					Optional[int]:
								UpperCAmelCase_		:   int													=       load_tool('speech-to-text')
								self.tool.setup()


							def     _snake_case				(      self)				->					Any:
								UpperCAmelCase_		:   str													=       self.tool(torch.ones(3000))
								self.assertEqual(_snake_case     ,      ' you')





							def     _snake_case				(      self)				->					str:
								UpperCAmelCase_		:   List[Any]													=       self.tool(audio=torch.ones(3000))
								self.assertEqual(_snake_case     ,      ' you')






