
from dataclasses import dataclass
from typing import Optional

import numpy as np
import torch
import torch.nn as nn

from ..utils import BaseOutput, is_torch_version, randn_tensor
from .attention_processor import SpatialNorm
from .unet_ad_blocks import UNetMidBlockaD, get_down_block, get_up_block




@dataclass
class        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



	_UpperCAmelCase	:		torch.FloatTensor




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Optional[Any]      ,  lowercase     :   Optional[int]=3      ,  lowercase     :   Union[str, Any]=3      ,  lowercase     :   Tuple=("DownEncoderBlock2D",)      ,  lowercase     :   List[str]=(64,)      ,  lowercase     :   List[str]=2      ,  lowercase     :   List[Any]=32      ,  lowercase     :   List[Any]="silu"      ,  lowercase     :   Dict=True      ,  ):


				'''simple docstring'''


				super().__init__()
				_snake_case          =      layers_per_block

				_snake_case          =      torch.nn.Convad(
				    lowercase      ,  block_out_channels[0]      ,  kernel_size=3      ,  stride=1      ,  padding=1      ,  )

				_snake_case          =      None
				_snake_case          =      nn.ModuleList([]     )

				# down
				_snake_case          =      block_out_channels[0]
				for i, down_block_type in enumerate(lowercase     ):
							_snake_case          =      output_channel
							_snake_case          =      block_out_channels[i]
							_snake_case          =      i == len(lowercase     ) - 1

							_snake_case          =      get_down_block(
							    lowercase      ,  num_layers=self.layers_per_block      ,  in_channels=lowercase      ,  out_channels=lowercase      ,  add_downsample=not is_final_block      ,  resnet_eps=1E-6      ,  downsample_padding=0      ,  resnet_act_fn=lowercase      ,  resnet_groups=lowercase      ,  attention_head_dim=lowercase      ,  temb_channels=lowercase      ,  )
							self.down_blocks.append(lowercase     )

				# mid
				_snake_case          =      UNetMidBlockaD(
				    in_channels=block_out_channels[-1]      ,  resnet_eps=1E-6      ,  resnet_act_fn=lowercase      ,  output_scale_factor=1      ,  resnet_time_scale_shift='default'      ,  attention_head_dim=block_out_channels[-1]      ,  resnet_groups=lowercase      ,  temb_channels=lowercase      ,  )

				# out
				_snake_case          =      nn.GroupNorm(num_channels=block_out_channels[-1]      ,  num_groups=lowercase      ,  eps=1E-6     )
				_snake_case          =      nn.SiLU()

				_snake_case          =      2 * out_channels if double_z else out_channels
				_snake_case          =      nn.Convad(block_out_channels[-1]      ,  lowercase      ,  3      ,  padding=1     )

				_snake_case          =      False





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


				'''simple docstring'''


				_snake_case          =      x
				_snake_case          =      self.conv_in(lowercase     )

				if self.training and self.gradient_checkpointing:

							def create_custom_forward(lowercase     :   int     ):
										def custom_forward(*lowercase     :   Tuple     ):
													return module(*lowercase     )

										return custom_forward

							# down
							if is_torch_version('>='      ,  '1.11.0'     ):
										for down_block in self.down_blocks:
													_snake_case          =      torch.utils.checkpoint.checkpoint(
													    create_custom_forward(lowercase     )      ,  lowercase      ,  use_reentrant=lowercase     )
										# middle
										_snake_case          =      torch.utils.checkpoint.checkpoint(
										    create_custom_forward(self.mid_block     )      ,  lowercase      ,  use_reentrant=lowercase     )
							else:
										for down_block in self.down_blocks:
													_snake_case          =      torch.utils.checkpoint.checkpoint(create_custom_forward(lowercase     )      ,  lowercase     )
										# middle
										_snake_case          =      torch.utils.checkpoint.checkpoint(create_custom_forward(self.mid_block     )      ,  lowercase     )

				else:
							# down
							for down_block in self.down_blocks:
										_snake_case          =      down_block(lowercase     )

							# middle
							_snake_case          =      self.mid_block(lowercase     )

				# post-process
				_snake_case          =      self.conv_norm_out(lowercase     )
				_snake_case          =      self.conv_act(lowercase     )
				_snake_case          =      self.conv_out(lowercase     )

				return sample




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Optional[int]      ,  lowercase     :   Optional[Any]=3      ,  lowercase     :   Any=3      ,  lowercase     :   Tuple=("UpDecoderBlock2D",)      ,  lowercase     :   str=(64,)      ,  lowercase     :   int=2      ,  lowercase     :   List[Any]=32      ,  lowercase     :   Optional[int]="silu"      ,  lowercase     :   Optional[int]="group"      ,  ):


				'''simple docstring'''


				super().__init__()
				_snake_case          =      layers_per_block

				_snake_case          =      nn.Convad(
				    lowercase      ,  block_out_channels[-1]      ,  kernel_size=3      ,  stride=1      ,  padding=1      ,  )

				_snake_case          =      None
				_snake_case          =      nn.ModuleList([]     )

				_snake_case          =      in_channels if norm_type == 'spatial' else None

				# mid
				_snake_case          =      UNetMidBlockaD(
				    in_channels=block_out_channels[-1]      ,  resnet_eps=1E-6      ,  resnet_act_fn=lowercase      ,  output_scale_factor=1      ,  resnet_time_scale_shift='default' if norm_type == 'group' else norm_type      ,  attention_head_dim=block_out_channels[-1]      ,  resnet_groups=lowercase      ,  temb_channels=lowercase      ,  )

				# up
				_snake_case          =      list(reversed(lowercase     )     )
				_snake_case          =      reversed_block_out_channels[0]
				for i, up_block_type in enumerate(lowercase     ):
							_snake_case          =      output_channel
							_snake_case          =      reversed_block_out_channels[i]

							_snake_case          =      i == len(lowercase     ) - 1

							_snake_case          =      get_up_block(
							    lowercase      ,  num_layers=self.layers_per_block + 1      ,  in_channels=lowercase      ,  out_channels=lowercase      ,  prev_output_channel=lowercase      ,  add_upsample=not is_final_block      ,  resnet_eps=1E-6      ,  resnet_act_fn=lowercase      ,  resnet_groups=lowercase      ,  attention_head_dim=lowercase      ,  temb_channels=lowercase      ,  resnet_time_scale_shift=lowercase      ,  )
							self.up_blocks.append(lowercase     )
							_snake_case          =      output_channel

				# out
				if norm_type == "spatial":
							_snake_case          =      SpatialNorm(block_out_channels[0]      ,  lowercase     )
				else:
							_snake_case          =      nn.GroupNorm(num_channels=block_out_channels[0]      ,  num_groups=lowercase      ,  eps=1E-6     )
				_snake_case          =      nn.SiLU()
				_snake_case          =      nn.Convad(block_out_channels[0]      ,  lowercase      ,  3      ,  padding=1     )

				_snake_case          =      False





	def 			A							(  self     :   Any      ,  lowercase     :   Dict      ,  lowercase     :   Optional[Any]=None     ):


				'''simple docstring'''


				_snake_case          =      z
				_snake_case          =      self.conv_in(lowercase     )

				_snake_case          =      next(iter(self.up_blocks.parameters()     )     ).dtype
				if self.training and self.gradient_checkpointing:

							def create_custom_forward(lowercase     :   List[str]     ):
										def custom_forward(*lowercase     :   Tuple     ):
													return module(*lowercase     )

										return custom_forward

							if is_torch_version('>='      ,  '1.11.0'     ):
										# middle
										_snake_case          =      torch.utils.checkpoint.checkpoint(
										    create_custom_forward(self.mid_block     )      ,  lowercase      ,  lowercase      ,  use_reentrant=lowercase     )
										_snake_case          =      sample.to(lowercase     )

										# up
										for up_block in self.up_blocks:
													_snake_case          =      torch.utils.checkpoint.checkpoint(
													    create_custom_forward(lowercase     )      ,  lowercase      ,  lowercase      ,  use_reentrant=lowercase     )
							else:
										# middle
										_snake_case          =      torch.utils.checkpoint.checkpoint(
										    create_custom_forward(self.mid_block     )      ,  lowercase      ,  lowercase     )
										_snake_case          =      sample.to(lowercase     )

										# up
										for up_block in self.up_blocks:
													_snake_case          =      torch.utils.checkpoint.checkpoint(create_custom_forward(lowercase     )      ,  lowercase      ,  lowercase     )
				else:
							# middle
							_snake_case          =      self.mid_block(lowercase      ,  lowercase     )
							_snake_case          =      sample.to(lowercase     )

							# up
							for up_block in self.up_blocks:
										_snake_case          =      up_block(lowercase      ,  lowercase     )

        # post-process
				if latent_embeds is None:
							_snake_case          =      self.conv_norm_out(lowercase     )
				else:
							_snake_case          =      self.conv_norm_out(lowercase      ,  lowercase     )
				_snake_case          =      self.conv_act(lowercase     )
				_snake_case          =      self.conv_out(lowercase     )

				return sample




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Any      ,  lowercase     :   Union[str, Any]      ,  lowercase     :   Dict      ,  lowercase     :   Optional[int]      ,  lowercase     :   Tuple=None      ,  lowercase     :   Any="random"      ,  lowercase     :   int=False      ,  lowercase     :   List[str]=True     ):


				'''simple docstring'''


				super().__init__()
				_snake_case          =      n_e
				_snake_case          =      vq_embed_dim
				_snake_case          =      beta
				_snake_case          =      legacy

				_snake_case          =      nn.Embedding(self.n_e      ,  self.vq_embed_dim     )
				self.embedding.weight.data.uniform_(-1.0 / self.n_e      ,  1.0 / self.n_e     )

				_snake_case          =      remap
				if self.remap is not None:
							self.register_buffer('used'      ,  torch.tensor(np.load(self.remap     )     )     )
							_snake_case          =      self.used.shape[0]
							_snake_case          =      unknown_index  # "random" or "extra" or integer
							if self.unknown_index == "extra":
										_snake_case          =      self.re_embed
										_snake_case          =      self.re_embed + 1
							print(
							    f'''Remapping {self.n_e} indices to {self.re_embed} indices. '''
							    f'''Using {self.unknown_index} for unknown indices.'''     )
				else:
							_snake_case          =      n_e

				_snake_case          =      sane_index_shape



	def 			A							(  self     :   List[Any]      ,  lowercase     :   int     ):


				'''simple docstring'''


				_snake_case          =      inds.shape
				assert len(lowercase     ) > 1
				_snake_case          =      inds.reshape(ishape[0]      ,  -1     )
				_snake_case          =      self.used.to(lowercase     )
				_snake_case          =      (inds[:, :, None] == used[None, None, ...]).long()
				_snake_case          =      match.argmax(-1     )
				_snake_case          =      match.sum(2     ) < 1
				if self.unknown_index == "random":
							_snake_case          =      torch.randint(0      ,  self.re_embed      ,  size=new[unknown].shape     ).to(device=new.device     )
				else:
							_snake_case          =      self.unknown_index
				return new.reshape(lowercase     )



	def 			A							(  self     :   Tuple      ,  lowercase     :   List[str]     ):


				'''simple docstring'''


				_snake_case          =      inds.shape
				assert len(lowercase     ) > 1
				_snake_case          =      inds.reshape(ishape[0]      ,  -1     )
				_snake_case          =      self.used.to(lowercase     )
				if self.re_embed > self.used.shape[0]:  # extra token
							_snake_case          =      0  # simply set to zero
				_snake_case          =      torch.gather(used[None, :][inds.shape[0] * [0], :]      ,  1      ,  lowercase     )
				return back.reshape(lowercase     )



	def 			A							(  self     :   str      ,  lowercase     :   str     ):


				'''simple docstring'''


				_snake_case          =      z.permute(0      ,  2      ,  3      ,  1     ).contiguous()
				_snake_case          =      z.view(-1      ,  self.vq_embed_dim     )

				# distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
				_snake_case          =      torch.argmin(torch.cdist(lowercase      ,  self.embedding.weight     )      ,  dim=1     )

				_snake_case          =      self.embedding(lowercase     ).view(z.shape     )
				_snake_case          =      None
				_snake_case          =      None

				# compute loss for embedding
				if not self.legacy:
							_snake_case          =      self.beta * torch.mean((z_q.detach() - z) ** 2     ) + torch.mean((z_q - z.detach()) ** 2     )
				else:
							_snake_case          =      torch.mean((z_q.detach() - z) ** 2     ) + self.beta * torch.mean((z_q - z.detach()) ** 2     )

				# preserve gradients
				_snake_case          =      z + (z_q - z).detach()

				# reshape back to match original input shape
				_snake_case          =      z_q.permute(0      ,  3      ,  1      ,  2     ).contiguous()

				if self.remap is not None:
							_snake_case          =      min_encoding_indices.reshape(z.shape[0]      ,  -1     )  # add batch axis
							_snake_case          =      self.remap_to_used(lowercase     )
							_snake_case          =      min_encoding_indices.reshape(-1      ,  1     )  # flatten

				if self.sane_index_shape:
							_snake_case          =      min_encoding_indices.reshape(z_q.shape[0]      ,  z_q.shape[2]      ,  z_q.shape[3]     )

				return z_q, loss, (perplexity, min_encodings, min_encoding_indices)





	def 			A							(  self     :   List[Any]      ,  lowercase     :   Optional[int]      ,  lowercase     :   Tuple     ):


				'''simple docstring'''


				if self.remap is not None:
							_snake_case          =      indices.reshape(shape[0]      ,  -1     )  # add batch axis
							_snake_case          =      self.unmap_to_all(lowercase     )
							_snake_case          =      indices.reshape(-1     )  # flatten again

				# get quantized latent vectors
				_snake_case          =      self.embedding(lowercase     )

				if shape is not None:
							_snake_case          =      z_q.view(lowercase     )
							# reshape back to match original input shape
							_snake_case          =      z_q.permute(0      ,  3      ,  1      ,  2     ).contiguous()

				return z_q




class        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



	def __init__(  self     :   Optional[int]      ,  lowercase     :   Dict      ,  lowercase     :   Tuple=False     ):


				'''simple docstring'''


				_snake_case          =      parameters
				_snake_case     , _snake_case          =      torch.chunk(lowercase      ,  2      ,  dim=1     )
				_snake_case          =      torch.clamp(self.logvar      ,  -30.0      ,  20.0     )
				_snake_case          =      deterministic
				_snake_case          =      torch.exp(0.5 * self.logvar     )
				_snake_case          =      torch.exp(self.logvar     )
				if self.deterministic:
							_snake_case          =      _snake_case          =      torch.zeros_like(
							    self.mean      ,  device=self.parameters.device      ,  dtype=self.parameters.dtype     )



	def 			A							(  self     :   Union[str, Any]      ,  lowercase     :   Optional[torch.Generator] = None     ):


				'''simple docstring'''


				_snake_case          =      randn_tensor(
				    self.mean.shape      ,  generator=lowercase      ,  device=self.parameters.device      ,  dtype=self.parameters.dtype     )
				_snake_case          =      self.mean + self.std * sample
				return x



	def 			A							(  self     :   Union[str, Any]      ,  lowercase     :   int=None     ):


				'''simple docstring'''


				if self.deterministic:
							return torch.Tensor([0.0]     )
				else:
							if other is None:
										return 0.5 * torch.sum(torch.pow(self.mean      ,  2     ) + self.var - 1.0 - self.logvar      ,  dim=[1, 2, 3]     )
							else:
										return 0.5 * torch.sum(
										    torch.pow(self.mean - other.mean      ,  2     ) / other.var
										    + self.var / other.var
										    - 1.0
										    - self.logvar
										    + other.logvar      ,  dim=[1, 2, 3]      ,  )



	def 			A							(  self     :   Dict      ,  lowercase     :   str      ,  lowercase     :   Union[str, Any]=[1, 2, 3]     ):


				'''simple docstring'''


				if self.deterministic:
							return torch.Tensor([0.0]     )
				_snake_case          =      np.log(2.0 * np.pi     )
				return 0.5 * torch.sum(logtwopi + self.logvar + torch.pow(sample - self.mean      ,  2     ) / self.var      ,  dim=lowercase     )





	def 			A							(  self     :   Optional[int]     ):


				'''simple docstring'''


				return self.mean

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


_lowerCamelCase	:    List[Any]          =      logging.get_logger(__name__)

# General docstring
_lowerCamelCase	:    Dict          =      '''PoolFormerConfig'''

# Base docstring
_lowerCamelCase	:    int          =      '''sail/poolformer_s12'''
_lowerCamelCase	:    Optional[Any]          =      [1, 512, 7, 7]

# Image classification docstring
_lowerCamelCase	:    Optional[int]          =      '''sail/poolformer_s12'''
_lowerCamelCase	:    List[Any]          =      '''tabby, tabby cat'''

_lowerCamelCase	:    List[str]          =      [
    '''sail/poolformer_s12''',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]






def 	a_		(	__lowercase       :     List[Any]	, __lowercase       :     float = 0.0	, __lowercase       :     bool = False			)			->  Optional[int]:
			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(__lowercase	, dtype=input.dtype	, device=input.device			)
			random_tensor.floor_()  # binarize
			_snake_case          =      input.div(__lowercase			) * random_tensor
			return output




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



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


				'''simple docstring'''


				super().__init__()
				_snake_case          =      drop_prob



	def 			A							(  self     :   Any      ,  lowercase     :   torch.Tensor     ):


				'''simple docstring'''


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





	def 			A							(  self     :   Tuple     ):


				'''simple docstring'''


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




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Tuple      ,  lowercase     :   Dict      ,  lowercase     :   Dict      ,  lowercase     :   str      ,  lowercase     :   int      ,  lowercase     :   Optional[Any]      ,  lowercase     :   str=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 			A							(  self     :   int      ,  lowercase     :   Union[str, Any]     ):


				'''simple docstring'''


				_snake_case          =      self.projection(lowercase     )
				_snake_case          =      self.norm(lowercase     )
				return embeddings




class        SCREAMING_SNAKE_CASE__					(     nn.GroupNorm ):





	'''simple docstring'''



	def __init__(  self     :   Dict      ,  lowercase     :   List[Any]      ,  **lowercase     :   str     ):


				'''simple docstring'''


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




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   int      ,  lowercase     :   List[Any]     ):


				'''simple docstring'''


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





	def 			A							(  self     :   int      ,  lowercase     :   List[str]     ):


				'''simple docstring'''


				return self.pool(lowercase     ) - hidden_states




class        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Dict      ,  lowercase     :   Tuple      ,  lowercase     :   str      ,  lowercase     :   Optional[Any]      ,  lowercase     :   Union[str, 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 			A							(  self     :   Optional[int]      ,  lowercase     :   str     ):


				'''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        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   str      ,  lowercase     :   Tuple      ,  lowercase     :   int      ,  lowercase     :   str      ,  lowercase     :   Union[str, Any]      ,  lowercase     :   str      ,  lowercase     :   Dict     ):


				'''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 			A							(  self     :   Optional[int]      ,  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        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   Optional[Any]      ,  lowercase     :   Optional[int]     ):


				'''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 			A							(  self     :   Any      ,  lowercase     :   List[str]      ,  lowercase     :   str=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        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



	_UpperCAmelCase	:		List[Any]          = PoolFormerConfig
	_UpperCAmelCase	:		Optional[int]          = "poolformer"
	_UpperCAmelCase	:		str          = "pixel_values"
	_UpperCAmelCase	:		int          = True



	def 			A							(  self     :   Tuple      ,  lowercase     :   str     ):


				'''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 			A							(  self     :   Optional[Any]      ,  lowercase     :   str      ,  lowercase     :   Dict=False     ):


				'''simple docstring'''


				if isinstance(lowercase      ,  lowercase     ):
							_snake_case          =      value




_lowerCamelCase	:    Optional[Any]          =      r'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

_lowerCamelCase	:    Tuple          =      r'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`PoolFormerImageProcessor.__call__`] for details.
'''




@add_start_docstrings(
    "The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top."							,UpperCAmelCase							,)
class        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



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


				'''simple docstring'''


				super().__init__(lowercase     )
				_snake_case          =      config

				_snake_case          =      PoolFormerEncoder(lowercase     )

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



	def 			A							(  self     :   List[str]     ):


				'''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 			A							(  self     :   Tuple      ,  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        SCREAMING_SNAKE_CASE__					(     nn.Module ):





	'''simple docstring'''



	def __init__(  self     :   List[str]      ,  lowercase     :   Union[str, Any]     ):


				'''simple docstring'''


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





	def 			A							(  self     :   Optional[Any]      ,  lowercase     :   Optional[int]     ):


				'''simple docstring'''


				_snake_case          =      self.dense(lowercase     )
				return output




@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "							,UpperCAmelCase							,)
class        SCREAMING_SNAKE_CASE__					(     UpperCAmelCase ):





	'''simple docstring'''



	def __init__(  self     :   str      ,  lowercase     :   Any     ):


				'''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 			A							(  self     :   Union[str, Any]      ,  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     )