


'''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 							UpperCAmelCase ( unittest.TestCase	,   _snake_case	):


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Optional[int]   ):
    UpperCAmelCase__  :Optional[int]            =			load_tool('''translation'''   )
    self.tool.setup()
    UpperCAmelCase__  :Dict            =			load_tool('''translation''' ,					remote=__lowerCamelCase   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Optional[int]   ):
    UpperCAmelCase__  :Optional[Any]            =			self.tool('''Hey, what\'s up?''' ,					src_lang='''English''' ,					tgt_lang='''French'''   )
    self.assertEqual(__lowerCamelCase ,					'''- Hé, comment ça va?'''   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[str]   ):
    UpperCAmelCase__  :List[str]            =			self.remote_tool('''Hey, what\'s up?''' ,					src_lang='''English''' ,					tgt_lang='''French'''   )
    self.assertEqual(__lowerCamelCase ,					'''- Hé, comment ça va?'''   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[str]   ):
    UpperCAmelCase__  :List[Any]            =			self.tool(text='''Hey, what\'s up?''' ,					src_lang='''English''' ,					tgt_lang='''French'''   )
    self.assertEqual(__lowerCamelCase ,					'''- Hé, comment ça va?'''   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Dict   ):
    UpperCAmelCase__  :List[Any]            =			self.remote_tool(text='''Hey, what\'s up?''' ,					src_lang='''English''' ,					tgt_lang='''French'''   )
    self.assertEqual(__lowerCamelCase ,					'''- Hé, comment ça va?'''   )


 def 					__SCREAMING_SNAKE_CASE				(			self       :  Optional[int]   ):
    UpperCAmelCase__  :Any            =			['''Hey, what\'s up?''', '''English''', '''Spanish''']
    UpperCAmelCase__  :List[Any]            =			self.tool(*__lowerCamelCase   )

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

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


 def 					__SCREAMING_SNAKE_CASE				(			self       :  List[Any]   ):
    UpperCAmelCase__  :Any            =			['''Hey, what\'s up?''', '''English''', '''Spanish''']
    UpperCAmelCase__  :Tuple            =			self.tool(*__lowerCamelCase   )

    if not isinstance(__lowerCamelCase ,					__lowerCamelCase   ):
       UpperCAmelCase__  :int            =			[outputs]

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

    for output, output_type in zip(__lowerCamelCase ,					self.tool.outputs   ):
       UpperCAmelCase__  :Optional[int]            =			AGENT_TYPE_MAPPING[output_type]
       self.assertTrue(isinstance(__lowerCamelCase ,					__lowerCamelCase   )   )





 def 					__SCREAMING_SNAKE_CASE				(			self       :  Tuple   ):
    UpperCAmelCase__  :Union[str, Any]            =			['''Hey, what\'s up?''', '''English''', '''Spanish''']

    UpperCAmelCase__  :Any            =			[]

    for _input, input_type in zip(__lowerCamelCase ,					self.tool.inputs   ):
       if isinstance(__lowerCamelCase ,					__lowerCamelCase   ):
          _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__  :str            =			self.tool(*__lowerCamelCase   )

    if not isinstance(__lowerCamelCase ,					__lowerCamelCase   ):
       UpperCAmelCase__  :Union[str, Any]            =			[outputs]

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







'''simple docstring'''





from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
    import torch

__lowerCamelCase       				=	logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
    from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):


    def 					a__    (				cls	:			Optional[int]	):
       return cls


def 					a__    (				UpperCamelCase_	:			Union[Tuple, List],      UpperCamelCase_	:			Optional[Union[List["torch.Generator"], "torch.Generator"]] = None,      UpperCamelCase_	:			Optional["torch.device"] = None,      UpperCamelCase_	:			Optional["torch.dtype"] = None,      UpperCamelCase_	:			Optional["torch.layout"] = None,      ):
   UpperCAmelCase__  :Union[str, Any]            =			device
   UpperCAmelCase__  :List[Any]            =			shape[0]

   UpperCAmelCase__  :Tuple            =			layout or torch.strided
   UpperCAmelCase__  :Union[str, Any]            =			device or torch.device('''cpu'''	)

   if generator is not None:
      UpperCAmelCase__  :str            =			generator.device.type if not isinstance(UpperCamelCase_,      UpperCamelCase_	) else generator[0].device.type
      if gen_device_type != device.type and gen_device_type == "cpu":
         UpperCAmelCase__  :Tuple            =			'''cpu'''
         if device != "mps":
            logger.info(
                F'''The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'''
                F''' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'''
                F''' slighly speed up this function by passing a generator that was created on the {device} device.'''	)
      elif gen_device_type != device.type and gen_device_type == "cuda":
         raise ValueError(F'''Cannot generate a {device} tensor from a generator of type {gen_device_type}.'''	)

   if isinstance(UpperCamelCase_,      UpperCamelCase_	):
      UpperCAmelCase__  :List[str]            =			(1,) + shape[1:]
      UpperCAmelCase__  :Optional[int]            =			[
          torch.randn(UpperCamelCase_,      generator=generator[i],      device=UpperCamelCase_,      dtype=UpperCamelCase_,      layout=UpperCamelCase_	)
          for i in range(UpperCamelCase_	)
      ]
      UpperCAmelCase__  :int            =			torch.cat(UpperCamelCase_,      dim=0	).to(UpperCamelCase_	)
   else:
      UpperCAmelCase__  :Optional[int]            =			torch.randn(UpperCamelCase_,      generator=UpperCamelCase_,      device=UpperCamelCase_,      dtype=UpperCamelCase_,      layout=UpperCamelCase_	).to(UpperCamelCase_	)

   return latents


def 					a__    (				UpperCamelCase_	:			Dict	):
   if is_torch_version('''<''',      '''2.0.0'''	) or not hasattr(UpperCamelCase_,      '''_dynamo'''	):
      return False
   return isinstance(UpperCamelCase_,      torch._dynamo.eval_frame.OptimizedModule	)



