
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__							=    namedtuple("""ExampleNamedTuple""", """a b c""")
class      A__       (						unittest.TestCase    ):





							def       a__			(			self  :       Tuple    )						->		List[str]:



											"""simple docstring"""


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

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

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

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

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

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





							def       a__			(			self  :       Any    )						->		List[Any]:



											"""simple docstring"""


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



											"""simple docstring"""


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

											__lowercase								= torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]    )
											self.assertEqual(listify(_UpperCAmelCase    )					,			[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]    )

											__lowercase								= 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(_UpperCAmelCase    )					,			[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]    )





							def       a__			(			self  :       List[str]    )						->		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  :       Union[str, Any]    )						->		List[Any]:



											"""simple docstring"""


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





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



											"""simple docstring"""


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





							@require_cuda
							@require_torch_min_version(version='2.0'    )
							def       a__			(			self  :       List[str]    )						->		Union[str, Any]:



											"""simple docstring"""


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





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



											"""simple docstring"""


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

											self.assertEqual(_UpperCAmelCase					,			_UpperCAmelCase    )





							@require_torch_min_version(version='2.0'    )
							def       a__			(			self  :       List[str]    )						->		Any:



											"""simple docstring"""


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

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

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





							def       a__			(			self  :       Union[str, Any]    )						->		Dict:



											"""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']    )    )







import unittest

from transformers import load_tool
from transformers.tools.agent_types import AGENT_TYPE_MAPPING

from .test_tools_common import ToolTesterMixin, output_types
class      A__       (						unittest.TestCase   ,						lowerCAmelCase__    ):





							def       a__			(			self  :       Any    )						->		Dict:



											"""simple docstring"""


											__lowercase								= load_tool('translation'    )
											self.tool.setup()
											__lowercase								= load_tool('translation'					,			remote=_UpperCAmelCase    )





							def       a__			(			self  :       int    )						->		Optional[Any]:



											"""simple docstring"""


											__lowercase								= self.tool('Hey, what\'s up?'					,			src_lang='English'					,			tgt_lang='French'    )
											self.assertEqual(_UpperCAmelCase					,			'- Hé, comment ça va?'    )





							def       a__			(			self  :       Tuple    )						->		Optional[int]:



											"""simple docstring"""


											__lowercase								= self.remote_tool('Hey, what\'s up?'					,			src_lang='English'					,			tgt_lang='French'    )
											self.assertEqual(_UpperCAmelCase					,			'- Hé, comment ça va?'    )





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



											"""simple docstring"""


											__lowercase								= self.tool(text='Hey, what\'s up?'					,			src_lang='English'					,			tgt_lang='French'    )
											self.assertEqual(_UpperCAmelCase					,			'- Hé, comment ça va?'    )





							def       a__			(			self  :       Tuple    )						->		Optional[Any]:



											"""simple docstring"""


											__lowercase								= self.remote_tool(text='Hey, what\'s up?'					,			src_lang='English'					,			tgt_lang='French'    )
											self.assertEqual(_UpperCAmelCase					,			'- Hé, comment ça va?'    )





							def       a__			(			self  :       List[Any]    )						->		int:



											"""simple docstring"""


											__lowercase								= ['Hey, what\'s up?', 'English', 'Spanish']
											__lowercase								= self.tool(*_UpperCAmelCase    )

											# There is a single output
											if len(self.tool.outputs    ) == 1:
															__lowercase								= [outputs]

											self.assertListEqual(output_types(_UpperCAmelCase    )					,			self.tool.outputs    )





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



											"""simple docstring"""


											__lowercase								= ['Hey, what\'s up?', 'English', 'Spanish']
											__lowercase								= self.tool(*_UpperCAmelCase    )

											if not isinstance(_UpperCAmelCase					,			_UpperCAmelCase    ):
															__lowercase								= [outputs]

											self.assertEqual(len(_UpperCAmelCase    )					,			len(self.tool.outputs    )    )

											for output, output_type in zip(_UpperCAmelCase					,			self.tool.outputs    ):
															__lowercase								= AGENT_TYPE_MAPPING[output_type]
															self.assertTrue(isinstance(_UpperCAmelCase					,			_UpperCAmelCase    )    )





							def       a__			(			self  :       Tuple    )						->		List[Any]:



											"""simple docstring"""


											__lowercase								= ['Hey, what\'s up?', 'English', 'Spanish']

											__lowercase								= []

											for _input, input_type in zip(_UpperCAmelCase					,			self.tool.inputs    ):
															if isinstance(_UpperCAmelCase					,			_UpperCAmelCase    ):
																			_inputs.append([AGENT_TYPE_MAPPING[_input_type](_input    ) for _input_type in input_type]    )
															else:
																			_inputs.append(AGENT_TYPE_MAPPING[input_type](_input    )    )

        # Should not raise an error
											__lowercase								= self.tool(*_UpperCAmelCase    )

											if not isinstance(_UpperCAmelCase					,			_UpperCAmelCase    ):
															__lowercase								= [outputs]

											self.assertEqual(len(_UpperCAmelCase    )					,			len(self.tool.outputs    )    )





