






'''simple docstring'''







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     :   List[Any]	      =		logging.get_logger(__name__)

UpperCamelCase     :   List[Any]	      =		{
    '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 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		_UpperCamelCase						=       'bart'
		_UpperCamelCase						=       ['past_key_values']
		_UpperCamelCase						=       {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}






		def __init__(   self ,_lowerCAmelCase=5_02_65 ,_lowerCAmelCase=10_24 ,_lowerCAmelCase=12 ,_lowerCAmelCase=40_96 ,_lowerCAmelCase=16 ,_lowerCAmelCase=12 ,_lowerCAmelCase=40_96 ,_lowerCAmelCase=16 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase=0.0 ,_lowerCAmelCase="gelu" ,_lowerCAmelCase=10_24 ,_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 ,):
								lowerCamelCase__								=	vocab_size
								lowerCamelCase__								=	max_position_embeddings
								lowerCamelCase__								=	d_model
								lowerCamelCase__								=	encoder_ffn_dim
								lowerCamelCase__								=	encoder_layers
								lowerCamelCase__								=	encoder_attention_heads
								lowerCamelCase__								=	decoder_ffn_dim
								lowerCamelCase__								=	decoder_layers
								lowerCamelCase__								=	decoder_attention_heads
								lowerCamelCase__								=	dropout
								lowerCamelCase__								=	attention_dropout
								lowerCamelCase__								=	activation_dropout
								lowerCamelCase__								=	activation_function
								lowerCamelCase__								=	init_std
								lowerCamelCase__								=	encoder_layerdrop
								lowerCamelCase__								=	decoder_layerdrop
								lowerCamelCase__								=	classifier_dropout
								lowerCamelCase__								=	use_cache
								lowerCamelCase__								=	encoder_layers
								lowerCamelCase__								=	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							):
														lowerCamelCase__								=	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 		UpperCamelCase__     (a      ):




		'''simple docstring'''





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

														if self.use_past:
																				lowerCamelCase__								=	{0: """batch"""}
																				lowerCamelCase__								=	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
														else:
																				lowerCamelCase__								=	{0: """batch""", 1: """decoder_sequence"""}
																				lowerCamelCase__								=	{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.
														lowerCamelCase__								=	OrderedDict(
														    [
														        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
														        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
														    ]							)
														if self.use_past:
																				lowerCamelCase__							,      lowerCamelCase__								=	self.num_layers
																				for i in range(_lowerCAmelCase							):
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
								else:
														lowerCamelCase__								=	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       UpperCamelCase_       (   self							):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	super().outputs
								else:
														lowerCamelCase__								=	super(_lowerCAmelCase ,self							).outputs
														if self.use_past:
																				lowerCamelCase__							,      lowerCamelCase__								=	self.num_layers
																				for i in range(_lowerCAmelCase							):
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
																										lowerCamelCase__								=	{0: """batch""", 2: """past_sequence + sequence"""}
								return common_outputs






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								lowerCamelCase__								=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

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

														lowerCamelCase__								=	torch.cat(
														    [common_inputs["""decoder_attention_mask"""], torch.ones(_lowerCAmelCase ,_lowerCAmelCase							)] ,dim=1							)

														lowerCamelCase__								=	[]
														# If the number of encoder and decoder layers are present in the model configuration, both are considered
														lowerCamelCase__							,      lowerCamelCase__								=	self.num_layers
														lowerCamelCase__								=	min(_lowerCAmelCase ,_lowerCAmelCase							)
														lowerCamelCase__								=	max(_lowerCAmelCase ,_lowerCAmelCase							) - min_num_layers
														lowerCamelCase__								=	"""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.
														lowerCamelCase__								=	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       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								lowerCamelCase__								=	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
														lowerCamelCase__							,      lowerCamelCase__								=	common_inputs["""input_ids"""].shape
														# Not using the same length for past_key_values
														lowerCamelCase__								=	seqlen + 2
														lowerCamelCase__							,      lowerCamelCase__								=	self.num_layers
														lowerCamelCase__							,      lowerCamelCase__								=	self.num_attention_heads
														lowerCamelCase__								=	(
														    batch,
														    num_encoder_attention_heads,
														    past_key_values_length,
														    self._config.hidden_size // num_encoder_attention_heads,
														)

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






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								# 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
								lowerCamelCase__								=	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
								lowerCamelCase__								=	tokenizer.num_special_tokens_to_add(_lowerCAmelCase							)
								lowerCamelCase__								=	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
								lowerCamelCase__								=	[""" """.join([tokenizer.unk_token]							) * seq_length] * batch_size
								lowerCamelCase__								=	dict(tokenizer(_lowerCAmelCase ,return_tensors=_lowerCAmelCase							)							)
								return common_inputs






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase = -1 ,_lowerCAmelCase = -1 ,_lowerCAmelCase = False ,_lowerCAmelCase = None ,):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	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":
														lowerCamelCase__								=	self._generate_dummy_inputs_for_causal_lm(
														    _lowerCAmelCase ,batch_size=_lowerCAmelCase ,seq_length=_lowerCAmelCase ,is_pair=_lowerCAmelCase ,framework=_lowerCAmelCase							)
								else:
														lowerCamelCase__								=	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       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								if self.task in ["default", "seq2seq-lm"]:
														lowerCamelCase__								=	super()._flatten_past_key_values_(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)
								else:
														lowerCamelCase__								=	super(_lowerCAmelCase ,self							)._flatten_past_key_values_(
														    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)







'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


lowerCAmelCase_      :  Optional[Any]				     =  {}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  Any				     =  ['GPTSw3Tokenizer']


if TYPE_CHECKING:
 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_gpt_swa import GPTSwaTokenizer

else:
 import sys

 lowerCAmelCase_      :  int				     =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
