

from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.swin.modeling_tf_swin import (
         TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFSwinForImageClassification,
         TFSwinForMaskedImageModeling,
         TFSwinModel,
     )


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor





class    __UpperCAmelCase	:



   def __init__(				self				:	str,						__A				:	str,						__A				:	List[str]=1_3,						__A				:	List[Any]=3_2,						__A				:	Optional[int]=2,						__A				:	List[str]=3,						__A				:	Any=1_6,						__A				:	Dict=[1, 2, 1],						__A				:	Any=[2, 2, 4],						__A				:	Any=2,						__A				:	List[str]=2.0,						__A				:	Optional[Any]=True,						__A				:	Any=0.0,						__A				:	Tuple=0.0,						__A				:	str=0.1,						__A				:	Optional[int]="gelu",						__A				:	int=False,						__A				:	Union[str, Any]=True,						__A				:	Optional[int]=0.0_2,						__A				:	Tuple=1E-5,						__A				:	Optional[Any]=True,						__A				:	List[str]=None,						__A				:	List[Any]=True,						__A				:	Optional[int]=1_0,						__A				:	Optional[int]=8,						):
     UpperCAmelCase			:				Optional[Any]										=					parent
     UpperCAmelCase			:				Dict										=					batch_size
     UpperCAmelCase			:				Any										=					image_size
     UpperCAmelCase			:				Optional[Any]										=					patch_size
     UpperCAmelCase			:				List[str]										=					num_channels
     UpperCAmelCase			:				Tuple										=					embed_dim
     UpperCAmelCase			:				List[str]										=					depths
     UpperCAmelCase			:				Tuple										=					num_heads
     UpperCAmelCase			:				List[Any]										=					window_size
     UpperCAmelCase			:				List[str]										=					mlp_ratio
     UpperCAmelCase			:				Optional[int]										=					qkv_bias
     UpperCAmelCase			:				Tuple										=					hidden_dropout_prob
     UpperCAmelCase			:				Optional[Any]										=					attention_probs_dropout_prob
     UpperCAmelCase			:				Any										=					drop_path_rate
     UpperCAmelCase			:				List[str]										=					hidden_act
     UpperCAmelCase			:				Optional[int]										=					use_absolute_embeddings
     UpperCAmelCase			:				str										=					patch_norm
     UpperCAmelCase			:				Optional[Any]										=					layer_norm_eps
     UpperCAmelCase			:				Optional[Any]										=					initializer_range
     UpperCAmelCase			:				Optional[int]										=					is_training
     UpperCAmelCase			:				Dict										=					scope
     UpperCAmelCase			:				Optional[int]										=					use_labels
     UpperCAmelCase			:				Any										=					type_sequence_label_size
     UpperCAmelCase			:				str										=					encoder_stride



   def 			__magic_name__       (				self				:	Any		):
     UpperCAmelCase			:				str										=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

     UpperCAmelCase			:				Tuple										=					None
     if self.use_labels:
       UpperCAmelCase			:				List[Any]										=					ids_tensor([self.batch_size],						self.type_sequence_label_size		)

     UpperCAmelCase			:				str										=					self.get_config()

     return config, pixel_values, labels



   def 			__magic_name__       (				self				:	str		):
     return SwinConfig(
         image_size=self.image_size,						patch_size=self.patch_size,						num_channels=self.num_channels,						embed_dim=self.embed_dim,						depths=self.depths,						num_heads=self.num_heads,						window_size=self.window_size,						mlp_ratio=self.mlp_ratio,						qkv_bias=self.qkv_bias,						hidden_dropout_prob=self.hidden_dropout_prob,						attention_probs_dropout_prob=self.attention_probs_dropout_prob,						drop_path_rate=self.drop_path_rate,						hidden_act=self.hidden_act,						use_absolute_embeddings=self.use_absolute_embeddings,						path_norm=self.patch_norm,						layer_norm_eps=self.layer_norm_eps,						initializer_range=self.initializer_range,						encoder_stride=self.encoder_stride,						)



   def 			__magic_name__       (				self				:	Any,						__A				:	int,						__A				:	List[str],						__A				:	int		):
     UpperCAmelCase			:				List[str]										=					TFSwinModel(config=__A		)
     UpperCAmelCase			:				List[str]										=					model(__A		)

     UpperCAmelCase			:				int										=					((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths		) - 1))
     UpperCAmelCase			:				Union[str, Any]										=					int(config.embed_dim * 2 ** (len(config.depths		) - 1)		)

     self.parent.assertEqual(result.last_hidden_state.shape,						(self.batch_size, expected_seq_len, expected_dim)		)



   def 			__magic_name__       (				self				:	Any,						__A				:	Optional[Any],						__A				:	Tuple,						__A				:	List[str]		):
     UpperCAmelCase			:				str										=					TFSwinForMaskedImageModeling(config=__A		)
     UpperCAmelCase			:				str										=					model(__A		)
     self.parent.assertEqual(
         result.logits.shape,						(self.batch_size, self.num_channels, self.image_size, self.image_size)		)

     # test greyscale images
     UpperCAmelCase			:				Optional[int]										=					1
     UpperCAmelCase			:				str										=					TFSwinForMaskedImageModeling(__A		)

     UpperCAmelCase			:				Union[str, Any]										=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
     UpperCAmelCase			:				str										=					model(__A		)
     self.parent.assertEqual(result.logits.shape,						(self.batch_size, 1, self.image_size, self.image_size)		)



   def 			__magic_name__       (				self				:	List[Any],						__A				:	Tuple,						__A				:	List[str],						__A				:	Optional[Any]		):
     UpperCAmelCase			:				Union[str, Any]										=					self.type_sequence_label_size
     UpperCAmelCase			:				Tuple										=					TFSwinForImageClassification(__A		)
     UpperCAmelCase			:				int										=					model(__A,						labels=__A		)
     self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.type_sequence_label_size)		)

     # test greyscale images
     UpperCAmelCase			:				Dict										=					1
     UpperCAmelCase			:				Optional[int]										=					TFSwinForImageClassification(__A		)
     UpperCAmelCase			:				Optional[Any]										=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
     UpperCAmelCase			:				Optional[Any]										=					model(__A		)
     self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.type_sequence_label_size)		)





   def 			__magic_name__       (				self				:	Optional[int]		):
     UpperCAmelCase			:				Dict										=					self.prepare_config_and_inputs()
     UpperCAmelCase					,		UpperCAmelCase					,		UpperCAmelCase			:				Optional[int]										=					config_and_inputs
     UpperCAmelCase			:				Dict										=					{'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_tf
class    __UpperCAmelCase	(			lowerCamelCase__     ,    lowerCamelCase__     ,    unittest.TestCase		):
   UpperCamelCase  	=	(
       (
           TFSwinModel,
           TFSwinForImageClassification,
           TFSwinForMaskedImageModeling,
       )
       if is_tf_available()
       else ()
   )
   UpperCamelCase  	=	(
       {"""feature-extraction""": TFSwinModel, """image-classification""": TFSwinForImageClassification}
       if is_tf_available()
       else {}
   )

   UpperCamelCase  	=	False
   UpperCamelCase  	=	False
   UpperCamelCase  	=	False
   UpperCamelCase  	=	False



   def 			__magic_name__       (				self				:	List[str]		):
     UpperCAmelCase			:				Tuple										=					TFSwinModelTester(self		)
     UpperCAmelCase			:				Optional[Any]										=					ConfigTester(self,						config_class=__A,						embed_dim=3_7		)



   def 			__magic_name__       (				self				:	Optional[Any]		):
     self.create_and_test_config_common_properties()
     self.config_tester.create_and_test_config_to_json_string()
     self.config_tester.create_and_test_config_to_json_file()
     self.config_tester.create_and_test_config_from_and_save_pretrained()
     self.config_tester.create_and_test_config_with_num_labels()
     self.config_tester.check_config_can_be_init_without_params()
     self.config_tester.check_config_arguments_init()



   def 			__magic_name__       (				self				:	List[Any]		):
     return



   def 			__magic_name__       (				self				:	str		):
     UpperCAmelCase			:				int										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A		)



   def 			__magic_name__       (				self				:	Union[str, Any]		):
     UpperCAmelCase			:				Any										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*__A		)



   def 			__magic_name__       (				self				:	Optional[int]		):
     UpperCAmelCase			:				Union[str, Any]										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__A		)



   @unittest.skip(reason='''Swin does not use inputs_embeds'''		)
   def 			__magic_name__       (				self				:	int		):
     pass



   def 			__magic_name__       (				self				:	Union[str, Any]		):
     UpperCAmelCase					,		UpperCAmelCase			:				int										=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       UpperCAmelCase			:				List[str]										=					model_class(__A		)
       self.assertIsInstance(model.get_input_embeddings(),						tf.keras.layers.Layer		)
       UpperCAmelCase			:				List[str]										=					model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(__A,						tf.keras.layers.Dense		)		)



   def 			__magic_name__       (				self				:	Any		):
     UpperCAmelCase					,		UpperCAmelCase			:				str										=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       UpperCAmelCase			:				Tuple										=					model_class(__A		)
       UpperCAmelCase			:				Tuple										=					inspect.signature(model.call		)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCAmelCase			:				Optional[Any]										=					[*signature.parameters.keys()]

       UpperCAmelCase			:				Dict										=					['''pixel_values''']
       self.assertListEqual(arg_names[:1],						__A		)



   def 			__magic_name__       (				self				:	List[str]		):
     UpperCAmelCase					,		UpperCAmelCase			:				Dict										=					self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase			:				List[Any]										=					True

     for model_class in self.all_model_classes:
       UpperCAmelCase			:				Optional[int]										=					True
       UpperCAmelCase			:				Union[str, Any]										=					False
       UpperCAmelCase			:				str										=					True
       UpperCAmelCase			:				Union[str, Any]										=					model_class(__A		)
       UpperCAmelCase			:				List[str]										=					model(**self._prepare_for_class(__A,						__A		)		)
       UpperCAmelCase			:				List[str]										=					outputs.attentions
       UpperCAmelCase			:				Optional[Any]										=					len(self.model_tester.depths		)
       self.assertEqual(len(__A		),						__A		)

       # check that output_attentions also work using config
       del inputs_dict["output_attentions"]
       UpperCAmelCase			:				Dict										=					True
       UpperCAmelCase			:				List[Any]										=					config.window_size**2
       UpperCAmelCase			:				List[Any]										=					model_class(__A		)
       UpperCAmelCase			:				List[Any]										=					model(**self._prepare_for_class(__A,						__A		)		)
       UpperCAmelCase			:				List[str]										=					outputs.attentions
       self.assertEqual(len(__A		),						__A		)

       self.assertListEqual(
           list(attentions[0].shape[-3:]		),						[self.model_tester.num_heads[0], window_size_squared, window_size_squared],						)
       UpperCAmelCase			:				Dict										=					len(__A		)

       # Check attention is always last and order is fine
       UpperCAmelCase			:				Optional[int]										=					True
       UpperCAmelCase			:				Tuple										=					True
       UpperCAmelCase			:				Optional[Any]										=					model_class(__A		)
       UpperCAmelCase			:				int										=					model(**self._prepare_for_class(__A,						__A		)		)

       if hasattr(self.model_tester,						'''num_hidden_states_types'''		):
         UpperCAmelCase			:				str										=					self.model_tester.num_hidden_states_types
       else:
         # also another +1 for reshaped_hidden_states
         UpperCAmelCase			:				Optional[int]										=					2
       self.assertEqual(out_len + added_hidden_states,						len(__A		)		)

       UpperCAmelCase			:				List[str]										=					outputs.attentions

       self.assertEqual(len(__A		),						__A		)

       self.assertListEqual(
           list(self_attentions[0].shape[-3:]		),						[self.model_tester.num_heads[0], window_size_squared, window_size_squared],						)



   def 			__magic_name__       (				self				:	Tuple,						__A				:	Any,						__A				:	str,						__A				:	Tuple,						__A				:	Optional[int]		):
     UpperCAmelCase			:				List[Any]										=					model_class(__A		)
     UpperCAmelCase			:				int										=					model(**self._prepare_for_class(__A,						__A		)		)
     UpperCAmelCase			:				Tuple										=					outputs.hidden_states

     UpperCAmelCase			:				List[Any]										=					getattr(
         self.model_tester,						'''expected_num_hidden_layers''',						len(self.model_tester.depths		) + 1		)
     self.assertEqual(len(__A		),						__A		)

     # Swin has a different seq_length
     UpperCAmelCase			:				Optional[int]										=					to_atuple(config.patch_size		)

     UpperCAmelCase			:				Optional[int]										=					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

     self.assertListEqual(
         list(hidden_states[0].shape[-2:]		),						[num_patches, self.model_tester.embed_dim],						)

     UpperCAmelCase			:				str										=					outputs.reshaped_hidden_states
     self.assertEqual(len(__A		),						__A		)

     UpperCAmelCase					,		UpperCAmelCase					,		UpperCAmelCase					,		UpperCAmelCase			:				int										=					reshaped_hidden_states[0].shape

     UpperCAmelCase			:				Any										=					tf.reshape(reshaped_hidden_states[0],						(batch_size, num_channels, height * width)		)
     UpperCAmelCase			:				Dict										=					tf.transpose(__A,						(0, 2, 1)		)

     self.assertListEqual(
         list(reshaped_hidden_states.shape[-2:]		),						[num_patches, self.model_tester.embed_dim],						)



   def 			__magic_name__       (				self				:	List[str]		):
     UpperCAmelCase					,		UpperCAmelCase			:				Tuple										=					self.model_tester.prepare_config_and_inputs_for_common()

     UpperCAmelCase			:				List[Any]										=					to_atuple(self.model_tester.image_size		)

     for model_class in self.all_model_classes:
       UpperCAmelCase			:				Optional[Any]										=					True
       self.check_hidden_states_output(__A,						__A,						__A,						__A		)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       UpperCAmelCase			:				int										=					True

       self.check_hidden_states_output(__A,						__A,						__A,						__A		)



   def 			__magic_name__       (				self				:	Optional[Any]		):
     UpperCAmelCase					,		UpperCAmelCase			:				Optional[Any]										=					self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase			:				Optional[Any]										=					3

     UpperCAmelCase			:				Union[str, Any]										=					to_atuple(self.model_tester.image_size		)
     UpperCAmelCase			:				Union[str, Any]										=					to_atuple(config.patch_size		)

     UpperCAmelCase			:				int										=					image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
     UpperCAmelCase			:				Optional[int]										=					image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

     for model_class in self.all_model_classes:
       UpperCAmelCase			:				Dict										=					True
       self.check_hidden_states_output(__A,						__A,						__A,						(padded_height, padded_width)		)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       UpperCAmelCase			:				Any										=					True
       self.check_hidden_states_output(__A,						__A,						__A,						(padded_height, padded_width)		)





   @slow
   def 			__magic_name__       (				self				:	Tuple		):
     for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase			:				Tuple										=					TFSwinModel.from_pretrained(__A		)
       self.assertIsNotNone(__A		)






@require_vision
@require_tf
class    __UpperCAmelCase	(			unittest.TestCase		):



   @cached_property
   def 			__magic_name__       (				self				:	List[Any]		):
     return (
         AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''		)
         if is_vision_available()
         else None
     )





   @slow
   def 			__magic_name__       (				self				:	Any		):
     UpperCAmelCase			:				List[str]										=					TFSwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''		)
     UpperCAmelCase			:				Union[str, Any]										=					self.default_image_processor

     UpperCAmelCase			:				int										=					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
     UpperCAmelCase			:				Dict										=					image_processor(images=__A,						return_tensors='''tf'''		)

     # forward pass
     UpperCAmelCase			:				Union[str, Any]										=					model(__A		)

     # verify the logits
     UpperCAmelCase			:				str										=					tf.TensorShape((1, 1_0_0_0)		)
     self.assertEqual(outputs.logits.shape,						__A		)
     UpperCAmelCase			:				str										=					tf.constant([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]		)
     self.assertTrue(np.allclose(outputs.logits[0, :3],						__A,						atol=1E-4		)		)








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


_lowerCamelCase							:  Union[str, Any]     =				logging.get_logger(__name__)

_lowerCamelCase							:  Optional[int]     =				{
    "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    __UpperCAmelCase	(			lowerCamelCase__		):
   UpperCamelCase  	=	"""m2m_100"""
   UpperCamelCase  	=	["""past_key_values"""]
   UpperCamelCase  	=	{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



   def __init__(				self				:	int,						__A				:	Optional[Any]=1_2_8_1_1_2,						__A				:	str=1_0_2_4,						__A				:	Tuple=1_2,						__A				:	Any=4_0_9_6,						__A				:	str=1_6,						__A				:	List[str]=1_2,						__A				:	int=4_0_9_6,						__A				:	List[Any]=1_6,						__A				:	Tuple=0.0_5,						__A				:	Tuple=0.0_5,						__A				:	List[Any]=True,						__A				:	int=True,						__A				:	str="relu",						__A				:	str=1_0_2_4,						__A				:	str=0.1,						__A				:	str=0.1,						__A				:	List[str]=0.0,						__A				:	List[str]=0.0_2,						__A				:	Any=2,						__A				:	List[str]=True,						__A				:	Tuple=1,						__A				:	Optional[Any]=0,						__A				:	Optional[Any]=2,						**__A				:	Optional[Any],						):
     UpperCAmelCase			:				str										=					vocab_size
     UpperCAmelCase			:				Optional[Any]										=					max_position_embeddings
     UpperCAmelCase			:				List[str]										=					d_model
     UpperCAmelCase			:				Any										=					encoder_ffn_dim
     UpperCAmelCase			:				Optional[int]										=					encoder_layers
     UpperCAmelCase			:				List[Any]										=					encoder_attention_heads
     UpperCAmelCase			:				str										=					decoder_ffn_dim
     UpperCAmelCase			:				Any										=					decoder_layers
     UpperCAmelCase			:				List[str]										=					decoder_attention_heads
     UpperCAmelCase			:				str										=					dropout
     UpperCAmelCase			:				int										=					attention_dropout
     UpperCAmelCase			:				Dict										=					activation_dropout
     UpperCAmelCase			:				Tuple										=					activation_function
     UpperCAmelCase			:				Any										=					init_std
     UpperCAmelCase			:				Union[str, Any]										=					encoder_layerdrop
     UpperCAmelCase			:				List[Any]										=					decoder_layerdrop
     UpperCAmelCase			:				Optional[Any]										=					use_cache
     UpperCAmelCase			:				str										=					encoder_layers
     UpperCAmelCase			:				List[Any]										=					scale_embedding  # scale factor will be sqrt(d_model) if True

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






class    __UpperCAmelCase	(			lowerCamelCase__		):



   @property
   def 			__magic_name__       (				self				:	List[str]		):
     UpperCAmelCase			:				List[Any]										=					OrderedDict(
         [
             ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
             ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
         ]		)

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

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



   def 			__magic_name__       (				self				:	Dict,						__A				:	PreTrainedTokenizer,						__A				:	int = -1,						__A				:	int = -1,						__A				:	bool = False,						__A				:	Optional[TensorType] = None,						):
     # Copied from OnnxConfig.generate_dummy_inputs
     # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
     # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
     UpperCAmelCase			:				List[str]										=					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
     UpperCAmelCase			:				Optional[int]										=					tokenizer.num_special_tokens_to_add(__A		)
     UpperCAmelCase			:				int										=					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
     UpperCAmelCase			:				List[str]										=					[''' '''.join([tokenizer.unk_token]		) * seq_length] * batch_size
     UpperCAmelCase			:				Tuple										=					dict(tokenizer(__A,						return_tensors=__A		)		)
     return common_inputs



   def 			__magic_name__       (				self				:	Any,						__A				:	PreTrainedTokenizer,						__A				:	int = -1,						__A				:	int = -1,						__A				:	bool = False,						__A				:	Optional[TensorType] = None,						):
     UpperCAmelCase			:				int										=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         __A,						__A,						__A,						__A,						__A		)

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

       UpperCAmelCase			:				Union[str, Any]										=					torch.cat(
           [common_inputs['''decoder_attention_mask'''], torch.ones(__A,						__A		)],						dim=1		)

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





   UpperCamelCase  	=	_generate_dummy_inputs_for_default_and_seqaseq_lm





