
import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


_UpperCAmelCase					: Tuple      =       1e-4

if is_torch_available():
					import torch

					from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
					from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder


@require_torch
class 				lowercase					:



		def __init__( self				,      A_				,      A_=16				,      A_=13				,      A_=7				,      A_=14				,      A_=10				,      A_=19				,      A_=5				,      A_=4				,      A_=True				,      A_=16				,      A_=2				,      A_=4				,      A_=4				,      A_="gelu"				,      A_=0.1				,      A_=0.1				,      A_=[1, 2, 3, 4, 5]				,      A_=25				,      A_=5				,      )  ->					Any:
				"""simple docstring"""
				UpperCamelCase		=     d_model
				UpperCamelCase		=     parent
				UpperCamelCase		=     batch_size
				UpperCamelCase		=     prediction_length
				UpperCamelCase		=     context_length
				UpperCamelCase		=     cardinality
				UpperCamelCase		=     num_time_features
				UpperCamelCase		=     lags_sequence
				UpperCamelCase		=     embedding_dimension
				UpperCamelCase		=     is_training
				UpperCamelCase		=     hidden_size
				UpperCamelCase		=     num_hidden_layers
				UpperCamelCase		=     num_attention_heads
				UpperCamelCase		=     intermediate_size
				UpperCamelCase		=     hidden_act
				UpperCamelCase		=     hidden_dropout_prob
				UpperCamelCase		=     attention_probs_dropout_prob

				UpperCamelCase		=     context_length
				UpperCamelCase		=     prediction_length + label_length
				UpperCamelCase		=     label_length

				UpperCamelCase		=     moving_average
				UpperCamelCase		=     autocorrelation_factor



		def __UpperCamelCase ( self       )  ->					Optional[int]:
				"""simple docstring"""
				return AutoformerConfig(
				    d_model=self.d_model				,      encoder_layers=self.num_hidden_layers				,      decoder_layers=self.num_hidden_layers				,      encoder_attention_heads=self.num_attention_heads				,      decoder_attention_heads=self.num_attention_heads				,      encoder_ffn_dim=self.intermediate_size				,      decoder_ffn_dim=self.intermediate_size				,      dropout=self.hidden_dropout_prob				,      attention_dropout=self.attention_probs_dropout_prob				,      prediction_length=self.prediction_length				,      context_length=self.context_length				,      label_length=self.label_length				,      lags_sequence=self.lags_sequence				,      num_time_features=self.num_time_features				,      num_static_categorical_features=1				,      cardinality=[self.cardinality]				,      embedding_dimension=[self.embedding_dimension]				,      moving_average=self.moving_average				,      )



		def __UpperCamelCase ( self				,      A_       )  ->					Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase		=     config.context_length + max(config.lags_sequence       )

				UpperCamelCase		=     ids_tensor([self.batch_size, 1]				,      config.cardinality[0]       )
				UpperCamelCase		=     floats_tensor([self.batch_size, _past_length, config.num_time_features]       )
				UpperCamelCase		=     floats_tensor([self.batch_size, _past_length]       )
				UpperCamelCase		=     floats_tensor([self.batch_size, _past_length]       ) > 0.5

				# decoder inputs
				UpperCamelCase		=     floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]       )
				UpperCamelCase		=     floats_tensor([self.batch_size, config.prediction_length]       )

				UpperCamelCase		=     {
				    'past_values': past_values,
				    'static_categorical_features': static_categorical_features,
				    'past_time_features': past_time_features,
				    'past_observed_mask': past_observed_mask,
				    'future_time_features': future_time_features,
				    'future_values': future_values,
				}
				return inputs_dict



		def __UpperCamelCase ( self       )  ->					Any:
				"""simple docstring"""
				UpperCamelCase		=     self.get_config()
				UpperCamelCase		=     self.prepare_autoformer_inputs_dict(A_       )
				return config, inputs_dict



		def __UpperCamelCase ( self       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase	,  UpperCamelCase		=     self.prepare_config_and_inputs()
				return config, inputs_dict



		def __UpperCamelCase ( self				,      A_				,      A_       )  ->					List[str]:
				"""simple docstring"""
				UpperCamelCase		=     AutoformerModel(config=A_       ).to(A_       ).eval()
				UpperCamelCase		=     model(**A_       )

				UpperCamelCase		=     outputs.encoder_last_hidden_state
				UpperCamelCase		=     outputs.last_hidden_state

				with tempfile.TemporaryDirectory() as tmpdirname:
						UpperCamelCase		=     model.get_encoder()
						encoder.save_pretrained(A_       )
						UpperCamelCase		=     AutoformerEncoder.from_pretrained(A_       ).to(A_       )

				UpperCamelCase	,  UpperCamelCase	,  UpperCamelCase	,  UpperCamelCase	,  UpperCamelCase		=     model.create_network_inputs(**A_       )
				UpperCamelCase	,  UpperCamelCase		=     model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]       )

				UpperCamelCase		=     torch.cat(
				    (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])				,      dim=-1				,      )
				UpperCamelCase		=     encoder(inputs_embeds=A_       )[0]
				self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3       )

				UpperCamelCase		=     (
				    torch.mean(transformer_inputs[:, : config.context_length, ...]				,      dim=1       )
				    .unsqueeze(1       )
				    .repeat(1				,      config.prediction_length				,      1       )
				)
				UpperCamelCase		=     torch.zeros(
				    [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]				,      device=enc_input.device				,      )

				UpperCamelCase		=     torch.cat(
				    (
				        torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)				,      dim=1       ),
				        feature[:, config.context_length - config.label_length :, ...],
				    )				,      dim=-1				,      )
				UpperCamelCase		=     torch.cat(
				    (
				        torch.cat((trend_input[:, -config.label_length :, ...], mean)				,      dim=1       ),
				        feature[:, config.context_length - config.label_length :, ...],
				    )				,      dim=-1				,      )

				with tempfile.TemporaryDirectory() as tmpdirname:
						UpperCamelCase		=     model.get_decoder()
						decoder.save_pretrained(A_       )
						UpperCamelCase		=     AutoformerDecoder.from_pretrained(A_       ).to(A_       )

				UpperCamelCase		=     decoder(
				    trend=A_				,      inputs_embeds=A_				,      encoder_hidden_states=A_				,      )[0]

				self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3       )






@require_torch
class 				lowercase					(		_SCREAMING_SNAKE_CASE  ,   _SCREAMING_SNAKE_CASE  ,   unittest.TestCase   ):
		__lowercase       :       Optional[Any]	     =				(AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
		__lowercase       :       Optional[Any]	     =				(AutoformerForPrediction,) if is_torch_available() else ()
		__lowercase       :       List[str]	     =				{"feature-extraction": AutoformerModel} if is_torch_available() else {}
		__lowercase       :       Any	     =				False
		__lowercase       :       List[str]	     =				False
		__lowercase       :       List[str]	     =				False
		__lowercase       :       Optional[int]	     =				False
		__lowercase       :       Optional[int]	     =				False
		__lowercase       :       Any	     =				False



		def __UpperCamelCase ( self       )  ->					List[str]:
				"""simple docstring"""
				UpperCamelCase		=     AutoformerModelTester(self       )
				UpperCamelCase		=     ConfigTester(self				,      config_class=A_				,      has_text_modality=A_       )



		def __UpperCamelCase ( self       )  ->					List[Any]:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		def __UpperCamelCase ( self       )  ->					Optional[Any]:
				"""simple docstring"""
				UpperCamelCase	,  UpperCamelCase		=     self.model_tester.prepare_config_and_inputs()
				for model_class in self.all_model_classes:
						UpperCamelCase		=     model_class(A_       )

						with tempfile.TemporaryDirectory() as tmpdirname:
								model.save_pretrained(A_       )
								UpperCamelCase	,  UpperCamelCase		=     model_class.from_pretrained(A_				,      output_loading_info=A_       )
						self.assertEqual(info['missing_keys']				,      []       )



		def __UpperCamelCase ( self       )  ->					int:
				"""simple docstring"""
				UpperCamelCase		=     self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_encoder_decoder_model_standalone(*A_       )



		@unittest.skip(reason='Model has no tokens embeddings'       )
		def __UpperCamelCase ( self       )  ->					int:
				"""simple docstring"""
				pass



		def __UpperCamelCase ( self       )  ->					Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase		=     inspect.signature(getattr(A_				,      'forward'       )       )
				# The main input is the name of the argument after `self`
				UpperCamelCase		=     list(model_signature.parameters.keys()       )[1]
				self.assertEqual(AutoformerModel.main_input_name				,      A_       )



		def __UpperCamelCase ( self       )  ->					List[str]:
				"""simple docstring"""
				UpperCamelCase	,  UpperCamelCase		=     self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
						UpperCamelCase		=     model_class(A_       )
						UpperCamelCase		=     inspect.signature(model.forward       )
						# signature.parameters is an OrderedDict => so arg_names order is deterministic
						UpperCamelCase		=     [*signature.parameters.keys()]

						UpperCamelCase		=     [
						    'past_values',
						    'past_time_features',
						    'past_observed_mask',
						    'static_categorical_features',
						    'static_real_features',
						    'future_values',
						    'future_time_features',
						]

						if model.__class__.__name__ in ["AutoformerForPrediction"]:
								expected_arg_names.append('future_observed_mask'       )

						expected_arg_names.extend(
						    [
						        'decoder_attention_mask',
						        'head_mask',
						        'decoder_head_mask',
						        'cross_attn_head_mask',
						        'encoder_outputs',
						        'past_key_values',
						        'output_hidden_states',
						        'output_attentions',
						        'use_cache',
						        'return_dict',
						    ]       )

						self.assertListEqual(arg_names[: len(A_       )]				,      A_       )



		def __UpperCamelCase ( self       )  ->					Any:
				"""simple docstring"""
				UpperCamelCase	,  UpperCamelCase		=     self.model_tester.prepare_config_and_inputs_for_common()
				UpperCamelCase		=     True

				UpperCamelCase		=     getattr(self.model_tester				,      'seq_length'				,      A_       )
				UpperCamelCase		=     getattr(self.model_tester				,      'decoder_seq_length'				,      A_       )
				UpperCamelCase		=     getattr(self.model_tester				,      'encoder_seq_length'				,      A_       )
				UpperCamelCase		=     getattr(self.model_tester				,      'd_model'				,      A_       )
				UpperCamelCase		=     getattr(self.model_tester				,      'num_attention_heads'				,      A_       )
				UpperCamelCase		=     d_model // num_attention_heads

				for model_class in self.all_model_classes:
						UpperCamelCase		=     True
						UpperCamelCase		=     False
						UpperCamelCase		=     True
						UpperCamelCase		=     model_class(A_       )
						model.to(A_       )
						model.eval()
						with torch.no_grad():
								UpperCamelCase		=     model(**self._prepare_for_class(A_				,      A_       )       )
						UpperCamelCase		=     outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
						self.assertEqual(len(A_       )				,      self.model_tester.num_hidden_layers       )

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						UpperCamelCase		=     True
						UpperCamelCase		=     model_class(A_       )
						model.to(A_       )
						model.eval()
						with torch.no_grad():
								UpperCamelCase		=     model(**self._prepare_for_class(A_				,      A_       )       )
						UpperCamelCase		=     outputs.encoder_attentions
						self.assertEqual(len(A_       )				,      self.model_tester.num_hidden_layers       )

						self.assertListEqual(
						    list(attentions[0].shape[-3:]       )				,      [self.model_tester.num_attention_heads, encoder_seq_length, dim]				,      )
						UpperCamelCase		=     len(A_       )

						UpperCamelCase		=     7

						if "last_hidden_state" in outputs:
								correct_outlen += 1

						if "trend" in outputs:
								correct_outlen += 1

						if "past_key_values" in outputs:
								correct_outlen += 1  # past_key_values have been returned

						if "loss" in outputs:
								correct_outlen += 1

						if "params" in outputs:
								correct_outlen += 1

						self.assertEqual(A_				,      A_       )

						# decoder attentions
						UpperCamelCase		=     outputs.decoder_attentions
						self.assertIsInstance(A_				,      (list, tuple)       )
						self.assertEqual(len(A_       )				,      self.model_tester.num_hidden_layers       )
						self.assertListEqual(
						    list(decoder_attentions[0].shape[-3:]       )				,      [self.model_tester.num_attention_heads, decoder_seq_length, dim]				,      )

						# cross attentions
						UpperCamelCase		=     outputs.cross_attentions
						self.assertIsInstance(A_				,      (list, tuple)       )
						self.assertEqual(len(A_       )				,      self.model_tester.num_hidden_layers       )
						self.assertListEqual(
						    list(cross_attentions[0].shape[-3:]       )				,      [self.model_tester.num_attention_heads, decoder_seq_length, dim]				,      )

				# Check attention is always last and order is fine
				UpperCamelCase		=     True
				UpperCamelCase		=     True
				UpperCamelCase		=     model_class(A_       )
				model.to(A_       )
				model.eval()
				with torch.no_grad():
						UpperCamelCase		=     model(**self._prepare_for_class(A_				,      A_       )       )

				self.assertEqual(out_len + 2				,      len(A_       )       )

				UpperCamelCase		=     outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

				self.assertEqual(len(A_       )				,      self.model_tester.num_hidden_layers       )
				self.assertListEqual(
				    list(self_attentions[0].shape[-3:]       )				,      [self.model_tester.num_attention_heads, encoder_seq_length, dim]				,      )



		@is_flaky()
		def __UpperCamelCase ( self       )  ->					str:
				"""simple docstring"""
				super().test_retain_grad_hidden_states_attentions()






def       A       (					lowercase="train-batch.pt" )   ->  List[str]:
		'''simple docstring'''

		UpperCamelCase		=     hf_hub_download(repo_id='hf-internal-testing/tourism-monthly-batch'    ,       filename=lowercase    ,       repo_type='dataset' )
		UpperCamelCase		=     torch.load(lowercase    ,       map_location=lowercase )
		return batch


@require_torch
@slow
class 				lowercase					(		unittest.TestCase   ):



		def __UpperCamelCase ( self       )  ->					Any:
				"""simple docstring"""
				UpperCamelCase		=     AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly'       ).to(A_       )
				UpperCamelCase		=     prepare_batch()

				with torch.no_grad():
						UpperCamelCase		=     model(
						    past_values=batch['past_values']				,      past_time_features=batch['past_time_features']				,      past_observed_mask=batch['past_observed_mask']				,      static_categorical_features=batch['static_categorical_features']				,      future_values=batch['future_values']				,      future_time_features=batch['future_time_features']				,      )[0]

				UpperCamelCase		=     torch.Size(
				    (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)       )
				self.assertEqual(output.shape				,      A_       )

				UpperCamelCase		=     torch.tensor(
				    [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]]				,      device=A_       )
				self.assertTrue(torch.allclose(output[0, :3, :3]				,      A_				,      atol=A_       )       )



		def __UpperCamelCase ( self       )  ->					List[str]:
				"""simple docstring"""
				UpperCamelCase		=     AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'       ).to(A_       )
				UpperCamelCase		=     prepare_batch('val-batch.pt'       )
				with torch.no_grad():
						UpperCamelCase		=     model(
						    past_values=batch['past_values']				,      past_time_features=batch['past_time_features']				,      past_observed_mask=batch['past_observed_mask']				,      static_categorical_features=batch['static_categorical_features']				,      ).encoder_last_hidden_state
				UpperCamelCase		=     torch.Size((64, model.config.context_length, model.config.d_model)       )
				self.assertEqual(output.shape				,      A_       )

				UpperCamelCase		=     torch.tensor(
				    [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]]				,      device=A_       )
				self.assertTrue(torch.allclose(output[0, :3, :3]				,      A_				,      atol=A_       )       )



		def __UpperCamelCase ( self       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'       ).to(A_       )
				UpperCamelCase		=     prepare_batch('val-batch.pt'       )
				with torch.no_grad():
						UpperCamelCase		=     model.generate(
						    static_categorical_features=batch['static_categorical_features']				,      past_time_features=batch['past_time_features']				,      past_values=batch['past_values']				,      future_time_features=batch['future_time_features']				,      past_observed_mask=batch['past_observed_mask']				,      )
				UpperCamelCase		=     torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)       )
				self.assertEqual(outputs.sequences.shape				,      A_       )

				UpperCamelCase		=     torch.tensor([3130.6763, 4056.5293, 7053.0786]				,      device=A_       )
				UpperCamelCase		=     outputs.sequences.mean(dim=1       )
				self.assertTrue(torch.allclose(mean_prediction[0, -3:]				,      A_				,      rtol=1e-1       )       )


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


_UpperCAmelCase					: List[Any]      =       logging.get_logger(__name__)

_UpperCAmelCase					: Optional[Any]      =       {
    "Helsinki-NLP/opus-mt-en-de": "https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json",
    # See all Marian models at https://huggingface.co/models?filter=marian
}


class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):
		__lowercase       :       int	     =				"marian"
		__lowercase       :       List[str]	     =				["past_key_values"]
		__lowercase       :       Any	     =				{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}



		def __init__( self				,      A_=58_101				,      A_=None				,      A_=1_024				,      A_=12				,      A_=4_096				,      A_=16				,      A_=12				,      A_=4_096				,      A_=16				,      A_=0.0				,      A_=0.0				,      A_=True				,      A_=True				,      A_="gelu"				,      A_=1_024				,      A_=0.1				,      A_=0.0				,      A_=0.0				,      A_=0.02				,      A_=58_100				,      A_=False				,      A_=58_100				,      A_=0				,      A_=0				,      A_=True				,      **A_				,      )  ->					Optional[int]:
				"""simple docstring"""
				UpperCamelCase		=     vocab_size
				UpperCamelCase		=     decoder_vocab_size or vocab_size
				UpperCamelCase		=     max_position_embeddings
				UpperCamelCase		=     d_model
				UpperCamelCase		=     encoder_ffn_dim
				UpperCamelCase		=     encoder_layers
				UpperCamelCase		=     encoder_attention_heads
				UpperCamelCase		=     decoder_ffn_dim
				UpperCamelCase		=     decoder_layers
				UpperCamelCase		=     decoder_attention_heads
				UpperCamelCase		=     dropout
				UpperCamelCase		=     attention_dropout
				UpperCamelCase		=     activation_dropout
				UpperCamelCase		=     activation_function
				UpperCamelCase		=     init_std
				UpperCamelCase		=     encoder_layerdrop
				UpperCamelCase		=     decoder_layerdrop
				UpperCamelCase		=     use_cache
				UpperCamelCase		=     encoder_layers
				UpperCamelCase		=     scale_embedding  # scale factor will be sqrt(d_model) if True
				UpperCamelCase		=     share_encoder_decoder_embeddings
				super().__init__(
				    pad_token_id=A_				,      eos_token_id=A_				,      is_encoder_decoder=A_				,      decoder_start_token_id=A_				,      forced_eos_token_id=A_				,      **A_				,      )






class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):



		@property
		# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
		def __UpperCamelCase ( self       )  ->					Mapping[str, Mapping[int, str]]:
				"""simple docstring"""
				if self.task in ["default", "seq2seq-lm"]:
						UpperCamelCase		=     OrderedDict(
						    [
						        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
						        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
						    ]       )

						if self.use_past:
								UpperCamelCase		=     {0: 'batch'}
								UpperCamelCase		=     {0: 'batch', 1: 'past_decoder_sequence + sequence'}
						else:
								UpperCamelCase		=     {0: 'batch', 1: 'decoder_sequence'}
								UpperCamelCase		=     {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.
						UpperCamelCase		=     OrderedDict(
						    [
						        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
						        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
						    ]       )
						if self.use_past:
								UpperCamelCase	,  UpperCamelCase		=     self.num_layers
								for i in range(A_       ):
										UpperCamelCase		=     {0: 'batch', 2: 'past_sequence + sequence'}
										UpperCamelCase		=     {0: 'batch', 2: 'past_sequence + sequence'}
				else:
						UpperCamelCase		=     OrderedDict(
						    [
						        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
						        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
						        ('decoder_input_ids', {0: 'batch', 1: 'decoder_sequence'}),
						        ('decoder_attention_mask', {0: 'batch', 1: 'decoder_sequence'}),
						    ]       )

				return common_inputs



		@property
		# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
		def __UpperCamelCase ( self       )  ->					Mapping[str, Mapping[int, str]]:
				"""simple docstring"""
				if self.task in ["default", "seq2seq-lm"]:
						UpperCamelCase		=     super().outputs
				else:
						UpperCamelCase		=     super(A_				,      self       ).outputs
						if self.use_past:
								UpperCamelCase	,  UpperCamelCase		=     self.num_layers
								for i in range(A_       ):
										UpperCamelCase		=     {0: 'batch', 2: 'past_sequence + sequence'}
										UpperCamelCase		=     {0: 'batch', 2: 'past_sequence + sequence'}
				return common_outputs



		def __UpperCamelCase ( self				,      A_				,      A_ = -1				,      A_ = -1				,      A_ = False				,      A_ = None				,      )  ->					Mapping[str, Any]:
				"""simple docstring"""
				UpperCamelCase		=     self._generate_dummy_inputs_for_encoder_and_decoder(
				    A_				,      A_				,      A_				,      A_				,      A_       )

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

						UpperCamelCase		=     torch.cat(
						    [common_inputs['decoder_attention_mask'], torch.ones(A_				,      A_       )]				,      dim=1       )

						UpperCamelCase		=     []
						# If the number of encoder and decoder layers are present in the model configuration, both are considered
						UpperCamelCase	,  UpperCamelCase		=     self.num_layers
						UpperCamelCase		=     min(A_				,      A_       )
						UpperCamelCase		=     max(A_				,      A_       ) - min_num_layers
						UpperCamelCase		=     '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		=     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]:
				"""simple docstring"""
				UpperCamelCase		=     self._generate_dummy_inputs_for_encoder_and_decoder(
				    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
						UpperCamelCase	,  UpperCamelCase		=     common_inputs['input_ids'].shape
						# Not using the same length for past_key_values
						UpperCamelCase		=     seqlen + 2
						UpperCamelCase	,  UpperCamelCase		=     self.num_layers
						UpperCamelCase	,  UpperCamelCase		=     self.num_attention_heads
						UpperCamelCase		=     (
						    batch,
						    num_encoder_attention_heads,
						    past_key_values_length,
						    self._config.hidden_size // num_encoder_attention_heads,
						)

						UpperCamelCase		=     common_inputs['attention_mask'].dtype
						UpperCamelCase		=     torch.cat(
						    [common_inputs['attention_mask'], torch.ones(A_				,      A_				,      dtype=A_       )]				,      dim=1       )
						UpperCamelCase		=     [
						    (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]:
				"""simple docstring"""
				# 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		=     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		=     tokenizer.num_special_tokens_to_add(A_       )
				UpperCamelCase		=     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		=     [' '.join([tokenizer.unk_token]       ) * seq_length] * batch_size
				UpperCamelCase		=     dict(tokenizer(A_				,      return_tensors=A_       )       )
				return common_inputs



		def __UpperCamelCase ( self				,      A_				,      A_ = -1				,      A_ = -1				,      A_ = False				,      A_ = None				,      )  ->					Mapping[str, Any]:
				"""simple docstring"""
				if self.task in ["default", "seq2seq-lm"]:
						UpperCamelCase		=     self._generate_dummy_inputs_for_default_and_seqaseq_lm(
						    A_				,      batch_size=A_				,      seq_length=A_				,      is_pair=A_				,      framework=A_       )

				else:
						UpperCamelCase		=     self._generate_dummy_inputs_for_causal_lm(
						    A_				,      batch_size=A_				,      seq_length=A_				,      is_pair=A_				,      framework=A_       )

				return common_inputs



		def __UpperCamelCase ( self				,      A_				,      A_				,      A_				,      A_       )  ->					int:
				"""simple docstring"""
				if self.task in ["default", "seq2seq-lm"]:
						UpperCamelCase		=     super()._flatten_past_key_values_(A_				,      A_				,      A_				,      A_       )
				else:
						UpperCamelCase		=     super(A_				,      self       )._flatten_past_key_values_(
						    A_				,      A_				,      A_				,      A_       )



		@property
		def __UpperCamelCase ( self       )  ->					float:
				"""simple docstring"""
				return 1e-4
