





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_     							= logging.get_logger(__name__)

# General docstring
UpperCAmelCase_     							= 'PoolFormerConfig'

# Base docstring
UpperCAmelCase_     							= 'sail/poolformer_s12'
UpperCAmelCase_     							= [1, 512, 7, 7]

# Image classification docstring
UpperCAmelCase_     							= 'sail/poolformer_s12'
UpperCAmelCase_     							= 'tabby, tabby cat'

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




def 		lowerCAmelCase_    (  __UpperCAmelCase:			List[str]		,   __UpperCAmelCase:			float = 0.0		,   __UpperCAmelCase:			bool = False      )      ->	str:
    if drop_prob == 0.0 or not training:
        return input
    UpperCamelCase__							:				int          =      1 - drop_prob
    UpperCamelCase__							:				Tuple          =      (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
    UpperCamelCase__							:				str          =      keep_prob + torch.rand(__UpperCAmelCase		,   dtype=input.dtype		,   device=input.device      )
    random_tensor.floor_()  # binarize
    UpperCamelCase__							:				Dict          =      input.div(__UpperCAmelCase      ) * random_tensor
    return output






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__ = None							) ->      None:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Optional[Any]          =      drop_prob




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      torch.Tensor:






       """simple docstring"""
       return drop_path(__magic_name__,		self.drop_prob,		self.training							)




   def       UpperCamelCase__  (    self							) ->      str:






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






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__=None							) ->      Optional[Any]:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				str          =      patch_size if isinstance(__magic_name__,		collections.abc.Iterable							) else (patch_size, patch_size)
       UpperCamelCase__							:				Optional[Any]          =      stride if isinstance(__magic_name__,		collections.abc.Iterable							) else (stride, stride)
       UpperCamelCase__							:				Union[str, Any]          =      padding if isinstance(__magic_name__,		collections.abc.Iterable							) else (padding, padding)

       UpperCamelCase__							:				Optional[Any]          =      nn.Convad(__magic_name__,		__magic_name__,		kernel_size=__magic_name__,		stride=__magic_name__,		padding=__magic_name__							)
       UpperCamelCase__							:				Tuple          =      norm_layer(__magic_name__							) if norm_layer else nn.Identity()




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      self.projection(__magic_name__							)
       UpperCamelCase__							:				Dict          =      self.norm(__magic_name__							)
       return embeddings






class     lowercase__       (	nn.GroupNorm				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		**__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       super().__init__(1,		__magic_name__,		**__magic_name__							)






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      List[str]:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Any          =      nn.AvgPoolad(__magic_name__,		stride=1,		padding=pool_size // 2,		count_include_pad=__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Union[str, Any]:






       """simple docstring"""
       return self.pool(__magic_name__							) - hidden_states






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      List[Any]:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Tuple          =      nn.Convad(__magic_name__,		__magic_name__,		1							)
       UpperCamelCase__							:				str          =      nn.Convad(__magic_name__,		__magic_name__,		1							)
       UpperCamelCase__							:				Tuple          =      PoolFormerDropPath(__magic_name__							)
       if isinstance(config.hidden_act,		__magic_name__							):
           UpperCamelCase__							:				Union[str, Any]          =      ACTaFN[config.hidden_act]
       else:
           UpperCamelCase__							:				str          =      config.hidden_act




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Tuple:






       """simple docstring"""
       UpperCamelCase__							:				int          =      self.conva(__magic_name__							)
       UpperCamelCase__							:				Union[str, Any]          =      self.act_fn(__magic_name__							)
       UpperCamelCase__							:				Tuple          =      self.drop(__magic_name__							)
       UpperCamelCase__							:				List[str]          =      self.conva(__magic_name__							)
       UpperCamelCase__							:				List[Any]          =      self.drop(__magic_name__							)

       return hidden_states






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							) ->      Optional[Any]:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Optional[int]          =      PoolFormerPooling(__magic_name__							)
       UpperCamelCase__							:				List[Any]          =      PoolFormerOutput(__magic_name__,		__magic_name__,		__magic_name__,		__magic_name__							)
       UpperCamelCase__							:				Any          =      PoolFormerGroupNorm(__magic_name__							)
       UpperCamelCase__							:				List[str]          =      PoolFormerGroupNorm(__magic_name__							)

       # Useful for training neural nets
       UpperCamelCase__							:				Optional[int]          =      PoolFormerDropPath(__magic_name__							) if drop_path > 0.0 else nn.Identity()
       UpperCamelCase__							:				Optional[int]          =      config.use_layer_scale
       if config.use_layer_scale:
           UpperCamelCase__							:				int          =      nn.Parameter(
               config.layer_scale_init_value * torch.ones((__magic_name__)							),		requires_grad=__magic_name__							)
           UpperCamelCase__							:				Dict          =      nn.Parameter(
               config.layer_scale_init_value * torch.ones((__magic_name__)							),		requires_grad=__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Dict:






       """simple docstring"""
       if self.use_layer_scale:
           UpperCamelCase__							:				int          =      self.pooling(self.before_norm(__magic_name__							)							)
           UpperCamelCase__							:				Union[str, Any]          =      self.layer_scale_a.unsqueeze(-1							).unsqueeze(-1							) * pooling_output
           # First residual connection
           UpperCamelCase__							:				List[Any]          =      hidden_states + self.drop_path(__magic_name__							)
           UpperCamelCase__							:				Tuple          =      ()

           UpperCamelCase__							:				int          =      self.output(self.after_norm(__magic_name__							)							)
           UpperCamelCase__							:				Optional[Any]          =      self.layer_scale_a.unsqueeze(-1							).unsqueeze(-1							) * layer_output
           # Second residual connection
           UpperCamelCase__							:				str          =      hidden_states + self.drop_path(__magic_name__							)

           UpperCamelCase__							:				Optional[int]          =      (output,) + outputs
           return outputs

       else:
           UpperCamelCase__							:				Tuple          =      self.drop_path(self.pooling(self.before_norm(__magic_name__							)							)							)
           # First residual connection
           UpperCamelCase__							:				int          =      pooling_output + hidden_states
           UpperCamelCase__							:				Dict          =      ()

           # Second residual connection inside the PoolFormerOutput block
           UpperCamelCase__							:				Optional[Any]          =      self.drop_path(self.output(self.after_norm(__magic_name__							)							)							)
           UpperCamelCase__							:				Any          =      hidden_states + layer_output

           UpperCamelCase__							:				List[str]          =      (output,) + outputs
           return outputs






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      int:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				Tuple          =      config
       # stochastic depth decay rule
       UpperCamelCase__							:				Optional[Any]          =      [x.item() for x in torch.linspace(0,		config.drop_path_rate,		sum(config.depths							)							)]

       # patch embeddings
       UpperCamelCase__							:				Any          =      []
       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__							:				str          =      nn.ModuleList(__magic_name__							)

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

       UpperCamelCase__							:				Optional[int]          =      nn.ModuleList(__magic_name__							)




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__=False,		__magic_name__=True							) ->      Any:






       """simple docstring"""
       UpperCamelCase__							:				int          =      () if output_hidden_states else None

       UpperCamelCase__							:				Tuple          =      pixel_values
       for idx, layers in enumerate(zip(self.patch_embeddings,		self.block							)							):
           UpperCamelCase__   ,UpperCamelCase__							:				Dict          =      layers
           # Get patch embeddings from hidden_states
           UpperCamelCase__							:				Union[str, Any]          =      embedding_layer(__magic_name__							)
           # Send the embeddings through the blocks
           for _, blk in enumerate(__magic_name__							):
               UpperCamelCase__							:				int          =      blk(__magic_name__							)
               UpperCamelCase__							:				Optional[int]          =      layer_outputs[0]

           if output_hidden_states:
               UpperCamelCase__							:				Optional[int]          =      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=__magic_name__,		hidden_states=__magic_name__							)






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   a						:							Optional[Any]   		=		PoolFormerConfig
   a						:							List[Any]   		=		"poolformer"
   a						:							Union[str, Any]   		=		"pixel_values"
   a						:							Union[str, Any]   		=		True




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      List[Any]:






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




   def       UpperCamelCase__  (    self,		__magic_name__,		__magic_name__=False							) ->      Union[str, Any]:






       """simple docstring"""
       if isinstance(__magic_name__,		__magic_name__							):
           UpperCamelCase__							:				Tuple          =      value




UpperCAmelCase_     							= 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_     							= 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     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      Any:






       """simple docstring"""
       super().__init__(__magic_name__							)
       UpperCamelCase__							:				List[str]          =      config

       UpperCamelCase__							:				List[str]          =      PoolFormerEncoder(__magic_name__							)

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




   def       UpperCamelCase__  (    self							) ->      Union[str, Any]:






       """simple docstring"""
       return self.embeddings.patch_embeddings




   @add_start_docstrings_to_model_forward(__magic_name__							)
   @add_code_sample_docstrings(
       checkpoint=_CHECKPOINT_FOR_DOC,		output_type=__magic_name__,		config_class=_CONFIG_FOR_DOC,		modality='''vision''',		expected_output=_EXPECTED_OUTPUT_SHAPE,		)
   def       UpperCamelCase__  (    self,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__ = None,		) ->      Union[Tuple, BaseModelOutputWithNoAttention]:






       """simple docstring"""
       UpperCamelCase__							:				Any          =      (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       UpperCamelCase__							:				Optional[Any]          =      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__							:				Union[str, Any]          =      self.encoder(
           __magic_name__,		output_hidden_states=__magic_name__,		return_dict=__magic_name__,		)
       UpperCamelCase__							:				int          =      encoder_outputs[0]

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

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






class     lowercase__       (	nn.Module				):

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      Dict:






       """simple docstring"""
       super().__init__()
       UpperCamelCase__							:				List[str]          =      nn.Linear(config.hidden_size,		config.hidden_size							)




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Optional[int]:






       """simple docstring"""
       UpperCamelCase__							:				Dict          =      self.dense(__magic_name__							)
       return output






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

   '''simple docstring'''


   def __init__(    self,		__magic_name__							) ->      List[Any]:






       """simple docstring"""
       super().__init__(__magic_name__							)
       UpperCamelCase__							:				Optional[Any]          =      config.num_labels
       UpperCamelCase__							:				Union[str, Any]          =      PoolFormerModel(__magic_name__							)

       # Final norm
       UpperCamelCase__							:				Optional[int]          =      PoolFormerGroupNorm(config.hidden_sizes[-1]							)
       # Classifier head
       UpperCamelCase__							:				Optional[int]          =      (
           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(__magic_name__							)
   @add_code_sample_docstrings(
       checkpoint=_IMAGE_CLASS_CHECKPOINT,		output_type=__magic_name__,		config_class=_CONFIG_FOR_DOC,		expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT,		)
   def       UpperCamelCase__  (    self,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__ = None,		__magic_name__ = None,		) ->      Union[Tuple, ImageClassifierOutputWithNoAttention]:






       """simple docstring"""
       UpperCamelCase__							:				Tuple          =      return_dict if return_dict is not None else self.config.use_return_dict

       UpperCamelCase__							:				Tuple          =      self.poolformer(
           __magic_name__,		output_hidden_states=__magic_name__,		return_dict=__magic_name__,		)

       UpperCamelCase__							:				Dict          =      outputs[0]

       UpperCamelCase__							:				List[Any]          =      self.classifier(self.norm(__magic_name__							).mean([-2, -1]							)							)

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

           if self.config.problem_type == "regression":
               UpperCamelCase__							:				Union[str, Any]          =      MSELoss()
               if self.num_labels == 1:
                   UpperCamelCase__							:				Optional[Any]          =      loss_fct(logits.squeeze(),		labels.squeeze()							)
               else:
                   UpperCamelCase__							:				int          =      loss_fct(__magic_name__,		__magic_name__							)
           elif self.config.problem_type == "single_label_classification":
               UpperCamelCase__							:				Dict          =      CrossEntropyLoss()
               UpperCamelCase__							:				str          =      loss_fct(logits.view(-1,		self.num_labels							),		labels.view(-1							)							)
           elif self.config.problem_type == "multi_label_classification":
               UpperCamelCase__							:				Optional[Any]          =      BCEWithLogitsLoss()
               UpperCamelCase__							:				int          =      loss_fct(__magic_name__,		__magic_name__							)

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

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








import re
from pathlib import Path
from unittest import TestCase

import pytest






@pytest.mark.integration
class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Union[str, Any]:






       """simple docstring"""

       with open(__magic_name__,		encoding='''utf-8'''							) as input_file:
           UpperCamelCase__							:				Tuple          =      re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)'''							)
           UpperCamelCase__							:				str          =      input_file.read()
           UpperCamelCase__							:				List[Any]          =      regexp.search(__magic_name__							)

       return match




   def       UpperCamelCase__  (    self,		__magic_name__							) ->      Any:






       """simple docstring"""

       with open(__magic_name__,		encoding='''utf-8'''							) as input_file:
           UpperCamelCase__							:				Dict          =      re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''',		re.DOTALL							)
           UpperCamelCase__							:				Any          =      input_file.read()
           # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
           UpperCamelCase__							:				Tuple          =      regexp.finditer(__magic_name__							)

       UpperCamelCase__							:				Dict          =      [match for match in matches if match is not None and match.group(1							) is not None]
       return matches[0] if matches else None




   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      Path('''./datasets'''							)
       UpperCamelCase__							:				Any          =      list(dataset_paths.absolute().glob('''**/*.py'''							)							)

       for dataset in dataset_files:
           if self._no_encoding_on_file_open(str(__magic_name__							)							):
               raise AssertionError(f"open(...) must use utf-8 encoding in {dataset}"							)




   def       UpperCamelCase__  (    self							) ->      Dict:






       """simple docstring"""
       UpperCamelCase__							:				Optional[int]          =      Path('''./datasets'''							)
       UpperCamelCase__							:				Optional[Any]          =      list(dataset_paths.absolute().glob('''**/*.py'''							)							)

       for dataset in dataset_files:
           if self._no_print_statements(str(__magic_name__							)							):
               raise AssertionError(f"print statement found in {dataset}. Use datasets.logger/logging instead."							)

