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


lowercase			:				int    		=			logging.get_logger(__name__)

lowercase			:				List[str]    		=			{
    '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  A				(							__snake_case						):
     __magic_name__						=		'''bart'''
     __magic_name__						=		['''past_key_values''']
     __magic_name__						=		{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}





     def __init__(							self						,			SCREAMING_SNAKE_CASE=50265						,			SCREAMING_SNAKE_CASE=1024						,			SCREAMING_SNAKE_CASE=12						,			SCREAMING_SNAKE_CASE=4096						,			SCREAMING_SNAKE_CASE=16						,			SCREAMING_SNAKE_CASE=12						,			SCREAMING_SNAKE_CASE=4096						,			SCREAMING_SNAKE_CASE=16						,			SCREAMING_SNAKE_CASE=0.0						,			SCREAMING_SNAKE_CASE=0.0						,			SCREAMING_SNAKE_CASE="gelu"						,			SCREAMING_SNAKE_CASE=1024						,			SCREAMING_SNAKE_CASE=0.1						,			SCREAMING_SNAKE_CASE=0.0						,			SCREAMING_SNAKE_CASE=0.0						,			SCREAMING_SNAKE_CASE=0.02						,			SCREAMING_SNAKE_CASE=0.0						,			SCREAMING_SNAKE_CASE=False						,			SCREAMING_SNAKE_CASE=True						,			SCREAMING_SNAKE_CASE=3						,			SCREAMING_SNAKE_CASE=1						,			SCREAMING_SNAKE_CASE=0						,			SCREAMING_SNAKE_CASE=2						,			SCREAMING_SNAKE_CASE=True						,			SCREAMING_SNAKE_CASE=2						,			SCREAMING_SNAKE_CASE=2						,			**SCREAMING_SNAKE_CASE						,			) ->				Optional[int]:






         """simple docstring"""






         A       :   Dict					=						vocab_size
         A       :   Any					=						max_position_embeddings
         A       :   Union[str, Any]					=						d_model
         A       :   str					=						encoder_ffn_dim
         A       :   Dict					=						encoder_layers
         A       :   str					=						encoder_attention_heads
         A       :   Union[str, Any]					=						decoder_ffn_dim
         A       :   Optional[Any]					=						decoder_layers
         A       :   List[Any]					=						decoder_attention_heads
         A       :   Optional[int]					=						dropout
         A       :   Optional[int]					=						attention_dropout
         A       :   Optional[int]					=						activation_dropout
         A       :   Dict					=						activation_function
         A       :   List[str]					=						init_std
         A       :   Any					=						encoder_layerdrop
         A       :   List[Any]					=						decoder_layerdrop
         A       :   List[str]					=						classifier_dropout
         A       :   Any					=						use_cache
         A       :   Optional[Any]					=						encoder_layers
         A       :   Tuple					=						scale_embedding  # scale factor will be sqrt(d_model) if True

         super().__init__(
             num_labels=SCREAMING_SNAKE_CASE						,			pad_token_id=SCREAMING_SNAKE_CASE						,			bos_token_id=SCREAMING_SNAKE_CASE						,			eos_token_id=SCREAMING_SNAKE_CASE						,			is_encoder_decoder=SCREAMING_SNAKE_CASE						,			decoder_start_token_id=SCREAMING_SNAKE_CASE						,			forced_eos_token_id=SCREAMING_SNAKE_CASE						,			**SCREAMING_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'''						,			SCREAMING_SNAKE_CASE	):
             A       :   Union[str, Any]					=						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  A				(							__snake_case						):





     @property
     def 				__lowerCAmelCase							(							self	) ->				Mapping[str, Mapping[int, str]]:






         """simple docstring"""






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

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

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






         """simple docstring"""






         if self.task in ["default", "seq2seq-lm"]:
             A       :   str					=						super().outputs
         else:
             A       :   Tuple					=						super(SCREAMING_SNAKE_CASE						,			self	).outputs
             if self.use_past:
                 A,  A       :   Any					=						self.num_layers
                 for i in range(SCREAMING_SNAKE_CASE	):
                     A       :   List[Any]					=						{0: '''batch''', 2: '''past_sequence + sequence'''}
                     A       :   List[str]					=						{0: '''batch''', 2: '''past_sequence + sequence'''}
         return common_outputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = False						,			SCREAMING_SNAKE_CASE = None						,			) ->				Mapping[str, Any]:






         """simple docstring"""






         A       :   int					=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)

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

             A       :   Union[str, Any]					=						torch.cat(
                 [common_inputs['''decoder_attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)]						,			dim=1	)

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

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





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = False						,			SCREAMING_SNAKE_CASE = None						,			) ->				Mapping[str, Any]:






         """simple docstring"""






         A       :   Optional[int]					=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_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
             A,  A       :   Optional[int]					=						common_inputs['''input_ids'''].shape
             # Not using the same length for past_key_values
             A       :   List[Any]					=						seqlen + 2
             A,  A       :   List[Any]					=						self.num_layers
             A,  A       :   Optional[Any]					=						self.num_attention_heads
             A       :   Union[str, Any]					=						(
                 batch,
                 num_encoder_attention_heads,
                 past_key_values_length,
                 self._config.hidden_size // num_encoder_attention_heads,
             )

             A       :   Union[str, Any]					=						common_inputs['''attention_mask'''].dtype
             A       :   List[Any]					=						torch.cat(
                 [common_inputs['''attention_mask'''], torch.ones(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			dtype=SCREAMING_SNAKE_CASE	)]						,			dim=1	)
             A       :   Tuple					=						[
                 (torch.zeros(SCREAMING_SNAKE_CASE	), torch.zeros(SCREAMING_SNAKE_CASE	)) for _ in range(SCREAMING_SNAKE_CASE	)
             ]
         return common_inputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = False						,			SCREAMING_SNAKE_CASE = None						,			) ->				Mapping[str, Any]:






         """simple docstring"""






         A       :   Tuple					=						compute_effective_axis_dimension(
             SCREAMING_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
         A       :   int					=						tokenizer.num_special_tokens_to_add(SCREAMING_SNAKE_CASE	)
         A       :   int					=						compute_effective_axis_dimension(
             SCREAMING_SNAKE_CASE						,			fixed_dimension=OnnxConfig.default_fixed_sequence						,			num_token_to_add=SCREAMING_SNAKE_CASE	)

         # Generate dummy inputs according to compute batch and sequence
         A       :   Tuple					=						[''' '''.join([tokenizer.unk_token]	) * seq_length] * batch_size
         A       :   List[Any]					=						dict(tokenizer(SCREAMING_SNAKE_CASE						,			return_tensors=SCREAMING_SNAKE_CASE	)	)
         return common_inputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = -1						,			SCREAMING_SNAKE_CASE = False						,			SCREAMING_SNAKE_CASE = None						,			) ->				Mapping[str, Any]:






         """simple docstring"""






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

         elif self.task == "causal-lm":
             A       :   int					=						self._generate_dummy_inputs_for_causal_lm(
                 SCREAMING_SNAKE_CASE						,			batch_size=SCREAMING_SNAKE_CASE						,			seq_length=SCREAMING_SNAKE_CASE						,			is_pair=SCREAMING_SNAKE_CASE						,			framework=SCREAMING_SNAKE_CASE	)
         else:
             A       :   Dict					=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                 SCREAMING_SNAKE_CASE						,			batch_size=SCREAMING_SNAKE_CASE						,			seq_length=SCREAMING_SNAKE_CASE						,			is_pair=SCREAMING_SNAKE_CASE						,			framework=SCREAMING_SNAKE_CASE	)

         return common_inputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	) ->				List[str]:






         """simple docstring"""






         if self.task in ["default", "seq2seq-lm"]:
             A       :   List[str]					=						super()._flatten_past_key_values_(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)
         else:
             A       :   Optional[Any]					=						super(SCREAMING_SNAKE_CASE						,			self	)._flatten_past_key_values_(
                 SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)







from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


a__				=		{'''configuration_mmbt''': ['''MMBTConfig''']}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			a__				=		['''MMBTForClassification''', '''MMBTModel''', '''ModalEmbeddings''']


if TYPE_CHECKING:
			from .configuration_mmbt import MMBTConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_mmbt import MMBTForClassification, MMBTModel, ModalEmbeddings

else:
			import sys

			a__				=		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



