


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     :			Tuple      =logging.get_logger(__name__)

__lowerCAmelCase     :			Union[str, Any]      ={
    '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 	_lowercase				(      A__	):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__    :      int      =		'''m2m_100'''
				SCREAMING_SNAKE_CASE__    :      str      =		['''past_key_values''']
				SCREAMING_SNAKE_CASE__    :      List[Any]      =		{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}


				def __init__(	self      :int      ,      lowerCAmelCase__      :Optional[int]=128_112      ,      lowerCAmelCase__      :List[str]=1_024      ,      lowerCAmelCase__      :Union[str, Any]=12      ,      lowerCAmelCase__      :Any=4_096      ,      lowerCAmelCase__      :Tuple=16      ,      lowerCAmelCase__      :Optional[Any]=12      ,      lowerCAmelCase__      :List[str]=4_096      ,      lowerCAmelCase__      :Tuple=16      ,      lowerCAmelCase__      :Optional[Any]=0.05      ,      lowerCAmelCase__      :Tuple=0.05      ,      lowerCAmelCase__      :Tuple=True      ,      lowerCAmelCase__      :List[str]=True      ,      lowerCAmelCase__      :List[str]="relu"      ,      lowerCAmelCase__      :Union[str, Any]=1_024      ,      lowerCAmelCase__      :List[Any]=0.1      ,      lowerCAmelCase__      :str=0.1      ,      lowerCAmelCase__      :Tuple=0.0      ,      lowerCAmelCase__      :Union[str, Any]=0.02      ,      lowerCAmelCase__      :Tuple=2      ,      lowerCAmelCase__      :Optional[int]=True      ,      lowerCAmelCase__      :str=1      ,      lowerCAmelCase__      :List[str]=0      ,      lowerCAmelCase__      :Optional[int]=2      ,      **lowerCAmelCase__      :List[str]      ,      )		->      int:
										__SCREAMING_SNAKE_CASE						:					Any		      =		vocab_size
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		max_position_embeddings
										__SCREAMING_SNAKE_CASE						:					str		      =		d_model
										__SCREAMING_SNAKE_CASE						:					Tuple		      =		encoder_ffn_dim
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		encoder_layers
										__SCREAMING_SNAKE_CASE						:					str		      =		encoder_attention_heads
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		decoder_ffn_dim
										__SCREAMING_SNAKE_CASE						:					Dict		      =		decoder_layers
										__SCREAMING_SNAKE_CASE						:					Dict		      =		decoder_attention_heads
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		dropout
										__SCREAMING_SNAKE_CASE						:					int		      =		attention_dropout
										__SCREAMING_SNAKE_CASE						:					int		      =		activation_dropout
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		activation_function
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		init_std
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		encoder_layerdrop
										__SCREAMING_SNAKE_CASE						:					str		      =		decoder_layerdrop
										__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		use_cache
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		encoder_layers
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		scale_embedding  # scale factor will be sqrt(d_model) if True

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




class 	_lowercase				(      A__	):






				'''simple docstring'''



				@property
				def 		__magic_name__(	self      :List[str]     )		->      Mapping[str, Mapping[int, str]]:
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		OrderedDict(
										    [
										        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
										        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
										    ]     )

										if self.use_past:
																__SCREAMING_SNAKE_CASE						:					Tuple		      =		{0: '''batch'''}
																__SCREAMING_SNAKE_CASE						:					str		      =		{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
										else:
																__SCREAMING_SNAKE_CASE						:					Any		      =		{0: '''batch''', 1: '''decoder_sequence'''}
																__SCREAMING_SNAKE_CASE						:					Tuple		      =		{0: '''batch''', 1: '''decoder_sequence'''}

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


				def 		__magic_name__(	self      :List[str]      ,      lowerCAmelCase__      :PreTrainedTokenizer      ,      lowerCAmelCase__      :int = -1      ,      lowerCAmelCase__      :int = -1      ,      lowerCAmelCase__      :bool = False      ,      lowerCAmelCase__      :Optional[TensorType] = None      ,      )		->      Mapping[str, Any]:
										# Copied from OnnxConfig.generate_dummy_inputs
										# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
										# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
										__SCREAMING_SNAKE_CASE						:					List[str]		      =		compute_effective_axis_dimension(
										    lowerCAmelCase__      ,      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
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		tokenizer.num_special_tokens_to_add(lowerCAmelCase__     )
										__SCREAMING_SNAKE_CASE						:					int		      =		compute_effective_axis_dimension(
										    lowerCAmelCase__      ,      fixed_dimension=OnnxConfig.default_fixed_sequence      ,      num_token_to_add=lowerCAmelCase__     )

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


				def 		__magic_name__(	self      :Any      ,      lowerCAmelCase__      :PreTrainedTokenizer      ,      lowerCAmelCase__      :int = -1      ,      lowerCAmelCase__      :int = -1      ,      lowerCAmelCase__      :bool = False      ,      lowerCAmelCase__      :Optional[TensorType] = None      ,      )		->      Mapping[str, Any]:
										__SCREAMING_SNAKE_CASE						:					int		      =		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    lowerCAmelCase__      ,      lowerCAmelCase__      ,      lowerCAmelCase__      ,      lowerCAmelCase__      ,      lowerCAmelCase__     )

										# Generate decoder inputs
										__SCREAMING_SNAKE_CASE						:					Union[str, Any]		      =		seq_length if not self.use_past else 1
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    lowerCAmelCase__      ,      lowerCAmelCase__      ,      lowerCAmelCase__      ,      lowerCAmelCase__      ,      lowerCAmelCase__     )
										__SCREAMING_SNAKE_CASE						:					Optional[Any]		      =		{f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
										__SCREAMING_SNAKE_CASE						:					List[Any]		      =		dict(**lowerCAmelCase__      ,      **lowerCAmelCase__     )

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

																__SCREAMING_SNAKE_CASE						:					int		      =		torch.cat(
																    [common_inputs['''decoder_attention_mask'''], torch.ones(lowerCAmelCase__      ,      lowerCAmelCase__     )]      ,      dim=1     )

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

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



				SCREAMING_SNAKE_CASE__    :      Dict      =		_generate_dummy_inputs_for_default_and_seqaseq_lm




from ctypes import c_float, sizeof
from enum import Enum
from typing import TYPE_CHECKING, Optional, Union


if TYPE_CHECKING:
						from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore
class 	_lowercase				(      A__	):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__    :      int      =		c_float


				@property
				def 		__magic_name__(	self      :int     )		->      int:
										return sizeof(self.value     )




def 	_UpperCamelCase							(				lowercase__   ,	lowercase__   ,	lowercase__ = 0				):
						# < 0 is possible if using a dynamic axis
						if dimension <= 0:
												__SCREAMING_SNAKE_CASE						:					int		      =		fixed_dimension

						dimension -= num_token_to_add
						return dimension


def 	_UpperCamelCase							(				lowercase__   ,	lowercase__				):
						return num_parameters * dtype.size


def 	_UpperCamelCase							(				lowercase__				):
						from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore

						try:
												return AutoProcessor.from_pretrained(lowercase__				)
						except (ValueError, OSError, KeyError):
												__SCREAMING_SNAKE_CASE      ,      __SCREAMING_SNAKE_CASE						:					Tuple		      =		None, None
												try:
																		__SCREAMING_SNAKE_CASE						:					List[Any]		      =		AutoTokenizer.from_pretrained(lowercase__				)
												except (OSError, KeyError):
																		pass
												try:
																		__SCREAMING_SNAKE_CASE						:					Optional[int]		      =		AutoFeatureExtractor.from_pretrained(lowercase__				)
												except (OSError, KeyError):
																		pass

												if tokenizer is not None and feature_extractor is not None:
																		raise ValueError(
																		    F'''Couldn\'t auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor.'''				)
												elif tokenizer is None and feature_extractor is None:
																		return None
												elif tokenizer is not None:
																		return tokenizer
												else:
																		return feature_extractor
