




import itertools
import json
import os
import unittest

from transformers import AddedToken, RobertaTokenizer, RobertaTokenizerFast
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 						__lowercase		(UpperCamelCase__					,		unittest.TestCase							):




      """simple docstring"""





      _snake_case												=						RobertaTokenizer
      _snake_case												=						RobertaTokenizerFast
      _snake_case												=						True
      _snake_case												=						{"""cls_token""": """<s>"""}

      def      UpperCAmelCase				(				self     )		->    Union[str, Any]:
         super().setUp()

         # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
         snake_case				:    List[str]           =	[
             """l""",
             """o""",
             """w""",
             """e""",
             """r""",
             """s""",
             """t""",
             """i""",
             """d""",
             """n""",
             """\u0120""",
             """\u0120l""",
             """\u0120n""",
             """\u0120lo""",
             """\u0120low""",
             """er""",
             """\u0120lowest""",
             """\u0120newer""",
             """\u0120wider""",
             """<unk>""",
         ]
         snake_case				:    List[str]           =	dict(zip(A    ,				range(len(A     )     )     )     )
         snake_case				:    Optional[int]           =	["""#version: 0.2""", """\u0120 l""", """\u0120l o""", """\u0120lo w""", """e r""", """"""]
         snake_case				:    Union[str, Any]           =	{"""unk_token""": """<unk>"""}

         snake_case				:    Optional[Any]           =	os.path.join(self.tmpdirname    ,				VOCAB_FILES_NAMES["""vocab_file"""]     )
         snake_case				:    str           =	os.path.join(self.tmpdirname    ,				VOCAB_FILES_NAMES["""merges_file"""]     )
         with open(self.vocab_file    ,				"""w"""    ,				encoding="""utf-8"""     ) as fp:
            fp.write(json.dumps(A     ) + """\n"""     )
         with open(self.merges_file    ,				"""w"""    ,				encoding="""utf-8"""     ) as fp:
            fp.write("""\n""".join(A     )     )

      def      UpperCAmelCase				(				self    ,				**A     )		->    Tuple:
         kwargs.update(self.special_tokens_map     )
         return self.tokenizer_class.from_pretrained(self.tmpdirname    ,				**A     )

      def      UpperCAmelCase				(				self    ,				**A     )		->    str:
         kwargs.update(self.special_tokens_map     )
         return RobertaTokenizerFast.from_pretrained(self.tmpdirname    ,				**A     )

      def      UpperCAmelCase				(				self    ,				A     )		->    Optional[Any]:
         snake_case				:    Optional[Any]           =	"""lower newer"""
         snake_case				:    List[Any]           =	"""lower newer"""
         return input_text, output_text

      def      UpperCAmelCase				(				self     )		->    Union[str, Any]:
         snake_case				:    Union[str, Any]           =	self.tokenizer_class(self.vocab_file    ,				self.merges_file    ,				**self.special_tokens_map     )
         snake_case				:    Optional[Any]           =	"""lower newer"""
         snake_case				:    List[str]           =	["""l""", """o""", """w""", """er""", """\u0120""", """n""", """e""", """w""", """er"""]
         snake_case				:    List[str]           =	tokenizer.tokenize(A     )  # , add_prefix_space=True)
         self.assertListEqual(A    ,				A     )

         snake_case				:    Dict           =	tokens + [tokenizer.unk_token]
         snake_case				:    Dict           =	[0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(A     )    ,				A     )

      def      UpperCAmelCase				(				self     )		->    Any:
         snake_case				:    Any           =	self.get_tokenizer()

         self.assertListEqual(tokenizer.encode("""Hello world!"""    ,				add_special_tokens=A     )    ,				[0, 3_1_4_1_4, 2_3_2, 3_2_8, 2]     )
         self.assertListEqual(
             tokenizer.encode("""Hello world! cécé herlolip 418"""    ,				add_special_tokens=A     )    ,				[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]    ,				)

      @slow
      def      UpperCAmelCase				(				self     )		->    Optional[int]:
         snake_case				:    Optional[int]           =	self.tokenizer_class.from_pretrained("""roberta-base"""     )

         snake_case				:    Optional[int]           =	tokenizer.encode("""sequence builders"""    ,				add_special_tokens=A     )
         snake_case				:    int           =	tokenizer.encode("""multi-sequence build"""    ,				add_special_tokens=A     )

         snake_case				:    Optional[int]           =	tokenizer.encode(
             """sequence builders"""    ,				add_special_tokens=A    ,				add_prefix_space=A     )
         snake_case				:    Optional[int]           =	tokenizer.encode(
             """sequence builders"""    ,				"""multi-sequence build"""    ,				add_special_tokens=A    ,				add_prefix_space=A     )

         snake_case				:    Optional[Any]           =	tokenizer.build_inputs_with_special_tokens(A     )
         snake_case				:    List[str]           =	tokenizer.build_inputs_with_special_tokens(A    ,				A     )

         assert encoded_sentence == encoded_text_from_decode
         assert encoded_pair == encoded_pair_from_decode

      def      UpperCAmelCase				(				self     )		->    Dict:
         snake_case				:    int           =	self.get_tokenizer()

         snake_case				:    str           =	"""Encode this sequence."""
         snake_case				:    Optional[int]           =	tokenizer.byte_encoder[""" """.encode("""utf-8"""     )[0]]

         # Testing encoder arguments
         snake_case				:    Optional[Any]           =	tokenizer.encode(A    ,				add_special_tokens=A    ,				add_prefix_space=A     )
         snake_case				:    List[str]           =	tokenizer.convert_ids_to_tokens(encoded[0]     )[0]
         self.assertNotEqual(A    ,				A     )

         snake_case				:    int           =	tokenizer.encode(A    ,				add_special_tokens=A    ,				add_prefix_space=A     )
         snake_case				:    List[str]           =	tokenizer.convert_ids_to_tokens(encoded[0]     )[0]
         self.assertEqual(A    ,				A     )

         tokenizer.add_special_tokens({"""bos_token""": """<s>"""}     )
         snake_case				:    List[str]           =	tokenizer.encode(A    ,				add_special_tokens=A     )
         snake_case				:    str           =	tokenizer.convert_ids_to_tokens(encoded[1]     )[0]
         self.assertNotEqual(A    ,				A     )

         # Testing spaces after special tokens
         snake_case				:    Union[str, Any]           =	"""<mask>"""
         tokenizer.add_special_tokens(
             {"""mask_token""": AddedToken(A    ,				lstrip=A    ,				rstrip=A     )}     )  # mask token has a left space
         snake_case				:    List[str]           =	tokenizer.convert_tokens_to_ids(A     )

         snake_case				:    Optional[Any]           =	"""Encode <mask> sequence"""
         snake_case				:    List[Any]           =	"""Encode <mask>sequence"""

         snake_case				:    Union[str, Any]           =	tokenizer.encode(A     )
         snake_case				:    List[str]           =	encoded.index(A     )
         snake_case				:    Tuple           =	tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]     )[0]
         self.assertEqual(A    ,				A     )

         snake_case				:    str           =	tokenizer.encode(A     )
         snake_case				:    Tuple           =	encoded.index(A     )
         snake_case				:    Optional[int]           =	tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]     )[0]
         self.assertNotEqual(A    ,				A     )

      def      UpperCAmelCase				(				self     )		->    int:
         pass

      def      UpperCAmelCase				(				self     )		->    Dict:
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
            with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
               snake_case				:    Tuple           =	self.rust_tokenizer_class.from_pretrained(A    ,				**A     )
               snake_case				:    Optional[Any]           =	self.tokenizer_class.from_pretrained(A    ,				**A     )
               snake_case				:    Union[str, Any]           =	"""A, <mask> AllenNLP sentence."""
               snake_case				:    Optional[int]           =	tokenizer_r.encode_plus(A    ,				add_special_tokens=A    ,				return_token_type_ids=A     )
               snake_case				:    Tuple           =	tokenizer_p.encode_plus(A    ,				add_special_tokens=A    ,				return_token_type_ids=A     )

               # token_type_ids should put 0 everywhere
               self.assertEqual(sum(tokens_r["""token_type_ids"""]     )    ,				sum(tokens_p["""token_type_ids"""]     )     )

               # attention_mask should put 1 everywhere, so sum over length should be 1
               self.assertEqual(
                   sum(tokens_r["""attention_mask"""]     ) / len(tokens_r["""attention_mask"""]     )    ,				sum(tokens_p["""attention_mask"""]     ) / len(tokens_p["""attention_mask"""]     )    ,				)

               snake_case				:    str           =	tokenizer_r.convert_ids_to_tokens(tokens_r["""input_ids"""]     )
               snake_case				:    Optional[int]           =	tokenizer_p.convert_ids_to_tokens(tokens_p["""input_ids"""]     )

               # Rust correctly handles the space before the mask while python doesnt
               self.assertSequenceEqual(tokens_p["""input_ids"""]    ,				[0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2]     )
               self.assertSequenceEqual(tokens_r["""input_ids"""]    ,				[0, 2_5_0, 6, 5_0_2_6_4, 3_8_2_3, 4_8_7, 2_1_9_9_2, 3_6_4_5, 4, 2]     )

               self.assertSequenceEqual(
                   A    ,				["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]     )
               self.assertSequenceEqual(
                   A    ,				["""<s>""", """A""", """,""", """<mask>""", """ĠAllen""", """N""", """LP""", """Ġsentence""", """.""", """</s>"""]     )

      def      UpperCAmelCase				(				self     )		->    Optional[int]:
         for trim_offsets, add_prefix_space in itertools.product([True, False]    ,				repeat=2     ):
            snake_case				:    Union[str, Any]           =	self.rust_tokenizer_class.from_pretrained(
                self.tmpdirname    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )

            snake_case				:    Dict           =	json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__()     )
            snake_case				:    Any           =	json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__()     )

            self.assertEqual(pre_tokenizer_state["""add_prefix_space"""]    ,				A     )

            self.assertEqual(post_processor_state["""add_prefix_space"""]    ,				A     )
            self.assertEqual(post_processor_state["""trim_offsets"""]    ,				A     )

      def      UpperCAmelCase				(				self     )		->    List[Any]:
         # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and
         # `trim_offsets`
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
            with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""     ):
               snake_case				:    Tuple           =	"""hello"""  # `hello` is a token in the vocabulary of `pretrained_name`
               snake_case				:    int           =	f"""{text_of_1_token} {text_of_1_token}"""

               snake_case				:    List[Any]           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    Any           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(0, len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(len(A     ) + 1, len(A     ) + 1 + len(A     ))    ,				)

               snake_case				:    List[Any]           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    List[Any]           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(0, len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(len(A     ) + 1, len(A     ) + 1 + len(A     ))    ,				)

               snake_case				:    Optional[int]           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    int           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(0, len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(len(A     ), len(A     ) + 1 + len(A     ))    ,				)

               snake_case				:    Optional[int]           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    Optional[Any]           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(0, len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(len(A     ), len(A     ) + 1 + len(A     ))    ,				)

               snake_case				:    Union[str, Any]           =	f""" {text}"""

               # tokenizer_r = self.rust_tokenizer_class.from_pretrained(
               #     pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True
               # )
               # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False)
               # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token)))
               # self.assertEqual(
               #     encoding.offset_mapping[1],
               #     (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)),
               # )

               snake_case				:    Optional[int]           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    List[str]           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(1, 1 + len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(1 + len(A     ) + 1, 1 + len(A     ) + 1 + len(A     ))    ,				)

               snake_case				:    List[Any]           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    List[str]           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(0, 1 + len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(1 + len(A     ), 1 + len(A     ) + 1 + len(A     ))    ,				)

               snake_case				:    Any           =	self.rust_tokenizer_class.from_pretrained(
                   A    ,				use_fast=A    ,				add_prefix_space=A    ,				trim_offsets=A     )
               snake_case				:    List[str]           =	tokenizer_r(A    ,				return_offsets_mapping=A    ,				add_special_tokens=A     )
               self.assertEqual(encoding.offset_mapping[0]    ,				(0, 1 + len(A     ))     )
               self.assertEqual(
                   encoding.offset_mapping[1]    ,				(1 + len(A     ), 1 + len(A     ) + 1 + len(A     ))    ,				)











import warnings
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[Any]				=   logging.get_logger(__name__)

lowerCamelCase	:	Optional[int]				=   {
    'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/config.json',
    # See all BART models at https://huggingface.co/models?filter=bart
}




class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      _snake_case												=						"""bart"""
      _snake_case												=						["""past_key_values"""]
      _snake_case												=						{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

      def __init__(				self    ,				A=5_0_2_6_5    ,				A=1_0_2_4    ,				A=1_2    ,				A=4_0_9_6    ,				A=1_6    ,				A=1_2    ,				A=4_0_9_6    ,				A=1_6    ,				A=0.0    ,				A=0.0    ,				A="gelu"    ,				A=1_0_2_4    ,				A=0.1    ,				A=0.0    ,				A=0.0    ,				A=0.02    ,				A=0.0    ,				A=False    ,				A=True    ,				A=3    ,				A=1    ,				A=0    ,				A=2    ,				A=True    ,				A=2    ,				A=2    ,				**A    ,				)		->    Any:
         snake_case				:    Optional[int]           =	vocab_size
         snake_case				:    Union[str, Any]           =	max_position_embeddings
         snake_case				:    List[str]           =	d_model
         snake_case				:    List[Any]           =	encoder_ffn_dim
         snake_case				:    Optional[Any]           =	encoder_layers
         snake_case				:    Union[str, Any]           =	encoder_attention_heads
         snake_case				:    str           =	decoder_ffn_dim
         snake_case				:    Union[str, Any]           =	decoder_layers
         snake_case				:    Any           =	decoder_attention_heads
         snake_case				:    Union[str, Any]           =	dropout
         snake_case				:    List[str]           =	attention_dropout
         snake_case				:    List[Any]           =	activation_dropout
         snake_case				:    Optional[int]           =	activation_function
         snake_case				:    Union[str, Any]           =	init_std
         snake_case				:    List[str]           =	encoder_layerdrop
         snake_case				:    int           =	decoder_layerdrop
         snake_case				:    str           =	classifier_dropout
         snake_case				:    List[str]           =	use_cache
         snake_case				:    Tuple           =	encoder_layers
         snake_case				:    int           =	scale_embedding  # scale factor will be sqrt(d_model) if True

         super().__init__(
             num_labels=A    ,				pad_token_id=A    ,				bos_token_id=A    ,				eos_token_id=A    ,				is_encoder_decoder=A    ,				decoder_start_token_id=A    ,				forced_eos_token_id=A    ,				**A    ,				)

         # ensure backward compatibility for BART CNN models
         if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated"""    ,				A     ):
            snake_case				:    Any           =	self.bos_token_id
            warnings.warn(
                f"""Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. """
                """The config can simply be saved and uploaded again to be fixed."""     )




class 						__lowercase		(UpperCamelCase__							):




      """simple docstring"""





      @property
      def      UpperCAmelCase				(				self     )		->    Mapping[str, Mapping[int, str]]:
         if self.task in ["default", "seq2seq-lm"]:
            snake_case				:    Optional[Any]           =	OrderedDict(
                [
                    ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                    ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                ]     )

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

            if self.use_past:
               self.fill_with_past_key_values_(A    ,				direction="""inputs"""     )
         elif self.task == "causal-lm":
            # TODO: figure this case out.
            snake_case				:    Union[str, Any]           =	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(A     ):
                  snake_case				:    List[Any]           =	{0: """batch""", 2: """past_sequence + sequence"""}
                  snake_case				:    Optional[int]           =	{0: """batch""", 2: """past_sequence + sequence"""}
         else:
            snake_case				:    Union[str, 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      UpperCAmelCase				(				self     )		->    Mapping[str, Mapping[int, str]]:
         if self.task in ["default", "seq2seq-lm"]:
            snake_case				:    Any           =	super().outputs
         else:
            snake_case				:    Any           =	super(A    ,				self     ).outputs
            if self.use_past:
               snake_case     , snake_case				:    Any           =	self.num_layers
               for i in range(A     ):
                  snake_case				:    Any           =	{0: """batch""", 2: """past_sequence + sequence"""}
                  snake_case				:    Union[str, Any]           =	{0: """batch""", 2: """past_sequence + sequence"""}
         return common_outputs

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         snake_case				:    Optional[Any]           =	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             A    ,				A    ,				A    ,				A    ,				A     )

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

            snake_case				:    str           =	torch.cat(
                [common_inputs["""decoder_attention_mask"""], torch.ones(A    ,				A     )]    ,				dim=1     )

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

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         snake_case				:    str           =	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             A    ,				A    ,				A    ,				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     , snake_case				:    str           =	common_inputs["""input_ids"""].shape
            # Not using the same length for past_key_values
            snake_case				:    Optional[int]           =	seqlen + 2
            snake_case     , snake_case				:    Tuple           =	self.num_layers
            snake_case     , snake_case				:    Optional[Any]           =	self.num_attention_heads
            snake_case				:    Union[str, Any]           =	(
                batch,
                num_encoder_attention_heads,
                past_key_values_length,
                self._config.hidden_size // num_encoder_attention_heads,
            )

            snake_case				:    Optional[Any]           =	common_inputs["""attention_mask"""].dtype
            snake_case				:    int           =	torch.cat(
                [common_inputs["""attention_mask"""], torch.ones(A    ,				A    ,				dtype=A     )]    ,				dim=1     )
            snake_case				:    Union[str, Any]           =	[
                (torch.zeros(A     ), torch.zeros(A     )) for _ in range(A     )
            ]
         return common_inputs

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         # 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
         snake_case				:    int           =	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				:    int           =	tokenizer.num_special_tokens_to_add(A     )
         snake_case				:    Tuple           =	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				:    int           =	[""" """.join([tokenizer.unk_token]     ) * seq_length] * batch_size
         snake_case				:    str           =	dict(tokenizer(A    ,				return_tensors=A     )     )
         return common_inputs

      def      UpperCAmelCase				(				self    ,				A    ,				A = -1    ,				A = -1    ,				A = False    ,				A = None    ,				)		->    Mapping[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
            snake_case				:    Optional[Any]           =	self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                A    ,				batch_size=A    ,				seq_length=A    ,				is_pair=A    ,				framework=A     )

         elif self.task == "causal-lm":
            snake_case				:    Optional[int]           =	self._generate_dummy_inputs_for_causal_lm(
                A    ,				batch_size=A    ,				seq_length=A    ,				is_pair=A    ,				framework=A     )
         else:
            snake_case				:    List[Any]           =	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                A    ,				batch_size=A    ,				seq_length=A    ,				is_pair=A    ,				framework=A     )

         return common_inputs

      def      UpperCAmelCase				(				self    ,				A    ,				A    ,				A    ,				A     )		->    Union[str, Any]:
         if self.task in ["default", "seq2seq-lm"]:
            snake_case				:    Optional[Any]           =	super()._flatten_past_key_values_(A    ,				A    ,				A    ,				A     )
         else:
            snake_case				:    Union[str, Any]           =	super(A    ,				self     )._flatten_past_key_values_(
                A    ,				A    ,				A    ,				A     )





