'''simple docstring'''





# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
    import torch




class        __A      ( TensorFormatter[Mapping, """torch.Tensor""", Mapping]	):



    def __init__(self    :    Union[str, Any]   ,			__a    :    Tuple=None   ,			**__a    :    List[Any] ):
         super().__init__(features=__a )
         UpperCAmelCase_           =   torch_tensor_kwargs
         import torch  # noqa import torch at initialization



    def 					_lowercase						(self    :    Optional[Any]   ,			__a    :    int ):
         import torch

         if isinstance(__a   ,			__a ) and column:
              if all(
                  isinstance(__a   ,			torch.Tensor ) and x.shape == column[0].shape and x.dtype == column[0].dtype
                  for x in column ):
                   return torch.stack(__a )
         return column



    def 					_lowercase						(self    :    Optional[Any]   ,			__a    :    Tuple ):
         import torch

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

         UpperCAmelCase_           =   {}

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

              if isinstance(__a   ,			PIL.Image.Image ):
                   UpperCAmelCase_           =   np.asarray(__a )
         return torch.tensor(__a   ,			**{**default_dtype, **self.torch_tensor_kwargs} )



    def 					_lowercase						(self    :    int   ,			__a    :    str ):
         import torch

         # support for torch, tf, jax etc.
         if hasattr(__a   ,			"__array__" ) and not isinstance(__a   ,			torch.Tensor ):
              UpperCAmelCase_           =   data_struct.__array__()
         # support for nested types like struct of list of struct
         if isinstance(__a   ,			np.ndarray ):
              if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
                   return self._consolidate([self.recursive_tensorize(__a ) for substruct in data_struct] )
         elif isinstance(__a   ,			(list, tuple) ):
              return self._consolidate([self.recursive_tensorize(__a ) for substruct in data_struct] )
         return self._tensorize(__a )



    def 					_lowercase						(self    :    Any   ,			__a    :    dict ):
         return map_nested(self._recursive_tensorize   ,			__a   ,			map_list=__a )



    def 					_lowercase						(self    :    int   ,			__a    :    pa.Table ):
         UpperCAmelCase_           =   self.numpy_arrow_extractor().extract_row(__a )
         UpperCAmelCase_           =   self.python_features_decoder.decode_row(__a )
         return self.recursive_tensorize(__a )



    def 					_lowercase						(self    :    Union[str, Any]   ,			__a    :    pa.Table ):
         UpperCAmelCase_           =   self.numpy_arrow_extractor().extract_column(__a )
         UpperCAmelCase_           =   self.python_features_decoder.decode_column(__a   ,			pa_table.column_names[0] )
         UpperCAmelCase_           =   self.recursive_tensorize(__a )
         UpperCAmelCase_           =   self._consolidate(__a )
         return column






    def 					_lowercase						(self    :    Tuple   ,			__a    :    pa.Table ):
         UpperCAmelCase_           =   self.numpy_arrow_extractor().extract_batch(__a )
         UpperCAmelCase_           =   self.python_features_decoder.decode_batch(__a )
         UpperCAmelCase_           =   self.recursive_tensorize(__a )
         for column_name in batch:
              UpperCAmelCase_           =   self._consolidate(batch[column_name] )
         return batch









"""simple docstring"""







import unittest

from accelerate import debug_launcher
from accelerate.test_utils import require_cpu, test_ops, test_script
@require_cpu
class        __A       (	unittest.TestCase    ):







		'''simple docstring'''





		def 					UpperCAmelCase    (	self						:  Optional[int]						)					->		str:






				"""simple docstring"""



				debug_launcher(test_script.main						)







		def 					UpperCAmelCase    (	self						:  Dict						)					->		Tuple:






				"""simple docstring"""



				debug_launcher(test_ops.main						)
