


import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


lowerCAmelCase__						:Tuple         =							logging.get_logger(__name__)

class 	__a   (       nn.Module    ):
		def __init__(    self		)				-> Optional[int]:




									"""simple docstring"""





									super().__init__()
									if version.parse(torch.__version__		) < version.parse('1.12.0'		):
																raise ImportError(
																    f'''You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '''
																    'PytorchGELUTanh. Please upgrade torch.'		)






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return nn.functional.gelu(_SCREAMING_SNAKE_CASE    ,					approximate='tanh'		)



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi		) * (input + 0.044715 * torch.pow(_SCREAMING_SNAKE_CASE    ,					3.0		))		))



class 	__a   (       nn.Module    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE = False		)				-> int:




									"""simple docstring"""





									super().__init__()
									if use_gelu_python:
																_UpperCAmelCase					=    self._gelu_python
									else:
																_UpperCAmelCase					=    nn.functional.gelu
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0		)		))






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return self.act(_SCREAMING_SNAKE_CASE		)



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return 0.5 * input * (1.0 + torch.tanh(input * 0.7978845608 * (1.0 + 0.044715 * input * input)		))



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return input * torch.sigmoid(1.702 * input		)



class 	__a   (       nn.Module    ):
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> Optional[Any]:




									"""simple docstring"""





									if min > max:
																raise ValueError(f'''min should be < max (got min: {min}, max: {max})'''		)

									super().__init__()
									_UpperCAmelCase					=    min
									_UpperCAmelCase					=    max






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return torch.clip(gelu(_SCREAMING_SNAKE_CASE		)    ,					self.min    ,					self.max		)



class 	__a   (       nn.Module    ):
		def __init__(    self		)				-> Union[str, Any]:




									"""simple docstring"""





									super().__init__()
									_UpperCAmelCase					=    math.sqrt(2 / math.pi		)






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.044715 * torch.pow(_SCREAMING_SNAKE_CASE    ,					3		))		))



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return nn.functional.silu(_SCREAMING_SNAKE_CASE		)



class 	__a   (       nn.Module    ):
		def __init__(    self		)				-> Dict:




									"""simple docstring"""





									super().__init__()
									if version.parse(torch.__version__		) < version.parse('1.9.0'		):
																_UpperCAmelCase					=    self._mish_python
									else:
																_UpperCAmelCase					=    nn.functional.mish
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return input * torch.tanh(nn.functional.softplus(_SCREAMING_SNAKE_CASE		)		)






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return self.act(_SCREAMING_SNAKE_CASE		)



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tensor:




									"""simple docstring"""





									return input



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=0.707107    ,					_SCREAMING_SNAKE_CASE=0.282095		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    (input - mu).div(sigma * math.sqrt(2.0		)		)
									return 0.5 * (1.0 + torch.erf(_SCREAMING_SNAKE_CASE		))



class 	__a   (       nn.Module    ):
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    nn.functional.relu(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    torch.square(_SCREAMING_SNAKE_CASE		)
									return squared



class 	__a   (       UpperCAmelCase    ):
		def __getitem__(    self    ,					_SCREAMING_SNAKE_CASE		)				-> Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=    super().__getitem__(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase	, _UpperCAmelCase					=    content if isinstance(_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		) else (content, {})
									return cls(**_SCREAMING_SNAKE_CASE		)



lowerCAmelCase__						:int         =							{
    '''gelu''': GELUActivation,
    '''gelu_10''': (ClippedGELUActivation, {'''min''': -1_0, '''max''': 1_0}),
    '''gelu_fast''': FastGELUActivation,
    '''gelu_new''': NewGELUActivation,
    '''gelu_python''': (GELUActivation, {'''use_gelu_python''': True}),
    '''gelu_pytorch_tanh''': PytorchGELUTanh,
    '''gelu_accurate''': AccurateGELUActivation,
    '''laplace''': LaplaceActivation,
    '''linear''': LinearActivation,
    '''mish''': MishActivation,
    '''quick_gelu''': QuickGELUActivation,
    '''relu''': nn.ReLU,
    '''relu2''': ReLUSquaredActivation,
    '''relu6''': nn.ReLUa,
    '''sigmoid''': nn.Sigmoid,
    '''silu''': SiLUActivation,
    '''swish''': SiLUActivation,
    '''tanh''': nn.Tanh,
}
lowerCAmelCase__						:List[str]         =							ClassInstantier(ACTaCLS)

def  lowerCAmelCase__				(    a__:	Any )						->	Tuple:


							'''simple docstring'''





							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys() )}''' )


# For backwards compatibility with: from activations import gelu_python
lowerCAmelCase__						:List[Any]         =							get_activation('''gelu_python''')
lowerCAmelCase__						:Dict         =							get_activation('''gelu_new''')
lowerCAmelCase__						:List[str]         =							get_activation('''gelu''')
lowerCAmelCase__						:Optional[int]         =							get_activation('''gelu_fast''')
lowerCAmelCase__						:str         =							get_activation('''quick_gelu''')
lowerCAmelCase__						:List[str]         =							get_activation('''silu''')
lowerCAmelCase__						:Union[str, Any]         =							get_activation('''mish''')







lowerCAmelCase__						:Any         =							get_activation('''linear''')




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					,   UpperCAmelCase    ):
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									_UpperCAmelCase					=    load_tool('translation'		)
									self.tool.setup()
									_UpperCAmelCase					=    load_tool('translation'    ,					remote=_SCREAMING_SNAKE_CASE		)
		def 			UpperCAmelCase__       (    self		)				-> Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=    self.tool('Hey, what\'s up?'    ,					src_lang='English'    ,					tgt_lang='French'		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					'- Hé, comment ça va?'		)
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





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




									"""simple docstring"""





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




									"""simple docstring"""





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




									"""simple docstring"""





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

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

									self.assertListEqual(output_types(_SCREAMING_SNAKE_CASE		)    ,					self.tool.outputs		)
		def 			UpperCAmelCase__       (    self		)				-> Union[str, Any]:




									"""simple docstring"""





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

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

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

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






		def 			UpperCAmelCase__       (    self		)				-> Optional[int]:




									"""simple docstring"""





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

									_UpperCAmelCase					=    []

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

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

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