




from __future__ import annotations

import os
import tempfile
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import is_tensorflow_text_available, is_tf_available
from transformers.testing_utils import require_tensorflow_text, require_tf, slow

from ..test_modeling_tf_common import floats_tensor
from .test_framework_agnostic import GenerationIntegrationTestsMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import (
           AutoTokenizer,
           TFAutoModelForCausalLM,
           TFAutoModelForSeqaSeqLM,
           TFAutoModelForSpeechSeqaSeq,
           TFAutoModelForVisionaSeq,
           TFBartForConditionalGeneration,
           TFLogitsProcessorList,
           TFMinLengthLogitsProcessor,
           tf_top_k_top_p_filtering,
       )

if is_tensorflow_text_available():
       import tensorflow_text as text




@require_tf
class 						__lowercase		(unittest.TestCase							):




      """simple docstring"""





      def      UpperCAmelCase				(				self     )		->    Dict:
         snake_case				:    List[str]           =	tf.convert_to_tensor(
             [
                 [
                     8.2_22_09_91,  # 3rd highest value; idx. 0
                     -0.5_62_00_44,
                     5.23_22_97_52,
                     4.0_38_63_93,
                     -6.8_79_83_78,
                     -0.54_78_58_02,
                     -3.2_01_21_53,
                     2.92_77_71_76,
                     1.88_17_19_53,
                     7.35_34_12_76,  # 5th highest value; idx. 9
                     8.43_20_78_33,  # 2nd highest value; idx. 10
                     -9.85_71_18_36,
                     -5.96_20_92_36,
                     -1.13_03_91_61,
                     -7.1_11_52_94,
                     -0.8_36_96_33,
                     -5.3_18_64_08,
                     7.06_42_74_07,
                     0.81_36_93_44,
                     -0.82_02_38_17,
                     -5.9_17_97_96,
                     0.58_81_34_43,
                     -6.99_77_84_38,
                     4.71_55_11_89,
                     -0.18_77_16_37,
                     7.44_02_07_59,  # 4th highest value; idx. 25
                     9.38_45_09_87,  # 1st highest value; idx. 26
                     2.12_66_29_41,
                     -9.32_56_20_38,
                     2.35_65_25_22,
                 ],  # cummulative prob of 5 highest values <= 0.6
                 [
                     0.58_42_55_18,
                     4.53_13_92_38,
                     -5.57_51_04_64,
                     -6.28_03_06_99,
                     -7.19_52_95_03,
                     -4.02_12_25_51,
                     1.39_33_70_37,
                     -6.06_70_70_57,
                     1.59_48_05_17,
                     -9.64_31_19,
                     0.03_90_77_99,
                     0.67_23_17_62,
                     -8.88_20_67_26,
                     6.27_11_59_22,  # 4th highest value; idx. 13
                     2.28_52_07_23,
                     4.82_76_75_06,
                     4.30_42_13_68,
                     8.8_27_53_13,  # 2nd highest value; idx. 17
                     5.44_02_99_58,  # 5th highest value; idx. 18
                     -4.4_73_57_94,
                     7.38_57_95_36,  # 3rd highest value; idx. 20
                     -2.91_05_16_63,
                     2.61_94_60_77,
                     -2.5_67_47_62,
                     -9.48_95_93_02,
                     -4.02_92_26_45,
                     -1.35_41_69_18,
                     9.67_70_23_23,  # 1st highest value; idx. 27
                     -5.89_47_85_53,
                     1.85_37_04_67,
                 ],  # cummulative prob of 5 highest values <= 0.6
             ]    ,				dtype=tf.floataa    ,				)

         snake_case				:    Union[str, Any]           =	tf.convert_to_tensor(
             [[0, 0], [0, 9], [0, 1_0], [0, 2_5], [0, 2_6], [1, 1_3], [1, 1_7], [1, 1_8], [1, 2_0], [1, 2_7]]    ,				dtype=tf.intaa    ,				)  # expected non filtered idx as noted above

         snake_case				:    List[Any]           =	tf.convert_to_tensor(
             [8.22_20_99, 7.3_53_41_26, 8.43_20_78, 7.4_40_20_75, 9.3_84_51, 6.27_11_59, 8.82_75_31, 5.4_40_29_95, 7.3_85_79_56, 9.67_70_23]    ,				dtype=tf.floataa    ,				)  # expected non filtered values as noted above

         snake_case				:    str           =	tf_top_k_top_p_filtering(A    ,				top_k=1_0    ,				top_p=0.6    ,				min_tokens_to_keep=4     )

         snake_case				:    int           =	output[output != -float("""inf"""     )]
         snake_case				:    int           =	tf.cast(
             tf.where(tf.not_equal(A    ,				tf.constant(-float("""inf"""     )    ,				dtype=tf.floataa     )     )     )    ,				dtype=tf.intaa    ,				)

         tf.debugging.assert_near(A    ,				A    ,				rtol=1e-1_2     )
         tf.debugging.assert_equal(A    ,				A     )




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




      """simple docstring"""





      if is_tf_available():
            _snake_case												=						{
                """AutoModelForCausalLM""": TFAutoModelForCausalLM,
                """AutoModelForSpeechSeq2Seq""": TFAutoModelForSpeechSeqaSeq,
                """AutoModelForSeq2SeqLM""": TFAutoModelForSeqaSeqLM,
                """AutoModelForVision2Seq""": TFAutoModelForVisionaSeq,
                """LogitsProcessorList""": TFLogitsProcessorList,
                """MinLengthLogitsProcessor""": TFMinLengthLogitsProcessor,
                """create_tensor_fn""": tf.convert_to_tensor,
                """floats_tensor""": floats_tensor,
                """return_tensors""": """tf""",
            }

      @slow
      def      UpperCAmelCase				(				self     )		->    Optional[int]:
         # TF-only test: tf.saved_model export
         snake_case				:    Dict           =	TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""     )
         snake_case				:    Any           =	2
         snake_case				:    Union[str, Any]           =	2




         class 						__lowercase		(tf.Module							):




               """simple docstring"""





               def __init__(				self    ,				A     )		->    List[str]:
                  super(A    ,				self     ).__init__()
                  snake_case				:    Optional[int]           =	model

               @tf.function(
                   input_signature=(
                       tf.TensorSpec((None, input_length)    ,				tf.intaa    ,				name="""input_ids"""     ),
                       tf.TensorSpec((None, input_length)    ,				tf.intaa    ,				name="""attention_mask"""     ),
                   )    ,				jit_compile=A    ,				)
               def      UpperCAmelCase				(				self    ,				A    ,				A     )		->    List[Any]:
                  snake_case				:    str           =	self.model.generate(
                      input_ids=A    ,				attention_mask=A    ,				max_new_tokens=A    ,				return_dict_in_generate=A    ,				)
                  return {"sequences": outputs["sequences"]}




         snake_case				:    str           =	[[2, 0], [1_0_2, 1_0_3]]
         snake_case				:    List[str]           =	[[1, 0], [1, 1]]
         snake_case				:    List[Any]           =	DummyModel(model=A     )
         with tempfile.TemporaryDirectory() as tmp_dir:
            tf.saved_model.save(A    ,				A    ,				signatures={"""serving_default""": dummy_model.serving}     )
            snake_case				:    List[str]           =	tf.saved_model.load(A     ).signatures["""serving_default"""]
            for batch_size in range(1    ,				len(A     ) + 1     ):
               snake_case				:    int           =	{
                   """input_ids""": tf.constant(dummy_input_ids[:batch_size]     ),
                   """attention_mask""": tf.constant(dummy_attention_masks[:batch_size]     ),
               }
               snake_case				:    List[Any]           =	serving_func(**A     )["""sequences"""]
               snake_case				:    Tuple           =	test_model.generate(**A    ,				max_new_tokens=A     )
               tf.debugging.assert_equal(A    ,				A     )

      @slow
      def      UpperCAmelCase				(				self     )		->    Dict:
         # TF-only test: tf.saved_model export
         snake_case				:    Union[str, Any]           =	TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""     )
         snake_case				:    Optional[int]           =	1
         snake_case				:    Optional[Any]           =	2




         class 						__lowercase		(tf.Module							):




               """simple docstring"""





               def __init__(				self    ,				A     )		->    Tuple:
                  super(A    ,				self     ).__init__()
                  snake_case				:    Dict           =	model

               @tf.function(
                   input_signature=(
                       tf.TensorSpec((batch_size, None)    ,				tf.intaa    ,				name="""input_ids"""     ),
                       tf.TensorSpec((batch_size, None)    ,				tf.intaa    ,				name="""attention_mask"""     ),
                   )    ,				jit_compile=A    ,				)
               def      UpperCAmelCase				(				self    ,				A    ,				A     )		->    List[Any]:
                  snake_case				:    Tuple           =	self.model.generate(
                      input_ids=A    ,				attention_mask=A    ,				max_new_tokens=A    ,				return_dict_in_generate=A    ,				)
                  return {"sequences": outputs["sequences"]}




         snake_case				:    Dict           =	[[2], [1_0_2, 1_0_3]]
         snake_case				:    Union[str, Any]           =	[[1], [1, 1]]
         snake_case				:    Optional[int]           =	DummyModel(model=A     )
         with tempfile.TemporaryDirectory() as tmp_dir:
            tf.saved_model.save(A    ,				A    ,				signatures={"""serving_default""": dummy_model.serving}     )
            snake_case				:    Tuple           =	tf.saved_model.load(A     ).signatures["""serving_default"""]
            for input_row in range(len(A     )     ):
               snake_case				:    List[Any]           =	{
                   """input_ids""": tf.constant([dummy_input_ids[input_row]]     ),
                   """attention_mask""": tf.constant([dummy_attention_masks[input_row]]     ),
               }
               snake_case				:    int           =	serving_func(**A     )["""sequences"""]
               snake_case				:    Union[str, Any]           =	test_model.generate(**A    ,				max_new_tokens=A     )
               tf.debugging.assert_equal(A    ,				A     )

      @slow
      @require_tensorflow_text
      def      UpperCAmelCase				(				self     )		->    List[Any]:
         # TF-only test: tf.saved_model export
         with tempfile.TemporaryDirectory() as tmp_dir:
            # file needed to load the TF tokenizer
            hf_hub_download(repo_id="""google/flan-t5-small"""    ,				filename="""spiece.model"""    ,				local_dir=A     )




            class 						__lowercase		(tf.keras.layers.Layer							):




                  """simple docstring"""





                  def __init__(				self     )		->    Dict:
                     super().__init__()
                     snake_case				:    List[str]           =	text.SentencepieceTokenizer(
                         model=tf.io.gfile.GFile(os.path.join(A    ,				"""spiece.model"""     )    ,				"""rb"""     ).read()     )
                     snake_case				:    Tuple           =	TFAutoModelForSeqaSeqLM.from_pretrained("""hf-internal-testing/tiny-random-t5"""     )

                  def      UpperCAmelCase				(				self    ,				A    ,				*A    ,				**A     )		->    Union[str, Any]:
                     snake_case				:    Optional[Any]           =	self.tokenizer.tokenize(A     )
                     snake_case				:    Optional[int]           =	text.pad_model_inputs(
                         A    ,				max_seq_length=6_4    ,				pad_value=self.model.config.pad_token_id     )
                     snake_case				:    Optional[Any]           =	self.model.generate(input_ids=A    ,				attention_mask=A     )
                     return self.tokenizer.detokenize(A     )




            snake_case				:    Optional[Any]           =	CompleteSentenceTransformer()
            snake_case				:    Any           =	tf.keras.layers.Input(shape=(1,)    ,				dtype=tf.string    ,				name="""inputs"""     )
            snake_case				:    Dict           =	complete_model(A     )
            snake_case				:    List[str]           =	tf.keras.Model(A    ,				A     )
            keras_model.save(A     )

      def      UpperCAmelCase				(				self     )		->    List[Any]:
         # Has PT equivalent: this test relies on random sampling
         snake_case				:    int           =	{
             """do_sample""": True,
             """num_beams""": 1,
             """top_p""": 0.7,
             """top_k""": 1_0,
             """temperature""": 0.7,
         }
         snake_case				:    Optional[int]           =	1_4

         snake_case				:    List[str]           =	AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""     )
         snake_case				:    Dict           =	"""Hello, my dog is cute and"""
         snake_case				:    Tuple           =	tokenizer(A    ,				return_tensors="""tf"""     )
         snake_case				:    Tuple           =	TFAutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""     )

         snake_case				:    Any           =	6_3_8
         # forces the generation to happen on CPU, to avoid GPU-related quirks
         with tf.device(""":/CPU:0"""     ):
            tf.random.set_seed(0     )
            snake_case				:    int           =	model.generate(**A    ,				eos_token_id=A    ,				**A     )
         self.assertTrue(expectation == len(generated_tokens[0]     )     )

         snake_case				:    Union[str, Any]           =	[6_3_8, 1_9_8]
         with tf.device(""":/CPU:0"""     ):
            tf.random.set_seed(0     )
            snake_case				:    int           =	model.generate(**A    ,				eos_token_id=A    ,				**A     )
         self.assertTrue(expectation == len(generated_tokens[0]     )     )

      def      UpperCAmelCase				(				self     )		->    Any:
         # Has PT equivalent: ample use of framework-specific code
         snake_case				:    Dict           =	AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-bart"""     )
         snake_case				:    str           =	"""Hugging Face is a technology company based in New York and Paris."""
         snake_case				:    Union[str, Any]           =	bart_tokenizer(A    ,				return_tensors="""tf"""     ).input_ids
         snake_case				:    int           =	TFBartForConditionalGeneration.from_pretrained("""hf-internal-testing/tiny-random-bart"""     )
         snake_case				:    List[str]           =	bart_model.generate(A     ).numpy()




         class 						__lowercase		(UpperCamelCase__							):




               """simple docstring"""





               def      UpperCAmelCase				(				self    ,				A    ,				A=None    ,				**A     )		->    Optional[int]:
                  return super().call(A    ,				**A     )




         snake_case				:    Dict           =	FakeBart.from_pretrained("""hf-internal-testing/tiny-random-bart"""     )
         snake_case				:    Any           =	bart_model.generate(A    ,				foo="""bar"""     ).numpy()
         self.assertTrue(np.array_equal(A    ,				A     )     )




         class 						__lowercase		(bart_model.model.encoder.__class__							):




               """simple docstring"""





               def      UpperCAmelCase				(				self    ,				A    ,				**A     )		->    List[Any]:
                  return super().call(A    ,				**A     )




         snake_case				:    Dict           =	FakeEncoder(bart_model.config    ,				bart_model.model.shared     )
         snake_case				:    Tuple           =	fake_encoder

         # Normal generation still works (the output will be different because the encoder weights are different)
         snake_case				:    Dict           =	bart_model.generate(A     ).numpy()
         with self.assertRaises(A     ):
            # FakeEncoder.call() accepts **kwargs -> no filtering -> value error due to unexpected input "foo"
            bart_model.generate(A    ,				foo="""bar"""     )





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     )





