

from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


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

_lowerCamelCase      :					Any					    =      {
    '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json''',
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







       lowercase_						=    """blenderbot-small"""
       lowercase_						=    ["""past_key_values"""]
       lowercase_						=    {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

       def __init__(							self ,   lowercase__=5_0_2_6_5 ,   lowercase__=5_1_2 ,   lowercase__=8 ,   lowercase__=2_0_4_8 ,   lowercase__=1_6 ,   lowercase__=8 ,   lowercase__=2_0_4_8 ,   lowercase__=1_6 ,   lowercase__=0.0 ,   lowercase__=0.0 ,   lowercase__=True ,   lowercase__=True ,   lowercase__="gelu" ,   lowercase__=5_1_2 ,   lowercase__=0.1 ,   lowercase__=0.0 ,   lowercase__=0.0 ,   lowercase__=0.02 ,   lowercase__=1 ,   lowercase__=False ,   lowercase__=0 ,   lowercase__=1 ,   lowercase__=2 ,   lowercase__=2 ,   **lowercase__ ,   ):







             '''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    =use_cache
             __A    =encoder_layers
             __A    =scale_embedding  # scale factor will be sqrt(d_model) if True

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





class        lowerCAmelCase__  (					__magic_name__			):



       '''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_(lowercase__ ,   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(lowercase__     ):
                               __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(lowercase__ ,   self     ).outputs
                   if self.use_past:
                         __A    ,		__A    =self.num_layers
                         for i in range(lowercase__     ):
                               __A    ={0: '''batch''', 2: '''past_sequence + sequence'''}
                               __A    ={0: '''batch''', 2: '''past_sequence + sequence'''}
             return common_outputs

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = -1 ,   lowercase__ = -1 ,   lowercase__ = False ,   lowercase__ = None ,   ):







             '''simple docstring'''



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

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

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

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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = -1 ,   lowercase__ = -1 ,   lowercase__ = False ,   lowercase__ = None ,   ):







             '''simple docstring'''



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

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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = -1 ,   lowercase__ = -1 ,   lowercase__ = False ,   lowercase__ = None ,   ):







             '''simple docstring'''



             __A    =compute_effective_axis_dimension(
                 lowercase__ ,   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(lowercase__     )
             __A    =compute_effective_axis_dimension(
                 lowercase__ ,   fixed_dimension=OnnxConfig.default_fixed_sequence ,   num_token_to_add=lowercase__     )

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

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = -1 ,   lowercase__ = -1 ,   lowercase__ = False ,   lowercase__ = None ,   ):







             '''simple docstring'''



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

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

             return common_inputs


       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ ,   lowercase__ ,   lowercase__     ):







             '''simple docstring'''



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





from typing import Dict, List, Optional

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


_lowerCamelCase      :					Any					    =      logging.get_logger(__name__)


_lowerCamelCase      :					Tuple					    =      {
    '''nielsr/canine-s''': 2048,
}

# Unicode defines 1,114,112 total “codepoints”
_lowerCamelCase      :					List[str]					    =      111_4112

# Below: Constants defining canonical codepoints for special, pseudo-characters.
# Copied from https://github.com/google-research/language/blob/master/language/canine/special_codepoints.py
_lowerCamelCase      :					List[str]					    =      0

_lowerCamelCase      :					Any					    =      0xe_0_0_0
_lowerCamelCase      :					Union[str, Any]					    =      0xe_0_0_1
_lowerCamelCase      :					Any					    =      0xe_0_0_2
_lowerCamelCase      :					List[str]					    =      0xe_0_0_3
_lowerCamelCase      :					Any					    =      0xe_0_0_4

# Maps special codepoints to human-readable names.
_lowerCamelCase      :					Dict[int, str]					    =      {
    # Special symbols are represented using codepoints values that are valid,
    # but designated as "Private Use", meaning that they will never be assigned
    # characters by the Unicode Consortium, and are thus safe for use here.
    #
    # NOTE: Do *NOT* add any sort of [UNK_CHAR] here. They are explicitly
    # excluded and should fail with a hard error.
    CLS: "[CLS]",
    SEP: "[SEP]",
    BOS: "[BOS]",
    MASK: "[MASK]",
    PAD: "[PAD]",
    RESERVED: "[RESERVED]",
}

# Maps special codepoint human-readable names to their codepoint values.
_lowerCamelCase      :					Dict[str, int]					    =      {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()}





class        lowerCAmelCase__  (					__magic_name__			):



       '''simple docstring'''







       lowercase_						=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES

       def __init__(							self ,   lowercase__=chr(lowercase__     ) ,   lowercase__=chr(lowercase__     ) ,   lowercase__=chr(lowercase__     ) ,   lowercase__=chr(lowercase__     ) ,   lowercase__=chr(lowercase__     ) ,   lowercase__=chr(lowercase__     ) ,   lowercase__=False ,   lowercase__=2_0_4_8 ,   **lowercase__ ,   ):







             '''simple docstring'''



             __A    =AddedToken(lowercase__ ,   lstrip=lowercase__ ,   rstrip=lowercase__     ) if isinstance(lowercase__ ,   lowercase__     ) else bos_token
             __A    =AddedToken(lowercase__ ,   lstrip=lowercase__ ,   rstrip=lowercase__     ) if isinstance(lowercase__ ,   lowercase__     ) else eos_token
             __A    =AddedToken(lowercase__ ,   lstrip=lowercase__ ,   rstrip=lowercase__     ) if isinstance(lowercase__ ,   lowercase__     ) else sep_token
             __A    =AddedToken(lowercase__ ,   lstrip=lowercase__ ,   rstrip=lowercase__     ) if isinstance(lowercase__ ,   lowercase__     ) else cls_token
             __A    =AddedToken(lowercase__ ,   lstrip=lowercase__ ,   rstrip=lowercase__     ) if isinstance(lowercase__ ,   lowercase__     ) else pad_token

             # Mask token behave like a normal word, i.e. include the space before it
             __A    =AddedToken(lowercase__ ,   lstrip=lowercase__ ,   rstrip=lowercase__     ) if isinstance(lowercase__ ,   lowercase__     ) else mask_token

             super().__init__(
                 bos_token=lowercase__ ,   eos_token=lowercase__ ,   sep_token=lowercase__ ,   cls_token=lowercase__ ,   pad_token=lowercase__ ,   mask_token=lowercase__ ,   add_prefix_space=lowercase__ ,   model_max_length=lowercase__ ,   **lowercase__ ,   )

             # Creates a mapping for looking up the IDs of special symbols.
             __A    ={}
             for codepoint, name in SPECIAL_CODEPOINTS.items():
                   __A    =codepoint

             # Creates a mapping for looking up the string forms of special symbol IDs.
             __A    ={
                 codepoint: name for name, codepoint in self._special_codepoints.items()
             }

             __A    =UNICODE_VOCAB_SIZE
             __A    =len(self._special_codepoints     )

       @property
       def     __UpperCamelCase     (							self     ):







             '''simple docstring'''



             return self._unicode_vocab_size

       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



             return list(lowercase__     )

       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



             try:
                   return ord(lowercase__     )
             except TypeError:
                   raise ValueError(f'''invalid token: \'{token}\''''     )

       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



             try:
                   if index in SPECIAL_CODEPOINTS:
                         return SPECIAL_CODEPOINTS[index]
                   return chr(lowercase__     )
             except TypeError:
                   raise ValueError(f'''invalid id: {index}'''     )

       def     __UpperCamelCase     (							self ,   lowercase__     ):







             '''simple docstring'''



             return "".join(lowercase__     )

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = None     ):







             '''simple docstring'''



             __A    =[self.sep_token_id]
             __A    =[self.cls_token_id]

             __A    =cls + token_ids_a + sep
             if token_ids_a is not None:
                   result += token_ids_a + sep
             return result

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = None ,   lowercase__ = False     ):







             '''simple docstring'''



             if already_has_special_tokens:
                   return super().get_special_tokens_mask(
                       token_ids_a=lowercase__ ,   token_ids_a=lowercase__ ,   already_has_special_tokens=lowercase__     )

             __A    =[1] + ([0] * len(lowercase__     )) + [1]
             if token_ids_a is not None:
                   result += ([0] * len(lowercase__     )) + [1]
             return result

       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = None     ):







             '''simple docstring'''



             __A    =[self.sep_token_id]
             __A    =[self.cls_token_id]

             __A    =len(cls + token_ids_a + sep     ) * [0]
             if token_ids_a is not None:
                   result += len(token_ids_a + sep     ) * [1]
             return result


       def     __UpperCamelCase     (							self ,   lowercase__ ,   lowercase__ = None     ):







             '''simple docstring'''



             return ()


