




from __future__ import annotations

import copy
import os
import tempfile
from importlib import import_module
from math import isnan

from transformers import is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import _tf_gpu_memory_limit, require_tf, slow

from ..test_modeling_tf_common import ids_tensor


if is_tf_available():
       import numpy as np
       import tensorflow as tf

       from transformers import (
           TF_MODEL_FOR_CAUSAL_LM_MAPPING,
           TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
           TF_MODEL_FOR_MASKED_LM_MAPPING,
           TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
           TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING,
           TF_MODEL_FOR_PRETRAINING_MAPPING,
           TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
           TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
           TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
           TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
           TFSharedEmbeddings,
       )

       if _tf_gpu_memory_limit is not None:
              UpperCamelCase__		=						tf.config.list_physical_devices('GPU')
              for gpu in gpus:
                     # Restrict TensorFlow to only allocate x GB of memory on the GPUs
                     try:
                            tf.config.set_logical_device_configuration(
                                gpu, [tf.config.LogicalDeviceConfiguration(memory_limit=_tf_gpu_memory_limit)]
                            )
                            UpperCamelCase__		=						tf.config.list_logical_devices('GPU')
                            print('Logical GPUs', logical_gpus)
                     except RuntimeError as e:
                            # Virtual devices must be set before GPUs have been initialized
                            print(e)


@require_tf
class     __SCREAMING_SNAKE_CASE							:
     _lowerCamelCase		  =			None
     _lowerCamelCase		  =			()
     _lowerCamelCase		  =			()
     _lowerCamelCase		  =			True
     _lowerCamelCase		  =			True
     _lowerCamelCase		  =			True
     _lowerCamelCase		  =			False

     def      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase=False ):







      '''simple docstring'''




      lowerCamelCase_							:		Union[str, Any]							       =							copy.deepcopy(__lowercase )

      if model_class in get_values(__lowercase ):
       lowerCamelCase_							:		int							       =							{
           k: tf.tile(tf.expand_dims(__lowercase			,	1 )			,	(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1) )
           if isinstance(__lowercase			,	tf.Tensor ) and v.ndim > 0
           else v
           for k, v in inputs_dict.items()
       }

      if return_labels:
       if model_class in get_values(__lowercase ):
        lowerCamelCase_							:		Any							       =							tf.ones(self.model_tester.batch_size			,	dtype=tf.intaa )
       elif model_class in get_values(__lowercase ):
        lowerCamelCase_							:		Union[str, Any]							       =							tf.zeros(self.model_tester.batch_size			,	dtype=tf.intaa )
        lowerCamelCase_							:		Union[str, Any]							       =							tf.zeros(self.model_tester.batch_size			,	dtype=tf.intaa )
       elif model_class in [
           *get_values(__lowercase ),
           *get_values(__lowercase ),
       ]:
        lowerCamelCase_							:		Tuple							       =							tf.zeros(self.model_tester.batch_size			,	dtype=tf.intaa )
       elif model_class in get_values(__lowercase ):
        lowerCamelCase_							:		List[Any]							       =							tf.zeros(self.model_tester.batch_size			,	dtype=tf.intaa )
       elif model_class in [
           *get_values(__lowercase ),
           *get_values(__lowercase ),
           *get_values(__lowercase ),
           *get_values(__lowercase ),
           *get_values(__lowercase ),
       ]:
        lowerCamelCase_							:		Dict							       =							tf.zeros(
            (self.model_tester.batch_size, self.model_tester.seq_length)			,	dtype=tf.intaa )
      return inputs_dict

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      lowerCamelCase_       ,      lowerCamelCase_							:		Dict							       =							self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes[:2]:
       lowerCamelCase_							:		Tuple							       =							self._prepare_for_class(__lowercase			,	__lowercase )
       lowerCamelCase_							:		Union[str, Any]							       =							model_class(__lowercase )

       @tf.function
       def run_in_graph_mode():
        return model(__lowercase )

       lowerCamelCase_							:		Optional[Any]							       =							run_in_graph_mode()
       self.assertIsNotNone(__lowercase )

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      lowerCamelCase_       ,      lowerCamelCase_							:		Dict							       =							self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes[:2]:
       lowerCamelCase_							:		List[str]							       =							self._prepare_for_class(__lowercase			,	__lowercase )
       lowerCamelCase_							:		str							       =							model_class(__lowercase )

       @tf.function(experimental_compile=__lowercase )
       def run_in_graph_mode():
        return model(__lowercase )

       lowerCamelCase_							:		Tuple							       =							run_in_graph_mode()
       self.assertIsNotNone(__lowercase )

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      lowerCamelCase_       ,      lowerCamelCase_							:		List[Any]							       =							self.model_tester.prepare_config_and_inputs_for_common()
      for model_class in self.all_model_classes[:2]:
       lowerCamelCase_							:		Union[str, Any]							       =							model_class(__lowercase )
       if getattr(__lowercase			,	'hf_compute_loss'			,	__lowercase ):
        # Test that model correctly compute the loss with kwargs
        lowerCamelCase_							:		Dict							       =							self._prepare_for_class(inputs_dict.copy()			,	__lowercase			,	return_labels=__lowercase )
        # Is there a better way to remove these decoder inputs?
        lowerCamelCase_							:		Any							       =							{
            key: val
            for key, val in prepared_for_class.items()
            if key not in ('head_mask', 'decoder_head_mask', 'cross_attn_head_mask', 'decoder_input_ids')
        }

        lowerCamelCase_							:		Union[str, Any]							       =							{
            'labels',
            'label',
            'label_ids',
            'start_positions',
            'start_position',
            'end_positions',
            'end_position',
            'next_sentence_label',
        }
        lowerCamelCase_							:		Any							       =							possible_label_cols.intersection(set(__lowercase ) )
        self.assertGreater(len(__lowercase )			,	0			,	msg='No matching label names found!' )
        lowerCamelCase_							:		str							       =							{key: val for key, val in prepared_for_class.items() if key in label_names}
        lowerCamelCase_							:		int							       =							{key: val for key, val in prepared_for_class.items() if key not in label_names}
        self.assertGreater(len(__lowercase )			,	0 )

        # Make sure it works with XLA!
        model.compile(optimizer=tf.keras.optimizers.SGD(0.0 )			,	jit_compile=__lowercase )
        # Make sure the model fits without crashing regardless of where we pass the labels
        lowerCamelCase_							:		List[Any]							       =							model.fit(
            __lowercase			,	validation_data=__lowercase			,	steps_per_epoch=1			,	validation_steps=1			,	shuffle=__lowercase			,	verbose=0			,	)
        lowerCamelCase_							:		List[Any]							       =							history.history['loss'][0]
        self.assertTrue(not isnan(__lowercase ) )
        lowerCamelCase_							:		str							       =							history.history['val_loss'][0]
        self.assertTrue(not isnan(__lowercase ) )

        # Now test it with separate labels, to make sure that path works in XLA too.
        lowerCamelCase_							:		Union[str, Any]							       =							model_class(__lowercase )
        model.compile(optimizer=tf.keras.optimizers.SGD(0.0 )			,	jit_compile=__lowercase )
        lowerCamelCase_							:		Any							       =							model.fit(
            __lowercase			,	__lowercase			,	validation_data=(inputs_minus_labels, labels)			,	steps_per_epoch=1			,	validation_steps=1			,	shuffle=__lowercase			,	verbose=0			,	)

        lowerCamelCase_							:		List[Any]							       =							history.history['loss'][0]
        self.assertTrue(not isnan(__lowercase ) )
        lowerCamelCase_							:		Dict							       =							history.history['val_loss'][0]
        self.assertTrue(not isnan(__lowercase ) )

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							self.model_tester.prepare_config_and_inputs_for_common()
      lowerCamelCase_							:		List[Any]							       =							True
      lowerCamelCase_							:		List[str]							       =							True

      if hasattr(__lowercase			,	'use_cache' ):
       lowerCamelCase_							:		List[Any]							       =							True

      lowerCamelCase_							:		Optional[Any]							       =							getattr(self.model_tester			,	'encoder_seq_length'			,	self.model_tester.seq_length )
      lowerCamelCase_							:		Optional[Any]							       =							getattr(self.model_tester			,	'key_length'			,	__lowercase )

      for model_class in self.all_model_classes[:2]:
       lowerCamelCase_							:		str							       =							self._prepare_for_class(__lowercase			,	__lowercase )
       lowerCamelCase_							:		Dict							       =							model_class(__lowercase )
       model.build()
       lowerCamelCase_							:		List[Any]							       =							len(model(__lowercase ) )

       for key in list(class_inputs_dict.keys() ):
        # Remove keys not in the serving signature, as the SavedModel will not be compiled to deal with them
        if key not in model.input_signature:
         del class_inputs_dict[key]
        # Check it's a tensor, in case the inputs dict has some bools in it too
        elif isinstance(class_inputs_dict[key]			,	tf.Tensor ) and class_inputs_dict[key].dtype.is_integer:
         lowerCamelCase_							:		Optional[int]							       =							tf.cast(class_inputs_dict[key]			,	tf.intaa )

       if set(class_inputs_dict.keys() ) != set(model.input_signature.keys() ):
        continue  # Some models have inputs that the preparation functions don't create, we skip those

       with tempfile.TemporaryDirectory() as tmpdirname:
        model.save_pretrained(__lowercase			,	saved_model=__lowercase )
        lowerCamelCase_							:		int							       =							os.path.join(__lowercase			,	'saved_model'			,	'1' )
        lowerCamelCase_							:		List[str]							       =							tf.keras.models.load_model(__lowercase )
        lowerCamelCase_							:		Optional[int]							       =							model(__lowercase )

        if self.is_encoder_decoder:
         lowerCamelCase_							:		List[str]							       =							outputs['encoder_hidden_states']
         lowerCamelCase_							:		int							       =							outputs['encoder_attentions']
        else:
         lowerCamelCase_							:		Optional[Any]							       =							outputs['hidden_states']
         lowerCamelCase_							:		Optional[Any]							       =							outputs['attentions']

        self.assertEqual(len(__lowercase )			,	__lowercase )

        lowerCamelCase_							:		List[str]							       =							getattr(
            self.model_tester			,	'expected_num_hidden_layers'			,	self.model_tester.num_hidden_layers + 1 )

        self.assertEqual(len(__lowercase )			,	__lowercase )
        self.assertListEqual(
            list(output_hidden_states[0].shape[-2:] )			,	[self.model_tester.seq_length, self.model_tester.hidden_size]			,	)

        self.assertEqual(len(__lowercase )			,	self.model_tester.num_hidden_layers )
        self.assertListEqual(
            list(output_attentions[0].shape[-3:] )			,	[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]			,	)

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      tf.keras.mixed_precision.set_global_policy('mixed_float16' )

      # try/finally block to ensure subsequent tests run in float32
      try:
       lowerCamelCase_       ,      lowerCamelCase_							:		List[str]							       =							self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes[:2]:
        lowerCamelCase_							:		Any							       =							self._prepare_for_class(__lowercase			,	__lowercase )
        lowerCamelCase_							:		Union[str, Any]							       =							model_class(__lowercase )
        lowerCamelCase_							:		str							       =							model(__lowercase )

        self.assertIsNotNone(__lowercase )
      finally:
       tf.keras.mixed_precision.set_global_policy('float32' )

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							self.model_tester.prepare_config_and_inputs_for_common()
      # head_mask and decoder_head_mask has different shapes than other input args
      if "head_mask" in inputs_dict:
       del inputs_dict["head_mask"]
      if "decoder_head_mask" in inputs_dict:
       del inputs_dict["decoder_head_mask"]
      if "cross_attn_head_mask" in inputs_dict:
       del inputs_dict["cross_attn_head_mask"]
      lowerCamelCase_							:		List[str]							       =							{
          module_member
          for model_class in self.all_model_classes
          for module in (import_module(model_class.__module__ ),)
          for module_member_name in dir(__lowercase )
          if module_member_name.endswith('MainLayer' )
          for module_member in (getattr(__lowercase			,	__lowercase ),)
          if isinstance(__lowercase			,	__lowercase )
          and tf.keras.layers.Layer in module_member.__bases__
          and getattr(__lowercase			,	'_keras_serializable'			,	__lowercase )
      }

      for main_layer_class in tf_main_layer_classes:
       # T5MainLayer needs an embed_tokens parameter when called without the inputs_embeds parameter
       if "T5" in main_layer_class.__name__:
        # Take the same values than in TFT5ModelTester for this shared layer
        lowerCamelCase_							:		Union[str, Any]							       =							TFSharedEmbeddings(self.model_tester.vocab_size			,	self.model_tester.hidden_size			,	name='shared' )
        lowerCamelCase_							:		Dict							       =							False
        lowerCamelCase_							:		List[Any]							       =							main_layer_class(__lowercase			,	embed_tokens=__lowercase )
       else:
        lowerCamelCase_							:		Optional[Any]							       =							main_layer_class(__lowercase )

       lowerCamelCase_							:		Union[str, Any]							       =							{
           name: tf.keras.Input(tensor.shape[1:]			,	dtype=tensor.dtype ) for name, tensor in inputs_dict.items()
       }

       if hasattr(self.model_tester			,	'num_labels' ):
        lowerCamelCase_							:		Dict							       =							self.model_tester.num_labels
       else:
        lowerCamelCase_							:		str							       =							2

       lowerCamelCase_							:		List[str]							       =							tf.data.Dataset.from_tensor_slices(
           (inputs_dict, np.ones((self.model_tester.batch_size, self.model_tester.seq_length, num_labels, 1) )) ).batch(1 )

       lowerCamelCase_							:		Optional[Any]							       =							main_layer(__lowercase )[0]
       lowerCamelCase_							:		Optional[Any]							       =							tf.keras.layers.Dense(__lowercase			,	activation='softmax'			,	name='outputs' )(__lowercase )
       lowerCamelCase_							:		List[str]							       =							tf.keras.models.Model(inputs=__lowercase			,	outputs=[outputs] )

       model.compile(loss='binary_crossentropy'			,	optimizer='adam'			,	metrics=['binary_accuracy'] )
       model.fit(__lowercase			,	epochs=1 )

       with tempfile.TemporaryDirectory() as tmpdirname:
        lowerCamelCase_							:		int							       =							os.path.join(__lowercase			,	'keras_model.h5' )
        model.save(__lowercase )
        if "T5" in main_layer_class.__name__:
         lowerCamelCase_							:		Optional[int]							       =							tf.keras.models.load_model(
             __lowercase			,	custom_objects={
                 main_layer_class.__name__: main_layer_class,
                 'TFSharedEmbeddings': TFSharedEmbeddings,
             }			,	)
        else:
         lowerCamelCase_							:		Optional[Any]							       =							tf.keras.models.load_model(
             __lowercase			,	custom_objects={main_layer_class.__name__: main_layer_class} )
        assert isinstance(__lowercase			,	tf.keras.Model )
        model(__lowercase )

     @slow
     def      lowercase_							(    self ):







      '''simple docstring'''




      lowerCamelCase_       ,      lowerCamelCase_							:		Any							       =							self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes[:2]:
       lowerCamelCase_							:		List[str]							       =							model_class(__lowercase )

       lowerCamelCase_							:		Dict							       =							copy.deepcopy(__lowercase )

       if not self.is_encoder_decoder:
        lowerCamelCase_							:		Dict							       =							inputs['input_ids']
        del inputs["input_ids"]
       else:
        lowerCamelCase_							:		List[str]							       =							inputs['input_ids']
        lowerCamelCase_							:		List[str]							       =							inputs.get('decoder_input_ids'			,	__lowercase )
        del inputs["input_ids"]
        inputs.pop('decoder_input_ids'			,	__lowercase )

       if not self.is_encoder_decoder:
        lowerCamelCase_							:		Tuple							       =							model.get_input_embeddings()(__lowercase )
       else:
        lowerCamelCase_							:		List[Any]							       =							model.get_input_embeddings()(__lowercase )
        lowerCamelCase_							:		Optional[int]							       =							model.get_input_embeddings()(__lowercase )

       lowerCamelCase_							:		str							       =							self._prepare_for_class(__lowercase			,	__lowercase )

       @tf.function
       def run_in_graph_mode():
        return model(__lowercase )

       lowerCamelCase_							:		Any							       =							run_in_graph_mode()
       self.assertIsNotNone(__lowercase )

     def      lowercase_							(    self			,	__lowercase			,	__lowercase ):







      '''simple docstring'''




      lowerCamelCase_							:		Any							       =							[]
      if model.config.bos_token_id is not None:
       special_tokens.append(model.config.bos_token_id )
      if model.config.pad_token_id is not None:
       special_tokens.append(model.config.pad_token_id )
      if model.config.eos_token_id is not None:
       special_tokens.append(model.config.eos_token_id )

      # create random bad tokens that are not special tokens
      lowerCamelCase_							:		str							       =							[]
      while len(__lowercase ) < num_bad_tokens:
       lowerCamelCase_							:		Union[str, Any]							       =							tf.squeeze(ids_tensor((1, 1)			,	self.model_tester.vocab_size )			,	0 ).numpy()[0]
       if token not in special_tokens:
        bad_tokens.append(__lowercase )
      return bad_tokens

     def      lowercase_							(    self			,	__lowercase ):







      '''simple docstring'''




      for token_id in output_ids[0].numpy().tolist():
       self.assertGreaterEqual(__lowercase			,	0 )
       self.assertLess(__lowercase			,	self.model_tester.vocab_size )





     def      lowercase_							(    self			,	__lowercase			,	__lowercase ):







      '''simple docstring'''




      for bad_word_ids in bad_words_ids:
       # for all slices in batch
       for generated_ids_slice in generated_ids:
        # for all word idx
        for i in range(len(__lowercase )			,	len(__lowercase ) ):
         # if tokens match
         if generated_ids_slice[i - len(__lowercase ) : i] == bad_word_ids:
          return True
      return False






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

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


UpperCamelCase__		=						logging.get_logger(__name__)

UpperCamelCase__		=						{
    'facebook/m2m100_418M': 'https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}


class     __SCREAMING_SNAKE_CASE							(		lowerCamelCase  ):
					_lowerCamelCase		  =			"""m2m_100"""
					_lowerCamelCase		  =			["""past_key_values"""]
					_lowerCamelCase		  =			{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

					def __init__(    self			,	__lowercase=128_112			,	__lowercase=1_024			,	__lowercase=12			,	__lowercase=4_096			,	__lowercase=16			,	__lowercase=12			,	__lowercase=4_096			,	__lowercase=16			,	__lowercase=0.05			,	__lowercase=0.05			,	__lowercase=True			,	__lowercase=True			,	__lowercase="relu"			,	__lowercase=1_024			,	__lowercase=0.1			,	__lowercase=0.1			,	__lowercase=0.0			,	__lowercase=0.02			,	__lowercase=2			,	__lowercase=True			,	__lowercase=1			,	__lowercase=0			,	__lowercase=2			,	**__lowercase			,	):







						'''simple docstring'''




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

						super().__init__(
						    pad_token_id=__lowercase			,	bos_token_id=__lowercase			,	eos_token_id=__lowercase			,	is_encoder_decoder=__lowercase			,	decoder_start_token_id=__lowercase			,	**__lowercase			,	)


class     __SCREAMING_SNAKE_CASE							(		lowerCamelCase  ):

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Any							       =							OrderedDict(
						    [
						        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
						        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
						    ] )

						if self.use_past:
							lowerCamelCase_							:		Union[str, Any]							       =							{0: 'batch'}
							lowerCamelCase_							:		Optional[Any]							       =							{0: 'batch', 1: 'past_decoder_sequence + sequence'}
						else:
							lowerCamelCase_							:		Any							       =							{0: 'batch', 1: 'decoder_sequence'}
							lowerCamelCase_							:		Union[str, Any]							       =							{0: 'batch', 1: 'decoder_sequence'}

						if self.use_past:
							self.fill_with_past_key_values_(__lowercase			,	direction='inputs' )
						return common_inputs

					def      lowercase_							(    self			,	__lowercase			,	__lowercase = -1			,	__lowercase = -1			,	__lowercase = False			,	__lowercase = None			,	):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							       =							compute_effective_axis_dimension(
						    __lowercase			,	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
						lowerCamelCase_							:		str							       =							tokenizer.num_special_tokens_to_add(__lowercase )
						lowerCamelCase_							:		Dict							       =							compute_effective_axis_dimension(
						    __lowercase			,	fixed_dimension=OnnxConfig.default_fixed_sequence			,	num_token_to_add=__lowercase )

						# Generate dummy inputs according to compute batch and sequence
						lowerCamelCase_							:		Optional[int]							       =							[' '.join([tokenizer.unk_token] ) * seq_length] * batch_size
						lowerCamelCase_							:		Optional[Any]							       =							dict(tokenizer(__lowercase			,	return_tensors=__lowercase ) )
						return common_inputs

					def      lowercase_							(    self			,	__lowercase			,	__lowercase = -1			,	__lowercase = -1			,	__lowercase = False			,	__lowercase = None			,	):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[Any]							       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    __lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase )

						# Generate decoder inputs
						lowerCamelCase_							:		Dict							       =							seq_length if not self.use_past else 1
						lowerCamelCase_							:		List[str]							       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    __lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase )
						lowerCamelCase_							:		Any							       =							{f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
						lowerCamelCase_							:		str							       =							dict(**__lowercase			,	**__lowercase )

						if self.use_past:
							if not is_torch_available():
								raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' )
							else:
								import torch
							lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							common_inputs['input_ids'].shape
							lowerCamelCase_							:		List[Any]							       =							common_inputs['decoder_input_ids'].shape[1]
							lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							self.num_attention_heads
							lowerCamelCase_							:		Any							       =							(
							    batch,
							    num_encoder_attention_heads,
							    encoder_seq_length,
							    self._config.hidden_size // num_encoder_attention_heads,
							)
							lowerCamelCase_							:		Tuple							       =							decoder_seq_length + 3
							lowerCamelCase_							:		Any							       =							(
							    batch,
							    num_decoder_attention_heads,
							    decoder_past_length,
							    self._config.hidden_size // num_decoder_attention_heads,
							)

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

							lowerCamelCase_							:		Tuple							       =							[]
							# If the number of encoder and decoder layers are present in the model configuration, both are considered
							lowerCamelCase_       ,      lowerCamelCase_							:		int							       =							self.num_layers
							lowerCamelCase_							:		Optional[int]							       =							min(__lowercase			,	__lowercase )
							lowerCamelCase_							:		int							       =							max(__lowercase			,	__lowercase ) - min_num_layers
							lowerCamelCase_							:		Union[str, Any]							       =							'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

							for _ in range(__lowercase ):
								common_inputs["past_key_values"].append(
								    (
								        torch.zeros(__lowercase ),
								        torch.zeros(__lowercase ),
								        torch.zeros(__lowercase ),
								        torch.zeros(__lowercase ),
								    ) )
							# TODO: test this.
							lowerCamelCase_							:		Optional[int]							       =							encoder_shape if remaining_side_name == 'encoder' else decoder_shape
							for _ in range(__lowercase			,	__lowercase ):
								common_inputs["past_key_values"].append((torch.zeros(__lowercase ), torch.zeros(__lowercase )) )
						return common_inputs





					_lowerCamelCase		  =			_generate_dummy_inputs_for_default_and_seqaseq_lm
