





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    =	logging.get_logger(__name__)

_UpperCamelCase    =	{
    '''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	(      a      ):



      """simple docstring"""






      UpperCAmelCase_  : Tuple				      ="blenderbot"
      UpperCAmelCase_  : Any				      =["past_key_values"]
      UpperCAmelCase_  : Optional[int]				      ={"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}







      def __init__(	self			,  UpperCAmelCase=8008			,  UpperCAmelCase=128			,  UpperCAmelCase=2			,  UpperCAmelCase=10240			,  UpperCAmelCase=32			,  UpperCAmelCase=24			,  UpperCAmelCase=10240			,  UpperCAmelCase=32			,  UpperCAmelCase=0.0			,  UpperCAmelCase=0.0			,  UpperCAmelCase=True			,  UpperCAmelCase=True			,  UpperCAmelCase="gelu"			,  UpperCAmelCase=2560			,  UpperCAmelCase=0.1			,  UpperCAmelCase=0.0			,  UpperCAmelCase=0.0			,  UpperCAmelCase=0.02			,  UpperCAmelCase=1			,  UpperCAmelCase=False			,  UpperCAmelCase=0			,  UpperCAmelCase=1			,  UpperCAmelCase=2			,  UpperCAmelCase=3			,  UpperCAmelCase=2			,  **UpperCAmelCase			,  )    ->				List[Any]:






        '''simple docstring'''






        __snake_case				:			List[Any]            =    vocab_size
        __snake_case				:			List[str]            =    max_position_embeddings
        __snake_case				:			str            =    d_model
        __snake_case				:			int            =    encoder_ffn_dim
        __snake_case				:			Dict            =    encoder_layers
        __snake_case				:			Any            =    encoder_attention_heads
        __snake_case				:			Optional[int]            =    decoder_ffn_dim
        __snake_case				:			int            =    decoder_layers
        __snake_case				:			Dict            =    decoder_attention_heads
        __snake_case				:			Optional[int]            =    dropout
        __snake_case				:			Optional[Any]            =    attention_dropout
        __snake_case				:			Dict            =    activation_dropout
        __snake_case				:			Any            =    activation_function
        __snake_case				:			List[Any]            =    init_std
        __snake_case				:			Any            =    encoder_layerdrop
        __snake_case				:			List[Any]            =    decoder_layerdrop
        __snake_case				:			Any            =    use_cache
        __snake_case				:			Optional[int]            =    encoder_layers
        __snake_case				:			Optional[int]            =    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	(      a      ):



      """simple docstring"""






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






        '''simple docstring'''






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






        '''simple docstring'''






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







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






        '''simple docstring'''






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






        '''simple docstring'''






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







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






        '''simple docstring'''






        __snake_case				:			Tuple            =    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
        __snake_case				:			str            =    tokenizer.num_special_tokens_to_add(UpperCAmelCase				)
        __snake_case				:			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
        __snake_case				:			Tuple            =    [" ".join([tokenizer.unk_token]				) * seq_length] * batch_size
        __snake_case				:			str            =    dict(tokenizer(UpperCAmelCase			,  return_tensors=UpperCAmelCase				)				)
        return common_inputs







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






        '''simple docstring'''






        if self.task in ["default", "seq2seq-lm"]:
          __snake_case				:			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":
          __snake_case				:			Union[str, Any]            =    self._generate_dummy_inputs_for_causal_lm(
              UpperCAmelCase			,  batch_size=UpperCAmelCase			,  seq_length=UpperCAmelCase			,  is_pair=UpperCAmelCase			,  framework=UpperCAmelCase				)
        else:
          __snake_case				:			Dict            =    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   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)    ->				int:






        '''simple docstring'''






        if self.task in ["default", "seq2seq-lm"]:
          __snake_case				:			str            =    super()._flatten_past_key_values_(UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)
        else:
          __snake_case				:			int            =    super(UpperCAmelCase			,  self				)._flatten_past_key_values_(
              UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase				)    ->				Any:






        '''simple docstring'''






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

        __snake_case				:			Tuple            =    "past_key_values" if direction == "inputs" else "present"
        __snake_case      ,				__snake_case				:			Any            =    self.num_layers

        __snake_case				:			int            =    "past_encoder_sequence"
        __snake_case				:			int            =    "past_decoder_sequence" if direction == "inputs" else "past_decoder_sequence + sequence"

        for i in range(UpperCAmelCase				):
          __snake_case				:			Any            =    {0: "batch", 2: decoder_sequence}
          __snake_case				:			List[Any]            =    {0: "batch", 2: decoder_sequence}
          __snake_case				:			Tuple            =    {0: "batch", 2: encoder_sequence}
          __snake_case				:			int            =    {0: "batch", 2: encoder_sequence}








from typing import TYPE_CHECKING

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


_UpperCamelCase    =	{
    '''configuration_dpr''': ['''DPR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPRConfig'''],
    '''tokenization_dpr''': [
        '''DPRContextEncoderTokenizer''',
        '''DPRQuestionEncoderTokenizer''',
        '''DPRReaderOutput''',
        '''DPRReaderTokenizer''',
    ],
}


try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase    =	[
         '''DPRContextEncoderTokenizerFast''',
         '''DPRQuestionEncoderTokenizerFast''',
         '''DPRReaderTokenizerFast''',
     ]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase    =	[
         '''DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''DPRContextEncoder''',
         '''DPRPretrainedContextEncoder''',
         '''DPRPreTrainedModel''',
         '''DPRPretrainedQuestionEncoder''',
         '''DPRPretrainedReader''',
         '''DPRQuestionEncoder''',
         '''DPRReader''',
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase    =	[
         '''TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''TFDPRContextEncoder''',
         '''TFDPRPretrainedContextEncoder''',
         '''TFDPRPretrainedQuestionEncoder''',
         '''TFDPRPretrainedReader''',
         '''TFDPRQuestionEncoder''',
         '''TFDPRReader''',
     ]


if TYPE_CHECKING:
     from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
     from .tokenization_dpr import (
         DPRContextEncoderTokenizer,
         DPRQuestionEncoderTokenizer,
         DPRReaderOutput,
         DPRReaderTokenizer,
     )

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_dpr_fast import (
              DPRContextEncoderTokenizerFast,
              DPRQuestionEncoderTokenizerFast,
              DPRReaderTokenizerFast,
          )

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_dpr import (
              DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
              DPRContextEncoder,
              DPRPretrainedContextEncoder,
              DPRPreTrainedModel,
              DPRPretrainedQuestionEncoder,
              DPRPretrainedReader,
              DPRQuestionEncoder,
              DPRReader,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_dpr import (
              TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFDPRContextEncoder,
              TFDPRPretrainedContextEncoder,
              TFDPRPretrainedQuestionEncoder,
              TFDPRPretrainedReader,
              TFDPRQuestionEncoder,
              TFDPRReader,
          )

else:
     import sys

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

