


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

   '''simple docstring'''







   def __init__(  self  : Optional[Any]       ,  _lowerCamelCase  : Optional[Any]=None       ,  **_lowerCamelCase  : Tuple							):






         '''simple docstring'''



         super().__init__(features=_lowerCamelCase							)
         __lowerCamelCase     :      List[str]       						=						torch_tensor_kwargs
         import torch  # noqa import torch at initialization

   def   _snake_case    (  self  : int       ,  _lowerCamelCase  : List[Any]							):






         '''simple docstring'''



         import torch

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

   def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : str							):






         '''simple docstring'''



         import torch

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

         __lowerCamelCase     :      Union[str, Any]       						=						{}

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

               if isinstance(_lowerCamelCase       ,  PIL.Image.Image							):
                     __lowerCamelCase     :      List[Any]       						=						np.asarray(_lowerCamelCase							)
         return torch.tensor(_lowerCamelCase       ,  **{**default_dtype, **self.torch_tensor_kwargs}							)

   def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : Tuple							):






         '''simple docstring'''



         import torch

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

   def   _snake_case    (  self  : Optional[Any]       ,  _lowerCamelCase  : dict							):






         '''simple docstring'''



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

   def   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : pa.Table							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						self.numpy_arrow_extractor().extract_row(_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						self.python_features_decoder.decode_row(_lowerCamelCase							)
         return self.recursive_tensorize(_lowerCamelCase							)

   def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : pa.Table							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						self.numpy_arrow_extractor().extract_column(_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						self.python_features_decoder.decode_column(_lowerCamelCase       ,  pa_table.column_names[0]							)
         __lowerCamelCase     :      Dict       						=						self.recursive_tensorize(_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						self._consolidate(_lowerCamelCase							)
         return column


   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : pa.Table							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.numpy_arrow_extractor().extract_batch(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						self.python_features_decoder.decode_batch(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						self.recursive_tensorize(_lowerCamelCase							)
         for column_name in batch:
               __lowerCamelCase     :      Optional[int]       						=						self._consolidate(batch[column_name]							)
         return batch







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__UpperCamelCase    :						Optional[Any]     =							{
    'configuration_swinv2': ['SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Swinv2Config'],
}


try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __UpperCamelCase    :						List[Any]     =							[
        'SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST',
        'Swinv2ForImageClassification',
        'Swinv2ForMaskedImageModeling',
        'Swinv2Model',
        'Swinv2PreTrainedModel',
    ]


if TYPE_CHECKING:
    from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_swinva import (
            SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST,
            SwinvaForImageClassification,
            SwinvaForMaskedImageModeling,
            SwinvaModel,
            SwinvaPreTrainedModel,
        )


else:
    import sys

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



