






"""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


SCREAMING_SNAKE_CASE						:						List[Any]					   =       logging.get_logger(__name__)

SCREAMING_SNAKE_CASE						:						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       snake_case_       (    _lowerCamelCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							List[str]   			=					"""bart"""
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					["""past_key_values"""]
	SCREAMING_SNAKE_CASE_:							List[Any]   			=					{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


	def __init__(      self  ,					__a=5_0265  ,					__a=1024  ,					__a=12  ,					__a=4096  ,					__a=16  ,					__a=12  ,					__a=4096  ,					__a=16  ,					__a=0.0  ,					__a=0.0  ,					__a="gelu"  ,					__a=1024  ,					__a=0.1  ,					__a=0.0  ,					__a=0.0  ,					__a=0.02  ,					__a=0.0  ,					__a=False  ,					__a=True  ,					__a=3  ,					__a=1  ,					__a=0  ,					__a=2  ,					__a=True  ,					__a=2  ,					__a=2  ,					**__a  ,					):


					"""simple docstring"""

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

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

					# ensure backward compatibility for BART CNN models
					if self.forced_bos_token_id is None and kwargs.get('force_bos_token_to_be_generated'  ,					__a						):
									A__							 =       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       snake_case_       (    _lowerCamelCase       ):



	"""simple docstring"""

	@property
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

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

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

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


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a = -1  ,					__a = -1  ,					__a = False  ,					__a = None  ,					):


					"""simple docstring"""

					A__							 =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    __a  ,					__a  ,					__a  ,					__a  ,					__a						)

					# Generate decoder inputs
					A__							 =       seq_length if not self.use_past else 1
					A__							 =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    __a  ,					__a  ,					__a  ,					__a  ,					__a						)
					A__							 =       {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
					A__							 =       dict(**__a  ,					**__a						)

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

									A__							 =       torch.cat(
									    [common_inputs['decoder_attention_mask'], torch.ones(__a  ,					__a						)]  ,					dim=1						)

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

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


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a = -1  ,					__a = -1  ,					__a = False  ,					__a = None  ,					):


					"""simple docstring"""

					A__							 =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    __a  ,					__a  ,					__a  ,					__a  ,					__a						)

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

									A__							 =       common_inputs['attention_mask'].dtype
									A__							 =       torch.cat(
									    [common_inputs['attention_mask'], torch.ones(__a  ,					__a  ,					dtype=__a						)]  ,					dim=1						)
									A__							 =       [
									    (torch.zeros(__a						), torch.zeros(__a						)) for _ in range(__a						)
									]
					return common_inputs


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a = -1  ,					__a = -1  ,					__a = False  ,					__a = None  ,					):


					"""simple docstring"""

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

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


	def       _UpperCAmelCase						(      self  ,					__a  ,					__a = -1  ,					__a = -1  ,					__a = False  ,					__a = None  ,					):


					"""simple docstring"""

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

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

					return common_inputs






	def       _UpperCAmelCase						(      self  ,					__a  ,					__a  ,					__a  ,					__a						):


					"""simple docstring"""

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










"""simple docstring"""






from ...processing_utils import ProcessorMixin





class       snake_case_       (    _lowerCamelCase       ):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:							Union[str, Any]   			=					["""image_processor""", """feature_extractor"""]
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					"""TvltImageProcessor"""
	SCREAMING_SNAKE_CASE_:							Optional[int]   			=					"""TvltFeatureExtractor"""


	def __init__(      self  ,					__a  ,					__a						):


					"""simple docstring"""

					super().__init__(image_processor=__a  ,					feature_extractor=__a						)

					A__							 =       image_processor
					A__							 =       feature_extractor


	def __call__(      self  ,					__a=None  ,					__a=None  ,					__a=None  ,					__a=None  ,					__a=False  ,					__a=False  ,					*__a  ,					**__a  ,					):


					"""simple docstring"""

					if images is None and audio is None:
									raise ValueError('You need to specify either an `images` or `audio` input to process.'						)

					A__							 =       None
					if images is not None:
									A__							 =       self.image_processor(__a  ,					mask_pixel=__a  ,					*__a  ,					**__a						)
					if images_mixed is not None:
									A__							 =       self.image_processor(__a  ,					is_mixed=__a  ,					*__a  ,					**__a						)
					if audio is not None:
									A__							 =       self.feature_extractor(
									    __a  ,					*__a  ,					sampling_rate=__a  ,					mask_audio=__a  ,					**__a						)

					A__							 =       {}
					if audio is not None:
									output_dict.update(__a						)
					if images is not None:
									output_dict.update(__a						)
					if images_mixed_dict is not None:
									output_dict.update(__a						)
					return output_dict






	@property
	def       _UpperCAmelCase						(      self						):


					"""simple docstring"""

					A__							 =       self.image_processor.model_input_names
					A__							 =       self.feature_extractor.model_input_names
					return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names						)						)


