

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

  def __init__(  self		:			Optional[int]					,A		:			Dict=None					,**A		:			List[Any]    ):






   '''simple docstring'''
   super().__init__(features=A    )
   UpperCAmelCase__					:  Optional[int]					   =					torch_tensor_kwargs
   import torch  # noqa import torch at initialization

  def    __lowercase       (  self		:			List[str]					,A		:			Any    ):






   '''simple docstring'''
   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		:			Union[str, Any]					,A		:			Optional[Any]    ):






   '''simple docstring'''
   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__					:  Optional[int]					   =					{}

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

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

  def    __lowercase       (  self		:			Optional[int]					,A		:			Optional[int]    ):






   '''simple docstring'''
   import torch

   # support for torch, tf, jax etc.
   if hasattr(A					,"""__array__"""    ) and not isinstance(A					,torch.Tensor    ):
    UpperCAmelCase__					:  str					   =					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		:			List[str]					,A		:			dict    ):






   '''simple docstring'''
   return map_nested(self._recursive_tensorize					,A					,map_list=A    )

  def    __lowercase       (  self		:			List[Any]					,A		:			pa.Table    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					self.numpy_arrow_extractor().extract_row(A    )
   UpperCAmelCase__					:  Union[str, Any]					   =					self.python_features_decoder.decode_row(A    )
   return self.recursive_tensorize(A    )

  def    __lowercase       (  self		:			Any					,A		:			pa.Table    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Tuple					   =					self.numpy_arrow_extractor().extract_column(A    )
   UpperCAmelCase__					:  Tuple					   =					self.python_features_decoder.decode_column(A					,pa_table.column_names[0]    )
   UpperCAmelCase__					:  Any					   =					self.recursive_tensorize(A    )
   UpperCAmelCase__					:  Dict					   =					self._consolidate(A    )
   return column




  def    __lowercase       (  self		:			Union[str, Any]					,A		:			pa.Table    ):






   '''simple docstring'''
   UpperCAmelCase__					:  Optional[int]					   =					self.numpy_arrow_extractor().extract_batch(A    )
   UpperCAmelCase__					:  str					   =					self.python_features_decoder.decode_batch(A    )
   UpperCAmelCase__					:  List[str]					   =					self.recursive_tensorize(A    )
   for column_name in batch:
    UpperCAmelCase__					:  Optional[Any]					   =					self._consolidate(batch[column_name]    )
   return batch








"""simple docstring"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase =				logging.get_logger(__name__)







class      __lowercase			(    __lowerCamelCase					):
  snake_case_				       =       """timm_backbone"""

  def __init__(  self		:			List[str]					,A		:			Any=None					,A		:			List[Any]=3					,A		:			Any=True					,A		:			Union[str, Any]=True					,A		:			List[Any]=None					,**A		:			Optional[int]					,):






   '''simple docstring'''
   super().__init__(**A    )
   UpperCAmelCase__					:  Optional[int]					   =					backbone
   UpperCAmelCase__					:  Dict					   =					num_channels
   UpperCAmelCase__					:  Optional[int]					   =					features_only
   UpperCAmelCase__					:  Tuple					   =					use_pretrained_backbone
   UpperCAmelCase__					:  str					   =					True
   UpperCAmelCase__					:  List[str]					   =					out_indices if out_indices is not None else (-1,)





