



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       ={
    "facebook/mbart-large-cc25": "https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json",
    # See all MBART models at https://huggingface.co/models?filter=mbart
}

class 						UpperCAmelCase__     (     __UpperCamelCase  ):




			'''simple docstring'''






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




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





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

class 						UpperCAmelCase__     (     __UpperCamelCase  ):




			'''simple docstring'''






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





					if self.task in ["default", "seq2seq-lm"]:
							__UpperCAmelCase       :	int						=      OrderedDict(
							    [
							        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
							        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
							    ]					)

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

							if self.use_past:
									self.fill_with_past_key_values_(a_			,			direction='''inputs'''					)
					elif self.task == "causal-lm":
							# TODO: figure this case out.
							__UpperCAmelCase       :	Tuple						=      OrderedDict(
							    [
							        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
							        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
							    ]					)
							if self.use_past:
									__UpperCAmelCase				,   __UpperCAmelCase       :	Dict						=      self.num_layers
									for i in range(a_					):
											__UpperCAmelCase       :	Any						=      {0: '''batch''', 2: '''past_sequence + sequence'''}
											__UpperCAmelCase       :	Tuple						=      {0: '''batch''', 2: '''past_sequence + sequence'''}
					else:
							__UpperCAmelCase       :	Union[str, Any]						=      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
			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					if self.task in ["default", "seq2seq-lm"]:
							__UpperCAmelCase       :	Union[str, Any]						=      super().outputs
					else:
							__UpperCAmelCase       :	int						=      super(a_			,			self					).outputs
							if self.use_past:
									__UpperCAmelCase				,   __UpperCAmelCase       :	Dict						=      self.num_layers
									for i in range(a_					):
											__UpperCAmelCase       :	Dict						=      {0: '''batch''', 2: '''past_sequence + sequence'''}
											__UpperCAmelCase       :	Optional[int]						=      {0: '''batch''', 2: '''past_sequence + sequence'''}
					return common_outputs




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





					__UpperCAmelCase       :	str						=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    a_			,			a_			,			a_			,			a_			,			a_					)

					# Generate decoder inputs
					__UpperCAmelCase       :	int						=      seq_length if not self.use_past else 1
					__UpperCAmelCase       :	List[Any]						=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    a_			,			a_			,			a_			,			a_			,			a_					)
					__UpperCAmelCase       :	Any						=      {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       :	Union[str, Any]						=      common_inputs['''input_ids'''].shape
							__UpperCAmelCase       :	Optional[int]						=      common_inputs['''decoder_input_ids'''].shape[1]
							__UpperCAmelCase				,   __UpperCAmelCase       :	int						=      self.num_attention_heads
							__UpperCAmelCase       :	int						=      (
							    batch,
							    num_encoder_attention_heads,
							    encoder_seq_length,
							    self._config.hidden_size // num_encoder_attention_heads,
							)
							__UpperCAmelCase       :	int						=      decoder_seq_length + 3
							__UpperCAmelCase       :	int						=      (
							    batch,
							    num_decoder_attention_heads,
							    decoder_past_length,
							    self._config.hidden_size // num_decoder_attention_heads,
							)

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

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




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





					__UpperCAmelCase       :	Optional[int]						=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    a_			,			a_			,			a_			,			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[int]						=      common_inputs['''input_ids'''].shape
							# Not using the same length for past_key_values
							__UpperCAmelCase       :	Any						=      seqlen + 2
							__UpperCAmelCase				,   __UpperCAmelCase       :	List[Any]						=      self.num_layers
							__UpperCAmelCase				,   __UpperCAmelCase       :	List[str]						=      self.num_attention_heads
							__UpperCAmelCase       :	Dict						=      (
							    batch,
							    num_encoder_attention_heads,
							    past_key_values_length,
							    self._config.hidden_size // num_encoder_attention_heads,
							)

							__UpperCAmelCase       :	Optional[int]						=      common_inputs['''attention_mask'''].dtype
							__UpperCAmelCase       :	int						=      torch.cat(
							    [common_inputs['''attention_mask'''], torch.ones(a_			,			a_			,			dtype=a_					)]			,			dim=1					)
							__UpperCAmelCase       :	Any						=      [
							    (torch.zeros(a_					), torch.zeros(a_					)) for _ in range(a_					)
							]
					return common_inputs




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





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




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





					if self.task in ["default", "seq2seq-lm"]:
							__UpperCAmelCase       :	List[str]						=      self._generate_dummy_inputs_for_default_and_seqaseq_lm(
							    a_			,			batch_size=a_			,			seq_length=a_			,			is_pair=a_			,			framework=a_					)

					elif self.task == "causal-lm":
							__UpperCAmelCase       :	str						=      self._generate_dummy_inputs_for_causal_lm(
							    a_			,			batch_size=a_			,			seq_length=a_			,			is_pair=a_			,			framework=a_					)
					else:
							__UpperCAmelCase       :	Optional[int]						=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    a_			,			batch_size=a_			,			seq_length=a_			,			is_pair=a_			,			framework=a_					)

					return common_inputs




			def 					snake_case__	(  self				:					Union[str, Any]			,			a_				:					Dict			,			a_				:					Optional[int]			,			a_				:					Tuple			,			a_				:					Optional[Any]					):
					'''simple docstring'''





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










from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


__A       ={
    "configuration_speech_to_text_2": ["SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2Text2Config"],
    "processing_speech_to_text_2": ["Speech2Text2Processor"],
    "tokenization_speech_to_text_2": ["Speech2Text2Tokenizer"],
}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A       =[
				    "SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "Speech2Text2ForCausalLM",
				    "Speech2Text2PreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
				from .processing_speech_to_text_a import SpeechaTextaProcessor
				from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_speech_to_text_a import (
								    SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SpeechaTextaForCausalLM,
								    SpeechaTextaPreTrainedModel,
								)

else:
				import sys

				__A       =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





