code: 
  
  
  
  # 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
  
  
  __lowerCamelCase  				=					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 		_snake_case      (			lowerCamelCase				):
  
  
  
  
  
       """simple docstring"""
  
  
  
       def __init__(						self	,    a = 1_0_1					)	->							Tuple:
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         _A           =    length
       def __len__(						self					)	->							Dict:
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         return self.length
  
  
  
       def __getitem__(						self	,    a					)	->							int:
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         return i
  
  
  
  
  
  
  
      class 		_snake_case      :
  
  
  
  
  
       """simple docstring"""
  
  
  
       def __call__(						self	,    a					)	->							List[str]:
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         return {"input_ids": torch.tensor(a					), "labels": torch.tensor(a					)}
  
  
  
  
  
  
  
      class 		_snake_case      (			nn.Module				):
  
  
  
  
  
       """simple docstring"""
  
  
  
       def __init__(						self					)	->							Optional[Any]:
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         super().__init__()
         # Add some (unused) params otherwise DDP will complain.
         _A           =    nn.Linear(1_2_0	,    8_0					)
  
  
  
       def lowercase_		(						self	,    a	,    a=None					)	->							Tuple:
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
  
         if labels is not None:
           return torch.tensor(0.0	,    device=input_ids.device					), input_ids
         else:
           return input_ids
  
  
  
  
  def     UpperCAmelCase__ (							):
    _A           =    HfArgumentParser((TrainingArguments,)							)
    sys.argv += ["--output_dir", "./examples"]
    _A           =    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]:
      _A           =    DummyDataset(__snake_case							)
  
      def compute_metrics(__snake_case							) -> Dict:
        _A           =    list(range(len(__snake_case							)							)							)
        _A           =    p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
        return {"success": success}
  
      _A           =    Trainer(
          model=DummyModel()						,				args=__snake_case						,				data_collator=DummyDataCollator()						,				eval_dataset=__snake_case						,				compute_metrics=__snake_case						,				)
      _A           =    trainer.evaluate()
      logger.info(__snake_case							)
      if metrics["eval_success"] is not True:
        logger.error(__snake_case							)
        exit(1							)
  
      _A           =    trainer.predict(__snake_case							)
      logger.info(p.metrics							)
      if p.metrics["test_success"] is not True:
        logger.error(p.metrics							)
        exit(1							)
  
      _A           =    2
  
      _A           =    trainer.evaluate()
      logger.info(__snake_case							)
      if metrics["eval_success"] is not True:
        logger.error(__snake_case							)
        exit(1							)
  
      _A           =    trainer.predict(__snake_case							)
      logger.info(p.metrics							)
      if p.metrics["test_success"] is not True:
        logger.error(p.metrics							)
        exit(1							)
  
      _A           =    None
  
    logger.info('''🔥 All distributed tests successful'''							)
  
  
  
  
  def     UpperCAmelCase__ (							__snake_case							):
    # For xla_spawn (TPUs)
    main()
  
  
  if __name__ == "__main__":
      main()
style_context: 
  
  
  
  def     UpperCAmelCase__ (							__snake_case						,				__snake_case = 1_000							)   -> str:
    if (not isinstance(__snake_case						,				__snake_case							)) or (digit_position <= 0):
      raise ValueError('''Digit position must be a positive integer'''							)
    elif (not isinstance(__snake_case						,				__snake_case							)) or (precision < 0):
      raise ValueError('''Precision must be a nonnegative integer'''							)
  
    # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
    # accurate
    _A           =    (
        4 * _subsum(__snake_case						,				1						,				__snake_case							)
        - 2 * _subsum(__snake_case						,				4						,				__snake_case							)
        - _subsum(__snake_case						,				5						,				__snake_case							)
        - _subsum(__snake_case						,				6						,				__snake_case							)
    )
  
    # return the first hex digit of the fractional part of the result
    return hex(int((sum_result % 1) * 16							)							)[2:]
  
  
  
  
  def     UpperCAmelCase__ (							__snake_case						,				__snake_case						,				__snake_case							)   -> float:
    _A           =    0.0
    for sum_index in range(digit_pos_to_extract + precision							):
      _A           =    8 * sum_index + denominator_addend
      if sum_index < digit_pos_to_extract:
        # if the exponential term is an integer and we mod it by the denominator
        # before dividing, only the integer part of the sum will change;
        # the fractional part will not
        _A           =    pow(
            16						,				digit_pos_to_extract - 1 - sum_index						,				__snake_case							)
      else:
        _A           =    pow(16						,				digit_pos_to_extract - 1 - sum_index							)
      total += exponential_term / denominator
    return total
  
  
  if __name__ == "__main__":
      import doctest
  
      doctest.testmod()