





'''simple docstring'''






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


snake_case_     			=						logging.get_logger(__name__)

snake_case_     			=						{
    "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 	_lowercase    (				lowercase__					):
   _UpperCamelCase									= """m2m_100"""
   _UpperCamelCase									= ["""past_key_values"""]
   _UpperCamelCase									= {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


   def __init__(			self     , _UpperCAmelCase=128_112     , _UpperCAmelCase=1_024     , _UpperCAmelCase=12     , _UpperCAmelCase=4_096     , _UpperCAmelCase=16     , _UpperCAmelCase=12     , _UpperCAmelCase=4_096     , _UpperCAmelCase=16     , _UpperCAmelCase=0.05     , _UpperCAmelCase=0.05     , _UpperCAmelCase=True     , _UpperCAmelCase=True     , _UpperCAmelCase="relu"     , _UpperCAmelCase=1_024     , _UpperCAmelCase=0.1     , _UpperCAmelCase=0.1     , _UpperCAmelCase=0.0     , _UpperCAmelCase=0.02     , _UpperCAmelCase=2     , _UpperCAmelCase=True     , _UpperCAmelCase=1     , _UpperCAmelCase=0     , _UpperCAmelCase=2     , **_UpperCAmelCase     , ):
       A							:							str				=    vocab_size
       A							:							List[Any]				=    max_position_embeddings
       A							:							Union[str, Any]				=    d_model
       A							:							Any				=    encoder_ffn_dim
       A							:							str				=    encoder_layers
       A							:							int				=    encoder_attention_heads
       A							:							Union[str, Any]				=    decoder_ffn_dim
       A							:							Optional[int]				=    decoder_layers
       A							:							List[str]				=    decoder_attention_heads
       A							:							int				=    dropout
       A							:							Any				=    attention_dropout
       A							:							int				=    activation_dropout
       A							:							Tuple				=    activation_function
       A							:							List[Any]				=    init_std
       A							:							Dict				=    encoder_layerdrop
       A							:							Optional[Any]				=    decoder_layerdrop
       A							:							Any				=    use_cache
       A							:							Optional[Any]				=    encoder_layers
       A							:							Any				=    scale_embedding  # scale factor will be sqrt(d_model) if True

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







class 	_lowercase    (				lowercase__					):


   @property
   def        snake_case					(			self       ):
       A							:							Optional[Any]				=    OrderedDict(
           [
               ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
               ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
           ]       )

       if self.use_past:
           A							:							Tuple				=    {0: '''batch'''}
           A							:							Any				=    {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
       else:
           A							:							Dict				=    {0: '''batch''', 1: '''decoder_sequence'''}
           A							:							int				=    {0: '''batch''', 1: '''decoder_sequence'''}

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


   def        snake_case					(			self     , _UpperCAmelCase     , _UpperCAmelCase = -1     , _UpperCAmelCase = -1     , _UpperCAmelCase = False     , _UpperCAmelCase = None     , ):
       # Copied from OnnxConfig.generate_dummy_inputs
       # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
       # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
       A							:							int				=    compute_effective_axis_dimension(
           _lowercase     , 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
       A							:							List[Any]				=    tokenizer.num_special_tokens_to_add(_lowercase       )
       A							:							str				=    compute_effective_axis_dimension(
           _lowercase     , fixed_dimension=OnnxConfig.default_fixed_sequence     , num_token_to_add=_lowercase       )

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


   def        snake_case					(			self     , _UpperCAmelCase     , _UpperCAmelCase = -1     , _UpperCAmelCase = -1     , _UpperCAmelCase = False     , _UpperCAmelCase = None     , ):
       A							:							Optional[Any]				=    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _lowercase     , _lowercase     , _lowercase     , _lowercase     , _lowercase       )

       # Generate decoder inputs
       A							:							Dict				=    seq_length if not self.use_past else 1
       A							:							Tuple				=    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _lowercase     , _lowercase     , _lowercase     , _lowercase     , _lowercase       )
       A							:							str				=    {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
       A							:							Tuple				=    dict(**_lowercase     , **_lowercase       )

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

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

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

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


   _UpperCamelCase									= _generate_dummy_inputs_for_default_and_seqaseq_lm

"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCamelCase       :    Any         =		{
    "configuration_openai": ["OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP", "OpenAIGPTConfig"],
    "tokenization_openai": ["OpenAIGPTTokenizer"],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCamelCase       :    Optional[int]         =		["OpenAIGPTTokenizerFast"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCamelCase       :    List[Any]         =		[
      "OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "OpenAIGPTDoubleHeadsModel",
      "OpenAIGPTForSequenceClassification",
      "OpenAIGPTLMHeadModel",
      "OpenAIGPTModel",
      "OpenAIGPTPreTrainedModel",
      "load_tf_weights_in_openai_gpt",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCamelCase       :    Tuple         =		[
      "TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFOpenAIGPTDoubleHeadsModel",
      "TFOpenAIGPTForSequenceClassification",
      "TFOpenAIGPTLMHeadModel",
      "TFOpenAIGPTMainLayer",
      "TFOpenAIGPTModel",
      "TFOpenAIGPTPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_openai import OPENAI_GPT_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenAIGPTConfig
  from .tokenization_openai import OpenAIGPTTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_openai_fast import OpenAIGPTTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_openai import (
        OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        OpenAIGPTDoubleHeadsModel,
        OpenAIGPTForSequenceClassification,
        OpenAIGPTLMHeadModel,
        OpenAIGPTModel,
        OpenAIGPTPreTrainedModel,
        load_tf_weights_in_openai_gpt,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_openai import (
        TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFOpenAIGPTDoubleHeadsModel,
        TFOpenAIGPTForSequenceClassification,
        TFOpenAIGPTLMHeadModel,
        TFOpenAIGPTMainLayer,
        TFOpenAIGPTModel,
        TFOpenAIGPTPreTrainedModel,
    )

else:
  import sys

  __UpperCamelCase       :    str         =		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)