




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

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


lowercase_		=							logging.get_logger(__name__)

lowercase_		=							{
    '''facebook/m2m100_418M''': '''https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json''',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}





class 		A__    (       __SCREAMING_SNAKE_CASE			):
 lowerCamelCase__		:			Optional[int]       ="m2m_100"
 lowerCamelCase__		:			List[str]       =["past_key_values"]
 lowerCamelCase__		:			Union[str, Any]       ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}






 def __init__(     self    ,					lowerCamelCase=128112    ,					lowerCamelCase=1024    ,					lowerCamelCase=12    ,					lowerCamelCase=4096    ,					lowerCamelCase=16    ,					lowerCamelCase=12    ,					lowerCamelCase=4096    ,					lowerCamelCase=16    ,					lowerCamelCase=0.0_5    ,					lowerCamelCase=0.0_5    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase="relu"    ,					lowerCamelCase=1024    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.0    ,					lowerCamelCase=0.0_2    ,					lowerCamelCase=2    ,					lowerCamelCase=True    ,					lowerCamelCase=1    ,					lowerCamelCase=0    ,					lowerCamelCase=2    ,					**lowerCamelCase    ,					)				->       List[str]:




      """simple docstring"""






      __magic_name__ :							List[Any]		       =							vocab_size
      __magic_name__ :							str		       =							max_position_embeddings
      __magic_name__ :							Union[str, Any]		       =							d_model
      __magic_name__ :							Dict		       =							encoder_ffn_dim
      __magic_name__ :							List[Any]		       =							encoder_layers
      __magic_name__ :							Optional[int]		       =							encoder_attention_heads
      __magic_name__ :							Dict		       =							decoder_ffn_dim
      __magic_name__ :							List[str]		       =							decoder_layers
      __magic_name__ :							Optional[Any]		       =							decoder_attention_heads
      __magic_name__ :							List[Any]		       =							dropout
      __magic_name__ :							List[str]		       =							attention_dropout
      __magic_name__ :							Optional[int]		       =							activation_dropout
      __magic_name__ :							Dict		       =							activation_function
      __magic_name__ :							Tuple		       =							init_std
      __magic_name__ :							Optional[int]		       =							encoder_layerdrop
      __magic_name__ :							int		       =							decoder_layerdrop
      __magic_name__ :							Union[str, Any]		       =							use_cache
      __magic_name__ :							Optional[int]		       =							encoder_layers
      __magic_name__ :							Dict		       =							scale_embedding  # scale factor will be sqrt(d_model) if True

      super().__init__(
          pad_token_id=lowerCamelCase    ,					bos_token_id=lowerCamelCase    ,					eos_token_id=lowerCamelCase    ,					is_encoder_decoder=lowerCamelCase    ,					decoder_start_token_id=lowerCamelCase    ,					**lowerCamelCase    ,					)





class 		A__    (       __SCREAMING_SNAKE_CASE			):






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




      """simple docstring"""






      __magic_name__ :							Dict		       =							OrderedDict(
          [
              ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
              ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
          ]						)

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

      if self.use_past:
           self.fill_with_past_key_values_(lowerCamelCase    ,					direction='''inputs'''						)
      return common_inputs






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




      """simple docstring"""






      __magic_name__ :							int		       =							compute_effective_axis_dimension(
          lowerCamelCase    ,					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
      __magic_name__ :							str		       =							tokenizer.num_special_tokens_to_add(lowerCamelCase						)
      __magic_name__ :							Optional[Any]		       =							compute_effective_axis_dimension(
          lowerCamelCase    ,					fixed_dimension=OnnxConfig.default_fixed_sequence    ,					num_token_to_add=lowerCamelCase						)

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






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




      """simple docstring"""






      __magic_name__ :							Any		       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)

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

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

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

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

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





 lowerCamelCase__		:			List[str]       =_generate_dummy_inputs_for_default_and_seqaseq_lm









import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
				from PIL import Image





class 		A__    (       unittest.TestCase		,					__SCREAMING_SNAKE_CASE			):






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




						"""simple docstring"""






						__magic_name__ :							Tuple		       =							load_tool('''image-question-answering'''						)
						self.tool.setup()
						__magic_name__ :							Any		       =							load_tool('''image-question-answering'''    ,					remote=lowerCamelCase						)






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




						"""simple docstring"""






						__magic_name__ :							Dict		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							int		       =							self.tool(lowerCamelCase    ,					'''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)






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




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							int		       =							self.remote_tool(lowerCamelCase    ,					'''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)






	def 			lowercase				(     self						)				->       Any:




						"""simple docstring"""






						__magic_name__ :							int		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							Tuple		       =							self.tool(image=lowerCamelCase    ,					question='''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							Optional[int]		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							Tuple		       =							self.remote_tool(image=lowerCamelCase    ,					question='''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)



