




'''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 					lowercase_			(	TensorFormatter[Mapping, np.ndarray, Mapping]       ):





     def __init__(    self    :				Optional[Any]    ,       __lowerCamelCase    :				List[Any]=None    ,       **__lowerCamelCase    :				str ):
           super().__init__(features=__lowerCamelCase )
           snake_case__					:      Tuple  			=						np_array_kwargs





     def  _lowerCAmelCase			(    self    :				Optional[int]    ,       __lowerCamelCase    :				Any ):
           if isinstance(__lowerCamelCase    ,       __lowerCamelCase ):
                 if column and all(
                     isinstance(__lowerCamelCase    ,       np.ndarray ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ):
                       return np.stack(__lowerCamelCase )
                 else:
                       # don't use np.array(column, dtype=object)
                       # since it fails in certain cases
                       # see https://stackoverflow.com/q/51005699
                       snake_case__					:      Dict  			=						np.empty(len(__lowerCamelCase )    ,       dtype=__lowerCamelCase )
                       snake_case__					:      str  			=						column
                       return out
           return column





     def  _lowerCAmelCase			(    self    :				Union[str, Any]    ,       __lowerCamelCase    :				str ):
           if isinstance(__lowerCamelCase    ,       (str, bytes, type(__lowerCamelCase )) ):
                 return value
           elif isinstance(__lowerCamelCase    ,       (np.character, np.ndarray) ) and np.issubdtype(value.dtype    ,       np.character ):
                 return value
           elif isinstance(__lowerCamelCase    ,       np.number ):
                 return value

           snake_case__					:      List[str]  			=						{}

           if isinstance(__lowerCamelCase    ,       np.ndarray ) and np.issubdtype(value.dtype    ,       np.integer ):
                 snake_case__					:      List[Any]  			=						{'dtype': np.intaa}
           elif isinstance(__lowerCamelCase    ,       np.ndarray ) and np.issubdtype(value.dtype    ,       np.floating ):
                 snake_case__					:      Optional[Any]  			=						{'dtype': np.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                 import PIL.Image

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

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





     def  _lowerCAmelCase			(    self    :				List[Any]    ,       __lowerCamelCase    :				Union[str, Any] ):
           # support for torch, tf, jax etc.
           if config.TORCH_AVAILABLE and "torch" in sys.modules:
                 import torch

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





     def  _lowerCAmelCase			(    self    :				Union[str, Any]    ,       __lowerCamelCase    :				dict ):
           return map_nested(self._recursive_tensorize    ,       __lowerCamelCase    ,       map_list=__lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Optional[Any]    ,       __lowerCamelCase    :				pa.Table ):
           snake_case__					:      Tuple  			=						self.numpy_arrow_extractor().extract_row(__lowerCamelCase )
           snake_case__					:      List[Any]  			=						self.python_features_decoder.decode_row(__lowerCamelCase )
           return self.recursive_tensorize(__lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Union[str, Any]    ,       __lowerCamelCase    :				pa.Table ):
           snake_case__					:      Any  			=						self.numpy_arrow_extractor().extract_column(__lowerCamelCase )
           snake_case__					:      Optional[Any]  			=						self.python_features_decoder.decode_column(__lowerCamelCase    ,       pa_table.column_names[0] )
           snake_case__					:      int  			=						self.recursive_tensorize(__lowerCamelCase )
           snake_case__					:      Optional[int]  			=						self._consolidate(__lowerCamelCase )
           return column





     def  _lowerCAmelCase			(    self    :				Optional[Any]    ,       __lowerCamelCase    :				pa.Table ):
           snake_case__					:      int  			=						self.numpy_arrow_extractor().extract_batch(__lowerCamelCase )
           snake_case__					:      Optional[Any]  			=						self.python_features_decoder.decode_batch(__lowerCamelCase )
           snake_case__					:      Union[str, Any]  			=						self.recursive_tensorize(__lowerCamelCase )
           for column_name in batch:
                 snake_case__					:      int  			=						self._consolidate(batch[column_name] )
           return batch






'''simple docstring'''






from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


A_ 			=		{
    "configuration_ernie_m": ["ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP", "ErnieMConfig"],
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A_ 			=		["ErnieMTokenizer"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A_ 			=		[
          "ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST",
          "ErnieMForMultipleChoice",
          "ErnieMForQuestionAnswering",
          "ErnieMForSequenceClassification",
          "ErnieMForTokenClassification",
          "ErnieMModel",
          "ErnieMPreTrainedModel",
          "ErnieMForInformationExtraction",
      ]


if TYPE_CHECKING:
      from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_ernie_m import ErnieMTokenizer

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_ernie_m import (
                ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
                ErnieMForInformationExtraction,
                ErnieMForMultipleChoice,
                ErnieMForQuestionAnswering,
                ErnieMForSequenceClassification,
                ErnieMForTokenClassification,
                ErnieMModel,
                ErnieMPreTrainedModel,
            )


else:
      import sys

      A_ 			=		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
