






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


__snake_case     :     Optional[Any]       =  logging.get_logger(__name__)

__snake_case     :     str       =  {
    """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      __SCREAMING_SNAKE_CASE       ( __lowercase):
						_SCREAMING_SNAKE_CASE						:							List[str]				=       '''m2m_100'''
						_SCREAMING_SNAKE_CASE						:							Any				=       ['''past_key_values''']
						_SCREAMING_SNAKE_CASE						:							Optional[int]				=       {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






						def __init__(						self  ,    _UpperCamelCase=12_81_12  ,    _UpperCamelCase=10_24  ,    _UpperCamelCase=12  ,    _UpperCamelCase=40_96  ,    _UpperCamelCase=16  ,    _UpperCamelCase=12  ,    _UpperCamelCase=40_96  ,    _UpperCamelCase=16  ,    _UpperCamelCase=0.05  ,    _UpperCamelCase=0.05  ,    _UpperCamelCase=True  ,    _UpperCamelCase=True  ,    _UpperCamelCase="relu"  ,    _UpperCamelCase=10_24  ,    _UpperCamelCase=0.1  ,    _UpperCamelCase=0.1  ,    _UpperCamelCase=0.0  ,    _UpperCamelCase=0.02  ,    _UpperCamelCase=2  ,    _UpperCamelCase=True  ,    _UpperCamelCase=1  ,    _UpperCamelCase=0  ,    _UpperCamelCase=2  ,    **_UpperCamelCase  ,    ):






												"""simple docstring"""
												lowerCAmelCase__         =    vocab_size
												lowerCAmelCase__         =    max_position_embeddings
												lowerCAmelCase__         =    d_model
												lowerCAmelCase__         =    encoder_ffn_dim
												lowerCAmelCase__         =    encoder_layers
												lowerCAmelCase__         =    encoder_attention_heads
												lowerCAmelCase__         =    decoder_ffn_dim
												lowerCAmelCase__         =    decoder_layers
												lowerCAmelCase__         =    decoder_attention_heads
												lowerCAmelCase__         =    dropout
												lowerCAmelCase__         =    attention_dropout
												lowerCAmelCase__         =    activation_dropout
												lowerCAmelCase__         =    activation_function
												lowerCAmelCase__         =    init_std
												lowerCAmelCase__         =    encoder_layerdrop
												lowerCAmelCase__         =    decoder_layerdrop
												lowerCAmelCase__         =    use_cache
												lowerCAmelCase__         =    encoder_layers
												lowerCAmelCase__         =    scale_embedding  # scale factor will be sqrt(d_model) if True

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




class      __SCREAMING_SNAKE_CASE       ( __lowercase):






						@property
						def    UpperCamelCase__   (						self ):






												"""simple docstring"""
												lowerCAmelCase__         =    OrderedDict(
												    [
												        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
												        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
												    ] )

												if self.use_past:
																		lowerCAmelCase__         =    {0: 'batch'}
																		lowerCAmelCase__         =    {0: 'batch', 1: 'past_decoder_sequence + sequence'}
												else:
																		lowerCAmelCase__         =    {0: 'batch', 1: 'decoder_sequence'}
																		lowerCAmelCase__         =    {0: 'batch', 1: 'decoder_sequence'}

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






						def    UpperCamelCase__   (						self  ,    _UpperCamelCase  ,    _UpperCamelCase = -1  ,    _UpperCamelCase = -1  ,    _UpperCamelCase = False  ,    _UpperCamelCase = None  ,    ):






												"""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
												lowerCAmelCase__         =    compute_effective_axis_dimension(
												    _UpperCamelCase  ,    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
												lowerCAmelCase__         =    tokenizer.num_special_tokens_to_add(_UpperCamelCase )
												lowerCAmelCase__         =    compute_effective_axis_dimension(
												    _UpperCamelCase  ,    fixed_dimension=OnnxConfig.default_fixed_sequence  ,    num_token_to_add=_UpperCamelCase )

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






						def    UpperCamelCase__   (						self  ,    _UpperCamelCase  ,    _UpperCamelCase = -1  ,    _UpperCamelCase = -1  ,    _UpperCamelCase = False  ,    _UpperCamelCase = None  ,    ):






												"""simple docstring"""
												lowerCAmelCase__         =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
												    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase )

												# Generate decoder inputs
												lowerCAmelCase__         =    seq_length if not self.use_past else 1
												lowerCAmelCase__         =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
												    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase  ,    _UpperCamelCase )
												lowerCAmelCase__         =    {F"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
												lowerCAmelCase__         =    dict(**_UpperCamelCase  ,    **_UpperCamelCase )

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

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

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

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







						_SCREAMING_SNAKE_CASE						:							str				=       _generate_dummy_inputs_for_default_and_seqaseq_lm













from __future__ import annotations







def 						_UpperCamelCase (     UpperCamelCase_							:       list[int]		)	-> list[int]:

						"""simple docstring"""



						if not list_of_ints:
												return []

						if min(UpperCamelCase_		) < 0:
												raise ValueError('All numbers must be positive'		)

						lowerCAmelCase__         =    max(len(bin(UpperCamelCase_		)[2:]		) for x in list_of_ints		)
						return _msd_radix_sort(UpperCamelCase_     ,							UpperCamelCase_		)







def 						_UpperCamelCase (     UpperCamelCase_							:       list[int]     ,							UpperCamelCase_							:       int		)	-> list[int]:

						"""simple docstring"""



						if bit_position == 0 or len(UpperCamelCase_		) in [0, 1]:
												return list_of_ints

						lowerCAmelCase__         =    []
						lowerCAmelCase__         =    []
						# Split numbers based on bit at bit_position from the right
						for number in list_of_ints:
												if (number >> (bit_position - 1)) & 1:
																		# number has a one at bit bit_position
																		ones.append(UpperCamelCase_		)
												else:
																		# number has a zero at bit bit_position
																		zeros.append(UpperCamelCase_		)

    # recursively split both lists further
						lowerCAmelCase__         =    _msd_radix_sort(UpperCamelCase_     ,							bit_position - 1		)
						lowerCAmelCase__         =    _msd_radix_sort(UpperCamelCase_     ,							bit_position - 1		)

						# recombine lists
						lowerCAmelCase__         =    zeros
						res.extend(UpperCamelCase_		)

						return res







def 						_UpperCamelCase (     UpperCamelCase_							:       list[int]		)	-> Any:

						"""simple docstring"""



						lowerCAmelCase__         =    len(UpperCamelCase_		)
						if not list_of_ints or length == 1:
												return

						if min(UpperCamelCase_		) < 0:
												raise ValueError('All numbers must be positive'		)

						lowerCAmelCase__         =    max(len(bin(UpperCamelCase_		)[2:]		) for x in list_of_ints		)
						_msd_radix_sort_inplace(UpperCamelCase_     ,							UpperCamelCase_     ,							0     ,							UpperCamelCase_		)







def 						_UpperCamelCase (     UpperCamelCase_							:       list[int]     ,							UpperCamelCase_							:       int     ,							UpperCamelCase_							:       int     ,							UpperCamelCase_							:       int		)	-> int:

						"""simple docstring"""



						if bit_position == 0 or end_index - begin_index <= 1:
												return

						bit_position -= 1

						lowerCAmelCase__         =    begin_index
						lowerCAmelCase__         =    end_index - 1
						while i <= j:
												lowerCAmelCase__         =    False
												if not (list_of_ints[i] >> bit_position) & 1:
																		# found zero at the beginning
																		i += 1
																		lowerCAmelCase__         =    True
												if (list_of_ints[j] >> bit_position) & 1:
																		# found one at the end
																		j -= 1
																		lowerCAmelCase__         =    True

												if changed:
																		continue

												lowerCAmelCase__    ,							lowerCAmelCase__         =    list_of_ints[j], list_of_ints[i]
												j -= 1
												if j != i:
																		i += 1

						_msd_radix_sort_inplace(UpperCamelCase_     ,							UpperCamelCase_     ,							UpperCamelCase_     ,							UpperCamelCase_		)
						_msd_radix_sort_inplace(UpperCamelCase_     ,							UpperCamelCase_     ,							UpperCamelCase_     ,							UpperCamelCase_		)


if __name__ == "__main__":
					import doctest

					doctest.testmod()





