






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


SCREAMING_SNAKE_CASE__      :			Any							=		logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__      :			Tuple							=		{
    """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_				(      lowerCamelCase ):
				a__					       =  '''bart'''
				a__					       =  ['''past_key_values''']
				a__					       =  {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}







				def __init__(						self       ,					__lowerCAmelCase=5_0_2_6_5       ,					__lowerCAmelCase=1_0_2_4       ,					__lowerCAmelCase=1_2       ,					__lowerCAmelCase=4_0_9_6       ,					__lowerCAmelCase=1_6       ,					__lowerCAmelCase=1_2       ,					__lowerCAmelCase=4_0_9_6       ,					__lowerCAmelCase=1_6       ,					__lowerCAmelCase=0.0       ,					__lowerCAmelCase=0.0       ,					__lowerCAmelCase="gelu"       ,					__lowerCAmelCase=1_0_2_4       ,					__lowerCAmelCase=0.1       ,					__lowerCAmelCase=0.0       ,					__lowerCAmelCase=0.0       ,					__lowerCAmelCase=0.02       ,					__lowerCAmelCase=0.0       ,					__lowerCAmelCase=False       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=3       ,					__lowerCAmelCase=1       ,					__lowerCAmelCase=0       ,					__lowerCAmelCase=2       ,					__lowerCAmelCase=True       ,					__lowerCAmelCase=2       ,					__lowerCAmelCase=2       ,					**__lowerCAmelCase       ,					):
								"""simple docstring"""
								__magic_name__		:Dict        =      vocab_size
								__magic_name__		:Optional[Any]        =      max_position_embeddings
								__magic_name__		:Optional[int]        =      d_model
								__magic_name__		:Any        =      encoder_ffn_dim
								__magic_name__		:Union[str, Any]        =      encoder_layers
								__magic_name__		:Union[str, Any]        =      encoder_attention_heads
								__magic_name__		:List[str]        =      decoder_ffn_dim
								__magic_name__		:List[str]        =      decoder_layers
								__magic_name__		:Optional[Any]        =      decoder_attention_heads
								__magic_name__		:Tuple        =      dropout
								__magic_name__		:Any        =      attention_dropout
								__magic_name__		:int        =      activation_dropout
								__magic_name__		:Union[str, Any]        =      activation_function
								__magic_name__		:Optional[Any]        =      init_std
								__magic_name__		:Union[str, Any]        =      encoder_layerdrop
								__magic_name__		:Optional[int]        =      decoder_layerdrop
								__magic_name__		:List[str]        =      classifier_dropout
								__magic_name__		:Optional[Any]        =      use_cache
								__magic_name__		:Optional[int]        =      encoder_layers
								__magic_name__		:List[str]        =      scale_embedding  # scale factor will be sqrt(d_model) if True

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

								# ensure backward compatibility for BART CNN models
								if self.forced_bos_token_id is None and kwargs.get('''force_bos_token_to_be_generated'''       ,					__lowerCAmelCase     ):
												__magic_name__		:int        =      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_				(      lowerCamelCase ):







				@property
				def 			A	(						self     ):
								"""simple docstring"""
								if self.task in ["default", "seq2seq-lm"]:
												__magic_name__		:Optional[Any]        =      OrderedDict(
												    [
												        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
												        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
												    ]     )

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

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







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = False       ,					__lowerCAmelCase = None       ,					):
								"""simple docstring"""
								__magic_name__		:str        =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    __lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     )

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

												__magic_name__		:List[str]        =      torch.cat(
												    [common_inputs['''decoder_attention_mask'''], torch.ones(__lowerCAmelCase       ,					__lowerCAmelCase     )]       ,					dim=1     )

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







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = False       ,					__lowerCAmelCase = None       ,					):
								"""simple docstring"""
								__magic_name__		:Optional[int]        =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    __lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__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__		:Tuple        =      common_inputs['''input_ids'''].shape
												# Not using the same length for past_key_values
												__magic_name__		:List[Any]        =      seqlen + 2
												__magic_name__     ,				__magic_name__		:Optional[Any]        =      self.num_layers
												__magic_name__     ,				__magic_name__		:Union[str, Any]        =      self.num_attention_heads
												__magic_name__		:Any        =      (
												    batch,
												    num_encoder_attention_heads,
												    past_key_values_length,
												    self._config.hidden_size // num_encoder_attention_heads,
												)

												__magic_name__		:Dict        =      common_inputs['''attention_mask'''].dtype
												__magic_name__		:Any        =      torch.cat(
												    [common_inputs['''attention_mask'''], torch.ones(__lowerCAmelCase       ,					__lowerCAmelCase       ,					dtype=__lowerCAmelCase     )]       ,					dim=1     )
												__magic_name__		:Any        =      [
												    (torch.zeros(__lowerCAmelCase     ), torch.zeros(__lowerCAmelCase     )) for _ in range(__lowerCAmelCase     )
												]
								return common_inputs







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = False       ,					__lowerCAmelCase = 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
								__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__		:Any        =      tokenizer.num_special_tokens_to_add(__lowerCAmelCase     )
								__magic_name__		:Optional[int]        =      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__		:List[Any]        =      [''' '''.join([tokenizer.unk_token]     ) * seq_length] * batch_size
								__magic_name__		:List[Any]        =      dict(tokenizer(__lowerCAmelCase       ,					return_tensors=__lowerCAmelCase     )     )
								return common_inputs







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = -1       ,					__lowerCAmelCase = False       ,					__lowerCAmelCase = None       ,					):
								"""simple docstring"""
								if self.task in ["default", "seq2seq-lm"]:
												__magic_name__		:Tuple        =      self._generate_dummy_inputs_for_default_and_seqaseq_lm(
												    __lowerCAmelCase       ,					batch_size=__lowerCAmelCase       ,					seq_length=__lowerCAmelCase       ,					is_pair=__lowerCAmelCase       ,					framework=__lowerCAmelCase     )

								elif self.task == "causal-lm":
												__magic_name__		:List[Any]        =      self._generate_dummy_inputs_for_causal_lm(
												    __lowerCAmelCase       ,					batch_size=__lowerCAmelCase       ,					seq_length=__lowerCAmelCase       ,					is_pair=__lowerCAmelCase       ,					framework=__lowerCAmelCase     )
								else:
												__magic_name__		:List[str]        =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
												    __lowerCAmelCase       ,					batch_size=__lowerCAmelCase       ,					seq_length=__lowerCAmelCase       ,					is_pair=__lowerCAmelCase       ,					framework=__lowerCAmelCase     )

								return common_inputs







				def 			A	(						self       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     ):
								"""simple docstring"""
								if self.task in ["default", "seq2seq-lm"]:
												__magic_name__		:Optional[Any]        =      super()._flatten_past_key_values_(__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     )
								else:
												__magic_name__		:int        =      super(__lowerCAmelCase       ,					self     )._flatten_past_key_values_(
												    __lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase       ,					__lowerCAmelCase     )














from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available


SCREAMING_SNAKE_CASE__      :			Union[str, Any]							=		{"""tokenization_herbert""": ["""HerbertTokenizer"""]}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE__      :			Optional[Any]							=		["""HerbertTokenizerFast"""]


if TYPE_CHECKING:
					from .tokenization_herbert import HerbertTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_herbert_fast import HerbertTokenizerFast

else:
					import sys

					SCREAMING_SNAKE_CASE__      :			Union[str, Any]							=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






