





# 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


lowercase     :			List[str]     						=	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 			__A(    lowerCamelCase_			):

            def __init__(   self,							A = 101   ):





                   """simple docstring"""

                   _UpperCamelCase														=    length

            def __len__(   self   ):





                   """simple docstring"""

                   return self.length



            def __getitem__(   self,							A   ):





                   """simple docstring"""

                   return i






       class 			__A:

            def __call__(   self,							A   ):





                   """simple docstring"""

                   return {"input_ids": torch.tensor(_lowercase   ), "labels": torch.tensor(_lowercase   )}






       class 			__A(    nn.Module			):

            def __init__(   self   ):





                   """simple docstring"""

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



            def 						_UpperCamelCase  (   self,							A,							A=None   ):





                   """simple docstring"""

                   if labels is not None:
                          return torch.tensor(0.0,							device=input_ids.device   ), input_ids
                   else:
                          return input_ids


def      SCREAMING_SNAKE_CASE      (   ):
       _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 [1_001, 256, 15]:
              _UpperCamelCase														=    DummyDataset(__UpperCamelCase       )

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

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

              _UpperCamelCase														=    trainer.predict(__UpperCamelCase       )
              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(__UpperCamelCase       )
              if metrics["eval_success"] is not True:
                     logger.error(__UpperCamelCase       )
                     exit(1       )

              _UpperCamelCase														=    trainer.predict(__UpperCamelCase       )
              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      SCREAMING_SNAKE_CASE      (   lowerCAmelCase       ):
       main()


if __name__ == "__main__":
       main()







import unittest

from transformers import is_torch_available, load_tool

from .test_tools_common import ToolTesterMixin


if is_torch_available():
   import torch







class __snake_case							(		unittest.TestCase  ,						lowerCamelCase_						):




   def       __a		(       self     :							Dict ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= load_tool("""speech-to-text""" )
     self.tool.setup()




   def       __a		(       self     :							Tuple ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= self.tool(torch.ones(30_00 ) )
     self.assertEqual(_lowercase			,      """ you""" )




   def       __a		(       self     :							Any ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__     				= self.tool(audio=torch.ones(30_00 ) )
     self.assertEqual(_lowercase			,      """ you""" )
