






'''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]						):
     '''simple docstring'''







     def __init__(self    :	Tuple				,				lowercase__    :	str=None				,				**lowercase__    :	Any					)  ->       Optional[int]:
           super().__init__(features=lowercase__					)
           __A		   =      np_array_kwargs






     def     _UpperCamelCase   (self    :	Optional[Any]				,				lowercase__    :	Tuple					)  ->       Union[str, Any]:






           if isinstance(lowercase__				,				lowercase__					):
                 if column and all(
                     isinstance(lowercase__				,				np.ndarray					) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column					):
                       return np.stack(lowercase__					)
                 else:
                       # don't use np.array(column, dtype=object)
                       # since it fails in certain cases
                       # see https://stackoverflow.com/q/51005699
                       __A		   =      np.empty(len(lowercase__					)				,				dtype=lowercase__					)
                       __A		   =      column
                       return out
           return column






     def     _UpperCamelCase   (self    :	Optional[Any]				,				lowercase__    :	int					)  ->       Union[str, Any]:






           if isinstance(lowercase__				,				(str, bytes, type(lowercase__					))					):
                 return value
           elif isinstance(lowercase__				,				(np.character, np.ndarray)					) and np.issubdtype(value.dtype				,				np.character					):
                 return value
           elif isinstance(lowercase__				,				np.number					):
                 return value

           __A		   =      {}

           if isinstance(lowercase__				,				np.ndarray					) and np.issubdtype(value.dtype				,				np.integer					):
                 __A		   =      {"dtype": np.intaa}
           elif isinstance(lowercase__				,				np.ndarray					) and np.issubdtype(value.dtype				,				np.floating					):
                 __A		   =      {"dtype": np.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                 import PIL.Image

                 if isinstance(lowercase__				,				PIL.Image.Image					):
                       return np.asarray(lowercase__				,				**self.np_array_kwargs					)

           return np.asarray(lowercase__				,				**{**default_dtype, **self.np_array_kwargs}					)






     def     _UpperCamelCase   (self    :	Optional[Any]				,				lowercase__    :	int					)  ->       List[str]:






           if config.TORCH_AVAILABLE and "torch" in sys.modules:
                 import torch

                 if isinstance(lowercase__				,				torch.Tensor					):
                       return self._tensorize(data_struct.detach().cpu().numpy()[()]					)
           if hasattr(lowercase__				,				"__array__"					) and not isinstance(lowercase__				,				(np.ndarray, np.character, np.number)					):
                 __A		   =      data_struct.__array__()
           # support for nested types like struct of list of struct
           if isinstance(lowercase__				,				np.ndarray					):
                 if data_struct.dtype == object:
                       return self._consolidate([self.recursive_tensorize(lowercase__					) for substruct in data_struct]					)
           if isinstance(lowercase__				,				(list, tuple)					):
                 return self._consolidate([self.recursive_tensorize(lowercase__					) for substruct in data_struct]					)
           return self._tensorize(lowercase__					)






     def     _UpperCamelCase   (self    :	int				,				lowercase__    :	str					)  ->       Optional[Any]:
           return map_nested(self._recursive_tensorize				,				lowercase__				,				map_list=lowercase__					)






     def     _UpperCamelCase   (self    :	List[str]				,				lowercase__    :	Dict					)  ->       Optional[int]:
           __A		   =      self.numpy_arrow_extractor().extract_row(lowercase__					)
           __A		   =      self.python_features_decoder.decode_row(lowercase__					)
           return self.recursive_tensorize(lowercase__					)






     def     _UpperCamelCase   (self    :	Tuple				,				lowercase__    :	Dict					)  ->       Tuple:
           __A		   =      self.numpy_arrow_extractor().extract_column(lowercase__					)
           __A		   =      self.python_features_decoder.decode_column(lowercase__				,				pa_table.column_names[0]					)
           __A		   =      self.recursive_tensorize(lowercase__					)
           __A		   =      self._consolidate(lowercase__					)
           return column






     def     _UpperCamelCase   (self    :	List[str]				,				lowercase__    :	Tuple					)  ->       str:
           __A		   =      self.numpy_arrow_extractor().extract_batch(lowercase__					)
           __A		   =      self.python_features_decoder.decode_batch(lowercase__					)
           __A		   =      self.recursive_tensorize(lowercase__					)
           for column_name in batch:
                 __A		   =      self._consolidate(batch[column_name]					)
           return batch









'''simple docstring'''







import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp


def 						_UpperCamelCase							(     ):
      __A		   =      ArgumentParser(
          description=(
              "PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes"
          )							)

      # Optional arguments for the launch helper
      parser.add_argument("--num_cores"							,			type=snake_case__							,			default=1							,			help="Number of TPU cores to use (1 or 8)."							)

      # positional
      parser.add_argument(
          "training_script"							,			type=snake_case__							,			help=(
              "The full path to the single TPU training "
              "program/script to be launched in parallel, "
              "followed by all the arguments for the "
              "training script"
          )							,			)

      # rest from the training program
      parser.add_argument("training_script_args"							,			nargs=snake_case__							)

      return parser.parse_args()


def 						_UpperCamelCase							(     ):
      __A		   =      parse_args()

      # Import training_script as a module.
      __A		   =      Path(args.training_script							)
      sys.path.append(str(script_fpath.parent.resolve()							)							)
      __A		   =      script_fpath.stem
      __A		   =      importlib.import_module(snake_case__							)

      # Patch sys.argv
      __A		   =      [args.training_script] + args.training_script_args + ["--tpu_num_cores", str(args.num_cores							)]

      xmp.spawn(mod._mp_fn							,			args=()							,			nprocs=args.num_cores							)


if __name__ == "__main__":
       main()


