
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


__lowerCAmelCase					:			Union[str, Any]			      =		logging.get_logger(__name__)

__lowerCAmelCase					:			Any			      =		{
    "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 						__lowerCAmelCase	(	lowerCAmelCase_      ):



      """simple docstring"""







      A__	:			List[str]        =    '''blenderbot-small'''
      A__	:			int        =    ['''past_key_values''']
      A__	:			Optional[Any]        =    {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}




      def __init__(      self		:       List[Any]				,       _snake_case		:       Tuple=5_0265				,       _snake_case		:       Optional[Any]=512				,       _snake_case		:       List[str]=8				,       _snake_case		:       Optional[int]=2048				,       _snake_case		:       List[str]=16				,       _snake_case		:       int=8				,       _snake_case		:       List[str]=2048				,       _snake_case		:       List[Any]=16				,       _snake_case		:       List[str]=0.0				,       _snake_case		:       Any=0.0				,       _snake_case		:       Optional[int]=True				,       _snake_case		:       Union[str, Any]=True				,       _snake_case		:       List[Any]="gelu"				,       _snake_case		:       List[str]=512				,       _snake_case		:       Optional[Any]=0.1				,       _snake_case		:       Optional[int]=0.0				,       _snake_case		:       Any=0.0				,       _snake_case		:       Tuple=0.02				,       _snake_case		:       List[str]=1				,       _snake_case		:       Optional[int]=False				,       _snake_case		:       List[str]=0				,       _snake_case		:       List[str]=1				,       _snake_case		:       Optional[int]=2				,       _snake_case		:       str=2				,       **_snake_case		:       Optional[int]				,       ):
          __lowercase		:			Optional[int]	    =       vocab_size
          __lowercase		:			Any	    =       max_position_embeddings
          __lowercase		:			Any	    =       d_model
          __lowercase		:			Optional[Any]	    =       encoder_ffn_dim
          __lowercase		:			int	    =       encoder_layers
          __lowercase		:			Dict	    =       encoder_attention_heads
          __lowercase		:			Union[str, Any]	    =       decoder_ffn_dim
          __lowercase		:			Union[str, Any]	    =       decoder_layers
          __lowercase		:			List[str]	    =       decoder_attention_heads
          __lowercase		:			List[str]	    =       dropout
          __lowercase		:			Tuple	    =       attention_dropout
          __lowercase		:			Tuple	    =       activation_dropout
          __lowercase		:			Any	    =       activation_function
          __lowercase		:			List[Any]	    =       init_std
          __lowercase		:			Dict	    =       encoder_layerdrop
          __lowercase		:			int	    =       decoder_layerdrop
          __lowercase		:			List[str]	    =       use_cache
          __lowercase		:			Union[str, Any]	    =       encoder_layers
          __lowercase		:			Optional[int]	    =       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 						__lowerCAmelCase	(	lowerCAmelCase_      ):



      """simple docstring"""







      @property
      def 		snake_case_			(      self		:       Tuple				):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase		:			List[str]	    =       OrderedDict(
                  [
                      ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                      ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                  ]				)

              if self.use_past:
                  __lowercase		:			int	    =       {0: '''batch'''}
                  __lowercase		:			Union[str, Any]	    =       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
              else:
                  __lowercase		:			Dict	    =       {0: '''batch''', 1: '''decoder_sequence'''}
                  __lowercase		:			List[Any]	    =       {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.
              __lowercase		:			int	    =       OrderedDict(
                  [
                      ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                      ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                  ]				)
              if self.use_past:
                  __lowercase    ,      __lowercase		:			Optional[Any]	    =       self.num_layers
                  for i in range(_snake_case				):
                      __lowercase		:			Tuple	    =       {0: '''batch''', 2: '''past_sequence + sequence'''}
                      __lowercase		:			List[str]	    =       {0: '''batch''', 2: '''past_sequence + sequence'''}
          else:
              __lowercase		:			Union[str, 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
      def 		snake_case_			(      self		:       Tuple				):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase		:			Optional[Any]	    =       super().outputs
          else:
              __lowercase		:			Optional[Any]	    =       super(_snake_case				,       self				).outputs
              if self.use_past:
                  __lowercase    ,      __lowercase		:			Union[str, Any]	    =       self.num_layers
                  for i in range(_snake_case				):
                      __lowercase		:			Optional[int]	    =       {0: '''batch''', 2: '''past_sequence + sequence'''}
                      __lowercase		:			Tuple	    =       {0: '''batch''', 2: '''past_sequence + sequence'''}
          return common_outputs




      def 		snake_case_			(      self		:       Tuple				,       _snake_case		:       PreTrainedTokenizer				,       _snake_case		:       int = -1				,       _snake_case		:       int = -1				,       _snake_case		:       bool = False				,       _snake_case		:       Optional[TensorType] = None				,       ):
          __lowercase		:			Optional[Any]	    =       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				)

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

              __lowercase		:			Dict	    =       torch.cat(
                  [common_inputs['''decoder_attention_mask'''], torch.ones(_snake_case				,       _snake_case				)]				,       dim=1				)

              __lowercase		:			List[str]	    =       []
              # If the number of encoder and decoder layers are present in the model configuration, both are considered
              __lowercase    ,      __lowercase		:			str	    =       self.num_layers
              __lowercase		:			Union[str, Any]	    =       min(_snake_case				,       _snake_case				)
              __lowercase		:			Optional[int]	    =       max(_snake_case				,       _snake_case				) - min_num_layers
              __lowercase		:			Any	    =       '''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.
              __lowercase		:			Any	    =       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 		snake_case_			(      self		:       Dict				,       _snake_case		:       PreTrainedTokenizer				,       _snake_case		:       int = -1				,       _snake_case		:       int = -1				,       _snake_case		:       bool = False				,       _snake_case		:       Optional[TensorType] = None				,       ):
          __lowercase		:			Optional[int]	    =       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
              __lowercase    ,      __lowercase		:			Tuple	    =       common_inputs['''input_ids'''].shape
              # Not using the same length for past_key_values
              __lowercase		:			Tuple	    =       seqlen + 2
              __lowercase    ,      __lowercase		:			Union[str, Any]	    =       self.num_layers
              __lowercase    ,      __lowercase		:			Any	    =       self.num_attention_heads
              __lowercase		:			List[Any]	    =       (
                  batch,
                  num_encoder_attention_heads,
                  past_key_values_length,
                  self._config.hidden_size // num_encoder_attention_heads,
              )

              __lowercase		:			str	    =       common_inputs['''attention_mask'''].dtype
              __lowercase		:			int	    =       torch.cat(
                  [common_inputs['''attention_mask'''], torch.ones(_snake_case				,       _snake_case				,       dtype=_snake_case				)]				,       dim=1				)
              __lowercase		:			List[str]	    =       [
                  (torch.zeros(_snake_case				), torch.zeros(_snake_case				)) for _ in range(_snake_case				)
              ]
          return common_inputs




      def 		snake_case_			(      self		:       Tuple				,       _snake_case		:       PreTrainedTokenizer				,       _snake_case		:       int = -1				,       _snake_case		:       int = -1				,       _snake_case		:       bool = False				,       _snake_case		:       Optional[TensorType] = None				,       ):
          # 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
          __lowercase		:			List[Any]	    =       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
          __lowercase		:			List[Any]	    =       tokenizer.num_special_tokens_to_add(_snake_case				)
          __lowercase		:			Optional[int]	    =       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
          __lowercase		:			Tuple	    =       [''' '''.join([tokenizer.unk_token]				) * seq_length] * batch_size
          __lowercase		:			List[Any]	    =       dict(tokenizer(_snake_case				,       return_tensors=_snake_case				)				)
          return common_inputs




      def 		snake_case_			(      self		:       Union[str, Any]				,       _snake_case		:       PreTrainedTokenizer				,       _snake_case		:       int = -1				,       _snake_case		:       int = -1				,       _snake_case		:       bool = False				,       _snake_case		:       Optional[TensorType] = None				,       ):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase		:			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":
              __lowercase		:			List[str]	    =       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:
              __lowercase		:			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 		snake_case_			(      self		:       Tuple				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       Optional[Any]				,       _snake_case		:       str				,       _snake_case		:       Tuple				):
          if self.task in ["default", "seq2seq-lm"]:
              __lowercase		:			Union[str, Any]	    =       super()._flatten_past_key_values_(_snake_case				,       _snake_case				,       _snake_case				,       _snake_case				)
          else:
              __lowercase		:			Optional[int]	    =       super(_snake_case				,       self				)._flatten_past_key_values_(
                  _snake_case				,       _snake_case				,       _snake_case				,       _snake_case				)




import logging
import os
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


__lowerCAmelCase					:			int			      =		{
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "critical": logging.CRITICAL,
}

__lowerCAmelCase					:			Optional[int]			      =		logging.WARNING


def     UpperCAmelCase_  (						) ->						Optional[Any]:
    __lowercase		:			Any	    =       os.getenv('''DATASETS_VERBOSITY'''	,					__lowerCAmelCase	)
    if env_level_str:
        if env_level_str in log_levels:
            return log_levels[env_level_str]
        else:
            logging.getLogger().warning(
                F'Unknown option DATASETS_VERBOSITY={env_level_str}, '
                F'has to be one of: { ", ".join(log_levels.keys()	) }'	)
    return _default_log_level


def     UpperCAmelCase_  (						) ->						str:
    return __name__.split('''.'''	)[0]


def     UpperCAmelCase_  (						) ->						logging.Logger:
    return logging.getLogger(_get_library_name()	)


def     UpperCAmelCase_  (						) ->						None:
    # Apply our default configuration to the library root logger.
    __lowercase		:			Union[str, Any]	    =       _get_library_root_logger()
    library_root_logger.setLevel(_get_default_logging_level()	)


def     UpperCAmelCase_  (						) ->						None:
    __lowercase		:			List[Any]	    =       _get_library_root_logger()
    library_root_logger.setLevel(logging.NOTSET	)


def     UpperCAmelCase_  (						__lowerCAmelCase = None	) ->						logging.Logger:
    if name is None:
        __lowercase		:			Optional[Any]	    =       _get_library_name()
    return logging.getLogger(__lowerCAmelCase	)


def     UpperCAmelCase_  (						) ->						int:
    return _get_library_root_logger().getEffectiveLevel()


def     UpperCAmelCase_  (						__lowerCAmelCase	) ->						None:
    _get_library_root_logger().setLevel(__lowerCAmelCase	)


def     UpperCAmelCase_  (						) ->						int:
    return set_verbosity(__lowerCAmelCase	)


def     UpperCAmelCase_  (						) ->						List[str]:
    return set_verbosity(__lowerCAmelCase	)


def     UpperCAmelCase_  (						) ->						Optional[int]:
    return set_verbosity(__lowerCAmelCase	)


def     UpperCAmelCase_  (						) ->						Any:
    return set_verbosity(__lowerCAmelCase	)


def     UpperCAmelCase_  (						) ->						None:
    __lowercase		:			Tuple	    =       False


def     UpperCAmelCase_  (						) ->						None:
    __lowercase		:			Dict	    =       True


# Configure the library root logger at the module level (singleton-like)
_configure_library_root_logger()


class 						__lowerCAmelCase	:



      """simple docstring"""







      def __init__(      self		:       List[Any]				,       *_snake_case		:       List[Any]				,       **_snake_case		:       Dict				):  # pylint: disable=unused-argument
          __lowercase		:			Tuple	    =       args[0] if args else None




      def __iter__(      self		:       Tuple				):
          return iter(self._iterator				)




      def __getattr__(      self		:       Optional[int]				,       _snake_case		:       Union[str, Any]				):

          def empty_fn(*_snake_case		:       Tuple				,       **_snake_case		:       int				):  # pylint: disable=unused-argument
              return

          return empty_fn




      def __enter__(      self		:       Optional[Any]				):
          return self




      def __exit__(      self		:       Optional[Any]				,       _snake_case		:       Any				,       _snake_case		:       List[Any]				,       _snake_case		:       List[Any]				):
          return






__lowerCAmelCase					:			int			      =		True


class 						__lowerCAmelCase	:



      """simple docstring"""







      def __call__(      self		:       List[str]				,       *_snake_case		:       Dict				,       _snake_case		:       str=False				,       **_snake_case		:       List[str]				):
          if _tqdm_active and not disable:
              return tqdm_lib.tqdm(*_snake_case				,       **_snake_case				)
          else:
              return EmptyTqdm(*_snake_case				,       **_snake_case				)




      def 		snake_case_			(      self		:       Optional[Any]				,       *_snake_case		:       str				,       **_snake_case		:       List[str]				):
          __lowercase		:			Dict	    =       None
          if _tqdm_active:
              return tqdm_lib.tqdm.set_lock(*_snake_case				,       **_snake_case				)




      def 		snake_case_			(      self		:       Tuple				):
          if _tqdm_active:
              return tqdm_lib.tqdm.get_lock()






__lowerCAmelCase					:			Tuple			      =		_tqdm_cls()


def     UpperCAmelCase_  (						) ->						bool:
    global _tqdm_active
    return bool(_tqdm_active	)


def     UpperCAmelCase_  (						) ->						Union[str, Any]:
    global _tqdm_active
    __lowercase		:			Dict	    =       True





def     UpperCAmelCase_  (						) ->						Tuple:
    global _tqdm_active
    __lowercase		:			Optional[int]	    =       False


