






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


snake_case    				=		logging.get_logger(__name__)

# General docstring
snake_case    				=		'''PoolFormerConfig'''

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

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

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


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

  """simple docstring"""






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


class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	Tuple					, UpperCAmelCase_       :	Optional[float] = None      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Tuple							=    drop_prob


  def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	torch.Tensor      ):
    return drop_path(__lowerCAmelCase					, self.drop_prob					, self.training      )






  def 				_A       (				self       :	List[str]      ):
    return "p={}".format(self.drop_prob      )





class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	Union[str, Any]					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Dict=None      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Dict							=    patch_size if isinstance(__lowerCAmelCase					, collections.abc.Iterable      ) else (patch_size, patch_size)
    SCREAMING_SNAKE_CASE       :      Optional[int]							=    stride if isinstance(__lowerCAmelCase					, collections.abc.Iterable      ) else (stride, stride)
    SCREAMING_SNAKE_CASE       :      Optional[int]							=    padding if isinstance(__lowerCAmelCase					, collections.abc.Iterable      ) else (padding, padding)

    SCREAMING_SNAKE_CASE       :      Tuple							=    nn.Convad(__lowerCAmelCase					, __lowerCAmelCase					, kernel_size=__lowerCAmelCase					, stride=__lowerCAmelCase					, padding=__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Optional[int]							=    norm_layer(__lowerCAmelCase      ) if norm_layer else nn.Identity()






  def 				_A       (				self       :	List[str]					, UpperCAmelCase_       :	Union[str, Any]      ):
    SCREAMING_SNAKE_CASE       :      Tuple							=    self.projection(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.norm(__lowerCAmelCase      )
    return embeddings





class  SCREAMING_SNAKE_CASE			(			nn.GroupNorm					):
  '''simple docstring'''


  def __init__(				self       :	Tuple					, UpperCAmelCase_       :	int					, **UpperCAmelCase_       :	Optional[Any]      ):
    super().__init__(1					, __lowerCAmelCase					, **__lowerCAmelCase      )





class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	int					, UpperCAmelCase_       :	Optional[Any]      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    nn.AvgPoolad(__lowerCAmelCase					, stride=1					, padding=pool_size // 2					, count_include_pad=__lowerCAmelCase      )






  def 				_A       (				self       :	Optional[Any]					, UpperCAmelCase_       :	List[Any]      ):
    return self.pool(__lowerCAmelCase      ) - hidden_states





class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	Tuple					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Any      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    nn.Convad(__lowerCAmelCase					, __lowerCAmelCase					, 1      )
    SCREAMING_SNAKE_CASE       :      str							=    nn.Convad(__lowerCAmelCase					, __lowerCAmelCase					, 1      )
    SCREAMING_SNAKE_CASE       :      str							=    PoolFormerDropPath(__lowerCAmelCase      )
    if isinstance(config.hidden_act					, __lowerCAmelCase      ):
      SCREAMING_SNAKE_CASE       :      Tuple							=    ACTaFN[config.hidden_act]
    else:
      SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    config.hidden_act






  def 				_A       (				self       :	Tuple					, UpperCAmelCase_       :	List[str]      ):
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.conva(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Tuple							=    self.act_fn(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Optional[int]							=    self.drop(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      List[Any]							=    self.conva(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Optional[Any]							=    self.drop(__lowerCAmelCase      )

    return hidden_states





class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	Dict					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	int					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Union[str, Any]      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Dict							=    PoolFormerPooling(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Tuple							=    PoolFormerOutput(__lowerCAmelCase					, __lowerCAmelCase					, __lowerCAmelCase					, __lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    PoolFormerGroupNorm(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    PoolFormerGroupNorm(__lowerCAmelCase      )

    # Useful for training neural nets
    SCREAMING_SNAKE_CASE       :      str							=    PoolFormerDropPath(__lowerCAmelCase      ) if drop_path > 0.0 else nn.Identity()
    SCREAMING_SNAKE_CASE       :      Any							=    config.use_layer_scale
    if config.use_layer_scale:
      SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    nn.Parameter(
          config.layer_scale_init_value * torch.ones((__lowerCAmelCase)      )					, requires_grad=__lowerCAmelCase      )
      SCREAMING_SNAKE_CASE       :      int							=    nn.Parameter(
          config.layer_scale_init_value * torch.ones((__lowerCAmelCase)      )					, requires_grad=__lowerCAmelCase      )






  def 				_A       (				self       :	Dict					, UpperCAmelCase_       :	Dict      ):






    if self.use_layer_scale:
      SCREAMING_SNAKE_CASE       :      Dict							=    self.pooling(self.before_norm(__lowerCAmelCase      )      )
      SCREAMING_SNAKE_CASE       :      Optional[int]							=    self.layer_scale_a.unsqueeze(-1      ).unsqueeze(-1      ) * pooling_output
      # First residual connection
      SCREAMING_SNAKE_CASE       :      List[Any]							=    hidden_states + self.drop_path(__lowerCAmelCase      )
      SCREAMING_SNAKE_CASE       :      Tuple							=    ()

      SCREAMING_SNAKE_CASE       :      Optional[int]							=    self.output(self.after_norm(__lowerCAmelCase      )      )
      SCREAMING_SNAKE_CASE       :      Dict							=    self.layer_scale_a.unsqueeze(-1      ).unsqueeze(-1      ) * layer_output
      # Second residual connection
      SCREAMING_SNAKE_CASE       :      Optional[int]							=    hidden_states + self.drop_path(__lowerCAmelCase      )

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

    else:
      SCREAMING_SNAKE_CASE       :      List[Any]							=    self.drop_path(self.pooling(self.before_norm(__lowerCAmelCase      )      )      )
      # First residual connection
      SCREAMING_SNAKE_CASE       :      Any							=    pooling_output + hidden_states
      SCREAMING_SNAKE_CASE       :      Optional[int]							=    ()

      # Second residual connection inside the PoolFormerOutput block
      SCREAMING_SNAKE_CASE       :      Tuple							=    self.drop_path(self.output(self.after_norm(__lowerCAmelCase      )      )      )
      SCREAMING_SNAKE_CASE       :      int							=    hidden_states + layer_output

      SCREAMING_SNAKE_CASE       :      Dict							=    (output,) + outputs
      return outputs





class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	Any					, UpperCAmelCase_       :	Dict      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    config
    # stochastic depth decay rule
    SCREAMING_SNAKE_CASE       :      Dict							=    [x.item() for x in torch.linspace(0					, config.drop_path_rate					, sum(config.depths      )      )]

    # patch embeddings
    SCREAMING_SNAKE_CASE       :      Optional[int]							=    []
    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]					, )      )
    SCREAMING_SNAKE_CASE       :      int							=    nn.ModuleList(__lowerCAmelCase      )

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

    SCREAMING_SNAKE_CASE       :      str							=    nn.ModuleList(__lowerCAmelCase      )






  def 				_A       (				self       :	Any					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Optional[Any]=False					, UpperCAmelCase_       :	List[str]=True      ):
    SCREAMING_SNAKE_CASE       :      int							=    () if output_hidden_states else None

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

      if output_hidden_states:
        SCREAMING_SNAKE_CASE       :      Any							=    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=__lowerCAmelCase					, hidden_states=__lowerCAmelCase      )





class  SCREAMING_SNAKE_CASE			(			UpperCAmelCase__					):
  '''simple docstring'''


  UpperCamelCase_							:		List[str]			=	PoolFormerConfig
  UpperCamelCase_							:		Optional[Any]			=	'''poolformer'''
  UpperCamelCase_							:		int			=	'''pixel_values'''
  UpperCamelCase_							:		Optional[Any]			=	True


  def 				_A       (				self       :	List[str]					, UpperCAmelCase_       :	List[str]      ):






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






  def 				_A       (				self       :	Optional[int]					, UpperCAmelCase_       :	Optional[int]					, UpperCAmelCase_       :	Optional[Any]=False      ):






    if isinstance(__lowerCAmelCase					, __lowerCAmelCase      ):
      SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    value





snake_case    				=		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.
'''

snake_case    				=		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       :	Any					, UpperCAmelCase_       :	Union[str, Any]      ):
    super().__init__(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      Tuple							=    config

    SCREAMING_SNAKE_CASE       :      Dict							=    PoolFormerEncoder(__lowerCAmelCase      )

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


  def 				_A       (				self       :	List[Any]      ):
    return self.embeddings.patch_embeddings






  @add_start_docstrings_to_model_forward(__lowerCAmelCase      )
  @add_code_sample_docstrings(
      checkpoint=_CHECKPOINT_FOR_DOC					, output_type=__lowerCAmelCase					, config_class=_CONFIG_FOR_DOC					, modality="vision"					, expected_output=_EXPECTED_OUTPUT_SHAPE					, )
  def 				_A       (				self       :	int					, UpperCAmelCase_       :	Optional[torch.FloatTensor] = None					, UpperCAmelCase_       :	Optional[bool] = None					, UpperCAmelCase_       :	Optional[bool] = None					, ):
    SCREAMING_SNAKE_CASE       :      str							=    (
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    SCREAMING_SNAKE_CASE       :      Dict							=    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"      )

    SCREAMING_SNAKE_CASE       :      List[str]							=    self.encoder(
        __lowerCAmelCase					, output_hidden_states=__lowerCAmelCase					, return_dict=__lowerCAmelCase					, )
    SCREAMING_SNAKE_CASE       :      Dict							=    encoder_outputs[0]

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

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





class  SCREAMING_SNAKE_CASE			(			nn.Module					):
  '''simple docstring'''


  def __init__(				self       :	Union[str, Any]					, UpperCAmelCase_       :	Any      ):
    super().__init__()
    SCREAMING_SNAKE_CASE       :      Optional[Any]							=    nn.Linear(config.hidden_size					, config.hidden_size      )






  def 				_A       (				self       :	int					, UpperCAmelCase_       :	List[Any]      ):
    SCREAMING_SNAKE_CASE       :      Optional[Any]							=    self.dense(__lowerCAmelCase      )
    return output





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


  def __init__(				self       :	Dict					, UpperCAmelCase_       :	Optional[int]      ):
    super().__init__(__lowerCAmelCase      )
    SCREAMING_SNAKE_CASE       :      List[Any]							=    config.num_labels
    SCREAMING_SNAKE_CASE       :      Tuple							=    PoolFormerModel(__lowerCAmelCase      )

    # Final norm
    SCREAMING_SNAKE_CASE       :      List[str]							=    PoolFormerGroupNorm(config.hidden_sizes[-1]      )
    # Classifier head
    SCREAMING_SNAKE_CASE       :      str							=    (
        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(__lowerCAmelCase      )
  @add_code_sample_docstrings(
      checkpoint=_IMAGE_CLASS_CHECKPOINT					, output_type=__lowerCAmelCase					, config_class=_CONFIG_FOR_DOC					, expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT					, )
  def 				_A       (				self       :	Union[str, Any]					, UpperCAmelCase_       :	Optional[torch.FloatTensor] = None					, UpperCAmelCase_       :	Optional[torch.LongTensor] = None					, UpperCAmelCase_       :	Optional[bool] = None					, UpperCAmelCase_       :	Optional[bool] = None					, ):
    SCREAMING_SNAKE_CASE       :      Any							=    return_dict if return_dict is not None else self.config.use_return_dict

    SCREAMING_SNAKE_CASE       :      str							=    self.poolformer(
        __lowerCAmelCase					, output_hidden_states=__lowerCAmelCase					, return_dict=__lowerCAmelCase					, )

    SCREAMING_SNAKE_CASE       :      Optional[Any]							=    outputs[0]

    SCREAMING_SNAKE_CASE       :      Any							=    self.classifier(self.norm(__lowerCAmelCase      ).mean([-2, -1]      )      )

    SCREAMING_SNAKE_CASE       :      Dict							=    None
    if labels is not None:
      if self.config.problem_type is None:
        if self.num_labels == 1:
          SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    "regression"
        elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
          SCREAMING_SNAKE_CASE       :      Optional[int]							=    "single_label_classification"
        else:
          SCREAMING_SNAKE_CASE       :      int							=    "multi_label_classification"

      if self.config.problem_type == "regression":
        SCREAMING_SNAKE_CASE       :      Any							=    MSELoss()
        if self.num_labels == 1:
          SCREAMING_SNAKE_CASE       :      List[Any]							=    loss_fct(logits.squeeze()					, labels.squeeze()      )
        else:
          SCREAMING_SNAKE_CASE       :      Tuple							=    loss_fct(__lowerCAmelCase					, __lowerCAmelCase      )
      elif self.config.problem_type == "single_label_classification":
        SCREAMING_SNAKE_CASE       :      Tuple							=    CrossEntropyLoss()
        SCREAMING_SNAKE_CASE       :      Optional[int]							=    loss_fct(logits.view(-1					, self.num_labels      )					, labels.view(-1      )      )
      elif self.config.problem_type == "multi_label_classification":
        SCREAMING_SNAKE_CASE       :      Optional[Any]							=    BCEWithLogitsLoss()
        SCREAMING_SNAKE_CASE       :      Tuple							=    loss_fct(__lowerCAmelCase					, __lowerCAmelCase      )

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

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














import qiskit


def   lowerCamelCase__			(     lowercase						,     lowercase				):

  """simple docstring"""






  SCREAMING_SNAKE_CASE       :      List[str]							=    qiskit.Aer.get_backend("aer_simulator"				)

  # Create a Quantum Circuit acting on the q register
  SCREAMING_SNAKE_CASE       :      Dict							=    qiskit.QuantumCircuit(lowercase						,     lowercase				)

  # Map the quantum measurement to the classical bits
  circuit.measure([0]						,     [0]				)

  # Execute the circuit on the simulator
  SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    qiskit.execute(lowercase						,     lowercase						,     shots=1000				)

  # Return the histogram data of the results of the experiment.
  return job.result().get_counts(lowercase				)


if __name__ == "__main__":
   print(F"""Total count for various states are: {single_qubit_measure(1, 1)}""")






