





import os
import pickle
import tempfile
import unittest
from typing import Callable, Optional

import numpy as np

from transformers import (
    BatchEncoding,
    BertTokenizer,
    BertTokenizerFast,
    PreTrainedTokenizer,
    PreTrainedTokenizerFast,
    TensorType,
    TokenSpan,
    is_tokenizers_available,
)
from transformers.models.gpta.tokenization_gpta import GPTaTokenizer
from transformers.testing_utils import CaptureStderr, require_flax, require_tf, require_tokenizers, require_torch, slow


if is_tokenizers_available():
     from tokenizers import Tokenizer
     from tokenizers.models import WordPiece




class 			_lowerCamelCase	(      unittest.TestCase      ):



      """simple docstring"""






      def   UpperCAmelCase						(	self			,  UpperCAmelCase				)    ->				List[Any]:






        '''simple docstring'''






        __snake_case				:			int            =    list(tokenizer_class.max_model_input_sizes.keys()				)
        for model_name in sa_models[:1]:
          __snake_case				:			Optional[Any]            =    tokenizer_class.from_pretrained(UpperCAmelCase				)
          self.assertIsNotNone(UpperCAmelCase				)
          self.assertIsInstance(UpperCAmelCase			,  UpperCAmelCase				)
          self.assertIsInstance(UpperCAmelCase			,  UpperCAmelCase				)

          for special_tok in tokenizer.all_special_tokens:
            self.assertIsInstance(UpperCAmelCase			,  UpperCAmelCase				)
            __snake_case				:			Tuple            =    tokenizer.convert_tokens_to_ids(UpperCAmelCase				)
            self.assertIsInstance(UpperCAmelCase			,  UpperCAmelCase				)







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase = None				)    ->				Union[str, Any]:






        '''simple docstring'''






        __snake_case				:			Union[str, Any]            =    pickle.dumps(UpperCAmelCase				)
        self.assertIsNotNone(UpperCAmelCase				)

        __snake_case				:			Optional[int]            =    pickle.loads(UpperCAmelCase				)

        # Ensure is_fast is correctly restored
        self.assertEqual(be_restored.is_fast			,  be_original.is_fast				)

        # Ensure encodings are potentially correctly restored
        if be_original.is_fast:
          self.assertIsNotNone(be_restored.encodings				)
        else:
          self.assertIsNone(be_restored.encodings				)

        # Ensure the keys are the same
        for original_v, restored_v in zip(be_original.values()			,  be_restored.values()				):
          if equal_op:
            self.assertTrue(equal_op(UpperCAmelCase			,  UpperCAmelCase				)				)
          else:
            self.assertEqual(UpperCAmelCase			,  UpperCAmelCase				)







      @slow
      def   UpperCAmelCase						(	self				)    ->				Optional[int]:






        '''simple docstring'''






        self.check_tokenizer_from_pretrained(UpperCAmelCase				)







      def   UpperCAmelCase						(	self				)    ->				List[Any]:






        '''simple docstring'''






        self.assertEqual(TensorType("tf"				)			,  TensorType.TENSORFLOW				)
        self.assertEqual(TensorType("pt"				)			,  TensorType.PYTORCH				)
        self.assertEqual(TensorType("np"				)			,  TensorType.NUMPY				)







      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				List[str]:






        '''simple docstring'''






        import numpy as np

        __snake_case				:			Optional[int]            =    BertTokenizer.from_pretrained("bert-base-cased"				)
        __snake_case				:			Union[str, Any]            =    BertTokenizerFast.from_pretrained("bert-base-cased"				)

        # Python no tensor
        with self.subTest("BatchEncoding (Python, return_tensors=None)"				):
          self.assert_dump_and_restore(tokenizer_p("Small example to encode"				)				)

        with self.subTest("BatchEncoding (Python, return_tensors=NUMPY)"				):
          self.assert_dump_and_restore(
              tokenizer_p("Small example to encode"			,  return_tensors=TensorType.NUMPY				)			,  np.array_equal				)

        with self.subTest("BatchEncoding (Rust, return_tensors=None)"				):
          self.assert_dump_and_restore(tokenizer_r("Small example to encode"				)				)

        with self.subTest("BatchEncoding (Rust, return_tensors=NUMPY)"				):
          self.assert_dump_and_restore(
              tokenizer_r("Small example to encode"			,  return_tensors=TensorType.NUMPY				)			,  np.array_equal				)







      @require_tf
      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				Optional[Any]:






        '''simple docstring'''






        import tensorflow as tf

        def tf_array_equals(UpperCAmelCase			,  UpperCAmelCase				):
          return tf.reduce_all(tf.equal(UpperCAmelCase			,  UpperCAmelCase				)				)

        __snake_case				:			int            =    BertTokenizer.from_pretrained("bert-base-cased"				)
        __snake_case				:			List[str]            =    BertTokenizerFast.from_pretrained("bert-base-cased"				)

        with self.subTest("BatchEncoding (Python, return_tensors=TENSORFLOW)"				):
          self.assert_dump_and_restore(
              tokenizer_p("Small example to encode"			,  return_tensors=TensorType.TENSORFLOW				)			,  UpperCAmelCase				)

        with self.subTest("BatchEncoding (Rust, return_tensors=TENSORFLOW)"				):
          self.assert_dump_and_restore(
              tokenizer_r("Small example to encode"			,  return_tensors=TensorType.TENSORFLOW				)			,  UpperCAmelCase				)







      @require_torch
      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				Union[str, Any]:






        '''simple docstring'''






        import torch

        __snake_case				:			List[str]            =    BertTokenizer.from_pretrained("bert-base-cased"				)
        __snake_case				:			Any            =    BertTokenizerFast.from_pretrained("bert-base-cased"				)

        with self.subTest("BatchEncoding (Python, return_tensors=PYTORCH)"				):
          self.assert_dump_and_restore(
              tokenizer_p("Small example to encode"			,  return_tensors=TensorType.PYTORCH				)			,  torch.equal				)

        with self.subTest("BatchEncoding (Rust, return_tensors=PYTORCH)"				):
          self.assert_dump_and_restore(
              tokenizer_r("Small example to encode"			,  return_tensors=TensorType.PYTORCH				)			,  torch.equal				)







      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				int:






        '''simple docstring'''






        __snake_case				:			Optional[Any]            =    BertTokenizer.from_pretrained("bert-base-cased"				)
        __snake_case				:			Optional[Any]            =    BertTokenizerFast.from_pretrained("bert-base-cased"				)

        with self.subTest("Python Tokenizer"				):
          self.assertFalse(tokenizer_p("Small example to_encode"				).is_fast				)

        with self.subTest("Rust Tokenizer"				):
          self.assertTrue(tokenizer_r("Small example to_encode"				).is_fast				)







      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				List[Any]:






        '''simple docstring'''






        __snake_case				:			Tuple            =    BertTokenizerFast.from_pretrained("bert-base-cased"				)
        __snake_case				:			str            =    tokenizer_r(["Test", "\xad", "test"]			,  is_split_into_words=UpperCAmelCase				)

        self.assertEqual(encoded.word_to_tokens(0				)			,  TokenSpan(start=1			,  end=2				)				)
        self.assertEqual(encoded.word_to_tokens(1				)			,  UpperCAmelCase				)
        self.assertEqual(encoded.word_to_tokens(2				)			,  TokenSpan(start=2			,  end=3				)				)







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






        '''simple docstring'''






        __snake_case				:			Union[str, Any]            =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}				)
        __snake_case				:			Any            =    batch.convert_to_tensors(tensor_type="np"				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (2, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (2,)				)
        # test converting the converted
        with CaptureStderr() as cs:
          __snake_case				:			List[Any]            =    batch.convert_to_tensors(tensor_type="np"				)
        self.assertFalse(len(cs.err				)			,  msg=F"""should have no warning, but got {cs.err}"""				)

        __snake_case				:			List[Any]            =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}				)
        __snake_case				:			Optional[Any]            =    batch.convert_to_tensors(tensor_type="np"			,  prepend_batch_axis=UpperCAmelCase				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (1, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (1,)				)







      @require_torch
      def   UpperCAmelCase						(	self				)    ->				Optional[Any]:






        '''simple docstring'''






        __snake_case				:			Dict            =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}				)
        __snake_case				:			Tuple            =    batch.convert_to_tensors(tensor_type="pt"				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (2, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (2,)				)
        # test converting the converted
        with CaptureStderr() as cs:
          __snake_case				:			Dict            =    batch.convert_to_tensors(tensor_type="pt"				)
        self.assertFalse(len(cs.err				)			,  msg=F"""should have no warning, but got {cs.err}"""				)

        __snake_case				:			List[Any]            =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}				)
        __snake_case				:			int            =    batch.convert_to_tensors(tensor_type="pt"			,  prepend_batch_axis=UpperCAmelCase				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (1, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (1,)				)







      @require_tf
      def   UpperCAmelCase						(	self				)    ->				str:






        '''simple docstring'''






        __snake_case				:			List[Any]            =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}				)
        __snake_case				:			List[Any]            =    batch.convert_to_tensors(tensor_type="tf"				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (2, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (2,)				)
        # test converting the converted
        with CaptureStderr() as cs:
          __snake_case				:			Tuple            =    batch.convert_to_tensors(tensor_type="tf"				)
        self.assertFalse(len(cs.err				)			,  msg=F"""should have no warning, but got {cs.err}"""				)

        __snake_case				:			Optional[Any]            =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}				)
        __snake_case				:			Optional[Any]            =    batch.convert_to_tensors(tensor_type="tf"			,  prepend_batch_axis=UpperCAmelCase				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (1, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (1,)				)







      @require_flax
      def   UpperCAmelCase						(	self				)    ->				Any:






        '''simple docstring'''






        __snake_case				:			Dict            =    BatchEncoding({"inputs": [[1, 2, 3], [4, 5, 6]], "labels": [0, 1]}				)
        __snake_case				:			Dict            =    batch.convert_to_tensors(tensor_type="jax"				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (2, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (2,)				)
        # test converting the converted
        with CaptureStderr() as cs:
          __snake_case				:			List[str]            =    batch.convert_to_tensors(tensor_type="jax"				)
        self.assertFalse(len(cs.err				)			,  msg=F"""should have no warning, but got {cs.err}"""				)

        __snake_case				:			Optional[Any]            =    BatchEncoding({"inputs": [1, 2, 3], "labels": 0}				)
        __snake_case				:			int            =    batch.convert_to_tensors(tensor_type="jax"			,  prepend_batch_axis=UpperCAmelCase				)
        self.assertEqual(tensor_batch["inputs"].shape			,  (1, 3)				)
        self.assertEqual(tensor_batch["labels"].shape			,  (1,)				)







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






        '''simple docstring'''






        __snake_case				:			Optional[int]            =    [{"input_ids": np.array([0, 1, 2]				)}, {"input_ids": np.array([0, 1, 2, 3]				)}]
        __snake_case				:			Tuple            =    BertTokenizer.from_pretrained("bert-base-cased"				)

        __snake_case				:			List[str]            =    tokenizer.pad(UpperCAmelCase			,  padding=UpperCAmelCase				)
        self.assertTrue(isinstance(batch["input_ids"]			,  np.ndarray				)				)
        self.assertEqual(batch["input_ids"].tolist()			,  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]				)
        __snake_case				:			Union[str, Any]            =    tokenizer.pad(UpperCAmelCase			,  padding=UpperCAmelCase			,  return_tensors="np"				)
        self.assertTrue(isinstance(batch["input_ids"]			,  np.ndarray				)				)
        self.assertEqual(batch["input_ids"].tolist()			,  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]				)







      @require_torch
      def   UpperCAmelCase						(	self				)    ->				Any:






        '''simple docstring'''






        import torch

        __snake_case				:			Optional[Any]            =    [{"input_ids": torch.tensor([0, 1, 2]				)}, {"input_ids": torch.tensor([0, 1, 2, 3]				)}]
        __snake_case				:			Optional[int]            =    BertTokenizer.from_pretrained("bert-base-cased"				)

        __snake_case				:			Any            =    tokenizer.pad(UpperCAmelCase			,  padding=UpperCAmelCase				)
        self.assertTrue(isinstance(batch["input_ids"]			,  torch.Tensor				)				)
        self.assertEqual(batch["input_ids"].tolist()			,  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]				)
        __snake_case				:			Dict            =    tokenizer.pad(UpperCAmelCase			,  padding=UpperCAmelCase			,  return_tensors="pt"				)
        self.assertTrue(isinstance(batch["input_ids"]			,  torch.Tensor				)				)
        self.assertEqual(batch["input_ids"].tolist()			,  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]				)







      @require_tf
      def   UpperCAmelCase						(	self				)    ->				Optional[int]:






        '''simple docstring'''






        import tensorflow as tf

        __snake_case				:			Union[str, Any]            =    [{"input_ids": tf.constant([0, 1, 2]				)}, {"input_ids": tf.constant([0, 1, 2, 3]				)}]
        __snake_case				:			List[Any]            =    BertTokenizer.from_pretrained("bert-base-cased"				)

        __snake_case				:			Optional[Any]            =    tokenizer.pad(UpperCAmelCase			,  padding=UpperCAmelCase				)
        self.assertTrue(isinstance(batch["input_ids"]			,  tf.Tensor				)				)
        self.assertEqual(batch["input_ids"].numpy().tolist()			,  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]				)
        __snake_case				:			Optional[Any]            =    tokenizer.pad(UpperCAmelCase			,  padding=UpperCAmelCase			,  return_tensors="tf"				)
        self.assertTrue(isinstance(batch["input_ids"]			,  tf.Tensor				)				)
        self.assertEqual(batch["input_ids"].numpy().tolist()			,  [[0, 1, 2, tokenizer.pad_token_id], [0, 1, 2, 3]]				)







      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				int:






        '''simple docstring'''






        __snake_case				:			List[Any]            =    Tokenizer(WordPiece(unk_token="[UNK]"				)				)
        PreTrainedTokenizerFast(tokenizer_object=UpperCAmelCase				)







      @require_tokenizers
      def   UpperCAmelCase						(	self				)    ->				Optional[Any]:






        '''simple docstring'''






        __snake_case				:			Any            =    Tokenizer(WordPiece(unk_token="[UNK]"				)				)
        with tempfile.TemporaryDirectory() as tmpdirname:
          bert_tokenizer.save(os.path.join(UpperCAmelCase			,  "tokenizer.json"				)				)
          PreTrainedTokenizerFast(tokenizer_file=os.path.join(UpperCAmelCase			,  "tokenizer.json"				)				)








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

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


_UpperCamelCase    =	logging.get_logger(__name__)

_UpperCamelCase    =	{
    '''facebook/blenderbot-3B''': '''https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json''',
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}




class 			_lowerCamelCase	(      a      ):



      """simple docstring"""






      UpperCAmelCase_  : Tuple				      ="blenderbot"
      UpperCAmelCase_  : Any				      =["past_key_values"]
      UpperCAmelCase_  : Optional[int]				      ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}







      def __init__(	self			,  UpperCAmelCase=8008			,  UpperCAmelCase=128			,  UpperCAmelCase=2			,  UpperCAmelCase=10240			,  UpperCAmelCase=32			,  UpperCAmelCase=24			,  UpperCAmelCase=10240			,  UpperCAmelCase=32			,  UpperCAmelCase=0.0			,  UpperCAmelCase=0.0			,  UpperCAmelCase=True			,  UpperCAmelCase=True			,  UpperCAmelCase="gelu"			,  UpperCAmelCase=2560			,  UpperCAmelCase=0.1			,  UpperCAmelCase=0.0			,  UpperCAmelCase=0.0			,  UpperCAmelCase=0.02			,  UpperCAmelCase=1			,  UpperCAmelCase=False			,  UpperCAmelCase=0			,  UpperCAmelCase=1			,  UpperCAmelCase=2			,  UpperCAmelCase=3			,  UpperCAmelCase=2			,  **UpperCAmelCase			,  )    ->				List[Any]:






        '''simple docstring'''






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

        super().__init__(
            pad_token_id=UpperCAmelCase			,  bos_token_id=UpperCAmelCase			,  eos_token_id=UpperCAmelCase			,  is_encoder_decoder=UpperCAmelCase			,  decoder_start_token_id=UpperCAmelCase			,  encoder_no_repeat_ngram_size=UpperCAmelCase			,  forced_eos_token_id=UpperCAmelCase			,  **UpperCAmelCase			,  )




class 			_lowerCamelCase	(      a      ):



      """simple docstring"""






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






        '''simple docstring'''






        if self.task in ["default", "seq2seq-lm"]:
          __snake_case				:			Any            =    OrderedDict(
              [
                  ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                  ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
              ]				)
          if self.use_past:
            __snake_case				:			List[str]            =    {0: "batch"}
            __snake_case				:			Optional[Any]            =    {0: "batch", 1: "past_decoder_sequence + sequence"}
          else:
            __snake_case				:			int            =    {0: "batch", 1: "decoder_sequence"}
            __snake_case				:			Optional[int]            =    {0: "batch", 1: "decoder_sequence"}
          if self.use_past:
            self.fill_with_past_key_values_(UpperCAmelCase			,  direction="inputs"				)
        elif self.task == "causal-lm":
          __snake_case				:			List[str]            =    OrderedDict(
              [
                  ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                  ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
              ]				)
          if self.use_past:
            __snake_case      ,				__snake_case				:			List[Any]            =    self.num_layers
            for i in range(UpperCAmelCase				):
              __snake_case				:			int            =    {0: "batch", 2: "past_sequence + sequence"}
              __snake_case				:			Tuple            =    {0: "batch", 2: "past_sequence + sequence"}
        else:
          __snake_case				:			Dict            =    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
      # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
      def   UpperCAmelCase						(	self				)    ->				Mapping[str, Mapping[int, str]]:






        '''simple docstring'''






        if self.task in ["default", "seq2seq-lm"]:
          __snake_case				:			Union[str, Any]            =    super().outputs
        else:
          __snake_case				:			Dict            =    super(UpperCAmelCase			,  self				).outputs
          if self.use_past:
            __snake_case      ,				__snake_case				:			Any            =    self.num_layers
            for i in range(UpperCAmelCase				):
              __snake_case				:			Optional[int]            =    {0: "batch", 2: "past_sequence + sequence"}
              __snake_case				:			Tuple            =    {0: "batch", 2: "past_sequence + sequence"}
        return common_outputs







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






        '''simple docstring'''






        __snake_case				:			int            =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)
        # Generate decoder inputs
        __snake_case				:			List[Any]            =    seq_length if not self.use_past else 1
        __snake_case				:			Tuple            =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)
        __snake_case				:			Tuple            =    {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
        __snake_case				:			Dict            =    dict(**UpperCAmelCase			,  **UpperCAmelCase				)

        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      ,				__snake_case				:			int            =    common_inputs["input_ids"].shape
          __snake_case				:			Union[str, Any]            =    common_inputs["decoder_input_ids"].shape[1]
          __snake_case      ,				__snake_case				:			Optional[Any]            =    self.num_attention_heads
          __snake_case				:			Tuple            =    (
              batch,
              num_encoder_attention_heads,
              encoder_seq_length,
              self._config.hidden_size // num_encoder_attention_heads,
          )
          __snake_case				:			Any            =    decoder_seq_length
          __snake_case				:			List[Any]            =    (
              batch,
              num_decoder_attention_heads,
              decoder_past_length,
              self._config.hidden_size // num_decoder_attention_heads,
          )
          __snake_case				:			Any            =    torch.cat(
              [common_inputs["decoder_attention_mask"], torch.ones(UpperCAmelCase			,  UpperCAmelCase				)]			,  dim=1				)
          __snake_case				:			Any            =    []
          __snake_case      ,				__snake_case				:			Tuple            =    self.num_layers

          for _ in range(UpperCAmelCase				):
            common_inputs["past_key_values"].append(
                (
                    torch.zeros(UpperCAmelCase				),
                    torch.zeros(UpperCAmelCase				),
                    torch.zeros(UpperCAmelCase				),
                    torch.zeros(UpperCAmelCase				),
                )				)
        return common_inputs







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






        '''simple docstring'''






        __snake_case				:			List[Any]            =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
            UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)

        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      ,				__snake_case				:			List[Any]            =    common_inputs["input_ids"].shape
          __snake_case				:			Any            =    seqlen
          __snake_case      ,				__snake_case				:			Dict            =    self.num_layers
          __snake_case      ,				__snake_case				:			int            =    self.num_attention_heads
          __snake_case				:			str            =    (
              batch,
              num_encoder_attention_heads,
              past_key_values_length,
              self._config.hidden_size // num_encoder_attention_heads,
          )
          __snake_case				:			Dict            =    common_inputs["attention_mask"].dtype
          __snake_case				:			Optional[Any]            =    torch.cat(
              [common_inputs["attention_mask"], torch.ones(UpperCAmelCase			,  UpperCAmelCase			,  dtype=UpperCAmelCase				)]			,  dim=1				)
          __snake_case				:			str            =    [
              (torch.zeros(UpperCAmelCase				), torch.zeros(UpperCAmelCase				)) for _ in range(UpperCAmelCase				)
          ]
        return common_inputs







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






        '''simple docstring'''






        __snake_case				:			Tuple            =    compute_effective_axis_dimension(
            UpperCAmelCase			,  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				:			str            =    tokenizer.num_special_tokens_to_add(UpperCAmelCase				)
        __snake_case				:			Any            =    compute_effective_axis_dimension(
            UpperCAmelCase			,  fixed_dimension=OnnxConfig.default_fixed_sequence			,  num_token_to_add=UpperCAmelCase				)

        # Generate dummy inputs according to compute batch and sequence
        __snake_case				:			Tuple            =    [" ".join([tokenizer.unk_token]				) * seq_length] * batch_size
        __snake_case				:			str            =    dict(tokenizer(UpperCAmelCase			,  return_tensors=UpperCAmelCase				)				)
        return common_inputs







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






        '''simple docstring'''






        if self.task in ["default", "seq2seq-lm"]:
          __snake_case				:			Any            =    self._generate_dummy_inputs_for_default_and_seqaseq_lm(
              UpperCAmelCase			,  batch_size=UpperCAmelCase			,  seq_length=UpperCAmelCase			,  is_pair=UpperCAmelCase			,  framework=UpperCAmelCase				)

        elif self.task == "causal-lm":
          __snake_case				:			Union[str, Any]            =    self._generate_dummy_inputs_for_causal_lm(
              UpperCAmelCase			,  batch_size=UpperCAmelCase			,  seq_length=UpperCAmelCase			,  is_pair=UpperCAmelCase			,  framework=UpperCAmelCase				)
        else:
          __snake_case				:			Dict            =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              UpperCAmelCase			,  batch_size=UpperCAmelCase			,  seq_length=UpperCAmelCase			,  is_pair=UpperCAmelCase			,  framework=UpperCAmelCase				)

        return common_inputs







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)    ->				int:






        '''simple docstring'''






        if self.task in ["default", "seq2seq-lm"]:
          __snake_case				:			str            =    super()._flatten_past_key_values_(UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)
        else:
          __snake_case				:			int            =    super(UpperCAmelCase			,  self				)._flatten_past_key_values_(
              UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase				)    ->				Any:






        '''simple docstring'''






        if direction not in ["inputs", "outputs"]:
          raise ValueError(F"""direction must either be \"inputs\" or \"outputs\", but {direction} was given"""				)

        __snake_case				:			Tuple            =    "past_key_values" if direction == "inputs" else "present"
        __snake_case      ,				__snake_case				:			Any            =    self.num_layers

        __snake_case				:			int            =    "past_encoder_sequence"
        __snake_case				:			int            =    "past_decoder_sequence" if direction == "inputs" else "past_decoder_sequence + sequence"

        for i in range(UpperCAmelCase				):
          __snake_case				:			Any            =    {0: "batch", 2: decoder_sequence}
          __snake_case				:			List[Any]            =    {0: "batch", 2: decoder_sequence}
          __snake_case				:			Tuple            =    {0: "batch", 2: encoder_sequence}
          __snake_case				:			int            =    {0: "batch", 2: encoder_sequence}

