




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

class       lowerCamelCase_							(	unittest.TestCase  ):






   '''simple docstring'''

   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			torch.randn(5     ,						2 )
       UpperCamelCase__      =			torch.device("cuda" ) if torch.cuda.is_available() else torch.device("cpu" )

       UpperCamelCase__      =			send_to_device(_lowerCAmelCase     ,						_lowerCAmelCase )
       self.assertTrue(torch.equal(resulta.cpu()     ,						_lowerCAmelCase ) )

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

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

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

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


   def 	_UpperCamelCase       (  self ):
       with self.assertRaises(_lowerCAmelCase ) as cm:
           UpperCamelCase__      =			recursively_apply(torch.tensor     ,						(torch.tensor(1 ), 1)     ,						error_on_other_type=_lowerCAmelCase )
       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 ):
       UpperCamelCase__      =			torch.tensor([1, 2, 3, 4, 5] )
       self.assertEqual(listify(_lowerCAmelCase )     ,						[1, 2, 3, 4, 5] )

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


   def 	_UpperCamelCase       (  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 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			RegressionModel()
       UpperCamelCase__      =			model.forward
       UpperCamelCase__      =			convert_outputs_to_fpaa(model.forward )
       UpperCamelCase__      =			extract_model_from_parallel(_lowerCAmelCase     ,						keep_fpaa_wrapper=_lowerCAmelCase )
       UpperCamelCase__      =			pickle.dumps(_lowerCAmelCase )


   @require_cuda
   def 	_UpperCamelCase       (  self ):
       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(_lowerCAmelCase     ,						keep_fpaa_wrapper=_lowerCAmelCase )
       UpperCamelCase__      =			pickle.dumps(_lowerCAmelCase )


   @require_cuda
   @require_torch_min_version(version="2.0" )
   def 	_UpperCamelCase       (  self ):
       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(_lowerCAmelCase )


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			RegressionModel()
       # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
       UpperCamelCase__      =			torch.nn.parallel.DataParallel(_lowerCAmelCase )
       UpperCamelCase__      =			extract_model_from_parallel(_lowerCAmelCase )

       self.assertEqual(_lowerCAmelCase     ,						_lowerCAmelCase )


   @require_torch_min_version(version="2.0" )
   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			RegressionModel()
       UpperCamelCase__      =			torch.compile(_lowerCAmelCase )

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

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







   def 	_UpperCamelCase       (  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'''







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       lowerCamelCase_							(	unittest.TestCase					,    lowerCAmelCase_  ):






   '''simple docstring'''

   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			load_tool("translation" )
       self.tool.setup()
       UpperCamelCase__      =			load_tool("translation"     ,						remote=__UpperCamelCase )


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			self.tool("Hey, what's up?"     ,						src_lang="English"     ,						tgt_lang="French" )
       self.assertEqual(__UpperCamelCase     ,						"- Hé, comment ça va?" )


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			self.remote_tool("Hey, what's up?"     ,						src_lang="English"     ,						tgt_lang="French" )
       self.assertEqual(__UpperCamelCase     ,						"- Hé, comment ça va?" )


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			self.tool(text="Hey, what's up?"     ,						src_lang="English"     ,						tgt_lang="French" )
       self.assertEqual(__UpperCamelCase     ,						"- Hé, comment ça va?" )


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			self.remote_tool(text="Hey, what's up?"     ,						src_lang="English"     ,						tgt_lang="French" )
       self.assertEqual(__UpperCamelCase     ,						"- Hé, comment ça va?" )


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			["Hey, what's up?", "English", "Spanish"]
       UpperCamelCase__      =			self.tool(*__UpperCamelCase )

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

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


   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			["Hey, what's up?", "English", "Spanish"]
       UpperCamelCase__      =			self.tool(*__UpperCamelCase )

       if not isinstance(__UpperCamelCase     ,						__UpperCamelCase ):
           UpperCamelCase__      =			[outputs]

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

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







   def 	_UpperCamelCase       (  self ):
       UpperCamelCase__      =			["Hey, what's up?", "English", "Spanish"]

       UpperCamelCase__      =			[]

       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
       UpperCamelCase__      =			self.tool(*__UpperCamelCase )

       if not isinstance(__UpperCamelCase     ,						__UpperCamelCase ):
           UpperCamelCase__      =			[outputs]

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




