


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


lowerCamelCase							:   int  						=	logging.get_logger(__name__)

lowerCamelCase							:   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   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Tuple  					=							"""bart"""
      lowerCAmelCase__					:     Any  					=							["""past_key_values"""]
      lowerCAmelCase__					:     Dict  					=							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}



      def __init__(self    :    Any   ,						UpperCamelCase    :    List[str]=50265   ,						UpperCamelCase    :    Optional[Any]=1024   ,						UpperCamelCase    :    Any=12   ,						UpperCamelCase    :    Dict=4096   ,						UpperCamelCase    :    Any=16   ,						UpperCamelCase    :    Tuple=12   ,						UpperCamelCase    :    Dict=4096   ,						UpperCamelCase    :    int=16   ,						UpperCamelCase    :    Union[str, Any]=0.0   ,						UpperCamelCase    :    Optional[int]=0.0   ,						UpperCamelCase    :    str="gelu"   ,						UpperCamelCase    :    Tuple=1024   ,						UpperCamelCase    :    int=0.1   ,						UpperCamelCase    :    Optional[int]=0.0   ,						UpperCamelCase    :    Optional[int]=0.0   ,						UpperCamelCase    :    Dict=0.02   ,						UpperCamelCase    :    List[str]=0.0   ,						UpperCamelCase    :    int=False   ,						UpperCamelCase    :    List[str]=True   ,						UpperCamelCase    :    Dict=3   ,						UpperCamelCase    :    Optional[Any]=1   ,						UpperCamelCase    :    str=0   ,						UpperCamelCase    :    List[Any]=2   ,						UpperCamelCase    :    int=True   ,						UpperCamelCase    :    Any=2   ,						UpperCamelCase    :    Optional[int]=2   ,						**UpperCamelCase    :    Union[str, Any]   ,						):

             '''simple docstring'''







             lowercase__      			= vocab_size
             lowercase__      			= max_position_embeddings
             lowercase__      			= d_model
             lowercase__      			= encoder_ffn_dim
             lowercase__      			= encoder_layers
             lowercase__      			= encoder_attention_heads
             lowercase__      			= decoder_ffn_dim
             lowercase__      			= decoder_layers
             lowercase__      			= decoder_attention_heads
             lowercase__      			= dropout
             lowercase__      			= attention_dropout
             lowercase__      			= activation_dropout
             lowercase__      			= activation_function
             lowercase__      			= init_std
             lowercase__      			= encoder_layerdrop
             lowercase__      			= decoder_layerdrop
             lowercase__      			= classifier_dropout
             lowercase__      			= use_cache
             lowercase__      			= encoder_layers
             lowercase__      			= 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 ):
                    lowercase__      			= 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   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      @property
      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







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

                    if self.use_past:
                           lowercase__      			= {0: '''batch'''}
                           lowercase__      			= {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
                    else:
                           lowercase__      			= {0: '''batch''', 1: '''decoder_sequence'''}
                           lowercase__      			= {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.
                    lowercase__      			= OrderedDict(
                        [
                            ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                            ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                        ] )
                    if self.use_past:
                           lowercase__			,lowercase__      			= self.num_layers
                           for i in range(UpperCamelCase ):
                                  lowercase__      			= {0: '''batch''', 2: '''past_sequence + sequence'''}
                                  lowercase__      			= {0: '''batch''', 2: '''past_sequence + sequence'''}
             else:
                    lowercase__      			= 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    :    Any ):

             '''simple docstring'''







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



      def       UpperCamelCase__		(self    :    int   ,						UpperCamelCase    :    PreTrainedTokenizer   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    bool = False   ,						UpperCamelCase    :    Optional[TensorType] = None   ,						):

             '''simple docstring'''







             lowercase__      			= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                 UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase   ,						UpperCamelCase )

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

                    lowercase__      			= torch.cat(
                        [common_inputs['''decoder_attention_mask'''], torch.ones(UpperCamelCase   ,						UpperCamelCase )]   ,						dim=1 )

                    lowercase__      			= []
                    # If the number of encoder and decoder layers are present in the model configuration, both are considered
                    lowercase__			,lowercase__      			= self.num_layers
                    lowercase__      			= min(UpperCamelCase   ,						UpperCamelCase )
                    lowercase__      			= max(UpperCamelCase   ,						UpperCamelCase ) - min_num_layers
                    lowercase__      			= '''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.
                    lowercase__      			= 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    :    int   ,						UpperCamelCase    :    PreTrainedTokenizer   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    bool = False   ,						UpperCamelCase    :    Optional[TensorType] = None   ,						):

             '''simple docstring'''







             lowercase__      			= 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
                    lowercase__			,lowercase__      			= common_inputs['''input_ids'''].shape
                    # Not using the same length for past_key_values
                    lowercase__      			= seqlen + 2
                    lowercase__			,lowercase__      			= self.num_layers
                    lowercase__			,lowercase__      			= self.num_attention_heads
                    lowercase__      			= (
                        batch,
                        num_encoder_attention_heads,
                        past_key_values_length,
                        self._config.hidden_size // num_encoder_attention_heads,
                    )

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



      def       UpperCamelCase__		(self    :    Union[str, Any]   ,						UpperCamelCase    :    PreTrainedTokenizer   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    bool = False   ,						UpperCamelCase    :    Optional[TensorType] = None   ,						):

             '''simple docstring'''







             lowercase__      			= 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
             lowercase__      			= tokenizer.num_special_tokens_to_add(UpperCamelCase )
             lowercase__      			= 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
             lowercase__      			= [''' '''.join([tokenizer.unk_token] ) * seq_length] * batch_size
             lowercase__      			= dict(tokenizer(UpperCamelCase   ,						return_tensors=UpperCamelCase ) )
             return common_inputs



      def       UpperCamelCase__		(self    :    int   ,						UpperCamelCase    :    PreTrainedTokenizer   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    bool = False   ,						UpperCamelCase    :    Optional[TensorType] = None   ,						):

             '''simple docstring'''







             if self.task in ["default", "seq2seq-lm"]:
                    lowercase__      			= 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":
                    lowercase__      			= self._generate_dummy_inputs_for_causal_lm(
                        UpperCamelCase   ,						batch_size=UpperCamelCase   ,						seq_length=UpperCamelCase   ,						is_pair=UpperCamelCase   ,						framework=UpperCamelCase )
             else:
                    lowercase__      			= 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    :    Tuple   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    int   ,						UpperCamelCase    :    str ):

             '''simple docstring'''







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








'''simple docstring'''

# Lint as: python3
import itertools
import os
import re


lowerCamelCase							:   Any  						=	re.compile(R'([A-Z]+)([A-Z][a-z])')
lowerCamelCase							:   str  						=	re.compile(R'([a-z\d])([A-Z])')

lowerCamelCase							:   Optional[int]  						=	re.compile(R'(?<!_)_(?!_)')
lowerCamelCase							:   List[Any]  						=	re.compile(R'(_{2,})')

lowerCamelCase							:   str  						=	R'^\w+(\.\w+)*$'

lowerCamelCase							:   Dict  						=	R'<>:/\|?*'

def _SCREAMING_SNAKE_CASE      (A       )       ->       Any:






       """simple docstring"""







       lowercase__      			= _uppercase_uppercase_re.sub(R'''\1_\2'''  , A       )
       lowercase__      			= _lowercase_uppercase_re.sub(R'''\1_\2'''  , A       )
       return name.lower()

def _SCREAMING_SNAKE_CASE      (A       )       ->       Tuple:






       """simple docstring"""







       lowercase__      			= _single_underscore_re.split(A       )
       lowercase__      			= [_multiple_underscores_re.split(A       ) for n in name]
       return "".join(n.capitalize() for n in itertools.chain.from_iterable(A       ) if n != ''''''       )

def _SCREAMING_SNAKE_CASE      (A       )       ->       Tuple:






       """simple docstring"""







       if os.path.basename(A       ) != name:
              raise ValueError(f"Should be a dataset name, not a path: {name}"       )
       return camelcase_to_snakecase(A       )

def _SCREAMING_SNAKE_CASE      (A  , A       )       ->       Optional[Any]:






       """simple docstring"""







       if os.path.basename(A       ) != name:
              raise ValueError(f"Should be a dataset name, not a path: {name}"       )
       if not re.match(_split_re  , A       ):
              raise ValueError(f"Split name should match '{_split_re}'' but got '{split}'."       )
       return f"{filename_prefix_for_name(A       )}-{split}"

def _SCREAMING_SNAKE_CASE      (A  , A  , A  , A=None       )       ->       List[str]:






       """simple docstring"""







       lowercase__      			= filename_prefix_for_split(A  , A       )
       if filetype_suffix:
              prefix += f".{filetype_suffix}"
       lowercase__      			= os.path.join(A  , A       )
       return f"{filepath}*"

def _SCREAMING_SNAKE_CASE      (A  , A  , A  , A=None  , A=None       )       ->       Optional[Any]:






       """simple docstring"""







       lowercase__      			= filename_prefix_for_split(A  , A       )
       lowercase__      			= os.path.join(A  , A       )

       if shard_lengths:
              lowercase__      			= len(A       )
              lowercase__      			= [f"{prefix}-{shard_id:05d}-of-{num_shards:05d}" for shard_id in range(A       )]
              if filetype_suffix:
                     lowercase__      			= [filename + f".{filetype_suffix}" for filename in filenames]
              return filenames
       else:
              lowercase__      			= prefix
              if filetype_suffix:
                     filename += f".{filetype_suffix}"
              return [filename]




