




'''simple docstring'''







from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block


@dataclass
class 			_a  (	__a ):
       __a				:     torch.FloatTensor






class 			_a  (	nn.Module ):
       def __init__(	self						:	Dict   ,				lowercase						:	Union[str, Any]=3   ,				lowercase						:	int=3   ,				lowercase						:	str=("DownEncoderBlock2D",)   ,				lowercase						:	Tuple=(64,)   ,				lowercase						:	Tuple=2   ,				lowercase						:	int=32   ,				lowercase						:	List[str]="silu"   ,				lowercase						:	Tuple=True   ,				):



           '''simple docstring'''

           super().__init__()
           UpperCAmelCase			       =		layers_per_block

           UpperCAmelCase			       =		torch.nn.Convad(
               lowercase   ,				block_out_channels[0]   ,				kernel_size=3   ,				stride=1   ,				padding=1   ,				)

           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		nn.ModuleList([]				)

           # down
           UpperCAmelCase			       =		block_out_channels[0]
           for i, down_block_type in enumerate(lowercase				):
               UpperCAmelCase			       =		output_channel
               UpperCAmelCase			       =		block_out_channels[i]
               UpperCAmelCase			       =		i == len(lowercase				) - 1

               UpperCAmelCase			       =		get_down_block(
                   lowercase   ,				num_layers=self.layers_per_block   ,				in_channels=lowercase   ,				out_channels=lowercase   ,				add_downsample=not is_final_block   ,				resnet_eps=1E-6   ,				downsample_padding=0   ,				resnet_act_fn=lowercase   ,				resnet_groups=lowercase   ,				attention_head_dim=lowercase   ,				temb_channels=lowercase   ,				)
               self.down_blocks.append(lowercase				)

           # mid
           UpperCAmelCase			       =		UNetMidBlockaD(
               in_channels=block_out_channels[-1]   ,				resnet_eps=1E-6   ,				resnet_act_fn=lowercase   ,				output_scale_factor=1   ,				resnet_time_scale_shift='''default'''   ,				attention_head_dim=block_out_channels[-1]   ,				resnet_groups=lowercase   ,				temb_channels=lowercase   ,				)

           # out
           UpperCAmelCase			       =		nn.GroupNorm(num_channels=block_out_channels[-1]   ,				num_groups=lowercase   ,				eps=1E-6				)
           UpperCAmelCase			       =		nn.SiLU()

           UpperCAmelCase			       =		2 * out_channels if double_z else out_channels
           UpperCAmelCase			       =		nn.Convad(block_out_channels[-1]   ,				lowercase   ,				3   ,				padding=1				)

           UpperCAmelCase			       =		False







       def     A      (	self						:	str   ,				lowercase						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		x
           UpperCAmelCase			       =		self.conv_in(lowercase				)

           if self.training and self.gradient_checkpointing:

               def create_custom_forward(lowercase						:	Any				):
                   def custom_forward(*lowercase						:	Dict				):
                       return module(*lowercase				)

                   return custom_forward

               # down
               if is_torch_version('''>='''   ,				'''1.11.0'''				):
                   for down_block in self.down_blocks:
                       UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(
                           create_custom_forward(lowercase				)   ,				lowercase   ,				use_reentrant=lowercase				)
                   # middle
                   UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(
                       create_custom_forward(self.mid_block				)   ,				lowercase   ,				use_reentrant=lowercase				)
               else:
                   for down_block in self.down_blocks:
                       UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(create_custom_forward(lowercase				)   ,				lowercase				)
                   # middle
                   UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block				)   ,				lowercase				)

           else:
               # down
               for down_block in self.down_blocks:
                   UpperCAmelCase			       =		down_block(lowercase				)

               # middle
               UpperCAmelCase			       =		self.mid_block(lowercase				)

           # post-process
           UpperCAmelCase			       =		self.conv_norm_out(lowercase				)
           UpperCAmelCase			       =		self.conv_act(lowercase				)
           UpperCAmelCase			       =		self.conv_out(lowercase				)

           return sample






class 			_a  (	nn.Module ):
       def __init__(	self						:	List[str]   ,				lowercase						:	Union[str, Any]=3   ,				lowercase						:	str=3   ,				lowercase						:	Any=("UpDecoderBlock2D",)   ,				lowercase						:	Optional[Any]=(64,)   ,				lowercase						:	Any=2   ,				lowercase						:	Optional[int]=32   ,				lowercase						:	Optional[Any]="silu"   ,				lowercase						:	int="group"   ,				):



           '''simple docstring'''

           super().__init__()
           UpperCAmelCase			       =		layers_per_block

           UpperCAmelCase			       =		nn.Convad(
               lowercase   ,				block_out_channels[-1]   ,				kernel_size=3   ,				stride=1   ,				padding=1   ,				)

           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		nn.ModuleList([]				)

           UpperCAmelCase			       =		in_channels if norm_type == '''spatial''' else None

           # mid
           UpperCAmelCase			       =		UNetMidBlockaD(
               in_channels=block_out_channels[-1]   ,				resnet_eps=1E-6   ,				resnet_act_fn=lowercase   ,				output_scale_factor=1   ,				resnet_time_scale_shift='''default''' if norm_type == '''group''' else norm_type   ,				attention_head_dim=block_out_channels[-1]   ,				resnet_groups=lowercase   ,				temb_channels=lowercase   ,				)

           # up
           UpperCAmelCase			       =		list(reversed(lowercase				)				)
           UpperCAmelCase			       =		reversed_block_out_channels[0]
           for i, up_block_type in enumerate(lowercase				):
               UpperCAmelCase			       =		output_channel
               UpperCAmelCase			       =		reversed_block_out_channels[i]

               UpperCAmelCase			       =		i == len(lowercase				) - 1

               UpperCAmelCase			       =		get_up_block(
                   lowercase   ,				num_layers=self.layers_per_block + 1   ,				in_channels=lowercase   ,				out_channels=lowercase   ,				prev_output_channel=lowercase   ,				add_upsample=not is_final_block   ,				resnet_eps=1E-6   ,				resnet_act_fn=lowercase   ,				resnet_groups=lowercase   ,				attention_head_dim=lowercase   ,				temb_channels=lowercase   ,				resnet_time_scale_shift=lowercase   ,				)
               self.up_blocks.append(lowercase				)
               UpperCAmelCase			       =		output_channel

           # out
           if norm_type == "spatial":
               UpperCAmelCase			       =		SpatialNorm(block_out_channels[0]   ,				lowercase				)
           else:
               UpperCAmelCase			       =		nn.GroupNorm(num_channels=block_out_channels[0]   ,				num_groups=lowercase   ,				eps=1E-6				)
           UpperCAmelCase			       =		nn.SiLU()
           UpperCAmelCase			       =		nn.Convad(block_out_channels[0]   ,				lowercase   ,				3   ,				padding=1				)

           UpperCAmelCase			       =		False







       def     A      (	self						:	str   ,				lowercase						:	Optional[Any]   ,				lowercase						:	List[Any]=None				):



           '''simple docstring'''

           UpperCAmelCase			       =		z
           UpperCAmelCase			       =		self.conv_in(lowercase				)

           UpperCAmelCase			       =		next(iter(self.up_blocks.parameters()				)				).dtype
           if self.training and self.gradient_checkpointing:

               def create_custom_forward(lowercase						:	Optional[int]				):
                   def custom_forward(*lowercase						:	Optional[int]				):
                       return module(*lowercase				)

                   return custom_forward

               if is_torch_version('''>='''   ,				'''1.11.0'''				):
                   # middle
                   UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(
                       create_custom_forward(self.mid_block				)   ,				lowercase   ,				lowercase   ,				use_reentrant=lowercase				)
                   UpperCAmelCase			       =		sample.to(lowercase				)

                   # up
                   for up_block in self.up_blocks:
                       UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(
                           create_custom_forward(lowercase				)   ,				lowercase   ,				lowercase   ,				use_reentrant=lowercase				)
               else:
                   # middle
                   UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(
                       create_custom_forward(self.mid_block				)   ,				lowercase   ,				lowercase				)
                   UpperCAmelCase			       =		sample.to(lowercase				)

                   # up
                   for up_block in self.up_blocks:
                       UpperCAmelCase			       =		torch.utils.checkpoint.checkpoint(create_custom_forward(lowercase				)   ,				lowercase   ,				lowercase				)
           else:
               # middle
               UpperCAmelCase			       =		self.mid_block(lowercase   ,				lowercase				)
               UpperCAmelCase			       =		sample.to(lowercase				)

               # up
               for up_block in self.up_blocks:
                   UpperCAmelCase			       =		up_block(lowercase   ,				lowercase				)

        # post-process
           if latent_embeds is None:
               UpperCAmelCase			       =		self.conv_norm_out(lowercase				)
           else:
               UpperCAmelCase			       =		self.conv_norm_out(lowercase   ,				lowercase				)
           UpperCAmelCase			       =		self.conv_act(lowercase				)
           UpperCAmelCase			       =		self.conv_out(lowercase				)

           return sample






class 			_a  (	nn.Module ):
       def __init__(	self						:	Tuple   ,				lowercase						:	Tuple   ,				lowercase						:	int   ,				lowercase						:	List[Any]   ,				lowercase						:	List[str]=None   ,				lowercase						:	Union[str, Any]="random"   ,				lowercase						:	List[Any]=False   ,				lowercase						:	str=True				):



           '''simple docstring'''

           super().__init__()
           UpperCAmelCase			       =		n_e
           UpperCAmelCase			       =		vq_embed_dim
           UpperCAmelCase			       =		beta
           UpperCAmelCase			       =		legacy

           UpperCAmelCase			       =		nn.Embedding(self.n_e   ,				self.vq_embed_dim				)
           self.embedding.weight.data.uniform_(-1.0 / self.n_e   ,				1.0 / self.n_e				)

           UpperCAmelCase			       =		remap
           if self.remap is not None:
               self.register_buffer('''used'''   ,				torch.tensor(np.load(self.remap				)				)				)
               UpperCAmelCase			       =		self.used.shape[0]
               UpperCAmelCase			       =		unknown_index  # "random" or "extra" or integer
               if self.unknown_index == "extra":
                   UpperCAmelCase			       =		self.re_embed
                   UpperCAmelCase			       =		self.re_embed + 1
               print(
                   f"Remapping {self.n_e} indices to {self.re_embed} indices. "
                   f"Using {self.unknown_index} for unknown indices."				)
           else:
               UpperCAmelCase			       =		n_e

           UpperCAmelCase			       =		sane_index_shape
       def     A      (	self						:	Tuple   ,				lowercase						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		inds.shape
           assert len(lowercase				) > 1
           UpperCAmelCase			       =		inds.reshape(ishape[0]   ,				-1				)
           UpperCAmelCase			       =		self.used.to(lowercase				)
           UpperCAmelCase			       =		(inds[:, :, None] == used[None, None, ...]).long()
           UpperCAmelCase			       =		match.argmax(-1				)
           UpperCAmelCase			       =		match.sum(2				) < 1
           if self.unknown_index == "random":
               UpperCAmelCase			       =		torch.randint(0   ,				self.re_embed   ,				size=new[unknown].shape				).to(device=new.device				)
           else:
               UpperCAmelCase			       =		self.unknown_index
           return new.reshape(lowercase				)
       def     A      (	self						:	Union[str, Any]   ,				lowercase						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		inds.shape
           assert len(lowercase				) > 1
           UpperCAmelCase			       =		inds.reshape(ishape[0]   ,				-1				)
           UpperCAmelCase			       =		self.used.to(lowercase				)
           if self.re_embed > self.used.shape[0]:  # extra token
               UpperCAmelCase			       =		0  # simply set to zero
           UpperCAmelCase			       =		torch.gather(used[None, :][inds.shape[0] * [0], :]   ,				1   ,				lowercase				)
           return back.reshape(lowercase				)
       def     A      (	self						:	Tuple   ,				lowercase						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		z.permute(0   ,				2   ,				3   ,				1				).contiguous()
           UpperCAmelCase			       =		z.view(-1   ,				self.vq_embed_dim				)

           # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
           UpperCAmelCase			       =		torch.argmin(torch.cdist(lowercase   ,				self.embedding.weight				)   ,				dim=1				)

           UpperCAmelCase			       =		self.embedding(lowercase				).view(z.shape				)
           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		None

           # compute loss for embedding
           if not self.legacy:
               UpperCAmelCase			       =		self.beta * torch.mean((z_q.detach() - z) ** 2				) + torch.mean((z_q - z.detach()) ** 2				)
           else:
               UpperCAmelCase			       =		torch.mean((z_q.detach() - z) ** 2				) + self.beta * torch.mean((z_q - z.detach()) ** 2				)

           # preserve gradients
           UpperCAmelCase			       =		z + (z_q - z).detach()

           # reshape back to match original input shape
           UpperCAmelCase			       =		z_q.permute(0   ,				3   ,				1   ,				2				).contiguous()

           if self.remap is not None:
               UpperCAmelCase			       =		min_encoding_indices.reshape(z.shape[0]   ,				-1				)  # add batch axis
               UpperCAmelCase			       =		self.remap_to_used(lowercase				)
               UpperCAmelCase			       =		min_encoding_indices.reshape(-1   ,				1				)  # flatten

           if self.sane_index_shape:
               UpperCAmelCase			       =		min_encoding_indices.reshape(z_q.shape[0]   ,				z_q.shape[2]   ,				z_q.shape[3]				)

           return z_q, loss, (perplexity, min_encodings, min_encoding_indices)







       def     A      (	self						:	int   ,				lowercase						:	str   ,				lowercase						:	List[Any]				):



           '''simple docstring'''

           if self.remap is not None:
               UpperCAmelCase			       =		indices.reshape(shape[0]   ,				-1				)  # add batch axis
               UpperCAmelCase			       =		self.unmap_to_all(lowercase				)
               UpperCAmelCase			       =		indices.reshape(-1				)  # flatten again

           # get quantized latent vectors
           UpperCAmelCase			       =		self.embedding(lowercase				)

           if shape is not None:
               UpperCAmelCase			       =		z_q.view(lowercase				)
               # reshape back to match original input shape
               UpperCAmelCase			       =		z_q.permute(0   ,				3   ,				1   ,				2				).contiguous()

           return z_q






class 			_a  (	__a ):
       def __init__(	self						:	List[Any]   ,				lowercase						:	Dict   ,				lowercase						:	List[Any]=False				):



           '''simple docstring'''

           UpperCAmelCase			       =		parameters
           UpperCAmelCase       ,     UpperCAmelCase			       =		torch.chunk(lowercase   ,				2   ,				dim=1				)
           UpperCAmelCase			       =		torch.clamp(self.logvar   ,				-30.0   ,				20.0				)
           UpperCAmelCase			       =		deterministic
           UpperCAmelCase			       =		torch.exp(0.5 * self.logvar				)
           UpperCAmelCase			       =		torch.exp(self.logvar				)
           if self.deterministic:
               UpperCAmelCase			       =		UpperCAmelCase			       =		torch.zeros_like(
                   self.mean   ,				device=self.parameters.device   ,				dtype=self.parameters.dtype				)
       def     A      (	self						:	List[Any]   ,				lowercase						:	Optional[torch.Generator] = None				):



           '''simple docstring'''

           UpperCAmelCase			       =		randn_tensor(
               self.mean.shape   ,				generator=lowercase   ,				device=self.parameters.device   ,				dtype=self.parameters.dtype				)
           UpperCAmelCase			       =		self.mean + self.std * sample
           return x
       def     A      (	self						:	Optional[int]   ,				lowercase						:	Dict=None				):



           '''simple docstring'''

           if self.deterministic:
               return torch.Tensor([0.0]				)
           else:
               if other is None:
                   return 0.5 * torch.sum(torch.pow(self.mean   ,				2				) + self.var - 1.0 - self.logvar   ,				dim=[1, 2, 3]				)
               else:
                   return 0.5 * torch.sum(
                       torch.pow(self.mean - other.mean   ,				2				) / other.var
                       + self.var / other.var
                       - 1.0
                       - self.logvar
                       + other.logvar   ,				dim=[1, 2, 3]   ,				)
       def     A      (	self						:	List[str]   ,				lowercase						:	List[Any]   ,				lowercase						:	List[str]=[1, 2, 3]				):



           '''simple docstring'''

           if self.deterministic:
               return torch.Tensor([0.0]				)
           UpperCAmelCase			       =		np.log(2.0 * np.pi				)
           return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean   ,				2				) / self.var   ,				dim=lowercase				)







       def     A      (	self						:	List[Any]				):



           '''simple docstring'''

           return self.mean










'''simple docstring'''







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


A     =logging.get_logger(__name__)

A     ={
    '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 			_a  (	__a ):
       __a				:     List[Any]   				=					"""marian"""
       __a				:     Union[str, Any]   				=					["""past_key_values"""]
       __a				:     List[str]   				=					{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}
       def __init__(	self						:	List[Any]   ,				lowercase						:	Union[str, Any]=58_101   ,				lowercase						:	Tuple=None   ,				lowercase						:	str=1_024   ,				lowercase						:	Optional[int]=12   ,				lowercase						:	Optional[int]=4_096   ,				lowercase						:	int=16   ,				lowercase						:	List[Any]=12   ,				lowercase						:	int=4_096   ,				lowercase						:	Optional[int]=16   ,				lowercase						:	int=0.0   ,				lowercase						:	Tuple=0.0   ,				lowercase						:	Tuple=True   ,				lowercase						:	Union[str, Any]=True   ,				lowercase						:	List[Any]="gelu"   ,				lowercase						:	Tuple=1_024   ,				lowercase						:	str=0.1   ,				lowercase						:	str=0.0   ,				lowercase						:	Optional[int]=0.0   ,				lowercase						:	Dict=0.02   ,				lowercase						:	Union[str, Any]=58_100   ,				lowercase						:	List[str]=False   ,				lowercase						:	str=58_100   ,				lowercase						:	Any=0   ,				lowercase						:	Optional[Any]=0   ,				lowercase						:	Tuple=True   ,				**lowercase						:	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=lowercase   ,				eos_token_id=lowercase   ,				is_encoder_decoder=lowercase   ,				decoder_start_token_id=lowercase   ,				forced_eos_token_id=lowercase   ,				**lowercase   ,				)






class 			_a  (	__a ):
       @property
       # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
       def     A      (	self						:	int				):



           '''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_(lowercase   ,				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(lowercase				):
                       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     A      (	self						:	Any				):



           '''simple docstring'''

           if self.task in ["default", "seq2seq-lm"]:
               UpperCAmelCase			       =		super().outputs
           else:
               UpperCAmelCase			       =		super(lowercase   ,				self				).outputs
               if self.use_past:
                   UpperCAmelCase       ,     UpperCAmelCase			       =		self.num_layers
                   for i in range(lowercase				):
                       UpperCAmelCase			       =		{0: '''batch''', 2: '''past_sequence + sequence'''}
                       UpperCAmelCase			       =		{0: '''batch''', 2: '''past_sequence + sequence'''}
           return common_outputs
       def     A      (	self						:	Dict   ,				lowercase						:	PreTrainedTokenizer   ,				lowercase						:	int = -1   ,				lowercase						:	int = -1   ,				lowercase						:	bool = False   ,				lowercase						:	Optional[TensorType] = None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		self._generate_dummy_inputs_for_encoder_and_decoder(
               lowercase   ,				lowercase   ,				lowercase   ,				lowercase   ,				lowercase				)

           # Generate decoder inputs
           UpperCAmelCase			       =		seq_length if not self.use_past else 1
           UpperCAmelCase			       =		self._generate_dummy_inputs_for_encoder_and_decoder(
               lowercase   ,				lowercase   ,				lowercase   ,				lowercase   ,				lowercase				)
           UpperCAmelCase			       =		{f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
           UpperCAmelCase			       =		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
               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(lowercase   ,				lowercase				)]   ,				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(lowercase   ,				lowercase				)
               UpperCAmelCase			       =		max(lowercase   ,				lowercase				) - min_num_layers
               UpperCAmelCase			       =		'''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.
               UpperCAmelCase			       =		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
       def     A      (	self						:	int   ,				lowercase						:	PreTrainedTokenizer   ,				lowercase						:	int = -1   ,				lowercase						:	int = -1   ,				lowercase						:	bool = False   ,				lowercase						:	Optional[TensorType] = None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		self._generate_dummy_inputs_for_encoder_and_decoder(
               lowercase   ,				lowercase   ,				lowercase   ,				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
               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(lowercase   ,				lowercase   ,				dtype=lowercase				)]   ,				dim=1				)
               UpperCAmelCase			       =		[
                   (torch.zeros(lowercase				), torch.zeros(lowercase				)) for _ in range(lowercase				)
               ]
           return common_inputs
       def     A      (	self						:	str   ,				lowercase						:	PreTrainedTokenizer   ,				lowercase						:	int = -1   ,				lowercase						:	int = -1   ,				lowercase						:	bool = False   ,				lowercase						:	Optional[TensorType] = None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		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
           UpperCAmelCase			       =		tokenizer.num_special_tokens_to_add(lowercase				)
           UpperCAmelCase			       =		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
           UpperCAmelCase			       =		[''' '''.join([tokenizer.unk_token]				) * seq_length] * batch_size
           UpperCAmelCase			       =		dict(tokenizer(lowercase   ,				return_tensors=lowercase				)				)
           return common_inputs
       def     A      (	self						:	List[str]   ,				lowercase						:	PreTrainedTokenizer   ,				lowercase						:	int = -1   ,				lowercase						:	int = -1   ,				lowercase						:	bool = False   ,				lowercase						:	Optional[TensorType] = None   ,				):



           '''simple docstring'''

           if self.task in ["default", "seq2seq-lm"]:
               UpperCAmelCase			       =		self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                   lowercase   ,				batch_size=lowercase   ,				seq_length=lowercase   ,				is_pair=lowercase   ,				framework=lowercase				)

           else:
               UpperCAmelCase			       =		self._generate_dummy_inputs_for_causal_lm(
                   lowercase   ,				batch_size=lowercase   ,				seq_length=lowercase   ,				is_pair=lowercase   ,				framework=lowercase				)

           return common_inputs
       def     A      (	self						:	List[Any]   ,				lowercase						:	Any   ,				lowercase						:	Tuple   ,				lowercase						:	Any   ,				lowercase						:	Any				):



           '''simple docstring'''

           if self.task in ["default", "seq2seq-lm"]:
               UpperCAmelCase			       =		super()._flatten_past_key_values_(lowercase   ,				lowercase   ,				lowercase   ,				lowercase				)
           else:
               UpperCAmelCase			       =		super(lowercase   ,				self				)._flatten_past_key_values_(
                   lowercase   ,				lowercase   ,				lowercase   ,				lowercase				)







       @property
       def     A      (	self						:	Any				):



           '''simple docstring'''

           return 1E-4




