

"""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


SCREAMING_SNAKE_CASE_					:	Tuple							  =						logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE_					:	str							  =						'PoolFormerConfig'

# Base docstring
SCREAMING_SNAKE_CASE_					:	Any							  =						'sail/poolformer_s12'
SCREAMING_SNAKE_CASE_					:	List[Any]							  =						[1, 5_1_2, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE_					:	int							  =						'sail/poolformer_s12'
SCREAMING_SNAKE_CASE_					:	Tuple							  =						'tabby, tabby cat'

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





def  _snake_case     (    UpperCAmelCase_  :					Optional[Any]					,   UpperCAmelCase_  :					float = 0.0					,   UpperCAmelCase_  :					bool = False			):
     if drop_prob == 0.0 or not training:
          return input
     A__										=  1 - drop_prob
     A__										=  (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
     A__										=  keep_prob + torch.rand(UpperCAmelCase_					,   dtype=input.dtype					,   device=input.device			)
     random_tensor.floor_()  # binarize
     A__										=  input.div(UpperCAmelCase_			) * random_tensor
     return output



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Union[str, Any]    ,						UpperCamelCase: Optional[float] = None       ):






         """simple docstring"""


         super().__init__()
         A__										=  drop_prob

    def 				UpperCamelCase	(     self: List[Any]    ,						UpperCamelCase: torch.Tensor       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: str       ):






         """simple docstring"""


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



class 					a						(       nn.Module				):



    """simple docstring"""



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






         """simple docstring"""


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

         A__										=  nn.Convad(UpperCamelCase    ,						UpperCamelCase    ,						kernel_size=UpperCamelCase    ,						stride=UpperCamelCase    ,						padding=UpperCamelCase       )
         A__										=  norm_layer(UpperCamelCase       ) if norm_layer else nn.Identity()





    def 				UpperCamelCase	(     self: Union[str, Any]    ,						UpperCamelCase: str       ):






         """simple docstring"""


         A__										=  self.projection(UpperCamelCase       )
         A__										=  self.norm(UpperCamelCase       )
         return embeddings



class 					a						(       nn.GroupNorm				):



    """simple docstring"""



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






         """simple docstring"""


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



class 					a						(       nn.Module				):



    """simple docstring"""



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






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: Dict    ,						UpperCamelCase: Optional[Any]       ):






         """simple docstring"""


         return self.pool(UpperCamelCase       ) - hidden_states



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Optional[Any]    ,						UpperCamelCase: Union[str, Any]    ,						UpperCamelCase: Tuple    ,						UpperCamelCase: Dict    ,						UpperCamelCase: Any       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         A__										=  self.conva(UpperCamelCase       )
         A__										=  self.act_fn(UpperCamelCase       )
         A__										=  self.drop(UpperCamelCase       )
         A__										=  self.conva(UpperCamelCase       )
         A__										=  self.drop(UpperCamelCase       )

         return hidden_states



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: List[str]    ,						UpperCamelCase: Dict    ,						UpperCamelCase: Optional[int]    ,						UpperCamelCase: Union[str, Any]    ,						UpperCamelCase: Tuple    ,						UpperCamelCase: str    ,						UpperCamelCase: int       ):






         """simple docstring"""


         super().__init__()
         A__										=  PoolFormerPooling(UpperCamelCase       )
         A__										=  PoolFormerOutput(UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase    ,						UpperCamelCase       )
         A__										=  PoolFormerGroupNorm(UpperCamelCase       )
         A__										=  PoolFormerGroupNorm(UpperCamelCase       )

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





    def 				UpperCamelCase	(     self: Union[str, Any]    ,						UpperCamelCase: str       ):






         """simple docstring"""


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

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

              A__										=  (output,) + outputs
              return outputs

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

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

              A__										=  (output,) + outputs
              return outputs



class 					a						(       nn.Module				):



    """simple docstring"""



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






         """simple docstring"""


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

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

         # Transformer blocks
         A__										=  []
         A__										=  0
         for i in range(config.num_encoder_blocks       ):
              # each block consists of layers
              A__										=  []
              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       )       )

         A__										=  nn.ModuleList(UpperCamelCase       )





    def 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: Any    ,						UpperCamelCase: int=False    ,						UpperCamelCase: Union[str, Any]=True       ):






         """simple docstring"""


         A__										=  () if output_hidden_states else None

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

              if output_hidden_states:
                   A__										=  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 					a						(       _lowerCamelCase				):



    """simple docstring"""



    UpperCAmelCase						 =					PoolFormerConfig
    UpperCAmelCase						 =					"poolformer"
    UpperCAmelCase						 =					"pixel_values"
    UpperCAmelCase						 =					True

    def 				UpperCamelCase	(     self: List[str]    ,						UpperCamelCase: str       ):






         """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: Optional[int]    ,						UpperCamelCase: Union[str, Any]    ,						UpperCamelCase: Tuple=False       ):






         """simple docstring"""


         if isinstance(UpperCamelCase    ,						UpperCamelCase       ):
              A__										=  value

SCREAMING_SNAKE_CASE_					:	str							  =						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'

SCREAMING_SNAKE_CASE_					:	Dict							  =						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 					a						(       _lowerCamelCase				):



    """simple docstring"""



    def __init__(     self: Tuple    ,						UpperCamelCase: Tuple       ):






         """simple docstring"""


         super().__init__(UpperCamelCase       )
         A__										=  config

         A__										=  PoolFormerEncoder(UpperCamelCase       )

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

    def 				UpperCamelCase	(     self: Dict       ):






         """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: int    ,						UpperCamelCase: Optional[torch.FloatTensor] = None    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Optional[bool] = None    ,						):






         """simple docstring"""


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

         A__										=  self.encoder(
             UpperCamelCase    ,						output_hidden_states=UpperCamelCase    ,						return_dict=UpperCamelCase    ,						)
         A__										=  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 					a						(       nn.Module				):



    """simple docstring"""



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






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: Dict    ,						UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         A__										=  self.dense(UpperCamelCase       )
         return output



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



    """simple docstring"""



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






         """simple docstring"""


         super().__init__(UpperCamelCase       )
         A__										=  config.num_labels
         A__										=  PoolFormerModel(UpperCamelCase       )

         # Final norm
         A__										=  PoolFormerGroupNorm(config.hidden_sizes[-1]       )
         # Classifier head
         A__										=  (
             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: Any    ,						UpperCamelCase: Optional[torch.FloatTensor] = None    ,						UpperCamelCase: Optional[torch.LongTensor] = None    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Optional[bool] = None    ,						):






         """simple docstring"""


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

         A__										=  self.poolformer(
             UpperCamelCase    ,						output_hidden_states=UpperCamelCase    ,						return_dict=UpperCamelCase    ,						)

         A__										=  outputs[0]

         A__										=  self.classifier(self.norm(UpperCamelCase       ).mean([-2, -1]       )       )

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

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

         if not return_dict:
              A__										=  (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"""



def  _snake_case     (    UpperCAmelCase_  :					List[str]			):
     A__										=  [0] * len(UpperCAmelCase_			)
     A__										=  []
     A__										=  [1] * len(UpperCAmelCase_			)

     for values in graph.values():
          for i in values:
               indegree[i] += 1

     for i in range(len(UpperCAmelCase_			)			):
          if indegree[i] == 0:
               queue.append(UpperCAmelCase_			)

     while queue:
          A__										=  queue.pop(0			)
          for x in graph[vertex]:
               indegree[x] -= 1

               if long_dist[vertex] + 1 > long_dist[x]:
                    A__										=  long_dist[vertex] + 1

               if indegree[x] == 0:
                    queue.append(UpperCAmelCase_			)

     print(max(UpperCAmelCase_			)			)


# Adjacency list of Graph
SCREAMING_SNAKE_CASE_					:	Any							  =						{0: [2, 3, 4], 1: [2, 7], 2: [5], 3: [5, 7], 4: [7], 5: [6], 6: [7], 7: []}

longest_distance(graph)






