'''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_:     Tuple  =logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:     Dict  ={
    '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      ( lowerCAmelCase__	):
    a__       :						List[str]          =  """bart"""
    a__       :						List[str]          =  ["""past_key_values"""]
    a__       :						Dict          =  {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



    def __init__(self    :    List[Any]   ,			__a    :    Any=50265   ,			__a    :    List[Any]=1024   ,			__a    :    Union[str, Any]=12   ,			__a    :    Dict=4096   ,			__a    :    str=16   ,			__a    :    Dict=12   ,			__a    :    List[Any]=4096   ,			__a    :    List[Any]=16   ,			__a    :    str=0.0   ,			__a    :    Optional[Any]=0.0   ,			__a    :    List[Any]="gelu"   ,			__a    :    Tuple=1024   ,			__a    :    str=0.1   ,			__a    :    Dict=0.0   ,			__a    :    Optional[Any]=0.0   ,			__a    :    List[str]=0.02   ,			__a    :    int=0.0   ,			__a    :    int=False   ,			__a    :    Optional[Any]=True   ,			__a    :    Any=3   ,			__a    :    int=1   ,			__a    :    Optional[int]=0   ,			__a    :    Optional[int]=2   ,			__a    :    Optional[Any]=True   ,			__a    :    Optional[int]=2   ,			__a    :    Union[str, Any]=2   ,			**__a    :    List[str]   ,			):
         UpperCAmelCase_           =   vocab_size
         UpperCAmelCase_           =   max_position_embeddings
         UpperCAmelCase_           =   d_model
         UpperCAmelCase_           =   encoder_ffn_dim
         UpperCAmelCase_           =   encoder_layers
         UpperCAmelCase_           =   encoder_attention_heads
         UpperCAmelCase_           =   decoder_ffn_dim
         UpperCAmelCase_           =   decoder_layers
         UpperCAmelCase_           =   decoder_attention_heads
         UpperCAmelCase_           =   dropout
         UpperCAmelCase_           =   attention_dropout
         UpperCAmelCase_           =   activation_dropout
         UpperCAmelCase_           =   activation_function
         UpperCAmelCase_           =   init_std
         UpperCAmelCase_           =   encoder_layerdrop
         UpperCAmelCase_           =   decoder_layerdrop
         UpperCAmelCase_           =   classifier_dropout
         UpperCAmelCase_           =   use_cache
         UpperCAmelCase_           =   encoder_layers
         UpperCAmelCase_           =   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__ ):
              UpperCAmelCase_           =   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      ( lowerCAmelCase__	):



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

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



    def 					_lowercase						(self    :    Any   ,			__a    :    Any   ,			__a    :    str = -1   ,			__a    :    List[Any] = -1   ,			__a    :    Tuple = False   ,			__a    :    Any = None   ,			):
         UpperCAmelCase_           =   self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
             UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__ )

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

              UpperCAmelCase_           =   torch.cat(
                  [common_inputs["decoder_attention_mask"], torch.ones(UpperCamelCase__   ,			UpperCamelCase__ )]   ,			dim=1 )

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

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



    def 					_lowercase						(self    :    Any   ,			__a    :    List[Any]   ,			__a    :    Optional[Any] = -1   ,			__a    :    Any = -1   ,			__a    :    str = False   ,			__a    :    Union[str, Any] = 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_           =   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
         UpperCAmelCase_           =   tokenizer.num_special_tokens_to_add(UpperCamelCase__ )
         UpperCAmelCase_           =   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
         UpperCAmelCase_           =   [" ".join([tokenizer.unk_token] ) * seq_length] * batch_size
         UpperCAmelCase_           =   dict(tokenizer(UpperCamelCase__   ,			return_tensors=UpperCamelCase__ ) )
         return common_inputs



    def 					_lowercase						(self    :    Optional[int]   ,			__a    :    List[Any]   ,			__a    :    Optional[Any] = -1   ,			__a    :    Union[str, Any] = -1   ,			__a    :    str = False   ,			__a    :    Dict = None   ,			):
         if self.task in ["default", "seq2seq-lm"]:
              UpperCAmelCase_           =   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":
              UpperCAmelCase_           =   self._generate_dummy_inputs_for_causal_lm(
                  UpperCamelCase__   ,			batch_size=UpperCamelCase__   ,			seq_length=UpperCamelCase__   ,			is_pair=UpperCamelCase__   ,			framework=UpperCamelCase__ )
         else:
              UpperCAmelCase_           =   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 					_lowercase						(self    :    Tuple   ,			__a    :    Optional[int]   ,			__a    :    List[Any]   ,			__a    :    int   ,			__a    :    Optional[int] ):
         if self.task in ["default", "seq2seq-lm"]:
              UpperCAmelCase_           =   super()._flatten_past_key_values_(UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__ )
         else:
              UpperCAmelCase_           =   super(UpperCamelCase__   ,			self )._flatten_past_key_values_(
                  UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__   ,			UpperCamelCase__ )










from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE__       :      Optional[Any]  =   {'configuration_mmbt': ['MMBTConfig']}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__       :      List[Any]  =   ['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

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






