






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

# General docstring
snake_case__      :	Optional[Any]       	=      """PoolFormerConfig"""

# Base docstring
snake_case__      :	Dict       	=      """sail/poolformer_s12"""
snake_case__      :	Any       	=      [1, 5_1_2, 7, 7]

# Image classification docstring
snake_case__      :	Tuple       	=      """sail/poolformer_s12"""
snake_case__      :	Any       	=      """tabby, tabby cat"""

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






def     _snake_case    (__lowercase    ,      __lowercase = 0.0    ,      __lowercase = False):
   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(__lowercase    ,      dtype=input.dtype    ,      device=input.device)
   random_tensor.floor_()  # binarize
   UpperCamelCase_									=     input.div(__lowercase) * random_tensor
   return output


class 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase = None )			-> None:
     super().__init__()
     UpperCamelCase_									=     drop_prob




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase )			-> torch.Tensor:
     return drop_path(_UpperCAmelCase		,		self.drop_prob		,		self.training )




  def    _UpperCAmelCase     (     self )			-> str:
     return "p={}".format(self.drop_prob )




class 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase=None )			-> Optional[Any]:
     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 )			-> Union[str, Any]:
     UpperCamelCase_									=     self.projection(_UpperCAmelCase )
     UpperCamelCase_									=     self.norm(_UpperCAmelCase )
     return embeddings




class 						_a       (  nn.GroupNorm    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase		,		**_UpperCAmelCase )			-> Tuple:
     super().__init__(1		,		_UpperCAmelCase		,		**_UpperCAmelCase )




class 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase )			-> int:
     super().__init__()
     UpperCamelCase_									=     nn.AvgPoolad(_UpperCAmelCase		,		stride=1		,		padding=pool_size // 2		,		count_include_pad=_UpperCAmelCase )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase )			-> List[Any]:
     return self.pool(_UpperCAmelCase ) - hidden_states




class 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Optional[int]:
     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 )			-> Optional[int]:
     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 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase )			-> Optional[int]:
     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 )			-> int:
     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 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase )			-> Dict:
     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 )			-> Any:
     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 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  A_						=   PoolFormerConfig
  A_						=   """poolformer"""
  A_						=   """pixel_values"""
  A_						=   True




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase )			-> List[str]:
     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 )			-> List[str]:
     if isinstance(_UpperCAmelCase		,		_UpperCAmelCase ):
        UpperCamelCase_									=     value




snake_case__      :	Union[str, Any]       	=      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__      :	str       	=      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 						_a       (  UpperCAmelCase__    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase )			-> List[Any]:
     super().__init__(_UpperCAmelCase )
     UpperCamelCase_									=     config

     UpperCamelCase_									=     PoolFormerEncoder(_UpperCAmelCase )

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




  def    _UpperCAmelCase     (     self )			-> str:
     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		,		)			-> Union[Tuple, BaseModelOutputWithNoAttention]:
     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 						_a       (  nn.Module    ):






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase )			-> Any:
     super().__init__()
     UpperCamelCase_									=     nn.Linear(config.hidden_size		,		config.hidden_size )




  def    _UpperCAmelCase     (     self		,		_UpperCAmelCase )			-> List[Any]:
     UpperCamelCase_									=     self.dense(_UpperCAmelCase )
     return output




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






  """simple docstring"""





  def __init__(     self		,		_UpperCAmelCase )			-> Optional[Any]:
     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		,		)			-> Union[Tuple, ImageClassifierOutputWithNoAttention]:
     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 )













from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
 if not (is_transformers_available() and is_torch_available()):
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 from ...utils.dummy_torch_and_transformers_objects import (
     ImageTextPipelineOutput,
     UniDiffuserPipeline,
 )
else:
 from .modeling_text_decoder import UniDiffuserTextDecoder
 from .modeling_uvit import UniDiffuserModel, UTransformeraDModel
 from .pipeline_unidiffuser import ImageTextPipelineOutput, UniDiffuserPipeline





