



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


__A             =logging.get_logger(__name__)

__A             ={
    "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__      (      __UpperCamelCase       ):




			'''simple docstring'''






			UpperCamelCase      						=	"""m2m_100"""
			UpperCamelCase      						=	["""past_key_values"""]
			UpperCamelCase      						=	{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}




			def __init__(       self							:			int	,	a_							:			Optional[int]=12_81_12	,	a_							:			Any=10_24	,	a_							:			Any=12	,	a_							:			Any=40_96	,	a_							:			Optional[Any]=16	,	a_							:			str=12	,	a_							:			Union[str, Any]=40_96	,	a_							:			int=16	,	a_							:			Dict=0.0_5	,	a_							:			Any=0.0_5	,	a_							:			int=True	,	a_							:			Optional[int]=True	,	a_							:			Union[str, Any]="relu"	,	a_							:			Dict=10_24	,	a_							:			Optional[Any]=0.1	,	a_							:			int=0.1	,	a_							:			Union[str, Any]=0.0	,	a_							:			Optional[int]=0.0_2	,	a_							:			List[str]=2	,	a_							:			Any=True	,	a_							:			int=1	,	a_							:			Dict=0	,	a_							:			List[str]=2	,	**a_							:			Optional[Any]	,	):
					'''simple docstring'''





					__UpperCAmelCase   :					List[str]													=     vocab_size
					__UpperCAmelCase   :					Tuple													=     max_position_embeddings
					__UpperCAmelCase   :					Union[str, Any]													=     d_model
					__UpperCAmelCase   :					Dict													=     encoder_ffn_dim
					__UpperCAmelCase   :					int													=     encoder_layers
					__UpperCAmelCase   :					Any													=     encoder_attention_heads
					__UpperCAmelCase   :					int													=     decoder_ffn_dim
					__UpperCAmelCase   :					Any													=     decoder_layers
					__UpperCAmelCase   :					List[str]													=     decoder_attention_heads
					__UpperCAmelCase   :					Optional[int]													=     dropout
					__UpperCAmelCase   :					List[Any]													=     attention_dropout
					__UpperCAmelCase   :					Tuple													=     activation_dropout
					__UpperCAmelCase   :					Tuple													=     activation_function
					__UpperCAmelCase   :					Any													=     init_std
					__UpperCAmelCase   :					Tuple													=     encoder_layerdrop
					__UpperCAmelCase   :					List[Any]													=     decoder_layerdrop
					__UpperCAmelCase   :					Any													=     use_cache
					__UpperCAmelCase   :					Tuple													=     encoder_layers
					__UpperCAmelCase   :					int													=     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__      (      __UpperCamelCase       ):




			'''simple docstring'''






			@property
			def 			snake_case__					(       self							:			Any			):
					'''simple docstring'''





					__UpperCAmelCase   :					Optional[int]													=     OrderedDict(
					    [
					        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
					        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
					    ]			)

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

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




			def 			snake_case__					(       self							:			Optional[int]	,	a_							:			PreTrainedTokenizer	,	a_							:			int = -1	,	a_							:			int = -1	,	a_							:			bool = False	,	a_							:			Optional[TensorType] = None	,	):
					'''simple docstring'''





					__UpperCAmelCase   :					Tuple													=     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   :					str													=     tokenizer.num_special_tokens_to_add(a_			)
					__UpperCAmelCase   :					List[Any]													=     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   :					Optional[int]													=     [''' '''.join([tokenizer.unk_token]			) * seq_length] * batch_size
					__UpperCAmelCase   :					int													=     dict(tokenizer(a_	,	return_tensors=a_			)			)
					return common_inputs




			def 			snake_case__					(       self							:			Optional[int]	,	a_							:			PreTrainedTokenizer	,	a_							:			int = -1	,	a_							:			int = -1	,	a_							:			bool = False	,	a_							:			Optional[TensorType] = None	,	):
					'''simple docstring'''





					__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   :					str													=     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    a_	,	a_	,	a_	,	a_	,	a_			)
					__UpperCAmelCase   :					List[str]													=     {F'decoder_{name}': tensor for name, tensor in decoder_inputs.items()}
					__UpperCAmelCase   :					Optional[Any]													=     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   :					Optional[Any]													=     common_inputs['''input_ids'''].shape
							__UpperCAmelCase   :					Optional[Any]													=     common_inputs['''decoder_input_ids'''].shape[1]
							__UpperCAmelCase							, __UpperCAmelCase   :					Union[str, Any]													=     self.num_attention_heads
							__UpperCAmelCase   :					str													=     (
							    batch,
							    num_encoder_attention_heads,
							    encoder_seq_length,
							    self._config.hidden_size // num_encoder_attention_heads,
							)
							__UpperCAmelCase   :					Any													=     decoder_seq_length + 3
							__UpperCAmelCase   :					str													=     (
							    batch,
							    num_decoder_attention_heads,
							    decoder_past_length,
							    self._config.hidden_size // num_decoder_attention_heads,
							)

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

							__UpperCAmelCase   :					str													=     []
							# 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   :					str													=     max(a_	,	a_			) - min_num_layers
							__UpperCAmelCase   :					str													=     '''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   :					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










def 	a						(				_UpperCAmelCase							: str						):







		'''simple docstring'''





		__UpperCAmelCase   :					Optional[Any]													=     53_81
		for x in s:
				__UpperCAmelCase   :					str													=     ((hash_value << 5) + hash_value) + ord(_UpperCAmelCase						)
		return hash_value & 0xFFFF_FFFF





