



import warnings
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


UpperCamelCase__ =						logging.get_logger(__name__)

UpperCamelCase__ =						{
    "facebook/bart-large": "https://huggingface.co/facebook/bart-large/resolve/main/config.json",
    # See all BART models at https://huggingface.co/models?filter=bart
}
class 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			lowercase__      :  int      =			"""bart"""
			lowercase__      :  str      =			["""past_key_values"""]
			lowercase__      :  Union[str, Any]      =			{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}





			def __init__(				self	:       Optional[Any]			,     lowercase	:       List[str]=50_265			,     lowercase	:       Any=1_024			,     lowercase	:       Dict=12			,     lowercase	:       Union[str, Any]=4_096			,     lowercase	:       str=16			,     lowercase	:       Optional[Any]=12			,     lowercase	:       str=4_096			,     lowercase	:       Optional[Any]=16			,     lowercase	:       int=0.0			,     lowercase	:       int=0.0			,     lowercase	:       Tuple="gelu"			,     lowercase	:       Dict=1_024			,     lowercase	:       Union[str, Any]=0.1			,     lowercase	:       List[Any]=0.0			,     lowercase	:       Dict=0.0			,     lowercase	:       str=0.02			,     lowercase	:       Any=0.0			,     lowercase	:       Dict=False			,     lowercase	:       str=True			,     lowercase	:       Union[str, Any]=3			,     lowercase	:       Optional[int]=1			,     lowercase	:       Dict=0			,     lowercase	:       Dict=2			,     lowercase	:       Tuple=True			,     lowercase	:       str=2			,     lowercase	:       Tuple=2			,     **lowercase	:       Any			,     )						->					int:
									"""simple docstring"""







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

									super().__init__(
									    num_labels=lowercase			,     pad_token_id=lowercase			,     bos_token_id=lowercase			,     eos_token_id=lowercase			,     is_encoder_decoder=lowercase			,     decoder_start_token_id=lowercase			,     forced_eos_token_id=lowercase			,     **lowercase			,     )

									# ensure backward compatibility for BART CNN models
									if self.forced_bos_token_id is None and kwargs.get("""force_bos_token_to_be_generated"""			,     lowercase   ):
															__lowercase					=				self.bos_token_id
															warnings.warn(
															    F"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. "
															    """The config can simply be saved and uploaded again to be fixed."""   )





class 			_lowerCAmelCase   ( _UpperCAmelCase			):






			"""simple docstring"""





			@property
			def 		snake_case__			(				self	:       Optional[int]   )						->					Mapping[str, Mapping[int, str]]:
									"""simple docstring"""







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

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

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







									if self.task in ["default", "seq2seq-lm"]:
															__lowercase					=				super().outputs
									else:
															__lowercase					=				super(lowercase			,     self   ).outputs
															if self.use_past:
																					__lowercase	,			__lowercase					=				self.num_layers
																					for i in range(lowercase   ):
																											__lowercase					=				{0: """batch""", 2: """past_sequence + sequence"""}
																											__lowercase					=				{0: """batch""", 2: """past_sequence + sequence"""}
									return common_outputs





			def 		snake_case__			(				self	:       Tuple			,     lowercase	:       PreTrainedTokenizer			,     lowercase	:       int = -1			,     lowercase	:       int = -1			,     lowercase	:       bool = False			,     lowercase	:       Optional[TensorType] = None			,     )						->					Mapping[str, Any]:
									"""simple docstring"""







									__lowercase					=				self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
									    lowercase			,     lowercase			,     lowercase			,     lowercase			,     lowercase   )

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

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

															__lowercase					=				torch.cat(
															    [common_inputs["""decoder_attention_mask"""], torch.ones(lowercase			,     lowercase   )]			,     dim=1   )

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

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





			def 		snake_case__			(				self	:       int			,     lowercase	:       PreTrainedTokenizer			,     lowercase	:       int = -1			,     lowercase	:       int = -1			,     lowercase	:       bool = False			,     lowercase	:       Optional[TensorType] = None			,     )						->					Mapping[str, Any]:
									"""simple docstring"""







									__lowercase					=				self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
									    lowercase			,     lowercase			,     lowercase			,     lowercase			,     lowercase   )

									if self.use_past:
															if not is_torch_available():
																					raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""   )
															else:
																					import torch
															__lowercase	,			__lowercase					=				common_inputs["""input_ids"""].shape
															# Not using the same length for past_key_values
															__lowercase					=				seqlen + 2
															__lowercase	,			__lowercase					=				self.num_layers
															__lowercase	,			__lowercase					=				self.num_attention_heads
															__lowercase					=				(
															    batch,
															    num_encoder_attention_heads,
															    past_key_values_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)

															__lowercase					=				common_inputs["""attention_mask"""].dtype
															__lowercase					=				torch.cat(
															    [common_inputs["""attention_mask"""], torch.ones(lowercase			,     lowercase			,     dtype=lowercase   )]			,     dim=1   )
															__lowercase					=				[
															    (torch.zeros(lowercase   ), torch.zeros(lowercase   )) for _ in range(lowercase   )
															]
									return common_inputs





			def 		snake_case__			(				self	:       Any			,     lowercase	:       PreTrainedTokenizer			,     lowercase	:       int = -1			,     lowercase	:       int = -1			,     lowercase	:       bool = False			,     lowercase	:       Optional[TensorType] = None			,     )						->					Mapping[str, Any]:
									"""simple docstring"""







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

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





			def 		snake_case__			(				self	:       Any			,     lowercase	:       PreTrainedTokenizer			,     lowercase	:       int = -1			,     lowercase	:       int = -1			,     lowercase	:       bool = False			,     lowercase	:       Optional[TensorType] = None			,     )						->					Mapping[str, Any]:
									"""simple docstring"""







									if self.task in ["default", "seq2seq-lm"]:
															__lowercase					=				self._generate_dummy_inputs_for_default_and_seqaseq_lm(
															    lowercase			,     batch_size=lowercase			,     seq_length=lowercase			,     is_pair=lowercase			,     framework=lowercase   )

									elif self.task == "causal-lm":
															__lowercase					=				self._generate_dummy_inputs_for_causal_lm(
															    lowercase			,     batch_size=lowercase			,     seq_length=lowercase			,     is_pair=lowercase			,     framework=lowercase   )
									else:
															__lowercase					=				self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
															    lowercase			,     batch_size=lowercase			,     seq_length=lowercase			,     is_pair=lowercase			,     framework=lowercase   )

									return common_inputs







			def 		snake_case__			(				self	:       Union[str, Any]			,     lowercase	:       Any			,     lowercase	:       Optional[Any]			,     lowercase	:       Optional[int]			,     lowercase	:       int   )						->					Union[str, Any]:
									"""simple docstring"""







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










from typing import TYPE_CHECKING

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


UpperCamelCase__ =						{
    "configuration_ctrl": ["CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP", "CTRLConfig"],
    "tokenization_ctrl": ["CTRLTokenizer"],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCamelCase__ =						[
			    "CTRL_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "CTRLForSequenceClassification",
			    "CTRLLMHeadModel",
			    "CTRLModel",
			    "CTRLPreTrainedModel",
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCamelCase__ =						[
			    "TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST",
			    "TFCTRLForSequenceClassification",
			    "TFCTRLLMHeadModel",
			    "TFCTRLModel",
			    "TFCTRLPreTrainedModel",
			]


if TYPE_CHECKING:
			from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig
			from .tokenization_ctrl import CTRLTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_ctrl import (
						    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    CTRLForSequenceClassification,
						    CTRLLMHeadModel,
						    CTRLModel,
						    CTRLPreTrainedModel,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_ctrl import (
						    TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFCTRLForSequenceClassification,
						    TFCTRLLMHeadModel,
						    TFCTRLModel,
						    TFCTRLPreTrainedModel,
						)

else:
			import sys

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





