

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


a_     :    List[str]     		=   logging.get_logger(__name__)

a_     :    Tuple     		=   {
    "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   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):
    __UpperCamelCase    ="blenderbot-small"
    __UpperCamelCase    =["past_key_values"]
    __UpperCamelCase    ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}

    def __init__(      self			:   int				,     snake_case__			:   Tuple=5_0_2_6_5				,     snake_case__			:   Dict=5_1_2				,     snake_case__			:   int=8				,     snake_case__			:   Dict=2_0_4_8				,     snake_case__			:   int=1_6				,     snake_case__			:   int=8				,     snake_case__			:   int=2_0_4_8				,     snake_case__			:   str=1_6				,     snake_case__			:   List[Any]=0.0				,     snake_case__			:   Union[str, Any]=0.0				,     snake_case__			:   Any=True				,     snake_case__			:   str=True				,     snake_case__			:   List[Any]="gelu"				,     snake_case__			:   Any=5_1_2				,     snake_case__			:   Dict=0.1				,     snake_case__			:   Any=0.0				,     snake_case__			:   Dict=0.0				,     snake_case__			:   Optional[Any]=0.02				,     snake_case__			:   int=1				,     snake_case__			:   List[Any]=False				,     snake_case__			:   List[Any]=0				,     snake_case__			:   Union[str, Any]=1				,     snake_case__			:   int=2				,     snake_case__			:   List[str]=2				,     **snake_case__			:   List[str]				,     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      vocab_size
     SCREAMING_SNAKE_CASE		    =      max_position_embeddings
     SCREAMING_SNAKE_CASE		    =      d_model
     SCREAMING_SNAKE_CASE		    =      encoder_ffn_dim
     SCREAMING_SNAKE_CASE		    =      encoder_layers
     SCREAMING_SNAKE_CASE		    =      encoder_attention_heads
     SCREAMING_SNAKE_CASE		    =      decoder_ffn_dim
     SCREAMING_SNAKE_CASE		    =      decoder_layers
     SCREAMING_SNAKE_CASE		    =      decoder_attention_heads
     SCREAMING_SNAKE_CASE		    =      dropout
     SCREAMING_SNAKE_CASE		    =      attention_dropout
     SCREAMING_SNAKE_CASE		    =      activation_dropout
     SCREAMING_SNAKE_CASE		    =      activation_function
     SCREAMING_SNAKE_CASE		    =      init_std
     SCREAMING_SNAKE_CASE		    =      encoder_layerdrop
     SCREAMING_SNAKE_CASE		    =      decoder_layerdrop
     SCREAMING_SNAKE_CASE		    =      use_cache
     SCREAMING_SNAKE_CASE		    =      encoder_layers
     SCREAMING_SNAKE_CASE		    =      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__				,     forced_eos_token_id=snake_case__				,     **snake_case__				,     )




class   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):

    @property
    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






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

      if self.use_past:
       SCREAMING_SNAKE_CASE		    =      {0: 'batch'}
       SCREAMING_SNAKE_CASE		    =      {0: 'batch', 1: 'past_decoder_sequence + sequence'}
      else:
       SCREAMING_SNAKE_CASE		    =      {0: 'batch', 1: 'decoder_sequence'}
       SCREAMING_SNAKE_CASE		    =      {0: 'batch', 1: 'decoder_sequence'}

      if self.use_past:
       self.fill_with_past_key_values_(snake_case__				,     direction='inputs'     )
     elif self.task == "causal-lm":
      # TODO: figure this case out.
      SCREAMING_SNAKE_CASE		    =      OrderedDict(
          [
              ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
              ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
          ]     )
      if self.use_past:
       SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      self.num_layers
       for i in range(snake_case__     ):
        SCREAMING_SNAKE_CASE		    =      {0: 'batch', 2: 'past_sequence + sequence'}
        SCREAMING_SNAKE_CASE		    =      {0: 'batch', 2: 'past_sequence + sequence'}
     else:
      SCREAMING_SNAKE_CASE		    =      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			:   Union[str, Any]     ):

     """simple docstring"""






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

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

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
         snake_case__				,     snake_case__				,     snake_case__				,     snake_case__				,     snake_case__     )

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

      SCREAMING_SNAKE_CASE		    =      torch.cat(
          [common_inputs['decoder_attention_mask'], torch.ones(snake_case__				,     snake_case__     )]				,     dim=1     )

      SCREAMING_SNAKE_CASE		    =      []
      # If the number of encoder and decoder layers are present in the model configuration, both are considered
      SCREAMING_SNAKE_CASE    ,						SCREAMING_SNAKE_CASE		    =      self.num_layers
      SCREAMING_SNAKE_CASE		    =      min(snake_case__				,     snake_case__     )
      SCREAMING_SNAKE_CASE		    =      max(snake_case__				,     snake_case__     ) - min_num_layers
      SCREAMING_SNAKE_CASE		    =      'encoder' if num_encoder_layers > num_decoder_layers else 'decoder'

      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__     ),
           )     )
      # TODO: test this.
      SCREAMING_SNAKE_CASE		    =      encoder_shape if remaining_side_name == 'encoder' else decoder_shape
      for _ in range(snake_case__				,     snake_case__     ):
       common_inputs["past_key_values"].append((torch.zeros(snake_case__     ), torch.zeros(snake_case__     ))     )
     return common_inputs

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

     """simple docstring"""






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

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

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

     """simple docstring"""






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

    def UpperCamelCase      (      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"]:
      SCREAMING_SNAKE_CASE		    =      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":
      SCREAMING_SNAKE_CASE		    =      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:
      SCREAMING_SNAKE_CASE		    =      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 UpperCamelCase      (      self			:   Tuple				,     snake_case__			:   List[str]				,     snake_case__			:   List[Any]				,     snake_case__			:   Dict				,     snake_case__			:   Tuple     ):

     """simple docstring"""






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




import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class   UpperCamelCase			(  SCREAMING_SNAKE_CASE ):
    __UpperCamelCase    =["image_processor", "tokenizer"]
    __UpperCamelCase    ="CLIPImageProcessor"
    __UpperCamelCase    =("CLIPTokenizer", "CLIPTokenizerFast")

    def __init__(      self			:   int				,     snake_case__			:   List[Any]=None				,     snake_case__			:   Any=None				,     **snake_case__			:   Any     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      None
     if "feature_extractor" in kwargs:
      warnings.warn(
          'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
          ' instead.'				,     snake_case__				,     )
      SCREAMING_SNAKE_CASE		    =      kwargs.pop('feature_extractor'     )

     SCREAMING_SNAKE_CASE		    =      image_processor if image_processor is not None else feature_extractor
     if image_processor is None:
      raise ValueError('You need to specify an `image_processor`.'     )
     if tokenizer is None:
      raise ValueError('You need to specify a `tokenizer`.'     )

     super().__init__(snake_case__				,     snake_case__     )

    def __call__(      self			:   List[Any]				,     snake_case__			:   Tuple=None				,     snake_case__			:   Optional[int]=None				,     snake_case__			:   List[str]=None				,     **snake_case__			:   Dict     ):

     """simple docstring"""






     if text is None and images is None:
      raise ValueError('You have to specify either text or images. Both cannot be none.'     )

     if text is not None:
      SCREAMING_SNAKE_CASE		    =      self.tokenizer(snake_case__				,     return_tensors=snake_case__				,     **snake_case__     )

     if images is not None:
      SCREAMING_SNAKE_CASE		    =      self.image_processor(snake_case__				,     return_tensors=snake_case__				,     **snake_case__     )

     if text is not None and images is not None:
      SCREAMING_SNAKE_CASE		    =      image_features.pixel_values
      return encoding
     elif text is not None:
      return encoding
     else:
      return BatchEncoding(data=dict(**snake_case__     )				,     tensor_type=snake_case__     )

    def UpperCamelCase      (      self			:   Union[str, Any]				,     *snake_case__			:   Optional[Any]				,     **snake_case__			:   Optional[int]     ):

     """simple docstring"""






     return self.tokenizer.batch_decode(*snake_case__				,     **snake_case__     )

    def UpperCamelCase      (      self			:   Dict				,     *snake_case__			:   Optional[Any]				,     **snake_case__			:   Dict     ):

     """simple docstring"""






     return self.tokenizer.decode(*snake_case__				,     **snake_case__     )

    @property
    def UpperCamelCase      (      self			:   Any     ):

     """simple docstring"""






     SCREAMING_SNAKE_CASE		    =      self.tokenizer.model_input_names
     SCREAMING_SNAKE_CASE		    =      self.image_processor.model_input_names
     return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )

    @property
    def UpperCamelCase      (      self			:   int     ):

     """simple docstring"""






     warnings.warn(
         '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'				,     snake_case__				,     )
     return self.image_processor_class




    @property
    def UpperCamelCase      (      self			:   Tuple     ):

     """simple docstring"""






     warnings.warn(
         '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'				,     snake_case__				,     )
     return self.image_processor

