




from typing import TYPE_CHECKING

from ..utils import OptionalDependencyNotAvailable, _LazyModule, is_flax_available, is_tf_available, is_torch_available


lowercase_		=							{
    '''configuration_utils''': ['''GenerationConfig'''],
    '''streamers''': ['''TextIteratorStreamer''', '''TextStreamer'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_		=							[
		    '''Constraint''',
		    '''ConstraintListState''',
		    '''DisjunctiveConstraint''',
		    '''PhrasalConstraint''',
		]
		lowercase_		=							[
		    '''BeamHypotheses''',
		    '''BeamScorer''',
		    '''BeamSearchScorer''',
		    '''ConstrainedBeamSearchScorer''',
		]
		lowercase_		=							[
		    '''EpsilonLogitsWarper''',
		    '''EtaLogitsWarper''',
		    '''ForcedBOSTokenLogitsProcessor''',
		    '''ForcedEOSTokenLogitsProcessor''',
		    '''HammingDiversityLogitsProcessor''',
		    '''InfNanRemoveLogitsProcessor''',
		    '''LogitsProcessor''',
		    '''LogitsProcessorList''',
		    '''LogitsWarper''',
		    '''MinLengthLogitsProcessor''',
		    '''MinNewTokensLengthLogitsProcessor''',
		    '''NoBadWordsLogitsProcessor''',
		    '''NoRepeatNGramLogitsProcessor''',
		    '''PrefixConstrainedLogitsProcessor''',
		    '''RepetitionPenaltyLogitsProcessor''',
		    '''SequenceBiasLogitsProcessor''',
		    '''EncoderRepetitionPenaltyLogitsProcessor''',
		    '''TemperatureLogitsWarper''',
		    '''TopKLogitsWarper''',
		    '''TopPLogitsWarper''',
		    '''TypicalLogitsWarper''',
		    '''EncoderNoRepeatNGramLogitsProcessor''',
		    '''ExponentialDecayLengthPenalty''',
		    '''LogitNormalization''',
		]
		lowercase_		=							[
		    '''MaxNewTokensCriteria''',
		    '''MaxLengthCriteria''',
		    '''MaxTimeCriteria''',
		    '''StoppingCriteria''',
		    '''StoppingCriteriaList''',
		    '''validate_stopping_criteria''',
		]
		lowercase_		=							[
		    '''GenerationMixin''',
		    '''top_k_top_p_filtering''',
		    '''GreedySearchEncoderDecoderOutput''',
		    '''GreedySearchDecoderOnlyOutput''',
		    '''SampleEncoderDecoderOutput''',
		    '''SampleDecoderOnlyOutput''',
		    '''BeamSearchEncoderDecoderOutput''',
		    '''BeamSearchDecoderOnlyOutput''',
		    '''BeamSampleEncoderDecoderOutput''',
		    '''BeamSampleDecoderOnlyOutput''',
		    '''ContrastiveSearchEncoderDecoderOutput''',
		    '''ContrastiveSearchDecoderOnlyOutput''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_		=							[
		    '''TFForcedBOSTokenLogitsProcessor''',
		    '''TFForcedEOSTokenLogitsProcessor''',
		    '''TFLogitsProcessor''',
		    '''TFLogitsProcessorList''',
		    '''TFLogitsWarper''',
		    '''TFMinLengthLogitsProcessor''',
		    '''TFNoBadWordsLogitsProcessor''',
		    '''TFNoRepeatNGramLogitsProcessor''',
		    '''TFRepetitionPenaltyLogitsProcessor''',
		    '''TFTemperatureLogitsWarper''',
		    '''TFTopKLogitsWarper''',
		    '''TFTopPLogitsWarper''',
		    '''TFForceTokensLogitsProcessor''',
		    '''TFSuppressTokensAtBeginLogitsProcessor''',
		    '''TFSuppressTokensLogitsProcessor''',
		]
		lowercase_		=							[
		    '''TFGenerationMixin''',
		    '''tf_top_k_top_p_filtering''',
		    '''TFGreedySearchDecoderOnlyOutput''',
		    '''TFGreedySearchEncoderDecoderOutput''',
		    '''TFSampleEncoderDecoderOutput''',
		    '''TFSampleDecoderOnlyOutput''',
		    '''TFBeamSearchEncoderDecoderOutput''',
		    '''TFBeamSearchDecoderOnlyOutput''',
		    '''TFBeamSampleEncoderDecoderOutput''',
		    '''TFBeamSampleDecoderOnlyOutput''',
		    '''TFContrastiveSearchEncoderDecoderOutput''',
		    '''TFContrastiveSearchDecoderOnlyOutput''',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowercase_		=							[
		    '''FlaxForcedBOSTokenLogitsProcessor''',
		    '''FlaxForcedEOSTokenLogitsProcessor''',
		    '''FlaxLogitsProcessor''',
		    '''FlaxLogitsProcessorList''',
		    '''FlaxLogitsWarper''',
		    '''FlaxMinLengthLogitsProcessor''',
		    '''FlaxTemperatureLogitsWarper''',
		    '''FlaxTopKLogitsWarper''',
		    '''FlaxTopPLogitsWarper''',
		]
		lowercase_		=							[
		    '''FlaxGenerationMixin''',
		    '''FlaxGreedySearchOutput''',
		    '''FlaxSampleOutput''',
		    '''FlaxBeamSearchOutput''',
		]

if TYPE_CHECKING:
		from .configuration_utils import GenerationConfig
		from .streamers import TextIteratorStreamer, TextStreamer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .beam_constraints import Constraint, ConstraintListState, DisjunctiveConstraint, PhrasalConstraint
				from .beam_search import BeamHypotheses, BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer
				from .logits_process import (
				    EncoderNoRepeatNGramLogitsProcessor,
				    EncoderRepetitionPenaltyLogitsProcessor,
				    EpsilonLogitsWarper,
				    EtaLogitsWarper,
				    ExponentialDecayLengthPenalty,
				    ForcedBOSTokenLogitsProcessor,
				    ForcedEOSTokenLogitsProcessor,
				    HammingDiversityLogitsProcessor,
				    InfNanRemoveLogitsProcessor,
				    LogitNormalization,
				    LogitsProcessor,
				    LogitsProcessorList,
				    LogitsWarper,
				    MinLengthLogitsProcessor,
				    MinNewTokensLengthLogitsProcessor,
				    NoBadWordsLogitsProcessor,
				    NoRepeatNGramLogitsProcessor,
				    PrefixConstrainedLogitsProcessor,
				    RepetitionPenaltyLogitsProcessor,
				    SequenceBiasLogitsProcessor,
				    TemperatureLogitsWarper,
				    TopKLogitsWarper,
				    TopPLogitsWarper,
				    TypicalLogitsWarper,
				)
				from .stopping_criteria import (
				    MaxLengthCriteria,
				    MaxNewTokensCriteria,
				    MaxTimeCriteria,
				    StoppingCriteria,
				    StoppingCriteriaList,
				    validate_stopping_criteria,
				)
				from .utils import (
				    BeamSampleDecoderOnlyOutput,
				    BeamSampleEncoderDecoderOutput,
				    BeamSearchDecoderOnlyOutput,
				    BeamSearchEncoderDecoderOutput,
				    ContrastiveSearchDecoderOnlyOutput,
				    ContrastiveSearchEncoderDecoderOutput,
				    GenerationMixin,
				    GreedySearchDecoderOnlyOutput,
				    GreedySearchEncoderDecoderOutput,
				    SampleDecoderOnlyOutput,
				    SampleEncoderDecoderOutput,
				    top_k_top_p_filtering,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tf_logits_process import (
				    TFForcedBOSTokenLogitsProcessor,
				    TFForcedEOSTokenLogitsProcessor,
				    TFForceTokensLogitsProcessor,
				    TFLogitsProcessor,
				    TFLogitsProcessorList,
				    TFLogitsWarper,
				    TFMinLengthLogitsProcessor,
				    TFNoBadWordsLogitsProcessor,
				    TFNoRepeatNGramLogitsProcessor,
				    TFRepetitionPenaltyLogitsProcessor,
				    TFSuppressTokensAtBeginLogitsProcessor,
				    TFSuppressTokensLogitsProcessor,
				    TFTemperatureLogitsWarper,
				    TFTopKLogitsWarper,
				    TFTopPLogitsWarper,
				)
				from .tf_utils import (
				    TFBeamSampleDecoderOnlyOutput,
				    TFBeamSampleEncoderDecoderOutput,
				    TFBeamSearchDecoderOnlyOutput,
				    TFBeamSearchEncoderDecoderOutput,
				    TFContrastiveSearchDecoderOnlyOutput,
				    TFContrastiveSearchEncoderDecoderOutput,
				    TFGenerationMixin,
				    TFGreedySearchDecoderOnlyOutput,
				    TFGreedySearchEncoderDecoderOutput,
				    TFSampleDecoderOnlyOutput,
				    TFSampleEncoderDecoderOutput,
				    tf_top_k_top_p_filtering,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .flax_logits_process import (
				    FlaxForcedBOSTokenLogitsProcessor,
				    FlaxForcedEOSTokenLogitsProcessor,
				    FlaxLogitsProcessor,
				    FlaxLogitsProcessorList,
				    FlaxLogitsWarper,
				    FlaxMinLengthLogitsProcessor,
				    FlaxTemperatureLogitsWarper,
				    FlaxTopKLogitsWarper,
				    FlaxTopPLogitsWarper,
				)
				from .flax_utils import FlaxBeamSearchOutput, FlaxGenerationMixin, FlaxGreedySearchOutput, FlaxSampleOutput
else:
		import sys

		lowercase_		=							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)









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


lowercase_		=							logging.get_logger(__name__)

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






	def __init__(     self    ,					lowerCamelCase=128112    ,					lowerCamelCase=1024    ,					lowerCamelCase=12    ,					lowerCamelCase=4096    ,					lowerCamelCase=16    ,					lowerCamelCase=12    ,					lowerCamelCase=4096    ,					lowerCamelCase=16    ,					lowerCamelCase=0.0_5    ,					lowerCamelCase=0.0_5    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase="relu"    ,					lowerCamelCase=1024    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.0    ,					lowerCamelCase=0.0_2    ,					lowerCamelCase=2    ,					lowerCamelCase=True    ,					lowerCamelCase=1    ,					lowerCamelCase=0    ,					lowerCamelCase=2    ,					**lowerCamelCase    ,					)				->       List[str]:




						"""simple docstring"""






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






	@property
	def 			lowercase				(     self						)				->       Mapping[str, Mapping[int, str]]:




						"""simple docstring"""






						__magic_name__ :							Dict		       =							OrderedDict(
						    [
						        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
						        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
						    ]						)

						if self.use_past:
											__magic_name__ :							Optional[Any]		       =							{0: '''batch'''}
											__magic_name__ :							Any		       =							{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
						else:
											__magic_name__ :							Union[str, Any]		       =							{0: '''batch''', 1: '''decoder_sequence'''}
											__magic_name__ :							Optional[Any]		       =							{0: '''batch''', 1: '''decoder_sequence'''}

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






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase = -1    ,					lowerCamelCase = -1    ,					lowerCamelCase = False    ,					lowerCamelCase = None    ,					)				->       Mapping[str, Any]:




						"""simple docstring"""






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






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase = -1    ,					lowerCamelCase = -1    ,					lowerCamelCase = False    ,					lowerCamelCase = None    ,					)				->       Mapping[str, Any]:




						"""simple docstring"""






						__magic_name__ :							Any		       =							self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)

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

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

											__magic_name__ :							List[str]		       =							[]
											# If the number of encoder and decoder layers are present in the model configuration, both are considered
											__magic_name__       ,			__magic_name__ :							int		       =							self.num_layers
											__magic_name__ :							List[Any]		       =							min(lowerCamelCase    ,					lowerCamelCase						)
											__magic_name__ :							int		       =							max(lowerCamelCase    ,					lowerCamelCase						) - min_num_layers
											__magic_name__ :							Optional[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.
											__magic_name__ :							Tuple		       =							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





	lowerCamelCase__		:			List[str]       =_generate_dummy_inputs_for_default_and_seqaseq_lm



