





'''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 tensorflow as tf




class 				UpperCAmelCase  (    TensorFormatter[Mapping, '''tf.Tensor''', Mapping]     ):




      def __init__(    self    :				Any    ,    __snake_case    :				Dict=None    ,    **__snake_case    :				Any						)       ->   Dict:
            super().__init__(features=__snake_case						)
            _lowerCAmelCase							  =							tf_tensor_kwargs
            import tensorflow as tf  # noqa: import tf at initialization




      def        lowercase__					(    self    :				Optional[int]    ,    __snake_case    :				Union[str, Any]						)       ->   int:
            import tensorflow as tf

            if isinstance(__snake_case    ,    __snake_case						) and column:
                  if all(
                      isinstance(__snake_case    ,    tf.Tensor						) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column						):
                        return tf.stack(__snake_case						)
                  elif all(
                      isinstance(__snake_case    ,    (tf.Tensor, tf.RaggedTensor)						) and x.ndim == 1 and x.dtype == column[0].dtype
                      for x in column						):
                        # only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
                        return tf.ragged.stack(__snake_case						)

            return column




      def        lowercase__					(    self    :				List[str]    ,    __snake_case    :				Union[str, Any]						)       ->   Any:
            import tensorflow as tf

            if value is None:
                  return value

            _lowerCAmelCase							  =							{}

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

                  if isinstance(__snake_case    ,    PIL.Image.Image						):
                        _lowerCAmelCase							  =							np.asarray(__snake_case						)

            return tf.convert_to_tensor(__snake_case    ,    **{**default_dtype, **self.tf_tensor_kwargs}						)




      def        lowercase__					(    self    :				Dict    ,    __snake_case    :				Optional[int]						)       ->   List[Any]:
            import tensorflow as tf

            # 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    ,    tf.Tensor						):
                  _lowerCAmelCase							  =							data_struct.__array__()
            # support for nested types like struct of list of struct
            if isinstance(__snake_case    ,    np.ndarray						):
                  if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
                        return self._consolidate([self.recursive_tensorize(__snake_case						) for substruct in data_struct]						)
            elif isinstance(__snake_case    ,    (list, tuple)						):
                  return self._consolidate([self.recursive_tensorize(__snake_case						) for substruct in data_struct]						)
            return self._tensorize(__snake_case						)




      def        lowercase__					(    self    :				Tuple    ,    __snake_case    :				dict						)       ->   Tuple:
            return map_nested(self._recursive_tensorize    ,    __snake_case    ,    map_list=__snake_case						)




      def        lowercase__					(    self    :				int    ,    __snake_case    :				pa.Table						)       ->   Mapping:
            _lowerCAmelCase							  =							self.numpy_arrow_extractor().extract_row(__snake_case						)
            _lowerCAmelCase							  =							self.python_features_decoder.decode_row(__snake_case						)
            return self.recursive_tensorize(__snake_case						)




      def        lowercase__					(    self    :				Union[str, Any]    ,    __snake_case    :				pa.Table						)       ->   "tf.Tensor":
            _lowerCAmelCase							  =							self.numpy_arrow_extractor().extract_column(__snake_case						)
            _lowerCAmelCase							  =							self.python_features_decoder.decode_column(__snake_case    ,    pa_table.column_names[0]						)
            _lowerCAmelCase							  =							self.recursive_tensorize(__snake_case						)
            _lowerCAmelCase							  =							self._consolidate(__snake_case						)
            return column




      def        lowercase__					(    self    :				Tuple    ,    __snake_case    :				pa.Table						)       ->   Mapping:
            _lowerCAmelCase							  =							self.numpy_arrow_extractor().extract_batch(__snake_case						)
            _lowerCAmelCase							  =							self.python_features_decoder.decode_batch(__snake_case						)
            _lowerCAmelCase							  =							self.recursive_tensorize(__snake_case						)
            for column_name in batch:
                  _lowerCAmelCase							  =							self._consolidate(batch[column_name]						)
            return batch













'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A__       :     Tuple          ={
    '''configuration_mpnet''': ['''MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MPNetConfig'''],
    '''tokenization_mpnet''': ['''MPNetTokenizer'''],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__       :     Tuple          =['''MPNetTokenizerFast''']

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__       :     List[Any]          =[
          '''MPNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''MPNetForMaskedLM''',
          '''MPNetForMultipleChoice''',
          '''MPNetForQuestionAnswering''',
          '''MPNetForSequenceClassification''',
          '''MPNetForTokenClassification''',
          '''MPNetLayer''',
          '''MPNetModel''',
          '''MPNetPreTrainedModel''',
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A__       :     str          =[
          '''TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''TFMPNetEmbeddings''',
          '''TFMPNetForMaskedLM''',
          '''TFMPNetForMultipleChoice''',
          '''TFMPNetForQuestionAnswering''',
          '''TFMPNetForSequenceClassification''',
          '''TFMPNetForTokenClassification''',
          '''TFMPNetMainLayer''',
          '''TFMPNetModel''',
          '''TFMPNetPreTrainedModel''',
      ]


if TYPE_CHECKING:
      from .configuration_mpnet import MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP, MPNetConfig
      from .tokenization_mpnet import MPNetTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_mpnet_fast import MPNetTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mpnet import (
                MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
                MPNetForMaskedLM,
                MPNetForMultipleChoice,
                MPNetForQuestionAnswering,
                MPNetForSequenceClassification,
                MPNetForTokenClassification,
                MPNetLayer,
                MPNetModel,
                MPNetPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_mpnet import (
                TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFMPNetEmbeddings,
                TFMPNetForMaskedLM,
                TFMPNetForMultipleChoice,
                TFMPNetForQuestionAnswering,
                TFMPNetForSequenceClassification,
                TFMPNetForTokenClassification,
                TFMPNetMainLayer,
                TFMPNetModel,
                TFMPNetPreTrainedModel,
            )

else:
      import sys

      A__       :     List[Any]          =_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






