

'''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


_snake_case     						=						logging.get_logger(__name__)

_snake_case     						=						{
    '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      a__						(							lowerCamelCase_						):
   _SCREAMING_SNAKE_CASE					:  Union[str, Any]			 =							'blenderbot'
   _SCREAMING_SNAKE_CASE					:  List[Any]			 =							['past_key_values']
   _SCREAMING_SNAKE_CASE					:  Optional[Any]			 =							{'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.0_2    ,       _UpperCamelCase=1    ,       _UpperCamelCase=False    ,       _UpperCamelCase=0    ,       _UpperCamelCase=1    ,       _UpperCamelCase=2    ,       _UpperCamelCase=3    ,       _UpperCamelCase=2    ,       **_UpperCamelCase    ,       ):





      """simple docstring"""





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


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





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





      """simple docstring"""





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


   def 		_lowerCamelCase     (				self    ,       _UpperCamelCase    ,       _UpperCamelCase = -1    ,       _UpperCamelCase = -1    ,       _UpperCamelCase = False    ,       _UpperCamelCase = None    ,       ):





      """simple docstring"""





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





      """simple docstring"""





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


   def 		_lowerCamelCase     (				self    ,       _UpperCamelCase    ,       _UpperCamelCase = -1    ,       _UpperCamelCase = -1    ,       _UpperCamelCase = False    ,       _UpperCamelCase = None    ,       ):





      """simple docstring"""





      _lowercase    :   Optional[Any]      =     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
      _lowercase    :   int      =     tokenizer.num_special_tokens_to_add(_UpperCamelCase						)
      _lowercase    :   int      =     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
      _lowercase    :   Union[str, Any]      =     [" ".join([tokenizer.unk_token]						) * seq_length] * batch_size
      _lowercase    :   List[str]      =     dict(tokenizer(_UpperCamelCase    ,       return_tensors=_UpperCamelCase						)						)
      return common_inputs


   def 		_lowerCamelCase     (				self    ,       _UpperCamelCase    ,       _UpperCamelCase = -1    ,       _UpperCamelCase = -1    ,       _UpperCamelCase = False    ,       _UpperCamelCase = None    ,       ):





      """simple docstring"""





      if self.task in ["default", "seq2seq-lm"]:
         _lowercase    :   List[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":
         _lowercase    :   Dict      =     self._generate_dummy_inputs_for_causal_lm(
             _UpperCamelCase    ,       batch_size=_UpperCamelCase    ,       seq_length=_UpperCamelCase    ,       is_pair=_UpperCamelCase    ,       framework=_UpperCamelCase						)
      else:
         _lowercase    :   str      =     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 		_lowerCamelCase     (				self    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase						):





      """simple docstring"""





      if self.task in ["default", "seq2seq-lm"]:
         _lowercase    :   List[str]      =     super()._flatten_past_key_values_(_UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase						)
      else:
         _lowercase    :   Tuple      =     super(_UpperCamelCase    ,       self						)._flatten_past_key_values_(
             _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase    ,       _UpperCamelCase						)






   def 		_lowerCamelCase     (				self    ,       _UpperCamelCase    ,       _UpperCamelCase						):





      """simple docstring"""





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

      _lowercase    :   Optional[int]      =     "past_key_values" if direction == "inputs" else "present"
      _lowercase  ,   _lowercase    :   Tuple      =     self.num_layers

      _lowercase    :   Optional[int]      =     "past_encoder_sequence"
      _lowercase    :   int      =     "past_decoder_sequence" if direction == "inputs" else "past_decoder_sequence + sequence"

      for i in range(_UpperCamelCase						):
         _lowercase    :   Optional[Any]      =     {0: "batch", 2: decoder_sequence}
         _lowercase    :   Optional[Any]      =     {0: "batch", 2: decoder_sequence}
         _lowercase    :   Union[str, Any]      =     {0: "batch", 2: encoder_sequence}
         _lowercase    :   int      =     {0: "batch", 2: encoder_sequence}









'''simple docstring'''
import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


_snake_case     						=						logging.get_logger(__name__)

if is_tf_available():
    import tensorflow as tf


@dataclass
class      a__						(							lowerCamelCase_						):
   _SCREAMING_SNAKE_CASE					:  int			 =							'tf'
   _SCREAMING_SNAKE_CASE					:  Optional[str]			 =							field(
       default=lowerCamelCase_						,   metadata={'help': 'Name of TPU'}						,   )

   _SCREAMING_SNAKE_CASE					:  Optional[str]			 =							field(
       default=lowerCamelCase_						,   metadata={'help': 'Zone of TPU'}						,   )

   _SCREAMING_SNAKE_CASE					:  Optional[str]			 =							field(
       default=lowerCamelCase_						,   metadata={'help': 'Name of Cloud TPU-enabled project'}						,   )

   _SCREAMING_SNAKE_CASE					:  float			 =							field(
       default=1.0						,   metadata={'help': 'Power for the Polynomial decay LR scheduler.'}						,   )

   _SCREAMING_SNAKE_CASE					:  bool			 =							field(default=lowerCamelCase_						,   metadata={'help': 'Whether to activate the XLA compilation or not'}						)


   @cached_property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      logger.info("Tensorflow: setting up strategy"						)

      _lowercase    :   Optional[int]      =     tf.config.list_physical_devices("GPU"						)

      # Set to float16 at first
      if self.fpaa:
         tf.keras.mixed_precision.set_global_policy("mixed_float16"						)

      if self.no_cuda:
         _lowercase    :   Optional[int]      =     tf.distribute.OneDeviceStrategy(device="/cpu:0"						)
      else:
         try:
            if self.tpu_name:
               _lowercase    :   int      =     tf.distribute.cluster_resolver.TPUClusterResolver(
                   self.tpu_name    ,       zone=self.tpu_zone    ,       project=self.gcp_project						)
            else:
               _lowercase    :   Any      =     tf.distribute.cluster_resolver.TPUClusterResolver()
         except ValueError:
            if self.tpu_name:
               raise RuntimeError(f'''Couldn\'t connect to TPU {self.tpu_name}!'''						)
            else:
               _lowercase    :   Tuple      =     None

         if tpu:
            # Set to bfloat16 in case of TPU
            if self.fpaa:
               tf.keras.mixed_precision.set_global_policy("mixed_bfloat16"						)

            tf.config.experimental_connect_to_cluster(_UpperCamelCase						)
            tf.tpu.experimental.initialize_tpu_system(_UpperCamelCase						)

            _lowercase    :   List[str]      =     tf.distribute.TPUStrategy(_UpperCamelCase						)

         elif len(_UpperCamelCase						) == 0:
            _lowercase    :   Optional[Any]      =     tf.distribute.OneDeviceStrategy(device="/cpu:0"						)
         elif len(_UpperCamelCase						) == 1:
            _lowercase    :   Optional[int]      =     tf.distribute.OneDeviceStrategy(device="/gpu:0"						)
         elif len(_UpperCamelCase						) > 1:
            # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
            _lowercase    :   Any      =     tf.distribute.MirroredStrategy()
         else:
            raise ValueError("Cannot find the proper strategy, please check your environment properties."						)

      return strategy


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      return self._setup_strategy


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      return self._setup_strategy.num_replicas_in_sync


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      return False  # TF Logging is handled by Keras not the Trainer


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      if self.per_gpu_train_batch_size:
         logger.warning(
             "Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future "
             "version. Using `--per_device_train_batch_size` is preferred."						)
      _lowercase    :   str      =     self.per_gpu_train_batch_size or self.per_device_train_batch_size
      return per_device_batch_size * self.n_replicas


   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      if self.per_gpu_eval_batch_size:
         logger.warning(
             "Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future "
             "version. Using `--per_device_eval_batch_size` is preferred."						)
      _lowercase    :   List[Any]      =     self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
      return per_device_batch_size * self.n_replicas






   @property
   def 		_lowerCamelCase     (				self						):





      """simple docstring"""





      requires_backends(self    ,       ["tf"]						)
      warnings.warn(
          "The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead."    ,       _UpperCamelCase    ,       )
      return self._setup_strategy.num_replicas_in_sync






