
"""simple docstring"""

from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


_A				=					logging.get_logger(__name__)

_A				=					{
    'facebook/mbart-large-cc25': 'https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json',
    # See all MBART models at https://huggingface.co/models?filter=mbart
}






class 			_lowercase  (							__UpperCAmelCase						):
	lowercase_								=      'mbart'
	lowercase_								=      ['past_key_values']
	lowercase_								=      {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}




	def __init__(					self						,					UpperCAmelCase_=50265						,					UpperCAmelCase_=1024						,					UpperCAmelCase_=12						,					UpperCAmelCase_=4096						,					UpperCAmelCase_=16						,					UpperCAmelCase_=12						,					UpperCAmelCase_=4096						,					UpperCAmelCase_=16						,					UpperCAmelCase_=0.0						,					UpperCAmelCase_=0.0						,					UpperCAmelCase_=True						,					UpperCAmelCase_=True						,					UpperCAmelCase_="gelu"						,					UpperCAmelCase_=1024						,					UpperCAmelCase_=0.1						,					UpperCAmelCase_=0.0						,					UpperCAmelCase_=0.0						,					UpperCAmelCase_=0.02						,					UpperCAmelCase_=0.0						,					UpperCAmelCase_=False						,					UpperCAmelCase_=1						,					UpperCAmelCase_=0						,					UpperCAmelCase_=2						,					UpperCAmelCase_=2						,					**UpperCAmelCase_						,					)						->				Dict:
			lowerCamelCase	:     Optional[Any]            =       vocab_size
			lowerCamelCase	:     str            =       max_position_embeddings
			lowerCamelCase	:     Dict            =       d_model
			lowerCamelCase	:     Optional[int]            =       encoder_ffn_dim
			lowerCamelCase	:     Union[str, Any]            =       encoder_layers
			lowerCamelCase	:     Optional[int]            =       encoder_attention_heads
			lowerCamelCase	:     Optional[int]            =       decoder_ffn_dim
			lowerCamelCase	:     List[str]            =       decoder_layers
			lowerCamelCase	:     List[Any]            =       decoder_attention_heads
			lowerCamelCase	:     Optional[int]            =       dropout
			lowerCamelCase	:     List[Any]            =       attention_dropout
			lowerCamelCase	:     str            =       activation_dropout
			lowerCamelCase	:     Dict            =       activation_function
			lowerCamelCase	:     Tuple            =       init_std
			lowerCamelCase	:     Dict            =       encoder_layerdrop
			lowerCamelCase	:     Any            =       decoder_layerdrop
			lowerCamelCase	:     Optional[Any]            =       classifier_dropout
			lowerCamelCase	:     List[str]            =       use_cache
			lowerCamelCase	:     Optional[Any]            =       encoder_layers
			lowerCamelCase	:     List[Any]            =       scale_embedding  # scale factor will be sqrt(d_model) if True
			super().__init__(
			    pad_token_id=_snake_case						,					bos_token_id=_snake_case						,					eos_token_id=_snake_case						,					is_encoder_decoder=_snake_case						,					forced_eos_token_id=_snake_case						,					**_snake_case						,					)






class 			_lowercase  (							__UpperCAmelCase						):




	@property
	def      _UpperCamelCase       (					self   )						->				Mapping[str, Mapping[int, str]]:






			if self.task in ["default", "seq2seq-lm"]:
					lowerCamelCase	:     Optional[Any]            =       OrderedDict(
					    [
					        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
					        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
					    ]   )

					if self.use_past:
							lowerCamelCase	:     List[str]            =       {0: 'batch'}
							lowerCamelCase	:     Optional[Any]            =       {0: 'batch', 1: 'past_decoder_sequence + sequence'}
					else:
							lowerCamelCase	:     int            =       {0: 'batch', 1: 'decoder_sequence'}
							lowerCamelCase	:     Any            =       {0: 'batch', 1: 'decoder_sequence'}

					if self.use_past:
							self.fill_with_past_key_values_(_snake_case						,					direction='inputs'   )
			elif self.task == "causal-lm":
					# TODO: figure this case out.
					lowerCamelCase	:     List[str]            =       OrderedDict(
					    [
					        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
					        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
					    ]   )
					if self.use_past:
							lowerCamelCase	:     Optional[Any]            =       self.num_layers
							for i in range(_snake_case   ):
									lowerCamelCase	:     Dict            =       {0: 'batch', 2: 'past_sequence + sequence'}
									lowerCamelCase	:     int            =       {0: 'batch', 2: 'past_sequence + sequence'}
			else:
					lowerCamelCase	:     Tuple            =       OrderedDict(
					    [
					        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
					        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
					        ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
					        ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
					    ]   )

			return common_inputs




	@property
	def      _UpperCamelCase       (					self   )						->				Mapping[str, Mapping[int, str]]:






			if self.task in ["default", "seq2seq-lm"]:
					lowerCamelCase	:     Optional[Any]            =       super().outputs
			else:
					lowerCamelCase	:     List[Any]            =       super(_snake_case						,					self   ).outputs
					if self.use_past:
							lowerCamelCase	:     Tuple            =       self.num_layers
							for i in range(_snake_case   ):
									lowerCamelCase	:     int            =       {0: 'batch', 2: 'past_sequence + sequence'}
									lowerCamelCase	:     List[str]            =       {0: 'batch', 2: 'past_sequence + sequence'}
			return common_outputs




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = False						,					UpperCAmelCase_ = None						,					)						->				Mapping[str, Any]:
			lowerCamelCase	:     Union[str, Any]            =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    _snake_case						,					_snake_case						,					_snake_case						,					_snake_case						,					_snake_case   )

			# Generate decoder inputs
			lowerCamelCase	:     int            =       seq_length if not self.use_past else 1
			lowerCamelCase	:     Tuple            =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    _snake_case						,					_snake_case						,					_snake_case						,					_snake_case						,					_snake_case   )
			lowerCamelCase	:     Optional[int]            =       {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
			lowerCamelCase	:     Optional[Any]            =       dict(**_snake_case						,					**_snake_case   )

			if self.use_past:
					if not is_torch_available():
							raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'   )
					else:
							import torch
					lowerCamelCase	:     Tuple            =       common_inputs['input_ids'].shape
					lowerCamelCase	:     Dict            =       common_inputs['decoder_input_ids'].shape[1]
					lowerCamelCase	:     List[Any]            =       self.num_attention_heads
					lowerCamelCase	:     int            =       (
					    batch,
					    num_encoder_attention_heads,
					    encoder_seq_length,
					    self._config.hidden_size // num_encoder_attention_heads,
					)
					lowerCamelCase	:     Optional[Any]            =       decoder_seq_length + 3
					lowerCamelCase	:     int            =       (
					    batch,
					    num_decoder_attention_heads,
					    decoder_past_length,
					    self._config.hidden_size // num_decoder_attention_heads,
					)

					lowerCamelCase	:     Tuple            =       torch.cat(
					    [common_inputs['decoder_attention_mask'], torch.ones(_snake_case						,					_snake_case   )]						,					dim=1   )

					lowerCamelCase	:     List[Any]            =       []
					# If the number of encoder and decoder layers are present in the model configuration, both are considered
					lowerCamelCase	:     Any            =       self.num_layers
					lowerCamelCase	:     Any            =       min(_snake_case						,					_snake_case   )
					lowerCamelCase	:     List[str]            =       max(_snake_case						,					_snake_case   ) - min_num_layers
					lowerCamelCase	:     Union[str, Any]            =       'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

					for _ in range(_snake_case   ):
							common_inputs["past_key_values"].append(
							    (
							        torch.zeros(_snake_case   ),
							        torch.zeros(_snake_case   ),
							        torch.zeros(_snake_case   ),
							        torch.zeros(_snake_case   ),
							    )   )
					# TODO: test this.
					lowerCamelCase	:     Dict            =       encoder_shape if remaining_side_name == 'encoder' else decoder_shape
					for _ in range(_snake_case						,					_snake_case   ):
							common_inputs["past_key_values"].append((torch.zeros(_snake_case   ), torch.zeros(_snake_case   ))   )
			return common_inputs




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = False						,					UpperCAmelCase_ = None						,					)						->				Mapping[str, Any]:
			lowerCamelCase	:     Dict            =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    _snake_case						,					_snake_case						,					_snake_case						,					_snake_case						,					_snake_case   )

			if self.use_past:
					if not is_torch_available():
							raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'   )
					else:
							import torch
					lowerCamelCase	:     Any            =       common_inputs['input_ids'].shape
					# Not using the same length for past_key_values
					lowerCamelCase	:     Optional[int]            =       seqlen + 2
					lowerCamelCase	:     List[str]            =       self.num_layers
					lowerCamelCase	:     Optional[int]            =       self.num_attention_heads
					lowerCamelCase	:     Dict            =       (
					    batch,
					    num_encoder_attention_heads,
					    past_key_values_length,
					    self._config.hidden_size // num_encoder_attention_heads,
					)

					lowerCamelCase	:     Dict            =       common_inputs['attention_mask'].dtype
					lowerCamelCase	:     List[str]            =       torch.cat(
					    [common_inputs['attention_mask'], torch.ones(_snake_case						,					_snake_case						,					dtype=_snake_case   )]						,					dim=1   )
					lowerCamelCase	:     int            =       [
					    (torch.zeros(_snake_case   ), torch.zeros(_snake_case   )) for _ in range(_snake_case   )
					]
			return common_inputs




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = False						,					UpperCAmelCase_ = None						,					)						->				Mapping[str, Any]:
			lowerCamelCase	:     Optional[int]            =       compute_effective_axis_dimension(
			    _snake_case						,					fixed_dimension=OnnxConfig.default_fixed_batch						,					num_token_to_add=0   )

			# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
			lowerCamelCase	:     Dict            =       tokenizer.num_special_tokens_to_add(_snake_case   )
			lowerCamelCase	:     str            =       compute_effective_axis_dimension(
			    _snake_case						,					fixed_dimension=OnnxConfig.default_fixed_sequence						,					num_token_to_add=_snake_case   )

			# Generate dummy inputs according to compute batch and sequence
			lowerCamelCase	:     Tuple            =       [' '.join([tokenizer.unk_token]   ) * seq_length] * batch_size
			lowerCamelCase	:     Dict            =       dict(tokenizer(_snake_case						,					return_tensors=_snake_case   )   )
			return common_inputs




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = -1						,					UpperCAmelCase_ = False						,					UpperCAmelCase_ = None						,					)						->				Mapping[str, Any]:






			if self.task in ["default", "seq2seq-lm"]:
					lowerCamelCase	:     Union[str, Any]            =       self._generate_dummy_inputs_for_default_and_seqaseq_lm(
					    _snake_case						,					batch_size=_snake_case						,					seq_length=_snake_case						,					is_pair=_snake_case						,					framework=_snake_case   )

			elif self.task == "causal-lm":
					lowerCamelCase	:     str            =       self._generate_dummy_inputs_for_causal_lm(
					    _snake_case						,					batch_size=_snake_case						,					seq_length=_snake_case						,					is_pair=_snake_case						,					framework=_snake_case   )
			else:
					lowerCamelCase	:     Optional[Any]            =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    _snake_case						,					batch_size=_snake_case						,					seq_length=_snake_case						,					is_pair=_snake_case						,					framework=_snake_case   )

			return common_inputs




	def      _UpperCamelCase       (					self						,					UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_						,					UpperCAmelCase_   )						->				List[str]:






			if self.task in ["default", "seq2seq-lm"]:
					lowerCamelCase	:     Dict            =       super()._flatten_past_key_values_(_snake_case						,					_snake_case						,					_snake_case						,					_snake_case   )
			else:
					lowerCamelCase	:     List[str]            =       super(_snake_case						,					self   )._flatten_past_key_values_(
					    _snake_case						,					_snake_case						,					_snake_case						,					_snake_case   )














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,   UpperCAmelCase ):






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


       """simple docstring"""






       lowerCamelCase_							:    str							=     load_tool('translation'		)
       self.tool.setup()
       lowerCamelCase_							:    Optional[int]							=     load_tool('translation'		,		remote=_snake_case		)






      def      UpperCAmelCase_     (self					:    Any		)       ->   Dict:


       """simple docstring"""






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






      def      UpperCAmelCase_     (self					:    Optional[int]		)       ->   Dict:


       """simple docstring"""






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






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


       """simple docstring"""






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






      def      UpperCAmelCase_     (self					:    Any		)       ->   Tuple:


       """simple docstring"""






       lowerCamelCase_							:    Optional[int]							=     self.remote_tool(text='Hey, what\'s up?'		,		src_lang='English'		,		tgt_lang='French'		)
       self.assertEqual(_snake_case		,		'- Hé, comment ça va?'		)






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


       """simple docstring"""






       lowerCamelCase_							:    str							=     ['Hey, what\'s up?', 'English', 'Spanish']
       lowerCamelCase_							:    Any							=     self.tool(*_snake_case		)

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

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






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


       """simple docstring"""






       lowerCamelCase_							:    List[str]							=     ['Hey, what\'s up?', 'English', 'Spanish']
       lowerCamelCase_							:    Optional[Any]							=     self.tool(*_snake_case		)

       if not isinstance(_snake_case		,		_snake_case		):
        lowerCamelCase_							:    List[str]							=     [outputs]

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

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






      def      UpperCAmelCase_     (self					:    Any		)       ->   Optional[Any]:


       """simple docstring"""






       lowerCamelCase_							:    Dict							=     ['Hey, what\'s up?', 'English', 'Spanish']

       lowerCamelCase_							:    List[str]							=     []

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

       if not isinstance(_snake_case		,		_snake_case		):
        lowerCamelCase_							:    Optional[Any]							=     [outputs]

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