





"""simple docstring"""


# 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


a__       :  Any       		=					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   lowercase_					(  __lowerCAmelCase				):

          def __init__(      self ,       __SCREAMING_SNAKE_CASE = 101  ):






           '''simple docstring'''

           snake_case__     				=					length

          def __len__(      self  ):






           '''simple docstring'''

           return self.length

          def __getitem__(      self ,       __SCREAMING_SNAKE_CASE  ):






           '''simple docstring'''

           return i



   class   lowercase_					:

          def __call__(      self ,       __SCREAMING_SNAKE_CASE  ):






           '''simple docstring'''

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



   class   lowercase_					(  nn.Module				):

          def __init__(      self  ):






           '''simple docstring'''

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

          def 		_SCREAMING_SNAKE_CASE(      self ,       __SCREAMING_SNAKE_CASE ,       __SCREAMING_SNAKE_CASE=None  ):






           '''simple docstring'''

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





def 				a__      (	)     ->	int:

 '''simple docstring'''






 snake_case__     				=					HfArgumentParser((TrainingArguments,)						)
 sys.argv += ["--output_dir", "./examples"]
 snake_case__     				=					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_0_0_1, 2_5_6, 1_5]:
  snake_case__     				=					DummyDataset(_UpperCAmelCase						)

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

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

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

  snake_case__     				=					2

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

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

  snake_case__     				=					None

 logger.info("🔥 All distributed tests successful"						)





def 				a__      (	_UpperCAmelCase						)     ->	Optional[int]:

 '''simple docstring'''






 main()


if __name__ == "__main__":
   main()








"""simple docstring"""


def 				a__      (	_UpperCAmelCase						)     ->	int:

 '''simple docstring'''






 snake_case__     				=					0
 for plain_chr in plain_text:
  snake_case__     				=					(
      ord(_UpperCAmelCase						) + (hash_value << 6) + (hash_value << 1_6) - hash_value
  )
 return hash_value

