



'''simple docstring'''




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

a  :     Any       =       {
    """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_					(				__magic_name__     ):
	lowercase		=					'mbart'
	lowercase		=					['past_key_values']
	lowercase		=					{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}





	def __init__(    self						,						A=50265						,						A=1024						,						A=12						,						A=4096						,						A=16						,						A=12						,						A=4096						,						A=16						,						A=0.0						,						A=0.0						,						A=True						,						A=True						,						A="gelu"						,						A=1024						,						A=0.1						,						A=0.0						,						A=0.0						,						A=0.0_2						,						A=0.0						,						A=False						,						A=1						,						A=0						,						A=2						,						A=2						,						**A						,						)     ->     Dict:
								UpperCAmelCase	:						Dict					=						vocab_size
								UpperCAmelCase	:						List[Any]					=						max_position_embeddings
								UpperCAmelCase	:						int					=						d_model
								UpperCAmelCase	:						str					=						encoder_ffn_dim
								UpperCAmelCase	:						List[str]					=						encoder_layers
								UpperCAmelCase	:						Dict					=						encoder_attention_heads
								UpperCAmelCase	:						str					=						decoder_ffn_dim
								UpperCAmelCase	:						int					=						decoder_layers
								UpperCAmelCase	:						Optional[int]					=						decoder_attention_heads
								UpperCAmelCase	:						Optional[Any]					=						dropout
								UpperCAmelCase	:						Union[str, Any]					=						attention_dropout
								UpperCAmelCase	:						Union[str, Any]					=						activation_dropout
								UpperCAmelCase	:						str					=						activation_function
								UpperCAmelCase	:						Optional[int]					=						init_std
								UpperCAmelCase	:						int					=						encoder_layerdrop
								UpperCAmelCase	:						Optional[Any]					=						decoder_layerdrop
								UpperCAmelCase	:						Optional[int]					=						classifier_dropout
								UpperCAmelCase	:						Dict					=						use_cache
								UpperCAmelCase	:						List[str]					=						encoder_layers
								UpperCAmelCase	:						str					=						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_					(				__magic_name__     ):





	@property
	def 					_lowercase(    self       )     ->     Mapping[str, Mapping[int, str]]:
								if self.task in ["default", "seq2seq-lm"]:
															UpperCAmelCase	:						Any					=						OrderedDict(
															    [
															        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
															        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
															    ]       )

															if self.use_past:
																						UpperCAmelCase	:						Any					=						{0: """batch"""}
																						UpperCAmelCase	:						Union[str, Any]					=						{0: """batch""", 1: """past_decoder_sequence + sequence"""}
															else:
																						UpperCAmelCase	:						Dict					=						{0: """batch""", 1: """decoder_sequence"""}
																						UpperCAmelCase	:						Optional[Any]					=						{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	:						Dict					=						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	:						Optional[int]					=						{0: """batch""", 2: """past_sequence + sequence"""}
																													UpperCAmelCase	:						Dict					=						{0: """batch""", 2: """past_sequence + sequence"""}
								else:
															UpperCAmelCase	:						Tuple					=						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 					_lowercase(    self       )     ->     Mapping[str, Mapping[int, str]]:
								if self.task in ["default", "seq2seq-lm"]:
															UpperCAmelCase	:						List[Any]					=						super().outputs
								else:
															UpperCAmelCase	:						Optional[Any]					=						super(A						,						self       ).outputs
															if self.use_past:
																						UpperCAmelCase	,	UpperCAmelCase	:						Optional[int]					=						self.num_layers
																						for i in range(A       ):
																													UpperCAmelCase	:						int					=						{0: """batch""", 2: """past_sequence + sequence"""}
																													UpperCAmelCase	:						List[Any]					=						{0: """batch""", 2: """past_sequence + sequence"""}
								return common_outputs





	def 					_lowercase(    self						,						A						,						A = -1						,						A = -1						,						A = False						,						A = None						,						)     ->     Mapping[str, Any]:
								UpperCAmelCase	:						Union[str, Any]					=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    A						,						A						,						A						,						A						,						A       )

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

															UpperCAmelCase	:						int					=						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	:						Optional[int]					=						self.num_layers
															UpperCAmelCase	:						Tuple					=						min(A						,						A       )
															UpperCAmelCase	:						int					=						max(A						,						A       ) - min_num_layers
															UpperCAmelCase	:						Dict					=						"""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	:						Dict					=						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 					_lowercase(    self						,						A						,						A = -1						,						A = -1						,						A = False						,						A = None						,						)     ->     Mapping[str, Any]:
								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	:						str					=						common_inputs["""input_ids"""].shape
															# Not using the same length for past_key_values
															UpperCAmelCase	:						int					=						seqlen + 2
															UpperCAmelCase	,	UpperCAmelCase	:						int					=						self.num_layers
															UpperCAmelCase	,	UpperCAmelCase	:						Union[str, Any]					=						self.num_attention_heads
															UpperCAmelCase	:						List[str]					=						(
															    batch,
															    num_encoder_attention_heads,
															    past_key_values_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)

															UpperCAmelCase	:						List[str]					=						common_inputs["""attention_mask"""].dtype
															UpperCAmelCase	:						List[str]					=						torch.cat(
															    [common_inputs["""attention_mask"""], torch.ones(A						,						A						,						dtype=A       )]						,						dim=1       )
															UpperCAmelCase	:						List[Any]					=						[
															    (torch.zeros(A       ), torch.zeros(A       )) for _ in range(A       )
															]
								return common_inputs





	def 					_lowercase(    self						,						A						,						A = -1						,						A = -1						,						A = False						,						A = 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
								UpperCAmelCase	:						Union[str, Any]					=						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	:						List[str]					=						tokenizer.num_special_tokens_to_add(A       )
								UpperCAmelCase	:						Dict					=						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	:						Dict					=						[""" """.join([tokenizer.unk_token]       ) * seq_length] * batch_size
								UpperCAmelCase	:						List[str]					=						dict(tokenizer(A						,						return_tensors=A       )       )
								return common_inputs





	def 					_lowercase(    self						,						A						,						A = -1						,						A = -1						,						A = False						,						A = None						,						)     ->     Mapping[str, Any]:
								if self.task in ["default", "seq2seq-lm"]:
															UpperCAmelCase	:						Optional[Any]					=						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	:						List[Any]					=						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 					_lowercase(    self						,						A						,						A						,						A						,						A       )     ->     List[str]:
								if self.task in ["default", "seq2seq-lm"]:
															UpperCAmelCase	:						Any					=						super()._flatten_past_key_values_(A						,						A						,						A						,						A       )
								else:
															UpperCAmelCase	:						List[str]					=						super(A						,						self       )._flatten_past_key_values_(
															    A						,						A						,						A						,						A       )






'''simple docstring'''




# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

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


a  :     Optional[Any]       =       {
    """agents""": ["""Agent""", """AzureOpenAiAgent""", """HfAgent""", """LocalAgent""", """OpenAiAgent"""],
    """base""": ["""PipelineTool""", """RemoteTool""", """Tool""", """launch_gradio_demo""", """load_tool"""],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a  :     Dict       =       ["""DocumentQuestionAnsweringTool"""]
							a  :     List[str]       =       ["""ImageCaptioningTool"""]
							a  :     Tuple       =       ["""ImageQuestionAnsweringTool"""]
							a  :     Tuple       =       ["""ImageSegmentationTool"""]
							a  :     List[str]       =       ["""SpeechToTextTool"""]
							a  :     Dict       =       ["""TextClassificationTool"""]
							a  :     Union[str, Any]       =       ["""TextQuestionAnsweringTool"""]
							a  :     List[str]       =       ["""TextSummarizationTool"""]
							a  :     Optional[Any]       =       ["""TextToSpeechTool"""]
							a  :     str       =       ["""TranslationTool"""]

if TYPE_CHECKING:
							from .agents import Agent, AzureOpenAiAgent, HfAgent, LocalAgent, OpenAiAgent
							from .base import PipelineTool, RemoteTool, Tool, launch_gradio_demo, load_tool

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .document_question_answering import DocumentQuestionAnsweringTool
														from .image_captioning import ImageCaptioningTool
														from .image_question_answering import ImageQuestionAnsweringTool
														from .image_segmentation import ImageSegmentationTool
														from .speech_to_text import SpeechToTextTool
														from .text_classification import TextClassificationTool
														from .text_question_answering import TextQuestionAnsweringTool
														from .text_summarization import TextSummarizationTool
														from .text_to_speech import TextToSpeechTool
														from .translation import TranslationTool
else:
							import sys

							a  :     Dict       =       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

