






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


__snake_case      :      Optional[Any]             =       logging.get_logger(__name__)

__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       __SCREAMING_SNAKE_CASE   (				__lowercase):
						_SCREAMING_SNAKE_CASE		:    int						=   '''bart'''
						_SCREAMING_SNAKE_CASE		:    Dict						=   ['''past_key_values''']
						_SCREAMING_SNAKE_CASE		:    List[Any]						=   {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






						def __init__(		self       ,  _UpperCamelCase=5_02_65       ,  _UpperCamelCase=10_24       ,  _UpperCamelCase=12       ,  _UpperCamelCase=40_96       ,  _UpperCamelCase=16       ,  _UpperCamelCase=12       ,  _UpperCamelCase=40_96       ,  _UpperCamelCase=16       ,  _UpperCamelCase=0.0       ,  _UpperCamelCase=0.0       ,  _UpperCamelCase="gelu"       ,  _UpperCamelCase=10_24       ,  _UpperCamelCase=0.1       ,  _UpperCamelCase=0.0       ,  _UpperCamelCase=0.0       ,  _UpperCamelCase=0.02       ,  _UpperCamelCase=0.0       ,  _UpperCamelCase=False       ,  _UpperCamelCase=True       ,  _UpperCamelCase=3       ,  _UpperCamelCase=1       ,  _UpperCamelCase=0       ,  _UpperCamelCase=2       ,  _UpperCamelCase=True       ,  _UpperCamelCase=2       ,  _UpperCamelCase=2       ,  **_UpperCamelCase       ,  ):






												"""simple docstring"""
												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=_UpperCamelCase       ,  pad_token_id=_UpperCamelCase       ,  bos_token_id=_UpperCamelCase       ,  eos_token_id=_UpperCamelCase       ,  is_encoder_decoder=_UpperCamelCase       ,  decoder_start_token_id=_UpperCamelCase       ,  forced_eos_token_id=_UpperCamelCase       ,  **_UpperCamelCase       ,  )

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






						@property
						def  UpperCamelCase__ (		self				):






												"""simple docstring"""
												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_(_UpperCamelCase       ,  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(_UpperCamelCase				):
																														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				):






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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = False       ,  _UpperCamelCase = None       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
												    _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				)

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

												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(_UpperCamelCase       ,  _UpperCamelCase				)]       ,  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(_UpperCamelCase       ,  _UpperCamelCase				)
																		lowerCAmelCase__        =  max(_UpperCamelCase       ,  _UpperCamelCase				) - min_num_layers
																		lowerCAmelCase__        =  'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = False       ,  _UpperCamelCase = None       ,  ):






												"""simple docstring"""
												lowerCAmelCase__        =  self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
												    _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				)

												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(_UpperCamelCase       ,  _UpperCamelCase       ,  dtype=_UpperCamelCase				)]       ,  dim=1				)
																		lowerCAmelCase__        =  [
																		    (torch.zeros(_UpperCamelCase				), torch.zeros(_UpperCamelCase				)) for _ in range(_UpperCamelCase				)
																		]
												return common_inputs






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = False       ,  _UpperCamelCase = 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
												lowerCAmelCase__        =  compute_effective_axis_dimension(
												    _UpperCamelCase       ,  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(_UpperCamelCase				)
												lowerCAmelCase__        =  compute_effective_axis_dimension(
												    _UpperCamelCase       ,  fixed_dimension=OnnxConfig.default_fixed_sequence       ,  num_token_to_add=_UpperCamelCase				)

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






						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = -1       ,  _UpperCamelCase = False       ,  _UpperCamelCase = None       ,  ):






												"""simple docstring"""
												if self.task in ["default", "seq2seq-lm"]:
																		lowerCAmelCase__        =  self._generate_dummy_inputs_for_default_and_seqaseq_lm(
																		    _UpperCamelCase       ,  batch_size=_UpperCamelCase       ,  seq_length=_UpperCamelCase       ,  is_pair=_UpperCamelCase       ,  framework=_UpperCamelCase				)

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

												return common_inputs







						def  UpperCamelCase__ (		self       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				):






												"""simple docstring"""
												if self.task in ["default", "seq2seq-lm"]:
																		lowerCAmelCase__        =  super()._flatten_past_key_values_(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				)
												else:
																		lowerCAmelCase__        =  super(_UpperCamelCase       ,  self				)._flatten_past_key_values_(
																		    _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase				)













def 		_UpperCamelCase				(      UpperCamelCase_    :   str						)					->				str:

						"""simple docstring"""



						return " ".join(input_str.split()[::-1]						)


if __name__ == "__main__":
					import doctest

					doctest.testmod()





