


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


lowercase__				     =		logging.get_logger(__name__)

# General docstring
lowercase__				     =		"PoolFormerConfig"

# Base docstring
lowercase__				     =		"sail/poolformer_s12"
lowercase__				     =		[1, 512, 7, 7]

# Image classification docstring
lowercase__				     =		"sail/poolformer_s12"
lowercase__				     =		"tabby, tabby cat"

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






def __UpperCamelCase   (			__lowerCamelCase		: Dict      ,  __lowerCamelCase		: float = 0.0      ,  __lowerCamelCase		: bool = False				)      ->     Any:







     '''simple docstring'''






     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(__lowerCamelCase      ,  dtype=input.dtype      ,  device=input.device				)
     random_tensor.floor_()  # binarize
     _a						=		input.div(__lowerCamelCase				) * random_tensor
     return output





class 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






     def __init__(			self     ,							__UpperCamelCase = None		)       ->    None:
          super().__init__()
          _a						=		drop_prob






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






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







class 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






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







class 						__SCREAMING_SNAKE_CASE					(      nn.GroupNorm ):






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







class 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






     def __init__(			self     ,							__UpperCamelCase		)       ->    str:
          super().__init__()
          _a						=		nn.AvgPoolad(__UpperCamelCase     ,							stride=1     ,							padding=pool_size // 2     ,							count_include_pad=__UpperCamelCase		)






     def  a_					(			self     ,							__UpperCamelCase		)       ->    Any:
          return self.pool(__UpperCamelCase		) - hidden_states







class 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






     def __init__(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    Optional[int]:
          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  a_					(			self     ,							__UpperCamelCase		)       ->    List[Any]:
          _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 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






     def __init__(			self     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase     ,							__UpperCamelCase		)       ->    List[Any]:
          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  a_					(			self     ,							__UpperCamelCase		)       ->    Optional[int]:
          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 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






     def __init__(			self     ,							__UpperCamelCase		)       ->    Optional[int]:
          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  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase=False     ,							__UpperCamelCase=True		)       ->    Optional[Any]:
          _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 						__SCREAMING_SNAKE_CASE					(      lowerCamelCase__ ):
     UpperCAmelCase 					=				PoolFormerConfig
     UpperCAmelCase 					=				'''poolformer'''
     UpperCAmelCase 					=				'''pixel_values'''
     UpperCAmelCase 					=				True






     def  a_					(			self     ,							__UpperCamelCase		)       ->    Optional[Any]:
          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  a_					(			self     ,							__UpperCamelCase     ,							__UpperCamelCase=False		)       ->    Optional[Any]:
          if isinstance(__UpperCamelCase     ,							__UpperCamelCase		):
               _a						=		value







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

lowercase__				     =		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 						__SCREAMING_SNAKE_CASE					(      lowerCamelCase__ ):






     def __init__(			self     ,							__UpperCamelCase		)       ->    Optional[Any]:
          super().__init__(__UpperCamelCase		)
          _a						=		config

          _a						=		PoolFormerEncoder(__UpperCamelCase		)

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






     def  a_					(			self		)       ->    List[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  a_					(			self     ,							__UpperCamelCase = None     ,							__UpperCamelCase = None     ,							__UpperCamelCase = None     ,							)       ->    Union[Tuple, BaseModelOutputWithNoAttention]:
          _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 						__SCREAMING_SNAKE_CASE					(      nn.Module ):






     def __init__(			self     ,							__UpperCamelCase		)       ->    str:
          super().__init__()
          _a						=		nn.Linear(config.hidden_size     ,							config.hidden_size		)






     def  a_					(			self     ,							__UpperCamelCase		)       ->    Optional[int]:
          _a						=		self.dense(__UpperCamelCase		)
          return output







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






     def __init__(			self     ,							__UpperCamelCase		)       ->    Optional[Any]:
          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  a_					(			self     ,							__UpperCamelCase = None     ,							__UpperCamelCase = None     ,							__UpperCamelCase = None     ,							__UpperCamelCase = None     ,							)       ->    Union[Tuple, ImageClassifierOutputWithNoAttention]:
          _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'''

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


lowercase__				     =		False





class 						__SCREAMING_SNAKE_CASE					(      unittest.TestCase ):
     pass







@nightly
@require_torch_gpu
class 						__SCREAMING_SNAKE_CASE					(      unittest.TestCase ):






     def  a_					(			self		)       ->    Dict:
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()






     def  a_					(			self		)       ->    List[str]:
          _a						=		VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion"		)
          # remove text_unet
          pipe.remove_unused_weights()
          pipe.to(__UpperCamelCase		)
          pipe.set_progress_bar_config(disable=__UpperCamelCase		)

          _a						=		"A painting of a squirrel eating a burger "
          _a						=		torch.manual_seed(0		)
          _a						=		pipe(
              prompt=__UpperCamelCase     ,							generator=__UpperCamelCase     ,							guidance_scale=7.5     ,							num_inference_steps=2     ,							output_type="numpy"		).images

          with tempfile.TemporaryDirectory() as tmpdirname:
               pipe.save_pretrained(__UpperCamelCase		)
               _a						=		VersatileDiffusionTextToImagePipeline.from_pretrained(__UpperCamelCase		)
          pipe.to(__UpperCamelCase		)
          pipe.set_progress_bar_config(disable=__UpperCamelCase		)

          _a						=		generator.manual_seed(0		)
          _a						=		pipe(
              prompt=__UpperCamelCase     ,							generator=__UpperCamelCase     ,							guidance_scale=7.5     ,							num_inference_steps=2     ,							output_type="numpy"		).images

          assert np.abs(image - new_image		).sum() < 1e-5, "Models don't have the same forward pass"






     def  a_					(			self		)       ->    List[Any]:
          _a						=		VersatileDiffusionTextToImagePipeline.from_pretrained(
              "shi-labs/versatile-diffusion"     ,							torch_dtype=torch.floataa		)
          pipe.to(__UpperCamelCase		)
          pipe.set_progress_bar_config(disable=__UpperCamelCase		)

          _a						=		"A painting of a squirrel eating a burger "
          _a						=		torch.manual_seed(0		)
          _a						=		pipe(
              prompt=__UpperCamelCase     ,							generator=__UpperCamelCase     ,							guidance_scale=7.5     ,							num_inference_steps=50     ,							output_type="numpy"		).images

          _a						=		image[0, 253:256, 253:256, -1]

          assert image.shape == (1, 512, 512, 3)
          _a						=		np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]		)

          assert np.abs(image_slice.flatten() - expected_slice		).max() < 1e-2

