code: 
  '''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
  
  
  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   a_  (_a    ):
  
  
  
  
              def __init__(     self   ,   snake_case_ = 1_0_1  ):
                _lowerCAmelCase       :  Union[str, Any]   				=					length
  
  
  
  
              def __len__(     self  ):
                return self.length
  
  
  
  
  
  
  
              def __getitem__(     self   ,   snake_case_  ):
                return i
  
  
  
       class   a_  :
  
  
  
  
              def __call__(     self   ,   snake_case_  ):
                return {"input_ids": torch.tensor(snake_case_  ), "labels": torch.tensor(snake_case_  )}
  
  
  
       class   a_  (nn.Module    ):
  
  
  
  
              def __init__(     self  ):
                super().__init__()
                # Add some (unused) params otherwise DDP will complain.
                _lowerCAmelCase       :  List[Any]   				=					nn.Linear(1_2_0   ,   8_0  )
  
  
  
  
  
  
  
              def 							__UpperCamelCase       (     self   ,   snake_case_   ,   snake_case_=None  ):
  
  
  
  
  
  
  
                if labels is not None:
                  return torch.tensor(0.0   ,   device=input_ids.device  ), input_ids
                else:
                  return input_ids
  
  
  def 	_UpperCAmelCase				(  )    ->						Optional[int]:
    _lowerCAmelCase       :  str   				=					HfArgumentParser((TrainingArguments,)		)
    sys.argv += ["--output_dir", "./examples"]
    _lowerCAmelCase       :  Tuple   				=					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 [10_01, 2_56, 15]:
      _lowerCAmelCase       :  Optional[int]   				=					DummyDataset(__snake_case		)
  
      def compute_metrics(_lowerCamelCase						:       int		) -> Dict:
        _lowerCAmelCase       :  List[Any]   				=					list(range(len(__snake_case		)		)		)
        _lowerCAmelCase       :  int   				=					p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
        return {"success": success}
  
      _lowerCAmelCase       :  Dict   				=					Trainer(
          model=DummyModel()  ,      args=__snake_case  ,      data_collator=DummyDataCollator()  ,      eval_dataset=__snake_case  ,      compute_metrics=__snake_case  ,      )
      _lowerCAmelCase       :  List[str]   				=					trainer.evaluate()
      logger.info(__snake_case		)
      if metrics["eval_success"] is not True:
        logger.error(__snake_case		)
        exit(1		)
  
      _lowerCAmelCase       :  Dict   				=					trainer.predict(__snake_case		)
      logger.info(p.metrics		)
      if p.metrics["test_success"] is not True:
        logger.error(p.metrics		)
        exit(1		)
  
      _lowerCAmelCase       :  str   				=					2
  
      _lowerCAmelCase       :  List[Any]   				=					trainer.evaluate()
      logger.info(__snake_case		)
      if metrics["eval_success"] is not True:
        logger.error(__snake_case		)
        exit(1		)
  
      _lowerCAmelCase       :  Tuple   				=					trainer.predict(__snake_case		)
      logger.info(p.metrics		)
      if p.metrics["test_success"] is not True:
        logger.error(p.metrics		)
        exit(1		)
  
      _lowerCAmelCase       :  Optional[int]   				=					None
  
    logger.info("""🔥 All distributed tests successful"""		)
  
  
  def 	_UpperCAmelCase				(  _lowerCamelCase						:       Union[str, Any]		)    ->						Tuple:
    # For xla_spawn (TPUs)
    main()
  
  
  if __name__ == "__main__":
       main()
  
  
  
style_context: 
  '''simple docstring'''
  
  import sys
  from collections.abc import Mapping
  
  import numpy as np
  import pyarrow as pa
  
  from .. import config
  from ..utils.py_utils import map_nested
  from .formatting import TensorFormatter
  
  
  class   a_  (TensorFormatter[Mapping, np.ndarray, Mapping]    ):
  
  
  
  
         def __init__(     self   ,   snake_case_=None   ,   **snake_case_  ):
           super().__init__(features=snake_case_  )
           _lowerCAmelCase       :  Any   				=					np_array_kwargs
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           if isinstance(snake_case_   ,   snake_case_  ):
             if column and all(
                 isinstance(snake_case_   ,   np.ndarray  ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column  ):
               return np.stack(snake_case_  )
             else:
               # don't use np.array(column, dtype=object)
               # since it fails in certain cases
               # see https://stackoverflow.com/q/51005699
               _lowerCAmelCase       :  str   				=					np.empty(len(snake_case_  )   ,   dtype=snake_case_  )
               _lowerCAmelCase       :  List[Any]   				=					column
               return out
           return column
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           if isinstance(snake_case_   ,   (str, bytes, type(snake_case_  ))  ):
             return value
           elif isinstance(snake_case_   ,   (np.character, np.ndarray)  ) and np.issubdtype(value.dtype   ,   np.character  ):
             return value
           elif isinstance(snake_case_   ,   np.number  ):
             return value
  
           _lowerCAmelCase       :  List[Any]   				=					{}
  
           if isinstance(snake_case_   ,   np.ndarray  ) and np.issubdtype(value.dtype   ,   np.integer  ):
             _lowerCAmelCase       :  Union[str, Any]   				=					{"""dtype""": np.intaa}
           elif isinstance(snake_case_   ,   np.ndarray  ) and np.issubdtype(value.dtype   ,   np.floating  ):
             _lowerCAmelCase       :  Tuple   				=					{"""dtype""": np.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
             import PIL.Image
  
             if isinstance(snake_case_   ,   PIL.Image.Image  ):
               return np.asarray(snake_case_   ,   **self.np_array_kwargs  )
  
           return np.asarray(snake_case_   ,   **{**default_dtype, **self.np_array_kwargs}  )
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           # support for torch, tf, jax etc.
           if config.TORCH_AVAILABLE and "torch" in sys.modules:
             import torch
  
             if isinstance(snake_case_   ,   torch.Tensor  ):
               return self._tensorize(data_struct.detach().cpu().numpy()[()]  )
           if hasattr(snake_case_   ,   """__array__"""  ) and not isinstance(snake_case_   ,   (np.ndarray, np.character, np.number)  ):
             _lowerCAmelCase       :  List[Any]   				=					data_struct.__array__()
           # support for nested types like struct of list of struct
           if isinstance(snake_case_   ,   np.ndarray  ):
             if data_struct.dtype == object:
               return self._consolidate([self.recursive_tensorize(snake_case_  ) for substruct in data_struct]  )
           if isinstance(snake_case_   ,   (list, tuple)  ):
             return self._consolidate([self.recursive_tensorize(snake_case_  ) for substruct in data_struct]  )
           return self._tensorize(snake_case_  )
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           return map_nested(self._recursive_tensorize   ,   snake_case_   ,   map_list=snake_case_  )
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           _lowerCAmelCase       :  Optional[Any]   				=					self.numpy_arrow_extractor().extract_row(snake_case_  )
           _lowerCAmelCase       :  Any   				=					self.python_features_decoder.decode_row(snake_case_  )
           return self.recursive_tensorize(snake_case_  )
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           _lowerCAmelCase       :  List[Any]   				=					self.numpy_arrow_extractor().extract_column(snake_case_  )
           _lowerCAmelCase       :  Tuple   				=					self.python_features_decoder.decode_column(snake_case_   ,   pa_table.column_names[0]  )
           _lowerCAmelCase       :  Optional[int]   				=					self.recursive_tensorize(snake_case_  )
           _lowerCAmelCase       :  Optional[Any]   				=					self._consolidate(snake_case_  )
           return column
  
  
  
  
  
  
  
         def 							__UpperCamelCase       (     self   ,   snake_case_  ):
           _lowerCAmelCase       :  List[Any]   				=					self.numpy_arrow_extractor().extract_batch(snake_case_  )
           _lowerCAmelCase       :  str   				=					self.python_features_decoder.decode_batch(snake_case_  )
           _lowerCAmelCase       :  Dict   				=					self.recursive_tensorize(snake_case_  )
           for column_name in batch:
             _lowerCAmelCase       :  int   				=					self._consolidate(batch[column_name]  )
           return batch
  
  
  