

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


a				:							List[Any]					=			logging.get_logger(__name__)

a				:							Any					=			{
    '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      lowerCamelCase	(  SCREAMING_SNAKE_CASE     ):
       _UpperCamelCase    		=      """m2m_100"""
       _UpperCamelCase    		=      ["""past_key_values"""]
       _UpperCamelCase    		=      {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

       def __init__(			self	,						a__=12_81_12	,						a__=10_24	,						a__=12	,						a__=40_96	,						a__=16	,						a__=12	,						a__=40_96	,						a__=16	,						a__=0.05	,						a__=0.05	,						a__=True	,						a__=True	,						a__="relu"	,						a__=10_24	,						a__=0.1	,						a__=0.1	,						a__=0.0	,						a__=0.02	,						a__=2	,						a__=True	,						a__=1	,						a__=0	,						a__=2	,						**a__	,						):






        '''simple docstring'''






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

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







class      lowerCamelCase	(  SCREAMING_SNAKE_CASE     ):

       @property
       def 				UpperCAmelCase				(			self					):






        '''simple docstring'''






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

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

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

       def 				UpperCAmelCase				(			self	,						a__	,						a__ = -1	,						a__ = -1	,						a__ = False	,						a__ = None	,						):






        '''simple docstring'''






        snake_case   : str	  =      compute_effective_axis_dimension(
            a__	,						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
        snake_case   : Tuple	  =      tokenizer.num_special_tokens_to_add(a__					)
        snake_case   : List[Any]	  =      compute_effective_axis_dimension(
            a__	,						fixed_dimension=OnnxConfig.default_fixed_sequence	,						num_token_to_add=a__					)

        # Generate dummy inputs according to compute batch and sequence
        snake_case   : Union[str, Any]	  =      [''' '''.join([tokenizer.unk_token]					) * seq_length] * batch_size
        snake_case   : List[str]	  =      dict(tokenizer(a__	,						return_tensors=a__					)					)
        return common_inputs

       def 				UpperCAmelCase				(			self	,						a__	,						a__ = -1	,						a__ = -1	,						a__ = False	,						a__ = None	,						):






        '''simple docstring'''






        snake_case   : Optional[int]	  =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            a__	,						a__	,						a__	,						a__	,						a__					)

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

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

         snake_case   : Optional[Any]	  =      torch.cat(
             [common_inputs['''decoder_attention_mask'''], torch.ones(a__	,						a__					)]	,						dim=1					)

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

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



       _UpperCamelCase    		=      _generate_dummy_inputs_for_default_and_seqaseq_lm










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

a				:							Union[str, Any]					=			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    _UpperCAmelCase	(   cls       )		->str:


   '''simple docstring'''
   return cls






def    _UpperCAmelCase	(   __lowerCamelCase ,     __lowerCamelCase = None ,     __lowerCamelCase = None ,     __lowerCamelCase = None ,     __lowerCamelCase = None ,     )		->Union[str, Any]:


 '''simple docstring'''
 snake_case   : Union[str, Any]	  =      device
 snake_case   : Any	  =      shape[0]

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

 if generator is not None:
  snake_case   : Any	  =      generator.device.type if not isinstance(__lowerCamelCase ,     __lowerCamelCase       ) else generator[0].device.type
  if gen_device_type != device.type and gen_device_type == "cpu":
   snake_case   : Optional[int]	  =      '''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(__lowerCamelCase ,     __lowerCamelCase       ):
  snake_case   : Tuple	  =      (1,) + shape[1:]
  snake_case   : List[Any]	  =      [
      torch.randn(__lowerCamelCase ,     generator=generator[i] ,     device=__lowerCamelCase ,     dtype=__lowerCamelCase ,     layout=__lowerCamelCase       )
      for i in range(__lowerCamelCase       )
  ]
  snake_case   : int	  =      torch.cat(__lowerCamelCase ,     dim=0       ).to(__lowerCamelCase       )
 else:
  snake_case   : Union[str, Any]	  =      torch.randn(__lowerCamelCase ,     generator=__lowerCamelCase ,     device=__lowerCamelCase ,     dtype=__lowerCamelCase ,     layout=__lowerCamelCase       ).to(__lowerCamelCase       )

 return latents






def    _UpperCAmelCase	(   __lowerCamelCase       )		->Tuple:


 '''simple docstring'''
 if is_torch_version('''<''' ,     '''2.0.0'''       ) or not hasattr(__lowerCamelCase ,     '''_dynamo'''       ):
  return False
 return isinstance(__lowerCamelCase ,     torch._dynamo.eval_frame.OptimizedModule       )






