'''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


_lowerCamelCase :				Tuple														= logging.get_logger(__name__)

_lowerCamelCase :				List[str]														= {
    """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__  (   __snake_case     ):
   __UpperCAmelCase           =						"""m2m_100"""
   __UpperCAmelCase           =						["""past_key_values"""]
   __UpperCAmelCase           =						{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



   def __init__(    self					,      lowerCAmelCase__=128_112					,      lowerCAmelCase__=1_024					,      lowerCAmelCase__=12					,      lowerCAmelCase__=4_096					,      lowerCAmelCase__=16					,      lowerCAmelCase__=12					,      lowerCAmelCase__=4_096					,      lowerCAmelCase__=16					,      lowerCAmelCase__=0.05					,      lowerCAmelCase__=0.05					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__="relu"					,      lowerCAmelCase__=1_024					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=0.0					,      lowerCAmelCase__=0.02					,      lowerCAmelCase__=2					,      lowerCAmelCase__=True					,      lowerCAmelCase__=1					,      lowerCAmelCase__=0					,      lowerCAmelCase__=2					,      **lowerCAmelCase__					,      )				->     str:
        """simple docstring"""





        _UpperCamelCase  :Any       =vocab_size
        _UpperCamelCase  :str       =max_position_embeddings
        _UpperCamelCase  :int       =d_model
        _UpperCamelCase  :int       =encoder_ffn_dim
        _UpperCamelCase  :int       =encoder_layers
        _UpperCamelCase  :Tuple       =encoder_attention_heads
        _UpperCamelCase  :Optional[int]       =decoder_ffn_dim
        _UpperCamelCase  :int       =decoder_layers
        _UpperCamelCase  :Any       =decoder_attention_heads
        _UpperCamelCase  :Union[str, Any]       =dropout
        _UpperCamelCase  :int       =attention_dropout
        _UpperCamelCase  :List[str]       =activation_dropout
        _UpperCamelCase  :str       =activation_function
        _UpperCamelCase  :List[str]       =init_std
        _UpperCamelCase  :str       =encoder_layerdrop
        _UpperCamelCase  :Any       =decoder_layerdrop
        _UpperCamelCase  :int       =use_cache
        _UpperCamelCase  :Union[str, Any]       =encoder_layers
        _UpperCamelCase  :str       =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    lowerCamelCase__  (   __snake_case     ):



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





        _UpperCamelCase  :Union[str, Any]       =OrderedDict(
            [
                ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
            ]     )

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

        if self.use_past:
             self.fill_with_past_key_values_(lowerCAmelCase__					,      direction="""inputs"""     )
        return common_inputs



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__ = -1					,      lowerCAmelCase__ = -1					,      lowerCAmelCase__ = False					,      lowerCAmelCase__ = None					,      )				->     Mapping[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :Any       =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
        _UpperCamelCase  :Dict       =tokenizer.num_special_tokens_to_add(lowerCAmelCase__     )
        _UpperCamelCase  :Dict       =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
        _UpperCamelCase  :Optional[int]       =[""" """.join([tokenizer.unk_token]     ) * seq_length] * batch_size
        _UpperCamelCase  :int       =dict(tokenizer(lowerCAmelCase__					,      return_tensors=lowerCAmelCase__     )     )
        return common_inputs



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__ = -1					,      lowerCAmelCase__ = -1					,      lowerCAmelCase__ = False					,      lowerCAmelCase__ = None					,      )				->     Mapping[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :Union[str, Any]       =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__     )

        # Generate decoder inputs
        _UpperCamelCase  :int       =seq_length if not self.use_past else 1
        _UpperCamelCase  :Optional[int]       =self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__     )
        _UpperCamelCase  :List[str]       ={f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
        _UpperCamelCase  :Tuple       =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
             _UpperCamelCase       ,      _UpperCamelCase  :Any       =common_inputs["""input_ids"""].shape
             _UpperCamelCase  :Tuple       =common_inputs["""decoder_input_ids"""].shape[1]
             _UpperCamelCase       ,      _UpperCamelCase  :Any       =self.num_attention_heads
             _UpperCamelCase  :List[str]       =(
                 batch,
                 num_encoder_attention_heads,
                 encoder_seq_length,
                 self._config.hidden_size // num_encoder_attention_heads,
             )
             _UpperCamelCase  :Tuple       =decoder_seq_length + 3
             _UpperCamelCase  :Tuple       =(
                 batch,
                 num_decoder_attention_heads,
                 decoder_past_length,
                 self._config.hidden_size // num_decoder_attention_heads,
             )

             _UpperCamelCase  :Tuple       =torch.cat(
                 [common_inputs["""decoder_attention_mask"""], torch.ones(lowerCAmelCase__					,      lowerCAmelCase__     )]					,      dim=1     )

             _UpperCamelCase  :List[Any]       =[]
             # If the number of encoder and decoder layers are present in the model configuration, both are considered
             _UpperCamelCase       ,      _UpperCamelCase  :int       =self.num_layers
             _UpperCamelCase  :Dict       =min(lowerCAmelCase__					,      lowerCAmelCase__     )
             _UpperCamelCase  :Union[str, Any]       =max(lowerCAmelCase__					,      lowerCAmelCase__     ) - min_num_layers
             _UpperCamelCase  :Union[str, Any]       ="""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.
             _UpperCamelCase  :List[Any]       =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




   __UpperCAmelCase           =						_generate_dummy_inputs_for_default_and_seqaseq_lm



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


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

UpperCamelCase			:					Tuple         =							{
    '''mnaylor/mega-base-wikitext''': '''https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json''',
}

class      __snake_case				(   _UpperCAmelCase):
					SCREAMING_SNAKE_CASE_		:       str								=			"mega"
					def __init__(						self       :						Optional[int]	,      _lowerCAmelCase       :						List[Any]=3_0_5_2_2	,      _lowerCAmelCase       :						List[str]=1_2_8	,      _lowerCAmelCase       :						Dict=4	,      _lowerCAmelCase       :						Optional[Any]=2_5_6	,      _lowerCAmelCase       :						Optional[Any]=1_6	,      _lowerCAmelCase       :						Dict=True	,      _lowerCAmelCase       :						Optional[Any]=6_4	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						Optional[int]=-1	,      _lowerCAmelCase       :						Any=None	,      _lowerCAmelCase       :						List[Any]=True	,      _lowerCAmelCase       :						List[Any]="scalenorm"	,      _lowerCAmelCase       :						str=True	,      _lowerCAmelCase       :						Union[str, Any]="silu"	,      _lowerCAmelCase       :						Optional[Any]="softmax"	,      _lowerCAmelCase       :						Optional[Any]=0.1	,      _lowerCAmelCase       :						Dict=0.1	,      _lowerCAmelCase       :						Any=0.1	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						Any=True	,      _lowerCAmelCase       :						int=2_5_6	,      _lowerCAmelCase       :						List[str]=True	,      _lowerCAmelCase       :						str=0.1	,      _lowerCAmelCase       :						List[str]=2_0_4_8	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Dict=0.02	,      _lowerCAmelCase       :						str=0.2	,      _lowerCAmelCase       :						List[str]=0.02	,      _lowerCAmelCase       :						int=1.0	,      _lowerCAmelCase       :						Optional[int]=1	,      _lowerCAmelCase       :						List[str]=0	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Optional[Any]="rotary"	,      _lowerCAmelCase       :						int=None	,      _lowerCAmelCase       :						int=True	,      _lowerCAmelCase       :						Optional[Any]=True	,      **_lowerCAmelCase       :						str	,      )							->					Any:



											'''simple docstring'''





											super().__init__(pad_token_id=_lowerCAmelCase	,      bos_token_id=_lowerCAmelCase	,      eos_token_id=_lowerCAmelCase	,      **_lowerCAmelCase  )

											__magic_name__					=      vocab_size
											__magic_name__					=      hidden_size
											__magic_name__					=      num_hidden_layers
											__magic_name__					=      activation
											__magic_name__					=      attention_activation
											__magic_name__					=      intermediate_size
											__magic_name__					=      ema_projection_size
											__magic_name__					=      bidirectional
											__magic_name__					=      shared_representation_size
											__magic_name__					=      use_chunking
											__magic_name__					=      chunk_size
											__magic_name__					=      truncation
											__magic_name__					=      normalize_before_mega
											__magic_name__					=      normalization_type
											__magic_name__					=      norm_affine
											__magic_name__					=      dropout_prob
											__magic_name__					=      hidden_dropout_prob
											__magic_name__					=      attention_probs_dropout_prob
											__magic_name__					=      use_feature_dropout
											__magic_name__					=      use_normalized_ffn
											__magic_name__					=      nffn_hidden_size
											__magic_name__					=      normalize_before_ffn
											__magic_name__					=      nffn_activation_dropout_prob
											__magic_name__					=      max_positions
											__magic_name__					=      add_token_type_embeddings
											__magic_name__					=      type_vocab_size
											__magic_name__					=      initializer_range
											__magic_name__					=      ema_delta_alpha_range
											__magic_name__					=      ema_beta_range
											__magic_name__					=      ema_gamma_omega_range
											__magic_name__					=      relative_positional_bias
											__magic_name__					=      use_cache
											__magic_name__					=      classifier_dropout
											__magic_name__					=      add_lm_hidden_dense_layer
											__magic_name__					=      1  # not used but required by Hugging Face







class      __snake_case				(   _UpperCAmelCase):
					@property
					def 	a			(						self       :						int  )							->					Mapping[str, Mapping[int, str]]:



											'''simple docstring'''





											if self.task == "multiple-choice":
																	__magic_name__					=      {0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																	__magic_name__					=      {0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('input_ids', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]  )

