



'''simple docstring'''


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


lowercase							       =			logging.get_logger(__name__)

lowercase							       =			{
    '''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	(		__a):


 '''simple docstring'''




 __magic_name__					: Optional[Any]  =      "blenderbot-small"
 __magic_name__					: List[Any]  =      ["past_key_values"]
 __magic_name__					: Union[str, Any]  =      {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}




 def __init__(   self							,  lowerCAmelCase_=5_0_2_6_5							,  lowerCAmelCase_=5_1_2							,  lowerCAmelCase_=8							,  lowerCAmelCase_=2_0_4_8							,  lowerCAmelCase_=1_6							,  lowerCAmelCase_=8							,  lowerCAmelCase_=2_0_4_8							,  lowerCAmelCase_=1_6							,  lowerCAmelCase_=0.0							,  lowerCAmelCase_=0.0							,  lowerCAmelCase_=True							,  lowerCAmelCase_=True							,  lowerCAmelCase_="gelu"							,  lowerCAmelCase_=5_1_2							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_=0.0							,  lowerCAmelCase_=0.0							,  lowerCAmelCase_=0.0_2							,  lowerCAmelCase_=1							,  lowerCAmelCase_=False							,  lowerCAmelCase_=0							,  lowerCAmelCase_=1							,  lowerCAmelCase_=2							,  lowerCAmelCase_=2							,  **lowerCAmelCase_							,  ) ->      List[Any]:



      """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=lowerCAmelCase_							,  bos_token_id=lowerCAmelCase_							,  eos_token_id=lowerCAmelCase_							,  is_encoder_decoder=lowerCAmelCase_							,  decoder_start_token_id=lowerCAmelCase_							,  forced_eos_token_id=lowerCAmelCase_							,  **lowerCAmelCase_							,  )







class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Mapping[str, Mapping[int, str]]:



      """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_(lowerCAmelCase_							,  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(lowerCAmelCase_):
                     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       lowercase_  (   self) ->      Mapping[str, Mapping[int, str]]:



      """simple docstring"""







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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = False							,  lowerCAmelCase_ = None							,  ) ->      Mapping[str, Any]:



      """simple docstring"""







      a_       		=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_)

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

      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(lowerCAmelCase_							,  lowerCAmelCase_)]							,  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(lowerCAmelCase_							,  lowerCAmelCase_)
           a_       		=max(lowerCAmelCase_							,  lowerCAmelCase_) - min_num_layers
           a_       		="encoder" if num_encoder_layers > num_decoder_layers else "decoder"

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = False							,  lowerCAmelCase_ = None							,  ) ->      Mapping[str, Any]:



      """simple docstring"""







      a_       		=self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
          lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_)

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = False							,  lowerCAmelCase_ = None							,  ) ->      Mapping[str, Any]:



      """simple docstring"""







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

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




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = -1							,  lowerCAmelCase_ = False							,  lowerCAmelCase_ = None							,  ) ->      Mapping[str, Any]:



      """simple docstring"""







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

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

      return common_inputs




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_							,  lowerCAmelCase_) ->      Tuple:



      """simple docstring"""







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







'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


lowercase							       =			{
    '''configuration_vision_encoder_decoder''': ['''VisionEncoderDecoderConfig''', '''VisionEncoderDecoderOnnxConfig''']
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase							       =			['''VisionEncoderDecoderModel''']

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase							       =			['''TFVisionEncoderDecoderModel''']

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  lowercase							       =			['''FlaxVisionEncoderDecoderModel''']

if TYPE_CHECKING:
  from .configuration_vision_encoder_decoder import VisionEncoderDecoderConfig, VisionEncoderDecoderOnnxConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_vision_encoder_decoder import VisionEncoderDecoderModel

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_vision_encoder_decoder import TFVisionEncoderDecoderModel

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_vision_encoder_decoder import FlaxVisionEncoderDecoderModel

else:
  import sys

  lowercase							       =			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


