





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

# General docstring
_lowerCamelCase      							=   'PoolFormerConfig'

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

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

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




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







class       __A							(   nn.Module	):




      """simple docstring"""







      def __init__(		self   ,				a__ = None):



             """simple docstring"""





             super().__init__()
             _lowerCamelCase				:  int						=		drop_prob


      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





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



      def 					__snake_case      (		self):



             """simple docstring"""





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







class       __A							(   nn.Module	):




      """simple docstring"""







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



             """simple docstring"""





             super().__init__()
             _lowerCamelCase				:  str						=		patch_size if isinstance(a__   ,				collections.abc.Iterable) else (patch_size, patch_size)
             _lowerCamelCase				:  Optional[Any]						=		stride if isinstance(a__   ,				collections.abc.Iterable) else (stride, stride)
             _lowerCamelCase				:  Dict						=		padding if isinstance(a__   ,				collections.abc.Iterable) else (padding, padding)

             _lowerCamelCase				:  List[str]						=		nn.Convad(a__   ,				a__   ,				kernel_size=a__   ,				stride=a__   ,				padding=a__)
             _lowerCamelCase				:  List[str]						=		norm_layer(a__) if norm_layer else nn.Identity()



      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  str						=		self.projection(a__)
             _lowerCamelCase				:  List[str]						=		self.norm(a__)
             return embeddings







class       __A							(   nn.GroupNorm	):




      """simple docstring"""







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



             """simple docstring"""





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







class       __A							(   nn.Module	):




      """simple docstring"""







      def __init__(		self   ,				a__):



             """simple docstring"""





             super().__init__()
             _lowerCamelCase				:  Optional[Any]						=		nn.AvgPoolad(a__   ,				stride=1   ,				padding=pool_size // 2   ,				count_include_pad=a__)



      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             return self.pool(a__) - hidden_states







class       __A							(   nn.Module	):




      """simple docstring"""







      def __init__(		self   ,				a__   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             super().__init__()
             _lowerCamelCase				:  List[Any]						=		nn.Convad(a__   ,				a__   ,				1)
             _lowerCamelCase				:  Optional[int]						=		nn.Convad(a__   ,				a__   ,				1)
             _lowerCamelCase				:  List[str]						=		PoolFormerDropPath(a__)
             if isinstance(config.hidden_act   ,				a__):
                    _lowerCamelCase				:  Dict						=		ACTaFN[config.hidden_act]
             else:
                    _lowerCamelCase				:  Any						=		config.hidden_act



      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  List[Any]						=		self.conva(a__)
             _lowerCamelCase				:  Dict						=		self.act_fn(a__)
             _lowerCamelCase				:  Tuple						=		self.drop(a__)
             _lowerCamelCase				:  List[str]						=		self.conva(a__)
             _lowerCamelCase				:  int						=		self.drop(a__)

             return hidden_states







class       __A							(   nn.Module	):




      """simple docstring"""







      def __init__(		self   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__   ,				a__):



             """simple docstring"""





             super().__init__()
             _lowerCamelCase				:  Optional[int]						=		PoolFormerPooling(a__)
             _lowerCamelCase				:  Optional[int]						=		PoolFormerOutput(a__   ,				a__   ,				a__   ,				a__)
             _lowerCamelCase				:  List[Any]						=		PoolFormerGroupNorm(a__)
             _lowerCamelCase				:  Any						=		PoolFormerGroupNorm(a__)

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



      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





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

                    _lowerCamelCase				:  Any						=		self.output(self.after_norm(a__))
                    _lowerCamelCase				:  Tuple						=		self.layer_scale_a.unsqueeze(-1).unsqueeze(-1) * layer_output
                    # Second residual connection
                    _lowerCamelCase				:  List[str]						=		hidden_states + self.drop_path(a__)

                    _lowerCamelCase				:  Tuple						=		(output,) + outputs
                    return outputs

             else:
                    _lowerCamelCase				:  List[Any]						=		self.drop_path(self.pooling(self.before_norm(a__)))
                    # First residual connection
                    _lowerCamelCase				:  Tuple						=		pooling_output + hidden_states
                    _lowerCamelCase				:  Union[str, Any]						=		()

                    # Second residual connection inside the PoolFormerOutput block
                    _lowerCamelCase				:  int						=		self.drop_path(self.output(self.after_norm(a__)))
                    _lowerCamelCase				:  List[str]						=		hidden_states + layer_output

                    _lowerCamelCase				:  Tuple						=		(output,) + outputs
                    return outputs







class       __A							(   nn.Module	):




      """simple docstring"""







      def __init__(		self   ,				a__):



             """simple docstring"""





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

             # patch embeddings
             _lowerCamelCase				:  Union[str, 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]   ,				))
             _lowerCamelCase				:  Any						=		nn.ModuleList(a__)

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

             _lowerCamelCase				:  Tuple						=		nn.ModuleList(a__)



      def 					__snake_case      (		self   ,				a__   ,				a__=False   ,				a__=True):



             """simple docstring"""





             _lowerCamelCase				:  Optional[Any]						=		() if output_hidden_states else None

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

                    if output_hidden_states:
                           _lowerCamelCase				:  List[str]						=		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=a__   ,				hidden_states=a__)







class       __A							(   lowerCamelCase__	):




      """simple docstring"""







      UpperCAmelCase__							=   PoolFormerConfig
      UpperCAmelCase__							=   """poolformer"""
      UpperCAmelCase__							=   """pixel_values"""
      UpperCAmelCase__							=   True


      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





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



      def 					__snake_case      (		self   ,				a__   ,				a__=False):



             """simple docstring"""





             if isinstance(a__   ,				a__):
                    _lowerCamelCase				:  Dict						=		value
_lowerCamelCase      							=   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'

_lowerCamelCase      							=   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   ,				a__):



             """simple docstring"""





             super().__init__(a__)
             _lowerCamelCase				:  List[Any]						=		config

             _lowerCamelCase				:  List[Any]						=		PoolFormerEncoder(a__)

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


      def 					__snake_case      (		self):



             """simple docstring"""





             return self.embeddings.patch_embeddings



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



             """simple docstring"""





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

             _lowerCamelCase				:  Optional[int]						=		self.encoder(
                 a__   ,				output_hidden_states=a__   ,				return_dict=a__   ,				)
             _lowerCamelCase				:  Optional[int]						=		encoder_outputs[0]

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

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







class       __A							(   nn.Module	):




      """simple docstring"""







      def __init__(		self   ,				a__):



             """simple docstring"""





             super().__init__()
             _lowerCamelCase				:  Optional[int]						=		nn.Linear(config.hidden_size   ,				config.hidden_size)



      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  Any						=		self.dense(a__)
             return output







@add_start_docstrings(
    """
    PoolFormer Model transformer with an image classification head on top
    """			,lowerCamelCase__			,)
class       __A							(   lowerCamelCase__	):




      """simple docstring"""







      def __init__(		self   ,				a__):



             """simple docstring"""





             super().__init__(a__)
             _lowerCamelCase				:  Dict						=		config.num_labels
             _lowerCamelCase				:  Optional[int]						=		PoolFormerModel(a__)

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



             """simple docstring"""





             _lowerCamelCase				:  Optional[Any]						=		return_dict if return_dict is not None else self.config.use_return_dict

             _lowerCamelCase				:  Dict						=		self.poolformer(
                 a__   ,				output_hidden_states=a__   ,				return_dict=a__   ,				)

             _lowerCamelCase				:  int						=		outputs[0]

             _lowerCamelCase				:  List[str]						=		self.classifier(self.norm(a__).mean([-2, -1]))

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

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

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

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








from __future__ import annotations

from scipy.special import comb  # type: ignore







class       __A							:




      """simple docstring"""







      def __init__(		self   ,				a__):



             """simple docstring"""





             _lowerCamelCase				:  str						=		list_of_points
             # Degree determines the flexibility of the curve.
             # Degree = 1 will produce a straight line.
             _lowerCamelCase				:  List[Any]						=		len(a__) - 1


      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             assert 0 <= t <= 1, "Time t must be between 0 and 1."
             _lowerCamelCase				:  list[float]						=		[]
             for i in range(len(self.list_of_points)):
                    # basis function for each i
                    output_values.append(
                        comb(self.degree   ,				a__) * ((1 - t) ** (self.degree - i)) * (t**i))
             # the basis must sum up to 1 for it to produce a valid Bezier curve.
             assert round(sum(a__)   ,				5) == 1
             return output_values


      def 					__snake_case      (		self   ,				a__):



             """simple docstring"""





             assert 0 <= t <= 1, "Time t must be between 0 and 1."

             _lowerCamelCase				:  Any						=		self.basis_function(a__)
             _lowerCamelCase				:  List[Any]						=		0.0
             _lowerCamelCase				:  List[Any]						=		0.0
             for i in range(len(self.list_of_points)):
                    # For all points, sum up the product of i-th basis function and i-th point.
                    x += basis_function[i] * self.list_of_points[i][0]
                    y += basis_function[i] * self.list_of_points[i][1]
             return (x, y)



      def 					__snake_case      (		self   ,				a__ = 0.01):



             """simple docstring"""





             from matplotlib import pyplot as plt  # type: ignore

             _lowerCamelCase				:  list[float]						=		[]  # x coordinates of points to plot
             _lowerCamelCase				:  list[float]						=		[]  # y coordinates of points to plot

             _lowerCamelCase				:  List[Any]						=		0.0
             while t <= 1:
                    _lowerCamelCase				:  Tuple						=		self.bezier_curve_function(a__)
                    to_plot_x.append(value[0])
                    to_plot_y.append(value[1])
                    t += step_size

             _lowerCamelCase				:  List[Any]						=		[i[0] for i in self.list_of_points]
             _lowerCamelCase				:  Union[str, Any]						=		[i[1] for i in self.list_of_points]

             plt.plot(
                 a__   ,				a__   ,				color='''blue'''   ,				label='''Curve of Degree ''' + str(self.degree)   ,				)
             plt.scatter(a__   ,				a__   ,				color='''red'''   ,				label='''Control Points''')
             plt.legend()
             plt.show()


if __name__ == "__main__":
       import doctest

       doctest.testmod()

       BezierCurve([(1, 2), (3, 5)]).plot_curve()  # degree 1
       BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve()  # degree 2
       BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve()  # degree 3

