


# 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					,   A_=None					,   **A_    )  ->       Union[str, Any]:
         super().__init__(features=A_    )
         __UpperCamelCase    							=torch_tensor_kwargs
         import torch  # noqa import torch at initialization






    def _a					(			self					,   A_    )  ->       Union[str, Any]:
         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 _a					(			self					,   A_    )  ->       Any:
         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    							={}

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

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






    def _a					(			self					,   A_    )  ->       Any:
         import torch

         # support for torch, tf, jax etc.
         if hasattr(A_					,   '__array__'    ) and not isinstance(A_					,   torch.Tensor    ):
              __UpperCamelCase    							=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 _a					(			self					,   A_    )  ->       List[Any]:
         return map_nested(self._recursive_tensorize					,   A_					,   map_list=A_    )






    def _a					(			self					,   A_    )  ->       Mapping:
         __UpperCamelCase    							=self.numpy_arrow_extractor().extract_row(A_    )
         __UpperCamelCase    							=self.python_features_decoder.decode_row(A_    )
         return self.recursive_tensorize(A_    )






    def _a					(			self					,   A_    )  ->       "torch.Tensor":
         __UpperCamelCase    							=self.numpy_arrow_extractor().extract_column(A_    )
         __UpperCamelCase    							=self.python_features_decoder.decode_column(A_					,   pa_table.column_names[0]    )
         __UpperCamelCase    							=self.recursive_tensorize(A_    )
         __UpperCamelCase    							=self._consolidate(A_    )
         return column






    def _a					(			self					,   A_    )  ->       Mapping:
         __UpperCamelCase    							=self.numpy_arrow_extractor().extract_batch(A_    )
         __UpperCamelCase    							=self.python_features_decoder.decode_batch(A_    )
         __UpperCamelCase    							=self.recursive_tensorize(A_    )
         for column_name in batch:
              __UpperCamelCase    							=self._consolidate(batch[column_name]    )
         return batch







from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__		:int   =    logging.get_logger(__name__)

lowercase__		:Any   =    {
    "sail/poolformer_s12": "https://huggingface.co/sail/poolformer_s12/resolve/main/config.json",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
}







class 				lowercase		(					SCREAMING_SNAKE_CASE__		):
							lowercase_       :       Optional[Any]       					='''poolformer'''




							def __init__(				self   ,A__=3   ,A__=1_6   ,A__=1_6   ,A__=3   ,A__=4.0   ,A__=[2, 2, 6, 2]   ,A__=[6_4, 1_2_8, 3_2_0, 5_1_2]   ,A__=[7, 3, 3, 3]   ,A__=[4, 2, 2, 2]   ,A__=[2, 1, 1, 1]   ,A__=4   ,A__=0.0   ,A__="gelu"   ,A__=True   ,A__=1E-5   ,A__=0.02   ,**A__   ,):
												lowercase							=						num_channels
												lowercase							=						patch_size
												lowercase							=						stride
												lowercase							=						padding
												lowercase							=						pool_size
												lowercase							=						hidden_sizes
												lowercase							=						mlp_ratio
												lowercase							=						depths
												lowercase							=						patch_sizes
												lowercase							=						strides
												lowercase							=						num_encoder_blocks
												lowercase							=						drop_path_rate
												lowercase							=						hidden_act
												lowercase							=						use_layer_scale
												lowercase							=						layer_scale_init_value
												lowercase							=						initializer_range
												super().__init__(**A__)







class 				lowercase		(					SCREAMING_SNAKE_CASE__		):
							lowercase_       :       List[Any]       					=version.parse('''1.11'''		)




							@property
							def  A__      (				self):
												return OrderedDict(
												    [
												        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
												    ])





							@property
							def  A__      (				self):
												return 2E-3




