





# 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    (		lowerCAmelCase_   ):

            def __init__(						self:       Dict    ,      _SCREAMING_SNAKE_CASE:       int = 101						)				->       Optional[int]:




                   """simple docstring"""


                   UpperCamelCase_      				=     length

            def __len__(						self:       List[str]						)				->       str:




                   """simple docstring"""


                   return self.length



            def __getitem__(						self:       Dict    ,      _SCREAMING_SNAKE_CASE:       Any						)				->       int:




                   """simple docstring"""


                   return i



     class 							_UpperCamelCase    :

            def __call__(						self:       Union[str, Any]    ,      _SCREAMING_SNAKE_CASE:       int						)				->       Optional[Any]:




                   """simple docstring"""


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



     class 							_UpperCamelCase    (		nn.Module   ):

            def __init__(						self:       Optional[Any]						)				->       List[Any]:




                   """simple docstring"""


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



            def lowercase       (						self:       Optional[int]    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]    ,      _SCREAMING_SNAKE_CASE:       List[str]=None						)				->       Optional[int]:




                   """simple docstring"""


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




def        lowerCAmelCase_       (							)   ->						Tuple:
       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 [1001, 256, 15]:
              UpperCamelCase_      				=     DummyDataset(UpperCamelCase_     )

              def compute_metrics(UpperCamelCase_     ) -> 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        lowerCAmelCase_       (							UpperCamelCase_     )   ->						List[Any]:
       # For xla_spawn (TPUs)
       main()


if __name__ == "__main__":
     main()








def        lowerCAmelCase_       (							UpperCamelCase_    ,						UpperCamelCase_     )   ->						list:
       UpperCamelCase_      				=     sorted(UpperCamelCase_    ,						key=lambda UpperCamelCase_     : value[2]    ,						reverse=UpperCamelCase_     )

       # Create a list of size equal to the maximum deadline
       # and initialize it with -1
       UpperCamelCase_      				=     max(UpperCamelCase_    ,						key=lambda UpperCamelCase_     : value[1]     )[1]
       UpperCamelCase_      				=     [-1] * max_deadline

       # Finding the maximum profit and the count of jobs
       UpperCamelCase_      				=     0
       UpperCamelCase_      				=     0
       for job in jobs:
              # Find a free time slot for this job
              # (Note that we start from the last possible slot)
              for i in range(job[1] - 1    ,						-1    ,						-1     ):
                     if time_slots[i] == -1:
                            UpperCamelCase_      				=     job[0]
                            count += 1
                            max_profit += job[2]
                            break
       return [count, max_profit]


if __name__ == "__main__":
     import doctest

     doctest.testmod()

