





import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionInstructPixaPixPipeline,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

class       A_			(a_	,				a_	,				a_	,				unittest.TestCase     ):
	UpperCAmelCase__   =     StableDiffusionInstructPixaPixPipeline
	UpperCAmelCase__   =     TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width''', '''cross_attention_kwargs'''}
	UpperCAmelCase__   =     TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
	UpperCAmelCase__   =     IMAGE_TO_IMAGE_IMAGE_PARAMS
	UpperCAmelCase__   =     IMAGE_TO_IMAGE_IMAGE_PARAMS
	def 	_lowercase							(       self						):






				'''simple docstring'''







				torch.manual_seed(0						)
				UpperCAmelCase  						=							UNetaDConditionModel(
				    block_out_channels=(3_2, 6_4)				,   layers_per_block=2				,   sample_size=3_2				,   in_channels=8				,   out_channels=4				,   down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')				,   up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')				,   cross_attention_dim=3_2				,   )
				UpperCAmelCase  						=							PNDMScheduler(skip_prk_steps=UpperCamelCase_						)
				torch.manual_seed(0						)
				UpperCAmelCase  						=							AutoencoderKL(
				    block_out_channels=[3_2, 6_4]				,   in_channels=3				,   out_channels=3				,   down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']				,   up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']				,   latent_channels=4				,   )
				torch.manual_seed(0						)
				UpperCAmelCase  						=							CLIPTextConfig(
				    bos_token_id=0				,   eos_token_id=2				,   hidden_size=3_2				,   intermediate_size=3_7				,   layer_norm_eps=1E-05				,   num_attention_heads=4				,   num_hidden_layers=5				,   pad_token_id=1				,   vocab_size=1_0_0_0				,   )
				UpperCAmelCase  						=							CLIPTextModel(UpperCamelCase_						)
				UpperCAmelCase  						=							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''						)

				UpperCAmelCase  						=							{
				    '''unet''': unet,
				    '''scheduler''': scheduler,
				    '''vae''': vae,
				    '''text_encoder''': text_encoder,
				    '''tokenizer''': tokenizer,
				    '''safety_checker''': None,
				    '''feature_extractor''': None,
				}
				return components
	def 	_lowercase							(       self				,   _A				,   _A=0						):






				'''simple docstring'''







				UpperCAmelCase  						=							floats_tensor((1, 3, 3_2, 3_2)				,   rng=random.Random(UpperCamelCase_						)						).to(UpperCamelCase_						)
				UpperCAmelCase  						=							image.cpu().permute(0				,   2				,   3				,   1						)[0]
				UpperCAmelCase  						=							Image.fromarray(np.uinta(UpperCamelCase_						)						).convert('''RGB'''						)
				if str(UpperCamelCase_						).startswith('''mps'''						):
							UpperCAmelCase  						=							torch.manual_seed(UpperCamelCase_						)
				else:
							UpperCAmelCase  						=							torch.Generator(device=UpperCamelCase_						).manual_seed(UpperCamelCase_						)
				UpperCAmelCase  						=							{
				    '''prompt''': '''A painting of a squirrel eating a burger''',
				    '''image''': image,
				    '''generator''': generator,
				    '''num_inference_steps''': 2,
				    '''guidance_scale''': 6.0,
				    '''image_guidance_scale''': 1,
				    '''output_type''': '''numpy''',
				}
				return inputs
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase  						=							self.get_dummy_components()
				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_						)
				UpperCAmelCase  						=							sd_pipe.to(UpperCamelCase_						)
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase_						)

				UpperCAmelCase  						=							self.get_dummy_inputs(UpperCamelCase_						)
				UpperCAmelCase  						=							sd_pipe(**UpperCamelCase_						).images
				UpperCAmelCase  						=							image[0, -3:, -3:, -1]
				assert image.shape == (1, 3_2, 3_2, 3)
				UpperCAmelCase  						=							np.array([0.75_26, 0.37_50, 0.45_47, 0.61_17, 0.58_66, 0.50_16, 0.43_27, 0.56_42, 0.48_15]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase  						=							self.get_dummy_components()
				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_						)
				UpperCAmelCase  						=							sd_pipe.to(UpperCamelCase_						)
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase_						)

				UpperCAmelCase  						=							self.get_dummy_inputs(UpperCamelCase_						)
				UpperCAmelCase  						=							'''french fries'''
				UpperCAmelCase  						=							sd_pipe(**UpperCamelCase_				,   negative_prompt=UpperCamelCase_						)
				UpperCAmelCase  						=							output.images
				UpperCAmelCase  						=							image[0, -3:, -3:, -1]

				assert image.shape == (1, 3_2, 3_2, 3)
				UpperCAmelCase  						=							np.array([0.75_11, 0.36_42, 0.45_53, 0.62_36, 0.57_97, 0.50_13, 0.43_43, 0.56_11, 0.48_31]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase  						=							self.get_dummy_components()
				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_						)
				UpperCAmelCase  						=							sd_pipe.to(UpperCamelCase_						)
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase_						)

				UpperCAmelCase  						=							self.get_dummy_inputs(UpperCamelCase_						)
				UpperCAmelCase  						=							[inputs['''prompt''']] * 2

				UpperCAmelCase  						=							np.array(inputs['''image''']						).astype(np.floataa						) / 2_5_5.0
				UpperCAmelCase  						=							torch.from_numpy(UpperCamelCase_						).unsqueeze(0						).to(UpperCamelCase_						)
				UpperCAmelCase  						=							image / 2 + 0.5
				UpperCAmelCase  						=							image.permute(0				,   3				,   1				,   2						)
				UpperCAmelCase  						=							image.repeat(2				,   1				,   1				,   1						)

				UpperCAmelCase  						=							sd_pipe(**UpperCamelCase_						).images
				UpperCAmelCase  						=							image[-1, -3:, -3:, -1]

				assert image.shape == (2, 3_2, 3_2, 3)
				UpperCAmelCase  						=							np.array([0.58_12, 0.57_48, 0.52_22, 0.59_08, 0.56_95, 0.71_74, 0.68_04, 0.55_23, 0.55_79]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase  						=							self.get_dummy_components()
				UpperCAmelCase  						=							EulerAncestralDiscreteScheduler(
				    beta_start=0.0_00_85				,   beta_end=0.0_12				,   beta_schedule='''scaled_linear'''						)
				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_						)
				UpperCAmelCase  						=							sd_pipe.to(UpperCamelCase_						)
				sd_pipe.set_progress_bar_config(disable=UpperCamelCase_						)

				UpperCAmelCase  						=							self.get_dummy_inputs(UpperCamelCase_						)
				UpperCAmelCase  						=							sd_pipe(**UpperCamelCase_						).images
				UpperCAmelCase  						=							image[0, -3:, -3:, -1]

				UpperCAmelCase  						=							[round(UpperCamelCase_				,   4						) for x in image_slice.flatten().tolist()]
				print(''','''.join([str(UpperCamelCase_						) for x in slice]						)						)

				assert image.shape == (1, 3_2, 3_2, 3)
				UpperCAmelCase  						=							np.array([0.74_17, 0.38_42, 0.47_32, 0.57_76, 0.58_91, 0.51_39, 0.40_52, 0.56_73, 0.49_86]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				super().test_inference_batch_single_identical(expected_max_diff=3E-3						)







	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							self.get_dummy_components()
				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_						)
				UpperCAmelCase  						=							VaeImageProcessor(do_resize=UpperCamelCase_				,   do_normalize=UpperCamelCase_						)
				UpperCAmelCase  						=							pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)

				UpperCAmelCase  						=							pipe(**self.get_dummy_inputs_by_type(UpperCamelCase_				,   input_image_type='''pt'''						)						)[0]

				UpperCAmelCase  						=							components['''vae''']
				UpperCAmelCase  						=							self.get_dummy_inputs_by_type(UpperCamelCase_				,   input_image_type='''pt'''						)

				for image_param in self.image_latents_params:
							if image_param in inputs.keys():
										UpperCAmelCase  						=							vae.encode(inputs[image_param]						).latent_dist.mode()

				UpperCAmelCase  						=							pipe(**UpperCamelCase_						)[0]

				UpperCAmelCase  						=							np.abs(out - out_latents_inputs						).max()
				self.assertLess(UpperCamelCase_				,   1E-4				,   '''passing latents as image input generate different result from passing image'''						)

@slow
@require_torch_gpu
class       A_			(unittest.TestCase     ):
	def 	_lowercase							(       self						):






				'''simple docstring'''







				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()
	def 	_lowercase							(       self				,   _A=0						):






				'''simple docstring'''







				UpperCAmelCase  						=							torch.manual_seed(UpperCamelCase_						)
				UpperCAmelCase  						=							load_image(
				    '''https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg'''						)
				UpperCAmelCase  						=							{
				    '''prompt''': '''turn him into a cyborg''',
				    '''image''': image,
				    '''generator''': generator,
				    '''num_inference_steps''': 3,
				    '''guidance_scale''': 7.5,
				    '''image_guidance_scale''': 1.0,
				    '''output_type''': '''numpy''',
				}
				return inputs
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline.from_pretrained(
				    '''timbrooks/instruct-pix2pix'''				,   safety_checker=UpperCamelCase_						)
				pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)
				pipe.enable_attention_slicing()

				UpperCAmelCase  						=							self.get_inputs()
				UpperCAmelCase  						=							pipe(**UpperCamelCase_						).images
				UpperCAmelCase  						=							image[0, -3:, -3:, -1].flatten()

				assert image.shape == (1, 5_1_2, 5_1_2, 3)
				UpperCAmelCase  						=							np.array([0.59_02, 0.60_15, 0.60_27, 0.59_83, 0.60_92, 0.60_61, 0.57_65, 0.57_85, 0.55_55]						)

				assert np.abs(expected_slice - image_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline.from_pretrained(
				    '''timbrooks/instruct-pix2pix'''				,   safety_checker=UpperCamelCase_						)
				UpperCAmelCase  						=							LMSDiscreteScheduler.from_config(pipe.scheduler.config						)
				pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)
				pipe.enable_attention_slicing()

				UpperCAmelCase  						=							self.get_inputs()
				UpperCAmelCase  						=							pipe(**UpperCamelCase_						).images
				UpperCAmelCase  						=							image[0, -3:, -3:, -1].flatten()

				assert image.shape == (1, 5_1_2, 5_1_2, 3)
				UpperCAmelCase  						=							np.array([0.65_78, 0.68_17, 0.69_72, 0.67_61, 0.68_56, 0.69_16, 0.64_28, 0.65_16, 0.63_01]						)

				assert np.abs(expected_slice - image_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline.from_pretrained(
				    '''timbrooks/instruct-pix2pix'''				,   safety_checker=UpperCamelCase_						)
				UpperCAmelCase  						=							DDIMScheduler.from_config(pipe.scheduler.config						)
				pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)
				pipe.enable_attention_slicing()

				UpperCAmelCase  						=							self.get_inputs()
				UpperCAmelCase  						=							pipe(**UpperCamelCase_						).images
				UpperCAmelCase  						=							image[0, -3:, -3:, -1].flatten()

				assert image.shape == (1, 5_1_2, 5_1_2, 3)
				UpperCAmelCase  						=							np.array([0.38_28, 0.38_34, 0.38_18, 0.37_92, 0.38_65, 0.37_52, 0.37_92, 0.38_47, 0.37_53]						)

				assert np.abs(expected_slice - image_slice						).max() < 1E-3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							0

				def callback_fn(_A				,   _A				,   _A						) -> None:
							UpperCAmelCase  						=							True
							nonlocal number_of_steps
							number_of_steps += 1
							if step == 1:
										UpperCAmelCase  						=							latents.detach().cpu().numpy()
										assert latents.shape == (1, 4, 6_4, 6_4)
										UpperCAmelCase  						=							latents[0, -3:, -3:, -1]
										UpperCAmelCase  						=							np.array([-0.24_63, -0.46_44, -0.97_56, 1.51_76, 1.44_14, 0.78_66, 0.98_97, 0.85_21, 0.79_83]						)

										assert np.abs(latents_slice.flatten() - expected_slice						).max() < 5E-2
							elif step == 2:
										UpperCAmelCase  						=							latents.detach().cpu().numpy()
										assert latents.shape == (1, 4, 6_4, 6_4)
										UpperCAmelCase  						=							latents[0, -3:, -3:, -1]
										UpperCAmelCase  						=							np.array([-0.26_44, -0.46_26, -0.96_53, 1.51_76, 1.45_51, 0.76_86, 0.98_05, 0.84_52, 0.81_15]						)

										assert np.abs(latents_slice.flatten() - expected_slice						).max() < 5E-2

				UpperCAmelCase  						=							False

				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline.from_pretrained(
				    '''timbrooks/instruct-pix2pix'''				,   safety_checker=UpperCamelCase_				,   torch_dtype=torch.floataa						)
				UpperCAmelCase  						=							pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)
				pipe.enable_attention_slicing()

				UpperCAmelCase  						=							self.get_inputs()
				pipe(**UpperCamelCase_				,   callback=UpperCamelCase_				,   callback_steps=1						)
				assert callback_fn.has_been_called
				assert number_of_steps == 3
	def 	_lowercase							(       self						):






				'''simple docstring'''







				torch.cuda.empty_cache()
				torch.cuda.reset_max_memory_allocated()
				torch.cuda.reset_peak_memory_stats()

				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline.from_pretrained(
				    '''timbrooks/instruct-pix2pix'''				,   safety_checker=UpperCamelCase_				,   torch_dtype=torch.floataa						)
				UpperCAmelCase  						=							pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)
				pipe.enable_attention_slicing(1						)
				pipe.enable_sequential_cpu_offload()

				UpperCAmelCase  						=							self.get_inputs()
				UpperCAmelCase  						=							pipe(**UpperCamelCase_						)

				UpperCAmelCase  						=							torch.cuda.max_memory_allocated()
				# make sure that less than 2.2 GB is allocated
				assert mem_bytes < 2.2 * 1_0**9







	def 	_lowercase							(       self						):






				'''simple docstring'''







				UpperCAmelCase  						=							self.get_inputs()
				# resize to resolution that is divisible by 8 but not 16 or 32
				UpperCAmelCase  						=							inputs['''image'''].resize((5_0_4, 5_0_4)						)

				UpperCAmelCase  						=							'''timbrooks/instruct-pix2pix'''
				UpperCAmelCase  						=							StableDiffusionInstructPixaPixPipeline.from_pretrained(
				    UpperCamelCase_				,   safety_checker=UpperCamelCase_				,   )
				pipe.to(UpperCamelCase_						)
				pipe.set_progress_bar_config(disable=UpperCamelCase_						)
				pipe.enable_attention_slicing()

				UpperCAmelCase  						=							pipe(**UpperCamelCase_						)
				UpperCAmelCase  						=							output.images[0]

				UpperCAmelCase  						=							image[2_5_5:2_5_8, 3_8_3:3_8_6, -1]

				assert image.shape == (5_0_4, 5_0_4, 3)
				UpperCAmelCase  						=							np.array([0.27_26, 0.25_29, 0.26_64, 0.26_55, 0.26_41, 0.26_42, 0.25_91, 0.26_49, 0.25_90]						)

				assert np.abs(image_slice.flatten() - expected_slice						).max() < 5E-3










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


a__					:			Dict          =     logging.get_logger(__name__)

# General docstring
a__					:			Any          =     '''PoolFormerConfig'''

# Base docstring
a__					:			List[str]          =     '''sail/poolformer_s12'''
a__					:			Tuple          =     [1, 512, 7, 7]

# Image classification docstring
a__					:			Dict          =     '''sail/poolformer_s12'''
a__					:			Optional[Any]          =     '''tabby, tabby cat'''

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




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






class 				__snake_case     (					nn.Module							):





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





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





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






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_=None		)      ->		Optional[Any]:
		super().__init__()
		snake_case__	       =   patch_size if isinstance(UpperCamelCase_      ,       collections.abc.Iterable		) else (patch_size, patch_size)
		snake_case__	       =   stride if isinstance(UpperCamelCase_      ,       collections.abc.Iterable		) else (stride, stride)
		snake_case__	       =   padding if isinstance(UpperCamelCase_      ,       collections.abc.Iterable		) else (padding, padding)

		snake_case__	       =   nn.Convad(UpperCamelCase_      ,       UpperCamelCase_      ,       kernel_size=UpperCamelCase_      ,       stride=UpperCamelCase_      ,       padding=UpperCamelCase_		)
		snake_case__	       =   norm_layer(UpperCamelCase_		) if norm_layer else nn.Identity()





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		int:
		snake_case__	       =   self.projection(UpperCamelCase_		)
		snake_case__	       =   self.norm(UpperCamelCase_		)
		return embeddings






class 				__snake_case     (					nn.GroupNorm							):





	def __init__(		self      ,       UpperCamelCase_      ,       **UpperCamelCase_		)      ->		Dict:
		super().__init__(1      ,       UpperCamelCase_      ,       **UpperCamelCase_		)






class 				__snake_case     (					nn.Module							):





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





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		str:
		return self.pool(UpperCamelCase_		) - hidden_states






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		int:
		super().__init__()
		snake_case__	       =   nn.Convad(UpperCamelCase_      ,       UpperCamelCase_      ,       1		)
		snake_case__	       =   nn.Convad(UpperCamelCase_      ,       UpperCamelCase_      ,       1		)
		snake_case__	       =   PoolFormerDropPath(UpperCamelCase_		)
		if isinstance(config.hidden_act      ,       UpperCamelCase_		):
			snake_case__	       =   ACTaFN[config.hidden_act]
		else:
			snake_case__	       =   config.hidden_act





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		Any:
		snake_case__	       =   self.conva(UpperCamelCase_		)
		snake_case__	       =   self.act_fn(UpperCamelCase_		)
		snake_case__	       =   self.drop(UpperCamelCase_		)
		snake_case__	       =   self.conva(UpperCamelCase_		)
		snake_case__	       =   self.drop(UpperCamelCase_		)

		return hidden_states






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)      ->		Tuple:
		super().__init__()
		snake_case__	       =   PoolFormerPooling(UpperCamelCase_		)
		snake_case__	       =   PoolFormerOutput(UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_      ,       UpperCamelCase_		)
		snake_case__	       =   PoolFormerGroupNorm(UpperCamelCase_		)
		snake_case__	       =   PoolFormerGroupNorm(UpperCamelCase_		)

		# Useful for training neural nets
		snake_case__	       =   PoolFormerDropPath(UpperCamelCase_		) if drop_path > 0.0 else nn.Identity()
		snake_case__	       =   config.use_layer_scale
		if config.use_layer_scale:
			snake_case__	       =   nn.Parameter(
			    config.layer_scale_init_value * torch.ones((UpperCamelCase_)		)      ,       requires_grad=UpperCamelCase_		)
			snake_case__	       =   nn.Parameter(
			    config.layer_scale_init_value * torch.ones((UpperCamelCase_)		)      ,       requires_grad=UpperCamelCase_		)





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		List[str]:
		if self.use_layer_scale:
			snake_case__	       =   self.pooling(self.before_norm(UpperCamelCase_		)		)
			snake_case__	       =   self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
			# First residual connection
			snake_case__	       =   hidden_states + self.drop_path(UpperCamelCase_		)
			snake_case__	       =   ()

			snake_case__	       =   self.output(self.after_norm(UpperCamelCase_		)		)
			snake_case__	       =   self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
			# Second residual connection
			snake_case__	       =   hidden_states + self.drop_path(UpperCamelCase_		)

			snake_case__	       =   (output,) + outputs
			return outputs

		else:
			snake_case__	       =   self.drop_path(self.pooling(self.before_norm(UpperCamelCase_		)		)		)
			# First residual connection
			snake_case__	       =   pooling_output + hidden_states
			snake_case__	       =   ()

			# Second residual connection inside the PoolFormerOutput block
			snake_case__	       =   self.drop_path(self.output(self.after_norm(UpperCamelCase_		)		)		)
			snake_case__	       =   hidden_states + layer_output

			snake_case__	       =   (output,) + outputs
			return outputs






class 				__snake_case     (					nn.Module							):





	def __init__(		self      ,       UpperCamelCase_		)      ->		Dict:
		super().__init__()
		snake_case__	       =   config
		# stochastic depth decay rule
		snake_case__	       =   [x.item() for x in torch.linspace(0      ,       config.drop_path_rate      ,       sum(config.depths		)		)]

		# patch embeddings
		snake_case__	       =   []
		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]      ,       )		)
		snake_case__	       =   nn.ModuleList(UpperCamelCase_		)

		# Transformer blocks
		snake_case__	       =   []
		snake_case__	       =   0
		for i in range(config.num_encoder_blocks		):
			# each block consists of layers
			snake_case__	       =   []
			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_		)		)

		snake_case__	       =   nn.ModuleList(UpperCamelCase_		)





	def   _snake_case   (		self      ,       UpperCamelCase_      ,       UpperCamelCase_=False      ,       UpperCamelCase_=True		)      ->		Dict:
		snake_case__	       =   () if output_hidden_states else None

		snake_case__	       =   pixel_values
		for idx, layers in enumerate(zip(self.patch_embeddings      ,       self.block		)		):
			snake_case__       ,  snake_case__	       =   layers
			# Get patch embeddings from hidden_states
			snake_case__	       =   embedding_layer(UpperCamelCase_		)
			# Send the embeddings through the blocks
			for _, blk in enumerate(UpperCamelCase_		):
				snake_case__	       =   blk(UpperCamelCase_		)
				snake_case__	       =   layer_outputs[0]

			if output_hidden_states:
				snake_case__	       =   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 				__snake_case     (					__magic_name__							):
	__lowerCAmelCase       						=  PoolFormerConfig
	__lowerCAmelCase       						=  '''poolformer'''
	__lowerCAmelCase       						=  '''pixel_values'''
	__lowerCAmelCase       						=  True





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		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   _snake_case   (		self      ,       UpperCamelCase_      ,       UpperCamelCase_=False		)      ->		int:
		if isinstance(UpperCamelCase_      ,       UpperCamelCase_		):
			snake_case__	       =   value





a__					:			int          =     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.
'''

a__					:			Union[str, Any]          =     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.'''       ,       __magic_name__       ,       )
class 				__snake_case     (					__magic_name__							):





	def __init__(		self      ,       UpperCamelCase_		)      ->		Any:
		super().__init__(UpperCamelCase_		)
		snake_case__	       =   config

		snake_case__	       =   PoolFormerEncoder(UpperCamelCase_		)

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





	def   _snake_case   (		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   _snake_case   (		self      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       )      ->		Union[Tuple, BaseModelOutputWithNoAttention]:
		snake_case__	       =   (
		    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
		)
		snake_case__	       =   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'		)

		snake_case__	       =   self.encoder(
		    UpperCamelCase_      ,       output_hidden_states=UpperCamelCase_      ,       return_dict=UpperCamelCase_      ,       )
		snake_case__	       =   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 				__snake_case     (					nn.Module							):





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





	def   _snake_case   (		self      ,       UpperCamelCase_		)      ->		Any:
		snake_case__	       =   self.dense(UpperCamelCase_		)
		return output






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





	def __init__(		self      ,       UpperCamelCase_		)      ->		Optional[Any]:
		super().__init__(UpperCamelCase_		)
		snake_case__	       =   config.num_labels
		snake_case__	       =   PoolFormerModel(UpperCamelCase_		)

		# Final norm
		snake_case__	       =   PoolFormerGroupNorm(config.hidden_sizes[-1]		)
		# Classifier head
		snake_case__	       =   (
		    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   _snake_case   (		self      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       UpperCamelCase_ = None      ,       )      ->		Union[Tuple, ImageClassifierOutputWithNoAttention]:
		snake_case__	       =   return_dict if return_dict is not None else self.config.use_return_dict

		snake_case__	       =   self.poolformer(
		    UpperCamelCase_      ,       output_hidden_states=UpperCamelCase_      ,       return_dict=UpperCamelCase_      ,       )

		snake_case__	       =   outputs[0]

		snake_case__	       =   self.classifier(self.norm(UpperCamelCase_		).mean([-2, -1]		)		)

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

			if self.config.problem_type == "regression":
				snake_case__	       =   MSELoss()
				if self.num_labels == 1:
					snake_case__	       =   loss_fct(logits.squeeze()      ,       labels.squeeze()		)
				else:
					snake_case__	       =   loss_fct(UpperCamelCase_      ,       UpperCamelCase_		)
			elif self.config.problem_type == "single_label_classification":
				snake_case__	       =   CrossEntropyLoss()
				snake_case__	       =   loss_fct(logits.view(-1      ,       self.num_labels		)      ,       labels.view(-1		)		)
			elif self.config.problem_type == "multi_label_classification":
				snake_case__	       =   BCEWithLogitsLoss()
				snake_case__	       =   loss_fct(UpperCamelCase_      ,       UpperCamelCase_		)

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

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



