

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


lowerCAmelCase__								=		logging.get_logger(__name__)

lowerCAmelCase__								=		{
    """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 	lowercase    (				_lowercase							):


		"""simple docstring"""




		a__					       =  "bart"
		a__					       =  ["past_key_values"]
		a__					       =  {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


		def __init__(				self	,       __snake_case=5_02_65	,       __snake_case=10_24	,       __snake_case=12	,       __snake_case=40_96	,       __snake_case=16	,       __snake_case=12	,       __snake_case=40_96	,       __snake_case=16	,       __snake_case=0.0	,       __snake_case=0.0	,       __snake_case="gelu"	,       __snake_case=10_24	,       __snake_case=0.1	,       __snake_case=0.0	,       __snake_case=0.0	,       __snake_case=0.0_2	,       __snake_case=0.0	,       __snake_case=False	,       __snake_case=True	,       __snake_case=3	,       __snake_case=1	,       __snake_case=0	,       __snake_case=2	,       __snake_case=True	,       __snake_case=2	,       __snake_case=2	,       **__snake_case	,       ):
			_UpperCamelCase   :		Any			=						vocab_size
			_UpperCamelCase   :		Optional[Any]			=						max_position_embeddings
			_UpperCamelCase   :		Tuple			=						d_model
			_UpperCamelCase   :		Optional[int]			=						encoder_ffn_dim
			_UpperCamelCase   :		Union[str, Any]			=						encoder_layers
			_UpperCamelCase   :		Tuple			=						encoder_attention_heads
			_UpperCamelCase   :		List[str]			=						decoder_ffn_dim
			_UpperCamelCase   :		Union[str, Any]			=						decoder_layers
			_UpperCamelCase   :		List[Any]			=						decoder_attention_heads
			_UpperCamelCase   :		Any			=						dropout
			_UpperCamelCase   :		int			=						attention_dropout
			_UpperCamelCase   :		List[Any]			=						activation_dropout
			_UpperCamelCase   :		List[str]			=						activation_function
			_UpperCamelCase   :		Tuple			=						init_std
			_UpperCamelCase   :		str			=						encoder_layerdrop
			_UpperCamelCase   :		Optional[int]			=						decoder_layerdrop
			_UpperCamelCase   :		Optional[int]			=						classifier_dropout
			_UpperCamelCase   :		Union[str, Any]			=						use_cache
			_UpperCamelCase   :		Dict			=						encoder_layers
			_UpperCamelCase   :		Union[str, Any]			=						scale_embedding  # scale factor will be sqrt(d_model) if True

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

			# ensure backward compatibility for BART CNN models
			if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated'	,       __snake_case):
				_UpperCamelCase   :		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 	lowercase    (				_lowercase							):


		"""simple docstring"""




		@property
		def 		A__					(				self):
			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   :		str			=						{0: 'batch'}
					_UpperCamelCase   :		Tuple			=						{0: 'batch', 1: 'past_decoder_sequence + sequence'}
				else:
					_UpperCamelCase   :		Tuple			=						{0: 'batch', 1: 'decoder_sequence'}
					_UpperCamelCase   :		List[Any]			=						{0: 'batch', 1: 'decoder_sequence'}

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


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			_UpperCamelCase   :		Union[str, Any]			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)

			# Generate decoder inputs
			_UpperCamelCase   :		Union[str, Any]			=						seq_length if not self.use_past else 1
			_UpperCamelCase   :		Any			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)
			_UpperCamelCase   :		List[str]			=						{f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
			_UpperCamelCase   :		Union[str, Any]			=						dict(**__snake_case	,       **__snake_case)

			if self.use_past:
				if not is_torch_available():
					raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.')
				else:
					import torch
				_UpperCamelCase   :		Tuple			=						common_inputs['input_ids'].shape
				_UpperCamelCase   :		Optional[int]			=						common_inputs['decoder_input_ids'].shape[1]
				_UpperCamelCase   :		List[Any]			=						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   :		List[Any]			=						decoder_seq_length + 3
				_UpperCamelCase   :		Optional[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(__snake_case	,       __snake_case)]	,       dim=1)

				_UpperCamelCase   :		int			=						[]
				# If the number of encoder and decoder layers are present in the model configuration, both are considered
				_UpperCamelCase   :		Any			=						self.num_layers
				_UpperCamelCase   :		int			=						min(__snake_case	,       __snake_case)
				_UpperCamelCase   :		Optional[int]			=						max(__snake_case	,       __snake_case) - min_num_layers
				_UpperCamelCase   :		int			=						'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

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


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			_UpperCamelCase   :		Any			=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
			    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)

			if self.use_past:
				if not is_torch_available():
					raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.')
				else:
					import torch
				_UpperCamelCase   :		Dict			=						common_inputs['input_ids'].shape
				# Not using the same length for past_key_values
				_UpperCamelCase   :		Tuple			=						seqlen + 2
				_UpperCamelCase   :		Tuple			=						self.num_layers
				_UpperCamelCase   :		str			=						self.num_attention_heads
				_UpperCamelCase   :		Tuple			=						(
				    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   :		Optional[Any]			=						torch.cat(
				    [common_inputs['attention_mask'], torch.ones(__snake_case	,       __snake_case	,       dtype=__snake_case)]	,       dim=1)
				_UpperCamelCase   :		List[Any]			=						[
				    (torch.zeros(__snake_case), torch.zeros(__snake_case)) for _ in range(__snake_case)
				]
			return common_inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = 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
			_UpperCamelCase   :		int			=						compute_effective_axis_dimension(
			    __snake_case	,       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   :		Optional[int]			=						tokenizer.num_special_tokens_to_add(__snake_case)
			_UpperCamelCase   :		Tuple			=						compute_effective_axis_dimension(
			    __snake_case	,       fixed_dimension=OnnxConfig.default_fixed_sequence	,       num_token_to_add=__snake_case)

			# Generate dummy inputs according to compute batch and sequence
			_UpperCamelCase   :		Union[str, Any]			=						[' '.join([tokenizer.unk_token]) * seq_length] * batch_size
			_UpperCamelCase   :		Any			=						dict(tokenizer(__snake_case	,       return_tensors=__snake_case))
			return common_inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case = -1	,       __snake_case = -1	,       __snake_case = False	,       __snake_case = None	,       ):
			if self.task in ["default", "seq2seq-lm"]:
				_UpperCamelCase   :		Optional[int]			=						self._generate_dummy_inputs_for_default_and_seqaseq_lm(
				    __snake_case	,       batch_size=__snake_case	,       seq_length=__snake_case	,       is_pair=__snake_case	,       framework=__snake_case)

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

			return common_inputs






		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case):
			if self.task in ["default", "seq2seq-lm"]:
				_UpperCamelCase   :		Dict			=						super()._flatten_past_key_values_(__snake_case	,       __snake_case	,       __snake_case	,       __snake_case)
			else:
				_UpperCamelCase   :		int			=						super(__snake_case	,       self)._flatten_past_key_values_(
				    __snake_case	,       __snake_case	,       __snake_case	,       __snake_case)







from .glue import glue_convert_examples_to_features, glue_output_modes, glue_processors, glue_tasks_num_labels
from .squad import SquadExample, SquadFeatures, SquadVaProcessor, SquadVaProcessor, squad_convert_examples_to_features
from .utils import DataProcessor, InputExample, InputFeatures, SingleSentenceClassificationProcessor
from .xnli import xnli_output_modes, xnli_processors, xnli_tasks_num_labels




