





# This test is meant to be run in on an instance with TPUs like this:
#
#   python examples/pytorch/xla_spawn.py --num_cores=8 tests/test_trainer_tpu.py
#
# Replace 8 with the number of TPU cores you have.
#

import sys
from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.utils import logging


UpperCAmelCase_											=  logging.get_logger(__name__)


if is_torch_available():
   import torch
   from torch import nn
   from torch.utils.data import Dataset

   from transformers import Trainer




   class  __UpperCamelCase						(       A__						):





        def __init__(    self							,       _UpperCamelCase = 101				):
           _UpperCAmelCase											=      length





        def __len__(    self				):
           return self.length





        def __getitem__(    self							,       _UpperCamelCase				):
           return i




   class  __UpperCamelCase						:





        def __call__(    self							,       _UpperCamelCase				):
           return {"input_ids": torch.tensor(_UpperCamelCase				), "labels": torch.tensor(_UpperCamelCase				)}




   class  __UpperCamelCase						(       nn.Module						):





        def __init__(    self				):
           super().__init__()
           # Add some (unused) params otherwise DDP will complain.
           _UpperCAmelCase											=      nn.Linear(120							,       80				)





        def 		UpperCamelCase(    self							,       _UpperCamelCase							,       _UpperCamelCase=None				):
           if labels is not None:
              return torch.tensor(0.0							,       device=input_ids.device				), input_ids
           else:
              return input_ids
def     A__					(						)     ->     List[Any]:






   """simple docstring"""







   _UpperCAmelCase											=      HfArgumentParser((TrainingArguments,)	)
   sys.argv += ["--output_dir", "./examples"]
   _UpperCAmelCase											=      parser.parse_args_into_dataclasses()[0]

   logger.warning(
       F'''Process rank: {training_args.local_rank}, device: {training_args.device}, '''
       F'''tpu_num_cores: {training_args.tpu_num_cores}'''							,  )

   # Essentially, what we want to verify in the distributed case is
   # that we get all samples back, in the right order.
   # (this is crucial for prediction for instance)
   for dataset_length in [10_01, 2_56, 15]:
      _UpperCAmelCase											=      DummyDataset(SCREAMING_SNAKE_CASE_	)

      def compute_metrics(SCREAMING_SNAKE_CASE_      :			EvalPrediction	) -> Dict:
         _UpperCAmelCase											=      list(range(len(SCREAMING_SNAKE_CASE_	)	)	)
         _UpperCAmelCase											=      p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
         return {"success": success}

      _UpperCAmelCase											=      Trainer(
          model=DummyModel()							,  args=SCREAMING_SNAKE_CASE_							,  data_collator=DummyDataCollator()							,  eval_dataset=SCREAMING_SNAKE_CASE_							,  compute_metrics=SCREAMING_SNAKE_CASE_							,  )
      _UpperCAmelCase											=      trainer.evaluate()
      logger.info(SCREAMING_SNAKE_CASE_	)
      if metrics["eval_success"] is not True:
         logger.error(SCREAMING_SNAKE_CASE_	)
         exit(1	)

      _UpperCAmelCase											=      trainer.predict(SCREAMING_SNAKE_CASE_	)
      logger.info(p.metrics	)
      if p.metrics["test_success"] is not True:
         logger.error(p.metrics	)
         exit(1	)

      _UpperCAmelCase											=      2

      _UpperCAmelCase											=      trainer.evaluate()
      logger.info(SCREAMING_SNAKE_CASE_	)
      if metrics["eval_success"] is not True:
         logger.error(SCREAMING_SNAKE_CASE_	)
         exit(1	)

      _UpperCAmelCase											=      trainer.predict(SCREAMING_SNAKE_CASE_	)
      logger.info(p.metrics	)
      if p.metrics["test_success"] is not True:
         logger.error(p.metrics	)
         exit(1	)

      _UpperCAmelCase											=      None

   logger.info('''🔥 All distributed tests successful'''	)
def     A__					(						SCREAMING_SNAKE_CASE_      :			int	)     ->     str:






   """simple docstring"""







   main()


if __name__ == "__main__":
   main()






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
   from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


UpperCAmelCase_											=  logging.get_logger(__name__)

UpperCAmelCase_											=  {
    "microsoft/deberta-base": "https://huggingface.co/microsoft/deberta-base/resolve/main/config.json",
    "microsoft/deberta-large": "https://huggingface.co/microsoft/deberta-large/resolve/main/config.json",
    "microsoft/deberta-xlarge": "https://huggingface.co/microsoft/deberta-xlarge/resolve/main/config.json",
    "microsoft/deberta-base-mnli": "https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/config.json",
    "microsoft/deberta-large-mnli": "https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/config.json",
    "microsoft/deberta-xlarge-mnli": "https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/config.json",
}




class  __UpperCamelCase						(       A__						):
     __A  :     Dict            =      """deberta"""





     def __init__(    self							,       _UpperCamelCase=50265							,       _UpperCamelCase=768							,       _UpperCamelCase=12							,       _UpperCamelCase=12							,       _UpperCamelCase=3072							,       _UpperCamelCase="gelu"							,       _UpperCamelCase=0.1							,       _UpperCamelCase=0.1							,       _UpperCamelCase=512							,       _UpperCamelCase=0							,       _UpperCamelCase=0.02							,       _UpperCamelCase=1e-7							,       _UpperCamelCase=False							,       _UpperCamelCase=-1							,       _UpperCamelCase=0							,       _UpperCamelCase=True							,       _UpperCamelCase=None							,       _UpperCamelCase=0							,       _UpperCamelCase="gelu"							,       **_UpperCamelCase							,       ):
        super().__init__(**_UpperCamelCase				)

        _UpperCAmelCase											=      hidden_size
        _UpperCAmelCase											=      num_hidden_layers
        _UpperCAmelCase											=      num_attention_heads
        _UpperCAmelCase											=      intermediate_size
        _UpperCAmelCase											=      hidden_act
        _UpperCAmelCase											=      hidden_dropout_prob
        _UpperCAmelCase											=      attention_probs_dropout_prob
        _UpperCAmelCase											=      max_position_embeddings
        _UpperCAmelCase											=      type_vocab_size
        _UpperCAmelCase											=      initializer_range
        _UpperCAmelCase											=      relative_attention
        _UpperCAmelCase											=      max_relative_positions
        _UpperCAmelCase											=      pad_token_id
        _UpperCAmelCase											=      position_biased_input

        # Backwards compatibility
        if type(_UpperCamelCase				) == str:
           _UpperCAmelCase											=      [x.strip() for x in pos_att_type.lower().split('''|'''				)]

        _UpperCAmelCase											=      pos_att_type
        _UpperCAmelCase											=      vocab_size
        _UpperCAmelCase											=      layer_norm_eps

        _UpperCAmelCase											=      kwargs.get('''pooler_hidden_size'''							,       _UpperCamelCase				)
        _UpperCAmelCase											=      pooler_dropout
        _UpperCAmelCase											=      pooler_hidden_act




class  __UpperCamelCase						(       A__						):





     @property
     def 		UpperCamelCase(    self				):
        if self.task == "multiple-choice":
           _UpperCAmelCase											=      {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
        else:
           _UpperCAmelCase											=      {0: '''batch''', 1: '''sequence'''}
        if self._config.type_vocab_size > 0:
           return OrderedDict(
               [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)]				)
        else:
           return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)]				)





     @property
     def 		UpperCamelCase(    self				):
        return 12





     def 		UpperCamelCase(    self							,       _UpperCamelCase							,       _UpperCamelCase = -1							,       _UpperCamelCase = -1							,       _UpperCamelCase = -1							,       _UpperCamelCase = False							,       _UpperCamelCase = None							,       _UpperCamelCase = 3							,       _UpperCamelCase = 40							,       _UpperCamelCase = 40							,       _UpperCamelCase = None							,       ):
        _UpperCAmelCase											=      super().generate_dummy_inputs(preprocessor=_UpperCamelCase							,       framework=_UpperCamelCase				)
        if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
           del dummy_inputs["token_type_ids"]
        return dummy_inputs