
"""simple docstring"""

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


UpperCamelCase     :    Dict       =			logging.get_logger(__name__)

# General docstring
UpperCamelCase     :    List[str]       =			"PoolFormerConfig"

# Base docstring
UpperCamelCase     :    Tuple       =			"sail/poolformer_s12"
UpperCamelCase     :    str       =			[1, 5_1_2, 7, 7]

# Image classification docstring
UpperCamelCase     :    Union[str, Any]       =			"sail/poolformer_s12"
UpperCamelCase     :    str       =			"tabby, tabby cat"

UpperCamelCase     :    Dict       =			[
    "sail/poolformer_s12",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]






def 							A						(    snake_case				:List[str]    , snake_case				:float = 0.0    , snake_case				:bool = False		)				->     Optional[Any]:





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

class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase = None				):

      '''simple docstring'''




      super().__init__()
      __UpperCamelCase      =	drop_prob





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




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







     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




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


class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase=None				):

      '''simple docstring'''




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

      __UpperCamelCase      =	nn.Convad(_UpperCAmelCase     ,					_UpperCAmelCase     ,					kernel_size=_UpperCAmelCase     ,					stride=_UpperCAmelCase     ,					padding=_UpperCAmelCase				)
      __UpperCamelCase      =	norm_layer(_UpperCAmelCase				) if norm_layer else nn.Identity()







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	self.projection(_UpperCAmelCase				)
      __UpperCamelCase      =	self.norm(_UpperCAmelCase				)
      return embeddings


class __lowerCAmelCase		(				nn.GroupNorm      ):





     def __init__(     self     ,					__UpperCAmelCase     ,					**__UpperCAmelCase				):

      '''simple docstring'''




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


class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




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







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      return self.pool(_UpperCAmelCase				) - hidden_states


class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




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







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	self.conva(_UpperCAmelCase				)
      __UpperCamelCase      =	self.act_fn(_UpperCAmelCase				)
      __UpperCamelCase      =	self.drop(_UpperCAmelCase				)
      __UpperCamelCase      =	self.conva(_UpperCAmelCase				)
      __UpperCamelCase      =	self.drop(_UpperCAmelCase				)

      return hidden_states


class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      super().__init__()
      __UpperCamelCase      =	PoolFormerPooling(_UpperCAmelCase				)
      __UpperCamelCase      =	PoolFormerOutput(_UpperCAmelCase     ,					_UpperCAmelCase     ,					_UpperCAmelCase     ,					_UpperCAmelCase				)
      __UpperCamelCase      =	PoolFormerGroupNorm(_UpperCAmelCase				)
      __UpperCamelCase      =	PoolFormerGroupNorm(_UpperCAmelCase				)

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







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




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

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

       __UpperCamelCase      =	(output,) + outputs
       return outputs

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

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

       __UpperCamelCase      =	(output,) + outputs
       return outputs


class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




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

      # patch embeddings
      __UpperCamelCase      =	[]
      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]     ,					)				)
      __UpperCamelCase      =	nn.ModuleList(_UpperCAmelCase				)

      # Transformer blocks
      __UpperCamelCase      =	[]
      __UpperCamelCase      =	0
      for i in range(config.num_encoder_blocks				):
       # each block consists of layers
       __UpperCamelCase      =	[]
       if i != 0:
        cur += config.depths[i - 1]
       for j in range(config.depths[i]				):
        layers.append(
            PoolFormerLayer(
                _UpperCAmelCase     ,					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(_UpperCAmelCase				)				)

      __UpperCamelCase      =	nn.ModuleList(_UpperCAmelCase				)







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=False     ,					__UpperCAmelCase=True				):

      '''simple docstring'''




      __UpperCamelCase      =	() if output_hidden_states else None

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

       if output_hidden_states:
        __UpperCamelCase      =	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=_UpperCAmelCase     ,					hidden_states=_UpperCAmelCase				)


class __lowerCAmelCase		(				lowerCamelCase_      ):
     lowercase						       =						PoolFormerConfig
     lowercase						       =						"""poolformer"""
     lowercase						       =						"""pixel_values"""
     lowercase						       =						True





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      if isinstance(_UpperCAmelCase     ,					(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(_UpperCAmelCase     ,					nn.LayerNorm				):
       module.bias.data.zero_()
       module.weight.data.fill_(1.0				)







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase=False				):

      '''simple docstring'''




      if isinstance(_UpperCAmelCase     ,					_UpperCAmelCase				):
       __UpperCamelCase      =	value


UpperCamelCase     :    Any       =			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"

UpperCamelCase     :    Tuple       =			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."      ,  lowerCamelCase_      ,  )
class __lowerCAmelCase		(				lowerCamelCase_      ):





     def __init__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      super().__init__(_UpperCAmelCase				)
      __UpperCamelCase      =	config

      __UpperCamelCase      =	PoolFormerEncoder(_UpperCAmelCase				)

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





     def 						UpperCAmelCase (     self				):

      '''simple docstring'''




      return self.embeddings.patch_embeddings







     @add_start_docstrings_to_model_forward(_UpperCAmelCase				)
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC     ,					output_type=_UpperCAmelCase     ,					config_class=_CONFIG_FOR_DOC     ,					modality='vision'     ,					expected_output=_EXPECTED_OUTPUT_SHAPE     ,					)
     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase = None     ,					__UpperCAmelCase = None     ,					__UpperCAmelCase = None     ,					):

      '''simple docstring'''




      __UpperCamelCase      =	(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      __UpperCamelCase      =	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'				)

      __UpperCamelCase      =	self.encoder(
          _UpperCAmelCase     ,					output_hidden_states=_UpperCAmelCase     ,					return_dict=_UpperCAmelCase     ,					)
      __UpperCamelCase      =	encoder_outputs[0]

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

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


class __lowerCAmelCase		(				nn.Module      ):





     def __init__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




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







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	self.dense(_UpperCAmelCase				)
      return output


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





     def __init__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      super().__init__(_UpperCAmelCase				)
      __UpperCamelCase      =	config.num_labels
      __UpperCamelCase      =	PoolFormerModel(_UpperCAmelCase				)

      # Final norm
      __UpperCamelCase      =	PoolFormerGroupNorm(config.hidden_sizes[-1]				)
      # Classifier head
      __UpperCamelCase      =	(
          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(_UpperCAmelCase				)
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT     ,					output_type=_UpperCAmelCase     ,					config_class=_CONFIG_FOR_DOC     ,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT     ,					)
     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase = None     ,					__UpperCAmelCase = None     ,					__UpperCAmelCase = None     ,					__UpperCAmelCase = None     ,					):

      '''simple docstring'''




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

      __UpperCamelCase      =	self.poolformer(
          _UpperCAmelCase     ,					output_hidden_states=_UpperCAmelCase     ,					return_dict=_UpperCAmelCase     ,					)

      __UpperCamelCase      =	outputs[0]

      __UpperCamelCase      =	self.classifier(self.norm(_UpperCAmelCase				).mean([-2, -1]				)				)

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

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

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

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





"""simple docstring"""

import heapq as hq
import math
from collections.abc import Iterator

class __lowerCAmelCase		:





     def __init__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	str(id_				)
      __UpperCamelCase      =	None
      __UpperCamelCase      =	None
      __UpperCamelCase      =	[]
      __UpperCamelCase      =	{}  # {vertex:distance}





     def __lt__(     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      return self.key < other.key





     def __repr__(     self				):

      '''simple docstring'''




      return self.id





     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase				):

      '''simple docstring'''




      self.neighbors.append(__UpperCAmelCase				)







     def 						UpperCAmelCase (     self     ,					__UpperCAmelCase     ,					__UpperCAmelCase				):

      '''simple docstring'''




      __UpperCamelCase      =	weight






def 							A						(    snake_case				:List[Any]    , snake_case				:Dict    , snake_case				:Any    , snake_case				:str		)				->     List[str]:
 # add the neighbors:
 graph[a - 1].add_neighbor(graph[b - 1]		)
 graph[b - 1].add_neighbor(graph[a - 1]		)
 # add the edges:
 graph[a - 1].add_edge(graph[b - 1]    , snake_case		)
 graph[b - 1].add_edge(graph[a - 1]    , snake_case		)






def 							A						(    snake_case				:list    , snake_case				:Vertex		)				->     list:
 __UpperCamelCase      =	[]
 for u in graph:
  __UpperCamelCase      =	math.inf
  __UpperCamelCase      =	None
 __UpperCamelCase      =	0
 __UpperCamelCase      =	graph[:]
 while q:
  __UpperCamelCase      =	min(snake_case		)
  q.remove(snake_case		)
  for v in u.neighbors:
   if (v in q) and (u.edges[v.id] < v.key):
    __UpperCamelCase      =	u
    __UpperCamelCase      =	u.edges[v.id]
 for i in range(1    , len(snake_case		)		):
  a.append((int(graph[i].id		) + 1, int(graph[i].pi.id		) + 1)		)
 return a






def 							A						(    snake_case				:list    , snake_case				:Vertex		)				->     Iterator[tuple]:
 for u in graph:
  __UpperCamelCase      =	math.inf
  __UpperCamelCase      =	None
 __UpperCamelCase      =	0

 __UpperCamelCase      =	list(snake_case		)
 hq.heapify(snake_case		)

 while h:
  __UpperCamelCase      =	hq.heappop(snake_case		)
  for v in u.neighbors:
   if (v in h) and (u.edges[v.id] < v.key):
    __UpperCamelCase      =	u
    __UpperCamelCase      =	u.edges[v.id]
    hq.heapify(snake_case		)

 for i in range(1    , len(snake_case		)		):
  yield (int(graph[i].id		) + 1, int(graph[i].pi.id		) + 1)






def 							A						(    )				->     None:
 pass


if __name__ == "__main__":
      import doctest

      doctest.testmod()



