





# 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     :			int     						=	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(    __UpperCAmelCase			):

            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(A   ), "labels": torch.tensor(A   )}






       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(lowerCAmelCase       )

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

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

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

              _UpperCamelCase														=    trainer.predict(lowerCAmelCase       )
              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       ):
       # For xla_spawn (TPUs)
       main()


if __name__ == "__main__":
       main()













import math
from decimal import Decimal, getcontext


def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase = 4_000_000       ):

       try:
              _UpperCamelCase														=    int(lowerCAmelCase       )
       except (TypeError, ValueError):
              raise TypeError('''Parameter n must be int or castable to int.'''       )
       if n <= 0:
              raise ValueError('''Parameter n must be greater than or equal to one.'''       )
       _UpperCamelCase														=    100
       _UpperCamelCase														=    (Decimal(5       ) ** Decimal(0.5       ) + 1) / Decimal(2       )

       _UpperCamelCase														=    (math.floor(math.log(n * (phi + 2)     ,							lowerCAmelCase       ) - 1       ) // 3) * 3 + 2
       _UpperCamelCase														=    Decimal(round(phi ** Decimal(index + 1       )       )       ) / (phi + 2)
       _UpperCamelCase														=    num // 2
       return int(lowerCAmelCase       )


if __name__ == "__main__":
       print(F'''{solution() = }''')






