





"""simple docstring"""





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

      def __init__(     self  :str		,				_lowerCamelCase  :Optional[int]=None		,				**_lowerCamelCase  :str		):
       super().__init__(features=_SCREAMING_SNAKE_CASE		)
       __SCREAMING_SNAKE_CASE					:     Optional[Any]   				=			torch_tensor_kwargs
       import torch  # noqa import torch at initialization

      def 		SCREAMING_SNAKE_CASE_     (     self  :Tuple		,				_lowerCamelCase  :List[str]		):
       import torch

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

      def 		SCREAMING_SNAKE_CASE_     (     self  :Dict		,				_lowerCamelCase  :str		):
       import torch

       if isinstance(_SCREAMING_SNAKE_CASE		,				(str, bytes, type(_SCREAMING_SNAKE_CASE		))		):
        return value
       elif isinstance(_SCREAMING_SNAKE_CASE		,				(np.character, np.ndarray)		) and np.issubdtype(value.dtype		,				np.character		):
        return value.tolist()

       __SCREAMING_SNAKE_CASE					:     Optional[int]   				=			{}

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

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

      def 		SCREAMING_SNAKE_CASE_     (     self  :int		,				_lowerCamelCase  :List[Any]		):
       import torch

       # support for torch, tf, jax etc.
       if hasattr(_SCREAMING_SNAKE_CASE		,				'''__array__'''		) and not isinstance(_SCREAMING_SNAKE_CASE		,				torch.Tensor		):
        __SCREAMING_SNAKE_CASE					:     Dict   				=			data_struct.__array__()
       # support for nested types like struct of list of struct
       if isinstance(_SCREAMING_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(_SCREAMING_SNAKE_CASE		) for substruct in data_struct]		)
       elif isinstance(_SCREAMING_SNAKE_CASE		,				(list, tuple)		):
        return self._consolidate([self.recursive_tensorize(_SCREAMING_SNAKE_CASE		) for substruct in data_struct]		)
       return self._tensorize(_SCREAMING_SNAKE_CASE		)

      def 		SCREAMING_SNAKE_CASE_     (     self  :List[str]		,				_lowerCamelCase  :Any		):
       return map_nested(self._recursive_tensorize		,				_SCREAMING_SNAKE_CASE		,				map_list=_SCREAMING_SNAKE_CASE		)

      def 		SCREAMING_SNAKE_CASE_     (     self  :str		,				_lowerCamelCase  :List[Any]		):
       __SCREAMING_SNAKE_CASE					:     Optional[int]   				=			self.numpy_arrow_extractor().extract_row(_SCREAMING_SNAKE_CASE		)
       __SCREAMING_SNAKE_CASE					:     Optional[Any]   				=			self.python_features_decoder.decode_row(_SCREAMING_SNAKE_CASE		)
       return self.recursive_tensorize(_SCREAMING_SNAKE_CASE		)

      def 		SCREAMING_SNAKE_CASE_     (     self  :str		,				_lowerCamelCase  :List[Any]		):
       __SCREAMING_SNAKE_CASE					:     Tuple   				=			self.numpy_arrow_extractor().extract_column(_SCREAMING_SNAKE_CASE		)
       __SCREAMING_SNAKE_CASE					:     Optional[int]   				=			self.python_features_decoder.decode_column(_SCREAMING_SNAKE_CASE		,				pa_table.column_names[0]		)
       __SCREAMING_SNAKE_CASE					:     Union[str, Any]   				=			self.recursive_tensorize(_SCREAMING_SNAKE_CASE		)
       __SCREAMING_SNAKE_CASE					:     Tuple   				=			self._consolidate(_SCREAMING_SNAKE_CASE		)
       return column



      def 		SCREAMING_SNAKE_CASE_     (     self  :Any		,				_lowerCamelCase  :Optional[Any]		):
       __SCREAMING_SNAKE_CASE					:     Optional[Any]   				=			self.numpy_arrow_extractor().extract_batch(_SCREAMING_SNAKE_CASE		)
       __SCREAMING_SNAKE_CASE					:     List[Any]   				=			self.python_features_decoder.decode_batch(_SCREAMING_SNAKE_CASE		)
       __SCREAMING_SNAKE_CASE					:     List[Any]   				=			self.recursive_tensorize(_SCREAMING_SNAKE_CASE		)
       for column_name in batch:
        __SCREAMING_SNAKE_CASE					:     List[str]   				=			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_torch_available,
)


_UpperCamelCase						:  Optional[int]							       =				{
    "configuration_vision_text_dual_encoder": ["VisionTextDualEncoderConfig"],
    "processing_vision_text_dual_encoder": ["VisionTextDualEncoderProcessor"],
}


try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase						:  Dict							       =				["VisionTextDualEncoderModel"]


try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase						:  int							       =				["FlaxVisionTextDualEncoderModel"]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase						:  Tuple							       =				["TFVisionTextDualEncoderModel"]


if TYPE_CHECKING:
     from .configuration_vision_text_dual_encoder import VisionTextDualEncoderConfig
     from .processing_vision_text_dual_encoder import VisionTextDualEncoderProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_vision_text_dual_encoder import VisionTextDualEncoderModel

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_vision_text_dual_encoder import FlaxVisionTextDualEncoderModel

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_vision_text_dual_encoder import TFVisionTextDualEncoderModel


else:
     import sys

     _UpperCamelCase						:  Optional[int]							       =				_LazyModule(__name__, globals()["__file__"], _import_structure)





