



'''simple docstring'''





from __future__ import annotations

from random import random
from typing import Generic, TypeVar

__a  			=						TypeVar("KT")
__a  			=						TypeVar("VT")







class        UpperCAmelCase_			(			Generic[KT, VT]      ):
    """simple docstring"""







    def __init__(       self   :	Optional[int]					,	snake_case_   :	KT | str = "root"					,	snake_case_   :	VT | None = None		):
          snake_case__    :       Optional[int]       		= key
          snake_case__    :       Any       		= value
          snake_case__    :       Optional[int]       		= []




    def __repr__(       self   :	int		):
          return f"Node({self.key}: {self.value})"




    @property
    def lowerCamelCase			(       self   :	List[Any]		):
          return len(self.forward		)







class        UpperCAmelCase_			(			Generic[KT, VT]      ):
    """simple docstring"""







    def __init__(       self   :	Any					,	snake_case_   :	float = 0.5					,	snake_case_   :	int = 16		):
          snake_case__    :       List[Any]       		= Node[KT, VT]()
          snake_case__    :       int       		= 0
          snake_case__    :       Tuple       		= p
          snake_case__    :       str       		= max_level




    def __str__(       self   :	List[Any]		):
          snake_case__    :       Optional[int]       		= list(self		)

          if len(__lowercase		) == 0:
                return f"SkipList(level={self.level})"

          snake_case__    :       Any       		= max((len(str(__lowercase		)		) for item in items)					,	default=4		)
          snake_case__    :       str       		= max(__lowercase					,	4		) + 4

          snake_case__    :       Dict       		= self.head
          snake_case__    :       Any       		= []

          snake_case__    :       Optional[Any]       		= node.forward.copy()
          lines.append(f"[{node.key}]".ljust(__lowercase					,	"""-"""		) + """* """ * len(__lowercase		)		)
          lines.append(""" """ * label_size + """| """ * len(__lowercase		)		)

          while len(node.forward		) != 0:
                snake_case__    :       Any       		= node.forward[0]

                lines.append(
                    f"[{node.key}]".ljust(__lowercase					,	"""-"""		)
                    + """ """.join(str(n.key		) if n.key == node.key else """|""" for n in forwards		)		)
                lines.append(""" """ * label_size + """| """ * len(__lowercase		)		)
                snake_case__    :       Dict       		= node.forward

          lines.append("""None""".ljust(__lowercase		) + """* """ * len(__lowercase		)		)
          return f"SkipList(level={self.level})\n" + "\n".join(__lowercase		)




    def __iter__(       self   :	Optional[int]		):
          snake_case__    :       Dict       		= self.head

          while len(node.forward		) != 0:
                yield node.forward[0].key
                snake_case__    :       Any       		= node.forward[0]




    def lowerCamelCase			(       self   :	Any		):
          snake_case__    :       List[str]       		= 1
          while random() < self.p and level < self.max_level:
                level += 1

          return level




    def lowerCamelCase			(       self   :	Dict					,	snake_case_   :	Dict		):
          snake_case__    :       Tuple       		= []

          snake_case__    :       List[Any]       		= self.head

          for i in reversed(range(self.level		)		):
                # i < node.level - When node level is lesser than `i` decrement `i`.
                # node.forward[i].key < key - Jumping to node with key value higher
                #                             or equal to searched key would result
                #                             in skipping searched key.
                while i < node.level and node.forward[i].key < key:
                      snake_case__    :       str       		= node.forward[i]
                # Each leftmost node (relative to searched node) will potentially have to
                # be updated.
                update_vector.append(__lowercase		)

          update_vector.reverse()  # Note that we were inserting values in reverse order.

          # len(node.forward) != 0 - If current node doesn't contain any further
          #                          references then searched key is not present.
          # node.forward[0].key == key - Next node key should be equal to search key
          #                              if key is present.
          if len(node.forward		) != 0 and node.forward[0].key == key:
                return node.forward[0], update_vector
          else:
                return None, update_vector




    def lowerCamelCase			(       self   :	Tuple					,	snake_case_   :	KT		):
          snake_case__		,							snake_case__    :       List[str]       		= self._locate_node(__lowercase		)

          if node is not None:
                for i, update_node in enumerate(__lowercase		):
                      # Remove or replace all references to removed node.
                      if update_node.level > i and update_node.forward[i].key == key:
                            if node.level > i:
                                  snake_case__    :       List[str]       		= node.forward[i]
                            else:
                                  snake_case__    :       str       		= update_node.forward[:i]




    def lowerCamelCase			(       self   :	str					,	snake_case_   :	KT					,	snake_case_   :	VT		):
          snake_case__		,							snake_case__    :       List[str]       		= self._locate_node(__lowercase		)
          if node is not None:
                snake_case__    :       List[str]       		= value
          else:
                snake_case__    :       Tuple       		= self.random_level()

                if level > self.level:
                      # After level increase we have to add additional nodes to head.
                      for _ in range(self.level - 1					,	__lowercase		):
                            update_vector.append(self.head		)
                      snake_case__    :       Dict       		= level

                snake_case__    :       Tuple       		= Node(__lowercase					,	__lowercase		)

                for i, update_node in enumerate(update_vector[:level]		):
                      # Change references to pass through new node.
                      if update_node.level > i:
                            new_node.forward.append(update_node.forward[i]		)

                      if update_node.level < i + 1:
                            update_node.forward.append(__lowercase		)
                      else:
                            snake_case__    :       List[Any]       		= new_node




    def lowerCamelCase			(       self   :	str					,	snake_case_   :	VT		):
          snake_case__		,							snake_case__    :       Optional[int]       		= self._locate_node(__lowercase		)

          if node is not None:
                return node.value

          return None





def 		__snake_case(							)						->					Any:
      snake_case__    :       Any       		= SkipList()
      skip_list.insert("""Key1"""	,  3						)
      skip_list.insert("""Key2"""	,  12						)
      skip_list.insert("""Key3"""	,  41						)
      skip_list.insert("""Key4"""	,  -19						)

      snake_case__    :       str       		= skip_list.head
      snake_case__    :       Union[str, Any]       		= {}
      while node.level != 0:
            snake_case__    :       Dict       		= node.forward[0]
            snake_case__    :       str       		= node.value

      assert len(_A						) == 4
      assert all_values["Key1"] == 3
      assert all_values["Key2"] == 12
      assert all_values["Key3"] == 41
      assert all_values["Key4"] == -19





def 		__snake_case(							)						->					Optional[Any]:
      snake_case__    :       Optional[Any]       		= SkipList()
      skip_list.insert("""Key1"""	,  10						)
      skip_list.insert("""Key1"""	,  12						)

      skip_list.insert("""Key5"""	,  7						)
      skip_list.insert("""Key7"""	,  10						)
      skip_list.insert("""Key10"""	,  5						)

      skip_list.insert("""Key7"""	,  7						)
      skip_list.insert("""Key5"""	,  5						)
      skip_list.insert("""Key10"""	,  10						)

      snake_case__    :       Tuple       		= skip_list.head
      snake_case__    :       Optional[int]       		= {}
      while node.level != 0:
            snake_case__    :       List[str]       		= node.forward[0]
            snake_case__    :       List[str]       		= node.value

      if len(_A						) != 4:
            print()
      assert len(_A						) == 4
      assert all_values["Key1"] == 12
      assert all_values["Key7"] == 7
      assert all_values["Key5"] == 5
      assert all_values["Key10"] == 10





def 		__snake_case(							)						->					Dict:
      snake_case__    :       Dict       		= SkipList()
      assert skip_list.find("""Some key"""						) is None





def 		__snake_case(							)						->					Tuple:
      snake_case__    :       Optional[Any]       		= SkipList()

      skip_list.insert("""Key2"""	,  20						)
      assert skip_list.find("""Key2"""						) == 20

      skip_list.insert("""Some Key"""	,  10						)
      skip_list.insert("""Key2"""	,  8						)
      skip_list.insert("""V"""	,  13						)

      assert skip_list.find("""Y"""						) is None
      assert skip_list.find("""Key2"""						) == 8
      assert skip_list.find("""Some Key"""						) == 10
      assert skip_list.find("""V"""						) == 13





def 		__snake_case(							)						->					Optional[Any]:
      snake_case__    :       Any       		= SkipList()
      skip_list.delete("""Some key"""						)

      assert len(skip_list.head.forward						) == 0





def 		__snake_case(							)						->					str:
      snake_case__    :       List[str]       		= SkipList()

      skip_list.insert("""Key1"""	,  12						)
      skip_list.insert("""V"""	,  13						)
      skip_list.insert("""X"""	,  14						)
      skip_list.insert("""Key2"""	,  15						)

      skip_list.delete("""V"""						)
      skip_list.delete("""Key2"""						)

      assert skip_list.find("""V"""						) is None
      assert skip_list.find("""Key2"""						) is None





def 		__snake_case(							)						->					Optional[int]:
      snake_case__    :       str       		= SkipList()

      skip_list.insert("""Key1"""	,  12						)
      skip_list.insert("""V"""	,  13						)
      skip_list.insert("""X"""	,  14						)
      skip_list.insert("""Key2"""	,  15						)

      skip_list.delete("""V"""						)
      assert skip_list.find("""V"""						) is None
      assert skip_list.find("""X"""						) == 14
      assert skip_list.find("""Key1"""						) == 12
      assert skip_list.find("""Key2"""						) == 15

      skip_list.delete("""X"""						)
      assert skip_list.find("""V"""						) is None
      assert skip_list.find("""X"""						) is None
      assert skip_list.find("""Key1"""						) == 12
      assert skip_list.find("""Key2"""						) == 15

      skip_list.delete("""Key1"""						)
      assert skip_list.find("""V"""						) is None
      assert skip_list.find("""X"""						) is None
      assert skip_list.find("""Key1"""						) is None
      assert skip_list.find("""Key2"""						) == 15

      skip_list.delete("""Key2"""						)
      assert skip_list.find("""V"""						) is None
      assert skip_list.find("""X"""						) is None
      assert skip_list.find("""Key1"""						) is None
      assert skip_list.find("""Key2"""						) is None





def 		__snake_case(							)						->					Tuple:
      snake_case__    :       str       		= SkipList()

      skip_list.insert("""Key1"""	,  12						)
      skip_list.insert("""V"""	,  13						)
      skip_list.insert("""X"""	,  142						)
      skip_list.insert("""Key2"""	,  15						)

      skip_list.delete("""X"""						)

      def traverse_keys(_lowerCAmelCase						):
            yield node.key
            for forward_node in node.forward:
                  yield from traverse_keys(_A						)

      assert len(set(traverse_keys(skip_list.head						)						)						) == 4





def 		__snake_case(							)						->					Any:





      def is_sorted(_lowerCAmelCase						):
            return all(next_item >= item for item, next_item in zip(_A	,  lst[1:]						)						)

      snake_case__    :       int       		= SkipList()
      for i in range(10						):
            skip_list.insert(_A	,  _A						)
      assert is_sorted(list(_A						)						)
      skip_list.delete(5						)
      skip_list.delete(8						)
      skip_list.delete(2						)
      assert is_sorted(list(_A						)						)
      skip_list.insert(-12	,  -12						)
      skip_list.insert(77	,  77						)
      assert is_sorted(list(_A						)						)





def 		__snake_case(							)						->					Optional[Any]:





      for _ in range(100						):
            # Repeat test 100 times due to the probabilistic nature of skip list
            # random values == random bugs
            test_insert()
            test_insert_overrides_existing_value()

            test_searching_empty_list_returns_none()
            test_search()

            test_deleting_item_from_empty_list_do_nothing()
            test_deleted_items_are_not_founded_by_find_method()
            test_delete_removes_only_given_key()
            test_delete_doesnt_leave_dead_nodes()

            test_iter_always_yields_sorted_values()





def 		__snake_case(							)						->					Dict:
      snake_case__    :       Tuple       		= SkipList()
      skip_list.insert(2	,  """2"""						)
      skip_list.insert(4	,  """4"""						)
      skip_list.insert(6	,  """4"""						)
      skip_list.insert(4	,  """5"""						)
      skip_list.insert(8	,  """4"""						)
      skip_list.insert(9	,  """4"""						)

      skip_list.delete(4						)

      print(_A						)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       main()






import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


lowercase__							:	Optional[Any]											=		logging.get_logger(__name__)

# General docstring
lowercase__							:	Optional[int]											=		"PoolFormerConfig"

# Base docstring
lowercase__							:	Optional[Any]											=		"sail/poolformer_s12"
lowercase__							:	Union[str, Any]											=		[1, 512, 7, 7]

# Image classification docstring
lowercase__							:	List[str]											=		"sail/poolformer_s12"
lowercase__							:	Dict											=		"tabby, tabby cat"

lowercase__							:	Union[str, Any]											=		[
    "sail/poolformer_s12",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]


def 		lowerCamelCase__       (						_A	,      _A = 0.0	,      _A = False				):







	'''simple docstring'''





	if drop_prob == 0.0 or not training:
		return input
	snake_case_						       =	1 - drop_prob
	snake_case_						       =	(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
	snake_case_						       =	keep_prob + torch.rand(_A	,      dtype=input.dtype	,      device=input.device				)
	random_tensor.floor_()  # binarize
	snake_case_						       =	input.div(_A				) * random_tensor
	return output
class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[Any]			,      __lowercase				:	Optional[float] = None		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	drop_prob






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	torch.Tensor		):






							"""simple docstring"""






							return drop_path(__lowercase			,      self.drop_prob			,      self.training		)






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






							return "p={}".format(self.drop_prob		)







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[Any]			,      __lowercase				:	Dict			,      __lowercase				:	Tuple			,      __lowercase				:	Optional[Any]			,      __lowercase				:	int			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	Dict=None		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	patch_size if isinstance(__lowercase			,      collections.abc.Iterable		) else (patch_size, patch_size)
							snake_case_						       =	stride if isinstance(__lowercase			,      collections.abc.Iterable		) else (stride, stride)
							snake_case_						       =	padding if isinstance(__lowercase			,      collections.abc.Iterable		) else (padding, padding)

							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      kernel_size=__lowercase			,      stride=__lowercase			,      padding=__lowercase		)
							snake_case_						       =	norm_layer(__lowercase		) if norm_layer else nn.Identity()






						def 			snake_case__		(		self				:	Any			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.projection(__lowercase		)
							snake_case_						       =	self.norm(__lowercase		)
							return embeddings







class 						UpperCAmelCase  (							nn.GroupNorm     ):







						'''simple docstring'''





						def __init__(		self				:	int			,      __lowercase				:	List[str]			,      **__lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							super().__init__(1			,      __lowercase			,      **__lowercase		)







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.AvgPoolad(__lowercase			,      stride=1			,      padding=pool_size // 2			,      count_include_pad=__lowercase		)






						def 			snake_case__		(		self				:	int			,      __lowercase				:	Tuple		):






							"""simple docstring"""






							return self.pool(__lowercase		) - hidden_states







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





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






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      1		)
							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      1		)
							snake_case_						       =	PoolFormerDropPath(__lowercase		)
							if isinstance(config.hidden_act			,      __lowercase		):
								snake_case_						       =	ACTaFN[config.hidden_act]
							else:
								snake_case_						       =	config.hidden_act






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	int		):






							"""simple docstring"""






							snake_case_						       =	self.conva(__lowercase		)
							snake_case_						       =	self.act_fn(__lowercase		)
							snake_case_						       =	self.drop(__lowercase		)
							snake_case_						       =	self.conva(__lowercase		)
							snake_case_						       =	self.drop(__lowercase		)

							return hidden_states







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	int			,      __lowercase				:	List[str]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	List[str]			,      __lowercase				:	int		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	PoolFormerPooling(__lowercase		)
							snake_case_						       =	PoolFormerOutput(__lowercase			,      __lowercase			,      __lowercase			,      __lowercase		)
							snake_case_						       =	PoolFormerGroupNorm(__lowercase		)
							snake_case_						       =	PoolFormerGroupNorm(__lowercase		)

							# Useful for training neural nets
							snake_case_						       =	PoolFormerDropPath(__lowercase		) if drop_path > 0.0 else nn.Identity()
							snake_case_						       =	config.use_layer_scale
							if config.use_layer_scale:
								snake_case_						       =	nn.Parameter(
								    config.layer_scale_init_value * torch.ones((__lowercase)		)			,      requires_grad=__lowercase		)
								snake_case_						       =	nn.Parameter(
								    config.layer_scale_init_value * torch.ones((__lowercase)		)			,      requires_grad=__lowercase		)






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							if self.use_layer_scale:
								snake_case_						       =	self.pooling(self.before_norm(__lowercase		)		)
								snake_case_						       =	self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
								# First residual connection
								snake_case_						       =	hidden_states + self.drop_path(__lowercase		)
								snake_case_						       =	()

								snake_case_						       =	self.output(self.after_norm(__lowercase		)		)
								snake_case_						       =	self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
								# Second residual connection
								snake_case_						       =	hidden_states + self.drop_path(__lowercase		)

								snake_case_						       =	(output,) + outputs
								return outputs

							else:
								snake_case_						       =	self.drop_path(self.pooling(self.before_norm(__lowercase		)		)		)
								# First residual connection
								snake_case_						       =	pooling_output + hidden_states
								snake_case_						       =	()

								# Second residual connection inside the PoolFormerOutput block
								snake_case_						       =	self.drop_path(self.output(self.after_norm(__lowercase		)		)		)
								snake_case_						       =	hidden_states + layer_output

								snake_case_						       =	(output,) + outputs
								return outputs







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	List[Any]			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	config
							# stochastic depth decay rule
							snake_case_						       =	[x.item() for x in torch.linspace(0			,      config.drop_path_rate			,      sum(config.depths		)		)]

							# patch embeddings
							snake_case_						       =	[]
							for i in range(config.num_encoder_blocks		):
								embeddings.append(
								    PoolFormerEmbeddings(
								        patch_size=config.patch_sizes[i]			,      stride=config.strides[i]			,      padding=config.padding[i]			,      num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]			,      hidden_size=config.hidden_sizes[i]			,      )		)
							snake_case_						       =	nn.ModuleList(__lowercase		)

							# Transformer blocks
							snake_case_						       =	[]
							snake_case_						       =	0
							for i in range(config.num_encoder_blocks		):
								# each block consists of layers
								snake_case_						       =	[]
								if i != 0:
									cur += config.depths[i - 1]
								for j in range(config.depths[i]		):
									layers.append(
									    PoolFormerLayer(
									        __lowercase			,      num_channels=config.hidden_sizes[i]			,      pool_size=config.pool_size			,      hidden_size=config.hidden_sizes[i]			,      intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio		)			,      drop_path=dpr[cur + j]			,      )		)
								blocks.append(nn.ModuleList(__lowercase		)		)

							snake_case_						       =	nn.ModuleList(__lowercase		)






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	List[Any]			,      __lowercase				:	int=False			,      __lowercase				:	Tuple=True		):






							"""simple docstring"""






							snake_case_						       =	() if output_hidden_states else None

							snake_case_						       =	pixel_values
							for idx, layers in enumerate(zip(self.patch_embeddings			,      self.block		)		):
								snake_case_       , snake_case_						       =	layers
								# Get patch embeddings from hidden_states
								snake_case_						       =	embedding_layer(__lowercase		)
								# Send the embeddings through the blocks
								for _, blk in enumerate(__lowercase		):
									snake_case_						       =	blk(__lowercase		)
									snake_case_						       =	layer_outputs[0]

								if output_hidden_states:
									snake_case_						       =	all_hidden_states + (hidden_states,)

							if not return_dict:
								return tuple(v for v in [hidden_states, all_hidden_states] if v is not None		)

							return BaseModelOutputWithNoAttention(last_hidden_state=__lowercase			,      hidden_states=__lowercase		)







class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						lowerCAmelCase_							=  PoolFormerConfig
						lowerCAmelCase_							=  '''poolformer'''
						lowerCAmelCase_							=  '''pixel_values'''
						lowerCAmelCase_							=  True






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							if isinstance(__lowercase			,      (nn.Linear, nn.Convad)		):
								module.weight.data.normal_(mean=0.0			,      std=self.config.initializer_range		)
								if module.bias is not None:
									module.bias.data.zero_()
							elif isinstance(__lowercase			,      nn.LayerNorm		):
								module.bias.data.zero_()
								module.weight.data.fill_(1.0		)






						def 			snake_case__		(		self				:	str			,      __lowercase				:	Any			,      __lowercase				:	Tuple=False		):






							"""simple docstring"""






							if isinstance(__lowercase			,      __lowercase		):
								snake_case_						       =	value







lowercase__							:	Optional[int]											=		R"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

lowercase__							:	List[str]											=		R"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n"
@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'''			,  UpperCAmelCase__			,  )
class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						def __init__(		self				:	Any			,      __lowercase				:	Any		):






							"""simple docstring"""






							super().__init__(__lowercase		)
							snake_case_						       =	config

							snake_case_						       =	PoolFormerEncoder(__lowercase		)

							# Initialize weights and apply final processing
							self.post_init()






						def 			snake_case__		(		self				:	List[Any]		):






							"""simple docstring"""






							return self.embeddings.patch_embeddings






						@add_start_docstrings_to_model_forward(__lowercase		)
						@add_code_sample_docstrings(
						    checkpoint=_CHECKPOINT_FOR_DOC			,      output_type=__lowercase			,      config_class=_CONFIG_FOR_DOC			,      modality="vision"			,      expected_output=_EXPECTED_OUTPUT_SHAPE			,      )
						def 			snake_case__		(		self				:	Optional[Any]			,      __lowercase				:	Optional[torch.FloatTensor] = None			,      __lowercase				:	Optional[bool] = None			,      __lowercase				:	Optional[bool] = None			,      ):






							"""simple docstring"""






							snake_case_						       =	(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							snake_case_						       =	return_dict if return_dict is not None else self.config.use_return_dict

							if pixel_values is None:
								raise ValueError("You have to specify pixel_values"		)

							snake_case_						       =	self.encoder(
							    __lowercase			,      output_hidden_states=__lowercase			,      return_dict=__lowercase			,      )
							snake_case_						       =	encoder_outputs[0]

							if not return_dict:
								return (sequence_output, None) + encoder_outputs[1:]

							return BaseModelOutputWithNoAttention(
							    last_hidden_state=__lowercase			,      hidden_states=encoder_outputs.hidden_states			,      )







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	Tuple		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.Linear(config.hidden_size			,      config.hidden_size		)






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							snake_case_						       =	self.dense(__lowercase		)
							return output







@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''			,  UpperCAmelCase__			,  )
class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





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






							"""simple docstring"""






							super().__init__(__lowercase		)
							snake_case_						       =	config.num_labels
							snake_case_						       =	PoolFormerModel(__lowercase		)

							# Final norm
							snake_case_						       =	PoolFormerGroupNorm(config.hidden_sizes[-1]		)
							# Classifier head
							snake_case_						       =	(
							    nn.Linear(config.hidden_sizes[-1]			,      config.num_labels		) if config.num_labels > 0 else nn.Identity()
							)

							# Initialize weights and apply final processing
							self.post_init()






						@add_start_docstrings_to_model_forward(__lowercase		)
						@add_code_sample_docstrings(
						    checkpoint=_IMAGE_CLASS_CHECKPOINT			,      output_type=__lowercase			,      config_class=_CONFIG_FOR_DOC			,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,      )
						def 			snake_case__		(		self				:	str			,      __lowercase				:	Optional[torch.FloatTensor] = None			,      __lowercase				:	Optional[torch.LongTensor] = None			,      __lowercase				:	Optional[bool] = None			,      __lowercase				:	Optional[bool] = None			,      ):






							"""simple docstring"""






							snake_case_						       =	return_dict if return_dict is not None else self.config.use_return_dict

							snake_case_						       =	self.poolformer(
							    __lowercase			,      output_hidden_states=__lowercase			,      return_dict=__lowercase			,      )

							snake_case_						       =	outputs[0]

							snake_case_						       =	self.classifier(self.norm(__lowercase		).mean([-2, -1]		)		)

							snake_case_						       =	None
							if labels is not None:
								if self.config.problem_type is None:
									if self.num_labels == 1:
										snake_case_						       =	"regression"
									elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
										snake_case_						       =	"single_label_classification"
									else:
										snake_case_						       =	"multi_label_classification"

								if self.config.problem_type == "regression":
									snake_case_						       =	MSELoss()
									if self.num_labels == 1:
										snake_case_						       =	loss_fct(logits.squeeze()			,      labels.squeeze()		)
									else:
										snake_case_						       =	loss_fct(__lowercase			,      __lowercase		)
								elif self.config.problem_type == "single_label_classification":
									snake_case_						       =	CrossEntropyLoss()
									snake_case_						       =	loss_fct(logits.view(-1			,      self.num_labels		)			,      labels.view(-1		)		)
								elif self.config.problem_type == "multi_label_classification":
									snake_case_						       =	BCEWithLogitsLoss()
									snake_case_						       =	loss_fct(__lowercase			,      __lowercase		)

							if not return_dict:
								snake_case_						       =	(logits,) + outputs[2:]
								return ((loss,) + output) if loss is not None else output

							return ImageClassifierOutputWithNoAttention(loss=__lowercase			,      logits=__lowercase			,      hidden_states=outputs.hidden_states		)
