



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


__SCREAMING_SNAKE_CASE     =	logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE     =	{
    """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json""",
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}

class 						lowerCamelCase_     (			lowerCamelCase__    ):






						'''simple docstring'''
						a__ =	"blenderbot"
						a__ =	["past_key_values"]
						a__ =	{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}







						def __init__(						self   :      List[Any]						,						__lowerCamelCase   :      Optional[int]=80_08						,						__lowerCamelCase   :      Optional[Any]=1_28						,						__lowerCamelCase   :      Optional[int]=2						,						__lowerCamelCase   :      Optional[int]=1_02_40						,						__lowerCamelCase   :      List[str]=32						,						__lowerCamelCase   :      Any=24						,						__lowerCamelCase   :      List[Any]=1_02_40						,						__lowerCamelCase   :      int=32						,						__lowerCamelCase   :      Optional[int]=0.0						,						__lowerCamelCase   :      Union[str, Any]=0.0						,						__lowerCamelCase   :      List[Any]=True						,						__lowerCamelCase   :      str=True						,						__lowerCamelCase   :      Dict="gelu"						,						__lowerCamelCase   :      str=25_60						,						__lowerCamelCase   :      str=0.1						,						__lowerCamelCase   :      List[Any]=0.0						,						__lowerCamelCase   :      Union[str, Any]=0.0						,						__lowerCamelCase   :      Optional[Any]=0.02						,						__lowerCamelCase   :      List[Any]=1						,						__lowerCamelCase   :      Optional[Any]=False						,						__lowerCamelCase   :      Any=0						,						__lowerCamelCase   :      int=1						,						__lowerCamelCase   :      Tuple=2						,						__lowerCamelCase   :      Optional[Any]=3						,						__lowerCamelCase   :      Dict=2						,						**__lowerCamelCase   :      Any						,						)   ->					Dict:
									A				:       List[str]														=    vocab_size
									A				:       Tuple														=    max_position_embeddings
									A				:       Dict														=    d_model
									A				:       int														=    encoder_ffn_dim
									A				:       List[str]														=    encoder_layers
									A				:       Any														=    encoder_attention_heads
									A				:       Any														=    decoder_ffn_dim
									A				:       Union[str, Any]														=    decoder_layers
									A				:       Optional[int]														=    decoder_attention_heads
									A				:       List[str]														=    dropout
									A				:       str														=    attention_dropout
									A				:       Dict														=    activation_dropout
									A				:       Dict														=    activation_function
									A				:       Tuple														=    init_std
									A				:       Optional[Any]														=    encoder_layerdrop
									A				:       List[str]														=    decoder_layerdrop
									A				:       List[Any]														=    use_cache
									A				:       Dict														=    encoder_layers
									A				:       List[Any]														=    scale_embedding  # scale factor will be sqrt(d_model) if True

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







class 						lowerCamelCase_     (			lowerCamelCase__    ):






						'''simple docstring'''







						@property
						def       SCREAMING_SNAKE_CASE__		(						self   :      Optional[Any]			)   ->					Union[str, Any]:
									if self.task in ["default", "seq2seq-lm"]:
												A				:       Any														=    OrderedDict(
												    [
												        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
												        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
												    ]			)
												if self.use_past:
															A				:       Any														=    {0: "batch"}
															A				:       Optional[int]														=    {0: "batch", 1: "past_decoder_sequence + sequence"}
												else:
															A				:       str														=    {0: "batch", 1: "decoder_sequence"}
															A				:       int														=    {0: "batch", 1: "decoder_sequence"}
												if self.use_past:
															self.fill_with_past_key_values_(_UpperCAmelCase						,						direction="inputs"			)
									elif self.task == "causal-lm":
												A				:       Dict														=    OrderedDict(
												    [
												        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
												        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
												    ]			)
												if self.use_past:
															A							,			A				:       Dict														=    self.num_layers
															for i in range(_UpperCAmelCase			):
																		A				:       Dict														=    {0: "batch", 2: "past_sequence + sequence"}
																		A				:       Optional[Any]														=    {0: "batch", 2: "past_sequence + sequence"}
									else:
												A				:       Dict														=    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
						# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
						def       SCREAMING_SNAKE_CASE__		(						self   :      Optional[int]			)   ->					str:
									if self.task in ["default", "seq2seq-lm"]:
												A				:       List[Any]														=    super().outputs
									else:
												A				:       int														=    super(_UpperCAmelCase						,						self			).outputs
												if self.use_past:
															A							,			A				:       Dict														=    self.num_layers
															for i in range(_UpperCAmelCase			):
																		A				:       Union[str, Any]														=    {0: "batch", 2: "past_sequence + sequence"}
																		A				:       str														=    {0: "batch", 2: "past_sequence + sequence"}
									return common_outputs







						def       SCREAMING_SNAKE_CASE__		(						self   :      Optional[Any]						,						__lowerCamelCase   :      Dict						,						__lowerCamelCase   :      List[str] = -1						,						__lowerCamelCase   :      str = -1						,						__lowerCamelCase   :      Optional[int] = False						,						__lowerCamelCase   :      int = None						,						)   ->					Dict:
									A				:       Tuple														=    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
									    _UpperCAmelCase						,						_UpperCAmelCase						,						_UpperCAmelCase						,						_UpperCAmelCase						,						_UpperCAmelCase			)
									# Generate decoder inputs
									A				:       List[Any]														=    seq_length if not self.use_past else 1
									A				:       Tuple														=    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
									    _UpperCAmelCase						,						_UpperCAmelCase						,						_UpperCAmelCase						,						_UpperCAmelCase						,						_UpperCAmelCase			)
									A				:       Tuple														=    {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
									A				:       Dict														=    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
												A							,			A				:       Tuple														=    common_inputs["input_ids"].shape
												A				:       List[str]														=    common_inputs["decoder_input_ids"].shape[1]
												A							,			A				:       int														=    self.num_attention_heads
												A				:       Dict														=    (
												    batch,
												    num_encoder_attention_heads,
												    encoder_seq_length,
												    self._config.hidden_size // num_encoder_attention_heads,
												)
												A				:       Optional[int]														=    decoder_seq_length
												A				:       Optional[Any]														=    (
												    batch,
												    num_decoder_attention_heads,
												    decoder_past_length,
												    self._config.hidden_size // num_decoder_attention_heads,
												)
												A				:       Tuple														=    torch.cat(
												    [common_inputs["decoder_attention_mask"], torch.ones(_UpperCAmelCase						,						_UpperCAmelCase			)]						,						dim=1			)
												A				:       str														=    []
												A							,			A				:       Union[str, Any]														=    self.num_layers

												for _ in range(_UpperCAmelCase			):
															common_inputs["past_key_values"].append(
															    (
															        torch.zeros(_UpperCAmelCase			),
															        torch.zeros(_UpperCAmelCase			),
															        torch.zeros(_UpperCAmelCase			),
															        torch.zeros(_UpperCAmelCase			),
															    )			)
									return common_inputs







						def       SCREAMING_SNAKE_CASE__		(						self   :      Any						,						__lowerCamelCase   :      Union[str, Any]						,						__lowerCamelCase   :      Optional[Any] = -1						,						__lowerCamelCase   :      str = -1						,						__lowerCamelCase   :      int = False						,						__lowerCamelCase   :      Dict = None						,						)   ->					str:
									A				:       List[str]														=    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
												A							,			A				:       Tuple														=    common_inputs["input_ids"].shape
												A				:       Tuple														=    seqlen
												A							,			A				:       str														=    self.num_layers
												A							,			A				:       Any														=    self.num_attention_heads
												A				:       Tuple														=    (
												    batch,
												    num_encoder_attention_heads,
												    past_key_values_length,
												    self._config.hidden_size // num_encoder_attention_heads,
												)
												A				:       int														=    common_inputs["attention_mask"].dtype
												A				:       Any														=    torch.cat(
												    [common_inputs["attention_mask"], torch.ones(_UpperCAmelCase						,						_UpperCAmelCase						,						dtype=_UpperCAmelCase			)]						,						dim=1			)
												A				:       int														=    [
												    (torch.zeros(_UpperCAmelCase			), torch.zeros(_UpperCAmelCase			)) for _ in range(_UpperCAmelCase			)
												]
									return common_inputs







						def       SCREAMING_SNAKE_CASE__		(						self   :      Dict						,						__lowerCamelCase   :      Optional[int]						,						__lowerCamelCase   :      Dict = -1						,						__lowerCamelCase   :      str = -1						,						__lowerCamelCase   :      Optional[Any] = False						,						__lowerCamelCase   :      int = None						,						)   ->					Union[str, Any]:
									# 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
									A				:       Optional[Any]														=    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
									A				:       Union[str, Any]														=    tokenizer.num_special_tokens_to_add(_UpperCAmelCase			)
									A				:       List[Any]														=    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
									A				:       Optional[int]														=    [" ".join([tokenizer.unk_token]			) * seq_length] * batch_size
									A				:       str														=    dict(tokenizer(_UpperCAmelCase						,						return_tensors=_UpperCAmelCase			)			)
									return common_inputs







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







						def       SCREAMING_SNAKE_CASE__		(						self   :      Union[str, Any]						,						__lowerCamelCase   :      int						,						__lowerCamelCase   :      Any			)   ->					int:
									if direction not in ["inputs", "outputs"]:
												raise ValueError(F"""direction must either be \"inputs\" or \"outputs\", but {direction} was given"""			)

									A				:       List[str]														=    "past_key_values" if direction == "inputs" else "present"
									A							,			A				:       Union[str, Any]														=    self.num_layers

									A				:       List[Any]														=    "past_encoder_sequence"
									A				:       List[str]														=    "past_decoder_sequence" if direction == "inputs" else "past_decoder_sequence + sequence"

									for i in range(_UpperCAmelCase			):
												A				:       Union[str, Any]														=    {0: "batch", 2: decoder_sequence}
												A				:       List[str]														=    {0: "batch", 2: decoder_sequence}
												A				:       str														=    {0: "batch", 2: encoder_sequence}
												A				:       List[str]														=    {0: "batch", 2: encoder_sequence}



from typing import Dict, List, Optional, Tuple

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


UpperCAmelCase     =       logging.get_logger(__name__)







class lowerCAmelCase_		(					lowerCamelCase__			):

 '''simple docstring'''

 __snake_case				=    ["input_ids", "attention_mask"]



 def __init__(      self						, _UpperCAmelCase="[PAD]"						, _UpperCAmelCase="[BOS]"						, _UpperCAmelCase="[EOS]"						, _UpperCAmelCase="[MASK]"						, _UpperCAmelCase="[CLS]"						, _UpperCAmelCase="[SEP]"						, _UpperCAmelCase=20_48						, **_UpperCAmelCase						, ):
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else pad_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else bos_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else eos_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else mask_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else cls_token
       snake_case_			= AddedToken(_UpperCAmelCase						, lstrip=_UpperCAmelCase						, rstrip=_UpperCAmelCase   ) if isinstance(_UpperCAmelCase						, _UpperCAmelCase   ) else sep_token

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

       snake_case_			= 2**8  # utf is 8 bits

       # define special tokens dict
       snake_case_			= {
           self.pad_token: 0,
           self.bos_token: 1,
           self.eos_token: 2,
           self.mask_token: 3,
           self.cls_token: 4,
           self.sep_token: 5,
       }
       snake_case_			= len(self.special_tokens_encoder   )
       snake_case_			= {v: k for k, v in self.special_tokens_encoder.items()}



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.special_tokens_encoder.copy()
       vocab.update(self.added_tokens_encoder   )
       for i in range(self._utf_vocab_size   ):
             snake_case_			= chr(_UpperCAmelCase   )
             snake_case_			= i + len(self.special_tokens_encoder   )
       return vocab



 @property
 def 		UpperCamelCase__	(      self   ):
       return self._utf_vocab_size + self._num_special_tokens



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = None						, _UpperCAmelCase = False   ):
       if already_has_special_tokens:
             return super().get_special_tokens_mask(
                 token_ids_a=_UpperCAmelCase						, token_ids_a=_UpperCAmelCase						, already_has_special_tokens=_UpperCAmelCase   )

       # normal case: some special tokens
       if token_ids_a is None:
             return [1] + [0] * len(_UpperCAmelCase   ) + [1]
       return [1] + ([0] * len(_UpperCAmelCase   )) + [1] + ([0] * len(_UpperCAmelCase   )) + [1]



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = None   ):
       if token_ids_a is None:
             return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
       else:
             return [self.cls_token_id] + token_ids_a + [self.sep_token_id] + token_ids_a + [self.sep_token_id]



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       snake_case_			= [chr(_UpperCAmelCase   ) for i in text.encode('''utf-8'''   )]
       return tokens



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       if token in self.special_tokens_encoder:
             snake_case_			= self.special_tokens_encoder[token]
       elif token in self.added_tokens_encoder:
             snake_case_			= self.added_tokens_encoder[token]
       elif len(_UpperCAmelCase   ) != 1:
             snake_case_			= self.unk_token_id
       else:
             snake_case_			= ord(_UpperCAmelCase   ) + self._num_special_tokens
       return token_id



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       if index in self.special_tokens_decoder:
             snake_case_			= self.special_tokens_decoder[index]
       elif index in self.added_tokens_decoder:
             snake_case_			= self.added_tokens_decoder[index]
       else:
             snake_case_			= chr(index - self._num_special_tokens   )
       return token



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase   ):
       snake_case_			= b''''''
       for token in tokens:
             if token in self.special_tokens_decoder:
                   snake_case_			= self.special_tokens_decoder[token].encode('''utf-8'''   )
             elif token in self.added_tokens_decoder:
                   snake_case_			= self.special_tokens_decoder[token].encode('''utf-8'''   )
             elif token in self.special_tokens_encoder:
                   snake_case_			= token.encode('''utf-8'''   )
             elif token in self.added_tokens_encoder:
                   snake_case_			= token.encode('''utf-8'''   )
             else:
                   snake_case_			= bytes([ord(_UpperCAmelCase   )]   )
             bstring += tok_string
       snake_case_			= bstring.decode('''utf-8'''						, errors='''replace'''   )
       return string





 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = None   ):
       return ()