
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


__lowerCAmelCase   :     Optional[int]      =						logging.get_logger(__name__)

__lowerCAmelCase   :     List[Any]      =						{
    "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   __lowerCAmelCase		(		lowerCAmelCase_ ):



      """simple docstring"""







      A__		:     Dict       				=      '''mbart'''
      A__		:     Tuple       				=      ['''past_key_values''']
      A__		:     Dict       				=      {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}




      def __init__( self						:    Union[str, Any]			,    _snake_case						:    int=5_0265			,    _snake_case						:    Any=1024			,    _snake_case						:    Optional[Any]=12			,    _snake_case						:    str=4096			,    _snake_case						:    Tuple=16			,    _snake_case						:    Dict=12			,    _snake_case						:    str=4096			,    _snake_case						:    Dict=16			,    _snake_case						:    Union[str, Any]=0.0			,    _snake_case						:    List[str]=0.0			,    _snake_case						:    Optional[Any]=True			,    _snake_case						:    Tuple=True			,    _snake_case						:    Dict="gelu"			,    _snake_case						:    int=1024			,    _snake_case						:    str=0.1			,    _snake_case						:    Optional[int]=0.0			,    _snake_case						:    List[Any]=0.0			,    _snake_case						:    List[str]=0.02			,    _snake_case						:    Any=0.0			,    _snake_case						:    List[str]=False			,    _snake_case						:    Tuple=1			,    _snake_case						:    Dict=0			,    _snake_case						:    Tuple=2			,    _snake_case						:    int=2			,    **_snake_case						:    Any			,    ):
          __lowercase						:     List[Any]		      =    vocab_size
          __lowercase						:     Union[str, Any]		      =    max_position_embeddings
          __lowercase						:     Any		      =    d_model
          __lowercase						:     Optional[int]		      =    encoder_ffn_dim
          __lowercase						:     Any		      =    encoder_layers
          __lowercase						:     Optional[Any]		      =    encoder_attention_heads
          __lowercase						:     int		      =    decoder_ffn_dim
          __lowercase						:     Union[str, Any]		      =    decoder_layers
          __lowercase						:     str		      =    decoder_attention_heads
          __lowercase						:     Optional[int]		      =    dropout
          __lowercase						:     List[str]		      =    attention_dropout
          __lowercase						:     str		      =    activation_dropout
          __lowercase						:     Union[str, Any]		      =    activation_function
          __lowercase						:     int		      =    init_std
          __lowercase						:     Dict		      =    encoder_layerdrop
          __lowercase						:     Dict		      =    decoder_layerdrop
          __lowercase						:     int		      =    classifier_dropout
          __lowercase						:     Tuple		      =    use_cache
          __lowercase						:     Any		      =    encoder_layers
          __lowercase						:     Optional[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   __lowerCAmelCase		(		lowerCAmelCase_ ):



      """simple docstring"""







      @property
      def 						snake_case_     ( self						:    Tuple			):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase						:     str		      =    OrderedDict(
                  [
                      ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                      ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                  ]			)

              if self.use_past:
                  __lowercase						:     Optional[Any]		      =    {0: '''batch'''}
                  __lowercase						:     List[Any]		      =    {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
              else:
                  __lowercase						:     Dict		      =    {0: '''batch''', 1: '''decoder_sequence'''}
                  __lowercase						:     Optional[int]		      =    {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.
              __lowercase						:     int		      =    OrderedDict(
                  [
                      ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                      ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                  ]			)
              if self.use_past:
                  __lowercase      , __lowercase						:     Optional[Any]		      =    self.num_layers
                  for i in range(_snake_case			):
                      __lowercase						:     List[str]		      =    {0: '''batch''', 2: '''past_sequence + sequence'''}
                      __lowercase						:     Tuple		      =    {0: '''batch''', 2: '''past_sequence + sequence'''}
          else:
              __lowercase						:     Optional[Any]		      =    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 						snake_case_     ( self						:    List[str]			):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase						:     Optional[Any]		      =    super().outputs
          else:
              __lowercase						:     Any		      =    super(_snake_case			,    self			).outputs
              if self.use_past:
                  __lowercase      , __lowercase						:     List[Any]		      =    self.num_layers
                  for i in range(_snake_case			):
                      __lowercase						:     Optional[int]		      =    {0: '''batch''', 2: '''past_sequence + sequence'''}
                      __lowercase						:     Optional[Any]		      =    {0: '''batch''', 2: '''past_sequence + sequence'''}
          return common_outputs




      def 						snake_case_     ( self						:    Union[str, Any]			,    _snake_case						:    PreTrainedTokenizer			,    _snake_case						:    int = -1			,    _snake_case						:    int = -1			,    _snake_case						:    bool = False			,    _snake_case						:    Optional[TensorType] = None			,    ):
          __lowercase						:     Dict		      =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              _snake_case			,    _snake_case			,    _snake_case			,    _snake_case			,    _snake_case			)

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

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

              __lowercase						:     str		      =    []
              # If the number of encoder and decoder layers are present in the model configuration, both are considered
              __lowercase      , __lowercase						:     Tuple		      =    self.num_layers
              __lowercase						:     List[str]		      =    min(_snake_case			,    _snake_case			)
              __lowercase						:     Union[str, Any]		      =    max(_snake_case			,    _snake_case			) - min_num_layers
              __lowercase						:     List[str]		      =    '''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.
              __lowercase						:     Any		      =    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 						snake_case_     ( self						:    List[Any]			,    _snake_case						:    PreTrainedTokenizer			,    _snake_case						:    int = -1			,    _snake_case						:    int = -1			,    _snake_case						:    bool = False			,    _snake_case						:    Optional[TensorType] = None			,    ):
          __lowercase						:     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
              __lowercase      , __lowercase						:     str		      =    common_inputs['''input_ids'''].shape
              # Not using the same length for past_key_values
              __lowercase						:     Optional[int]		      =    seqlen + 2
              __lowercase      , __lowercase						:     Union[str, Any]		      =    self.num_layers
              __lowercase      , __lowercase						:     Union[str, Any]		      =    self.num_attention_heads
              __lowercase						:     str		      =    (
                  batch,
                  num_encoder_attention_heads,
                  past_key_values_length,
                  self._config.hidden_size // num_encoder_attention_heads,
              )

              __lowercase						:     Tuple		      =    common_inputs['''attention_mask'''].dtype
              __lowercase						:     Tuple		      =    torch.cat(
                  [common_inputs['''attention_mask'''], torch.ones(_snake_case			,    _snake_case			,    dtype=_snake_case			)]			,    dim=1			)
              __lowercase						:     Optional[int]		      =    [
                  (torch.zeros(_snake_case			), torch.zeros(_snake_case			)) for _ in range(_snake_case			)
              ]
          return common_inputs




      def 						snake_case_     ( self						:    Optional[int]			,    _snake_case						:    PreTrainedTokenizer			,    _snake_case						:    int = -1			,    _snake_case						:    int = -1			,    _snake_case						:    bool = False			,    _snake_case						:    Optional[TensorType] = 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
          __lowercase						:     str		      =    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
          __lowercase						:     str		      =    tokenizer.num_special_tokens_to_add(_snake_case			)
          __lowercase						:     Dict		      =    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
          __lowercase						:     Union[str, Any]		      =    [''' '''.join([tokenizer.unk_token]			) * seq_length] * batch_size
          __lowercase						:     List[Any]		      =    dict(tokenizer(_snake_case			,    return_tensors=_snake_case			)			)
          return common_inputs




      def 						snake_case_     ( self						:    Optional[Any]			,    _snake_case						:    PreTrainedTokenizer			,    _snake_case						:    int = -1			,    _snake_case						:    int = -1			,    _snake_case						:    bool = False			,    _snake_case						:    Optional[TensorType] = None			,    ):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase						:     List[str]		      =    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":
              __lowercase						:     Union[str, Any]		      =    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:
              __lowercase						:     str		      =    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 						snake_case_     ( self						:    List[str]			,    _snake_case						:    Optional[int]			,    _snake_case						:    Tuple			,    _snake_case						:    Union[str, Any]			,    _snake_case						:    str			):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase						:     int		      =    super()._flatten_past_key_values_(_snake_case			,    _snake_case			,    _snake_case			,    _snake_case			)
          else:
              __lowercase						:     List[Any]		      =    super(_snake_case			,    self			)._flatten_past_key_values_(
                  _snake_case			,    _snake_case			,    _snake_case			,    _snake_case			)




import importlib.metadata
import sys

from transformers.testing_utils import TestCasePlus
from transformers.utils.versions import require_version, require_version_core


__lowerCAmelCase   :     Optional[Any]      =						importlib.metadata.version("numpy")
__lowerCAmelCase   :     Any      =						".".join([str(x) for x in sys.version_info[:3]])


class   __lowerCAmelCase		(		lowerCAmelCase_ ):



      """simple docstring"""







      def 						snake_case_     ( self						:    List[Any]			):
          # lt + different version strings
          require_version_core('''numpy<1000.4.5'''			)
          require_version_core('''numpy<1000.4'''			)
          require_version_core('''numpy<1000'''			)

          # le
          require_version_core('''numpy<=1000.4.5'''			)
          require_version_core(F'numpy<={numpy_ver}'			)

          # eq
          require_version_core(F'numpy=={numpy_ver}'			)

          # ne
          require_version_core('''numpy!=1000.4.5'''			)

          # ge
          require_version_core('''numpy>=1.0'''			)
          require_version_core('''numpy>=1.0.0'''			)
          require_version_core(F'numpy>={numpy_ver}'			)

          # gt
          require_version_core('''numpy>1.0.0'''			)

          # mix
          require_version_core('''numpy>1.0.0,<1000'''			)

          # requirement w/o version
          require_version_core('''numpy'''			)

          # unmet requirements due to version conflict
          for req in ["numpy==1.0.0", "numpy>=1000.0.0", F'numpy<{numpy_ver}']:
              try:
                  require_version_core(_snake_case			)
              except ImportError as e:
                  self.assertIn(F'{req} is required'			,    str(_snake_case			)			)
                  self.assertIn('''but found'''			,    str(_snake_case			)			)

        # unmet requirements due to missing module
          for req in ["numpipypie>1", "numpipypie2"]:
              try:
                  require_version_core(_snake_case			)
              except importlib.metadata.PackageNotFoundError as e:
                  self.assertIn(F'The \'{req}\' distribution was not found and is required by this application'			,    str(_snake_case			)			)
                  self.assertIn('''Try: pip install transformers -U'''			,    str(_snake_case			)			)

        # bogus requirements formats:
        # 1. whole thing
          for req in ["numpy??1.0.0", "numpy1.0.0"]:
              try:
                  require_version_core(_snake_case			)
              except ValueError as e:
                  self.assertIn('''requirement needs to be in the pip package format'''			,    str(_snake_case			)			)
        # 2. only operators
          for req in ["numpy=1.0.0", "numpy == 1.00", "numpy<>1.0.0", "numpy><1.00", "numpy>>1.0.0"]:
              try:
                  require_version_core(_snake_case			)
              except ValueError as e:
                  self.assertIn('''need one of '''			,    str(_snake_case			)			)




      def 						snake_case_     ( self						:    List[str]			):
          # matching requirement
          require_version('''python>=3.6.0'''			)

          # not matching requirements
          for req in ["python>9.9.9", "python<3.0.0"]:
              try:
                  require_version_core(_snake_case			)
              except ImportError as e:
                  self.assertIn(F'{req} is required'			,    str(_snake_case			)			)
                  self.assertIn(F'but found python=={python_ver}'			,    str(_snake_case			)			)


