


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


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

__UpperCamelCase					:	Tuple          =				{
    '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__(     snake_case__    ):






    '''simple docstring'''





    A_	:  Any								=			'blenderbot'
    A_	:  Any								=			['past_key_values']
    A_	:  int								=			{'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}





    def __init__(						self					: Dict							, __snake_case					: Dict=8_008							, __snake_case					: int=128							, __snake_case					: Optional[int]=2							, __snake_case					: Optional[Any]=10_240							, __snake_case					: Optional[int]=32							, __snake_case					: Optional[int]=24							, __snake_case					: Any=10_240							, __snake_case					: Optional[int]=32							, __snake_case					: Optional[Any]=0.0							, __snake_case					: int=0.0							, __snake_case					: List[str]=True							, __snake_case					: Any=True							, __snake_case					: Dict="gelu"							, __snake_case					: str=2_560							, __snake_case					: Any=0.1							, __snake_case					: Optional[Any]=0.0							, __snake_case					: List[str]=0.0							, __snake_case					: Any=0.02							, __snake_case					: Optional[int]=1							, __snake_case					: Optional[int]=False							, __snake_case					: Any=0							, __snake_case					: List[Any]=1							, __snake_case					: List[str]=2							, __snake_case					: Optional[Any]=3							, __snake_case					: List[str]=2							, **__snake_case					: str							, ):





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

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





class 			lowerCAmelCase__(     snake_case__    ):






    '''simple docstring'''





    @property
    def        _lowerCamelCase			(						self					: Dict						):





       '''simple docstring'''
       if self.task in ["default", "seq2seq-lm"]:
          UpperCAmelCase_				:   Tuple							= OrderedDict(
              [
                  ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                  ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
              ]						)
          if self.use_past:
             UpperCAmelCase_				:   Tuple							= {0: '''batch'''}
             UpperCAmelCase_				:   int							= {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
          else:
             UpperCAmelCase_				:   Optional[int]							= {0: '''batch''', 1: '''decoder_sequence'''}
             UpperCAmelCase_				:   Optional[int]							= {0: '''batch''', 1: '''decoder_sequence'''}
          if self.use_past:
             self.fill_with_past_key_values_(__snake_case							, direction='''inputs'''						)
       elif self.task == "causal-lm":
          UpperCAmelCase_				:   Tuple							= OrderedDict(
              [
                  ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                  ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
              ]						)
          if self.use_past:
             UpperCAmelCase_		,   UpperCAmelCase_				:   int							= self.num_layers
             for i in range(__snake_case						):
                UpperCAmelCase_				:   Union[str, Any]							= {0: '''batch''', 2: '''past_sequence + sequence'''}
                UpperCAmelCase_				:   Optional[Any]							= {0: '''batch''', 2: '''past_sequence + sequence'''}
       else:
          UpperCAmelCase_				:   Optional[Any]							= 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        _lowerCamelCase			(						self					: Union[str, Any]						):





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





    def        _lowerCamelCase			(						self					: Any							, __snake_case					: PreTrainedTokenizer							, __snake_case					: int = -1							, __snake_case					: int = -1							, __snake_case					: bool = False							, __snake_case					: Optional[TensorType] = None							, ):





       '''simple docstring'''
       UpperCAmelCase_				:   str							= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           __snake_case							, __snake_case							, __snake_case							, __snake_case							, __snake_case						)
       # Generate decoder inputs
       UpperCAmelCase_				:   int							= seq_length if not self.use_past else 1
       UpperCAmelCase_				:   List[Any]							= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           __snake_case							, __snake_case							, __snake_case							, __snake_case							, __snake_case						)
       UpperCAmelCase_				:   int							= {f'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
       UpperCAmelCase_				:   int							= dict(**__snake_case							, **__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
          UpperCAmelCase_		,   UpperCAmelCase_				:   Any							= common_inputs['''input_ids'''].shape
          UpperCAmelCase_				:   List[str]							= common_inputs['''decoder_input_ids'''].shape[1]
          UpperCAmelCase_		,   UpperCAmelCase_				:   Union[str, Any]							= self.num_attention_heads
          UpperCAmelCase_				:   Dict							= (
              batch,
              num_encoder_attention_heads,
              encoder_seq_length,
              self._config.hidden_size // num_encoder_attention_heads,
          )
          UpperCAmelCase_				:   Tuple							= decoder_seq_length
          UpperCAmelCase_				:   int							= (
              batch,
              num_decoder_attention_heads,
              decoder_past_length,
              self._config.hidden_size // num_decoder_attention_heads,
          )
          UpperCAmelCase_				:   Any							= torch.cat(
              [common_inputs['''decoder_attention_mask'''], torch.ones(__snake_case							, __snake_case						)]							, dim=1						)
          UpperCAmelCase_				:   Union[str, Any]							= []
          UpperCAmelCase_		,   UpperCAmelCase_				:   Any							= self.num_layers

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





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





       '''simple docstring'''
       UpperCAmelCase_				:   str							= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           __snake_case							, __snake_case							, __snake_case							, __snake_case							, __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
          UpperCAmelCase_		,   UpperCAmelCase_				:   Optional[int]							= common_inputs['''input_ids'''].shape
          UpperCAmelCase_				:   List[str]							= seqlen
          UpperCAmelCase_		,   UpperCAmelCase_				:   str							= self.num_layers
          UpperCAmelCase_		,   UpperCAmelCase_				:   Dict							= self.num_attention_heads
          UpperCAmelCase_				:   List[str]							= (
              batch,
              num_encoder_attention_heads,
              past_key_values_length,
              self._config.hidden_size // num_encoder_attention_heads,
          )
          UpperCAmelCase_				:   List[Any]							= common_inputs['''attention_mask'''].dtype
          UpperCAmelCase_				:   List[str]							= torch.cat(
              [common_inputs['''attention_mask'''], torch.ones(__snake_case							, __snake_case							, dtype=__snake_case						)]							, dim=1						)
          UpperCAmelCase_				:   Tuple							= [
              (torch.zeros(__snake_case						), torch.zeros(__snake_case						)) for _ in range(__snake_case						)
          ]
       return common_inputs





    def        _lowerCamelCase			(						self					: List[Any]							, __snake_case					: PreTrainedTokenizer							, __snake_case					: int = -1							, __snake_case					: int = -1							, __snake_case					: bool = False							, __snake_case					: Optional[TensorType] = None							, ):





       '''simple docstring'''
       # 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_				:   Optional[int]							= compute_effective_axis_dimension(
           __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
       UpperCAmelCase_				:   List[Any]							= tokenizer.num_special_tokens_to_add(__snake_case						)
       UpperCAmelCase_				:   List[Any]							= compute_effective_axis_dimension(
           __snake_case							, fixed_dimension=OnnxConfig.default_fixed_sequence							, num_token_to_add=__snake_case						)

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





    def        _lowerCamelCase			(						self					: List[str]							, __snake_case					: PreTrainedTokenizer							, __snake_case					: int = -1							, __snake_case					: int = -1							, __snake_case					: bool = False							, __snake_case					: Optional[TensorType] = None							, ):





       '''simple docstring'''
       if self.task in ["default", "seq2seq-lm"]:
          UpperCAmelCase_				:   Union[str, Any]							= self._generate_dummy_inputs_for_default_and_seqaseq_lm(
              __snake_case							, batch_size=__snake_case							, seq_length=__snake_case							, is_pair=__snake_case							, framework=__snake_case						)

       elif self.task == "causal-lm":
          UpperCAmelCase_				:   Tuple							= self._generate_dummy_inputs_for_causal_lm(
              __snake_case							, batch_size=__snake_case							, seq_length=__snake_case							, is_pair=__snake_case							, framework=__snake_case						)
       else:
          UpperCAmelCase_				:   Any							= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              __snake_case							, batch_size=__snake_case							, seq_length=__snake_case							, is_pair=__snake_case							, framework=__snake_case						)

       return common_inputs





    def        _lowerCamelCase			(						self					: List[str]							, __snake_case					: List[str]							, __snake_case					: List[Any]							, __snake_case					: Any							, __snake_case					: str						):





       '''simple docstring'''
       if self.task in ["default", "seq2seq-lm"]:
          UpperCAmelCase_				:   List[str]							= super()._flatten_past_key_values_(__snake_case							, __snake_case							, __snake_case							, __snake_case						)
       else:
          UpperCAmelCase_				:   Any							= super(__snake_case							, self						)._flatten_past_key_values_(
              __snake_case							, __snake_case							, __snake_case							, __snake_case						)





    def        _lowerCamelCase			(						self					: Optional[Any]							, __snake_case					: Mapping[str, Mapping[int, str]]							, __snake_case					: str						):





       '''simple docstring'''
       if direction not in ["inputs", "outputs"]:
          raise ValueError(f'''direction must either be "inputs" or "outputs", but {direction} was given'''						)

       UpperCAmelCase_				:   Union[str, Any]							= '''past_key_values''' if direction == '''inputs''' else '''present'''
       UpperCAmelCase_		,   UpperCAmelCase_				:   str							= self.num_layers

       UpperCAmelCase_				:   List[Any]							= '''past_encoder_sequence'''
       UpperCAmelCase_				:   Tuple							= '''past_decoder_sequence''' if direction == '''inputs''' else '''past_decoder_sequence + sequence'''

       for i in range(__snake_case						):
          UpperCAmelCase_				:   Tuple							= {0: '''batch''', 2: decoder_sequence}
          UpperCAmelCase_				:   List[str]							= {0: '''batch''', 2: decoder_sequence}
          UpperCAmelCase_				:   Union[str, Any]							= {0: '''batch''', 2: encoder_sequence}
          UpperCAmelCase_				:   List[str]							= {0: '''batch''', 2: encoder_sequence}



def   snake_case_    (			__lowercase      ,							__lowercase      ,							__lowercase      ,							__lowercase							):
   UpperCAmelCase_				:   Optional[int]							= int((lower_bound + upper_bound) // 2							)
   if array[r] == value:
      return r
   if lower_bound >= upper_bound:
      return -1
   if array[r] < value:
      return binary_search(__lowercase      ,							r + 1      ,							__lowercase      ,							__lowercase							)
   else:
      return binary_search(__lowercase      ,							__lowercase      ,							r - 1      ,							__lowercase							)
def   snake_case_    (			__lowercase      ,							__lowercase							):
   UpperCAmelCase_				:   Union[str, Any]							= 0
   if matrix[index][0] == value:
      return [index, 0]
   while index < len(__lowercase							) and matrix[index][0] < value:
      UpperCAmelCase_				:   str							= binary_search(matrix[index]      ,							0      ,							len(matrix[index]							) - 1      ,							__lowercase							)
      if r != -1:
         return [index, r]
      index += 1
   return [-1, -1]


if __name__ == "__main__":
       import doctest

       doctest.testmod()