





# 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   __lowerCAmelCase			(					TensorFormatter[Mapping, """torch.Tensor""", Mapping]  ):





   def __init__( self					,			snake_case=None					,			**snake_case      )				->  str:
      """simple docstring"""





      super().__init__(features=snake_case      )
      a__	:						Union[str, Any]      		=  torch_tensor_kwargs
      import torch  # noqa import torch at initialization





   def 	_snake_case ( self					,			snake_case      )				->  Any:
      """simple docstring"""





      import torch

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





   def 	_snake_case ( self					,			snake_case      )				->  Dict:
      """simple docstring"""





      import torch

      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.tolist()

      a__	:						Optional[Any]      		=  {}

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

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





   def 	_snake_case ( self					,			snake_case      )				->  Tuple:
      """simple docstring"""





      import torch

      # support for torch, tf, jax etc.
      if hasattr(snake_case					,			"__array__"      ) and not isinstance(snake_case					,			torch.Tensor      ):
         a__	:						Optional[int]      		=  data_struct.__array__()
      # support for nested types like struct of list of struct
      if isinstance(snake_case					,			np.ndarray      ):
         if data_struct.dtype == object:  # torch 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 	_snake_case ( self					,			snake_case      )				->  Union[str, Any]:
      """simple docstring"""





      return map_nested(self._recursive_tensorize					,			snake_case					,			map_list=snake_case      )





   def 	_snake_case ( self					,			snake_case      )				->  Mapping:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  self.numpy_arrow_extractor().extract_row(snake_case      )
      a__	:						str      		=  self.python_features_decoder.decode_row(snake_case      )
      return self.recursive_tensorize(snake_case      )





   def 	_snake_case ( self					,			snake_case      )				->  "torch.Tensor":
      """simple docstring"""





      a__	:						Any      		=  self.numpy_arrow_extractor().extract_column(snake_case      )
      a__	:						Union[str, Any]      		=  self.python_features_decoder.decode_column(snake_case					,			pa_table.column_names[0]      )
      a__	:						Optional[int]      		=  self.recursive_tensorize(snake_case      )
      a__	:						Optional[Any]      		=  self._consolidate(snake_case      )
      return column





   def 	_snake_case ( self					,			snake_case      )				->  Mapping:
      """simple docstring"""





      a__	:						int      		=  self.numpy_arrow_extractor().extract_batch(snake_case      )
      a__	:						Tuple      		=  self.python_features_decoder.decode_batch(snake_case      )
      a__	:						str      		=  self.recursive_tensorize(snake_case      )
      for column_name in batch:
         a__	:						List[Any]      		=  self._consolidate(batch[column_name]      )
      return batch








from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE__       :			str			      =						{
    """configuration_distilbert""": [
        """DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """DistilBertConfig""",
        """DistilBertOnnxConfig""",
    ],
    """tokenization_distilbert""": ["""DistilBertTokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__       :			Optional[int]			      =						["""DistilBertTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__       :			Tuple			      =						[
      """DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DistilBertForMaskedLM""",
      """DistilBertForMultipleChoice""",
      """DistilBertForQuestionAnswering""",
      """DistilBertForSequenceClassification""",
      """DistilBertForTokenClassification""",
      """DistilBertModel""",
      """DistilBertPreTrainedModel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__       :			List[Any]			      =						[
      """TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFDistilBertForMaskedLM""",
      """TFDistilBertForMultipleChoice""",
      """TFDistilBertForQuestionAnswering""",
      """TFDistilBertForSequenceClassification""",
      """TFDistilBertForTokenClassification""",
      """TFDistilBertMainLayer""",
      """TFDistilBertModel""",
      """TFDistilBertPreTrainedModel""",
  ]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__       :			Any			      =						[
      """FlaxDistilBertForMaskedLM""",
      """FlaxDistilBertForMultipleChoice""",
      """FlaxDistilBertForQuestionAnswering""",
      """FlaxDistilBertForSequenceClassification""",
      """FlaxDistilBertForTokenClassification""",
      """FlaxDistilBertModel""",
      """FlaxDistilBertPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_distilbert import (
      DISTILBERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
      DistilBertConfig,
      DistilBertOnnxConfig,
  )
  from .tokenization_distilbert import DistilBertTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_distilbert_fast import DistilBertTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_distilbert import (
        DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        DistilBertForMaskedLM,
        DistilBertForMultipleChoice,
        DistilBertForQuestionAnswering,
        DistilBertForSequenceClassification,
        DistilBertForTokenClassification,
        DistilBertModel,
        DistilBertPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_distilbert import (
        TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFDistilBertForMaskedLM,
        TFDistilBertForMultipleChoice,
        TFDistilBertForQuestionAnswering,
        TFDistilBertForSequenceClassification,
        TFDistilBertForTokenClassification,
        TFDistilBertMainLayer,
        TFDistilBertModel,
        TFDistilBertPreTrainedModel,
    )

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_distilbert import (
        FlaxDistilBertForMaskedLM,
        FlaxDistilBertForMultipleChoice,
        FlaxDistilBertForQuestionAnswering,
        FlaxDistilBertForSequenceClassification,
        FlaxDistilBertForTokenClassification,
        FlaxDistilBertModel,
        FlaxDistilBertPreTrainedModel,
    )

else:
  import sys

  SCREAMING_SNAKE_CASE__       :			Optional[int]			      =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

