





"""simple docstring"""


import inspect
from typing import List, Optional, Union

import torch
from torch import nn
from torch.nn import functional as F
from torchvision import transforms
from transformers import CLIPImageProcessor, CLIPModel, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    DPMSolverMultistepScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput



class      lowercase_				(     nn.Module							):

       def __init__(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE=1.0     ):






        '''simple docstring'''

        super().__init__()

        snake_case__    							=				cut_size
        snake_case__    							=				cut_power

       def 		_SCREAMING_SNAKE_CASE(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     ):






        '''simple docstring'''

        snake_case__							,   snake_case__    							=				pixel_values.shape[2:4]
        snake_case__    							=				min(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				min(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					self.cut_size     )
        snake_case__    							=				[]
        for _ in range(__SCREAMING_SNAKE_CASE     ):
         snake_case__    							=				int(torch.rand([]     ) ** self.cut_power * (max_size - min_size) + min_size     )
         snake_case__    							=				torch.randint(0   ,					sideX - size + 1   ,					()     )
         snake_case__    							=				torch.randint(0   ,					sideY - size + 1   ,					()     )
         snake_case__    							=				pixel_values[:, :, offsety : offsety + size, offsetx : offsetx + size]
         cutouts.append(F.adaptive_avg_poolad(__SCREAMING_SNAKE_CASE   ,					self.cut_size     )     )
        return torch.cat(__SCREAMING_SNAKE_CASE     )





def 							a__			(  _UpperCAmelCase	,     _UpperCAmelCase	)    ->  str:

 '''simple docstring'''






 snake_case__    							=				F.normalize(_UpperCAmelCase	,     dim=-1	)
 snake_case__    							=				F.normalize(_UpperCAmelCase	,     dim=-1	)
 return (x - y).norm(dim=-1	).div(2	).arcsin().pow(2	).mul(2	)





def 							a__			(  _UpperCAmelCase	,     _UpperCAmelCase	)    ->  Optional[int]:

 '''simple docstring'''






 for param in model.parameters():
  snake_case__    							=				value



class      lowercase_				(     __lowerCAmelCase							):

       def __init__(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					):






        '''simple docstring'''

        super().__init__()
        self.register_modules(
            vae=__SCREAMING_SNAKE_CASE   ,					text_encoder=__SCREAMING_SNAKE_CASE   ,					clip_model=__SCREAMING_SNAKE_CASE   ,					tokenizer=__SCREAMING_SNAKE_CASE   ,					unet=__SCREAMING_SNAKE_CASE   ,					scheduler=__SCREAMING_SNAKE_CASE   ,					feature_extractor=__SCREAMING_SNAKE_CASE   ,					)

        snake_case__    							=				transforms.Normalize(mean=feature_extractor.image_mean   ,					std=feature_extractor.image_std     )
        snake_case__    							=				(
            feature_extractor.size
            if isinstance(feature_extractor.size   ,					__SCREAMING_SNAKE_CASE     )
            else feature_extractor.size["shortest_edge"]
        )
        snake_case__    							=				MakeCutouts(self.cut_out_size     )

        set_requires_grad(self.text_encoder   ,					__SCREAMING_SNAKE_CASE     )
        set_requires_grad(self.clip_model   ,					__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self   ,					__SCREAMING_SNAKE_CASE = "auto"     ):






        '''simple docstring'''

        if slice_size == "auto":
         # half the attention head size is usually a good trade-off between
         # speed and memory
         snake_case__    							=				self.unet.config.attention_head_dim // 2
        self.unet.set_attention_slice(__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        self.enable_attention_slicing(__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        set_requires_grad(self.vae   ,					__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        set_requires_grad(self.vae   ,					__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        set_requires_grad(self.unet   ,					__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        set_requires_grad(self.unet   ,					__SCREAMING_SNAKE_CASE     )

       @torch.enable_grad()
       def 		_SCREAMING_SNAKE_CASE(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE=True   ,					):






        '''simple docstring'''

        snake_case__    							=				latents.detach().requires_grad_()

        snake_case__    							=				self.scheduler.scale_model_input(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )

        # predict the noise residual
        snake_case__    							=				self.unet(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					encoder_hidden_states=__SCREAMING_SNAKE_CASE     ).sample

        if isinstance(self.scheduler   ,					(PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler)     ):
         snake_case__    							=				self.scheduler.alphas_cumprod[timestep]
         snake_case__    							=				1 - alpha_prod_t
         # compute predicted original sample from predicted noise also called
         # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
         snake_case__    							=				(latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5

         snake_case__    							=				torch.sqrt(__SCREAMING_SNAKE_CASE     )
         snake_case__    							=				pred_original_sample * (fac) + latents * (1 - fac)
        elif isinstance(self.scheduler   ,					__SCREAMING_SNAKE_CASE     ):
         snake_case__    							=				self.scheduler.sigmas[index]
         snake_case__    							=				latents - sigma * noise_pred
        else:
         raise ValueError(F"scheduler type {type(self.scheduler     )} not supported"     )

        snake_case__    							=				1 / self.vae.config.scaling_factor * sample
        snake_case__    							=				self.vae.decode(__SCREAMING_SNAKE_CASE     ).sample
        snake_case__    							=				(image / 2 + 0.5).clamp(0   ,					1     )

        if use_cutouts:
         snake_case__    							=				self.make_cutouts(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )
        else:
         snake_case__    							=				transforms.Resize(self.cut_out_size     )(__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				self.normalize(__SCREAMING_SNAKE_CASE     ).to(latents.dtype     )

        snake_case__    							=				self.clip_model.get_image_features(__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				image_embeddings_clip / image_embeddings_clip.norm(p=2   ,					dim=-1   ,					keepdim=__SCREAMING_SNAKE_CASE     )

        if use_cutouts:
         snake_case__    							=				spherical_dist_loss(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )
         snake_case__    							=				dists.view([num_cutouts, sample.shape[0], -1]     )
         snake_case__    							=				dists.sum(2     ).mean(0     ).sum() * clip_guidance_scale
        else:
         snake_case__    							=				spherical_dist_loss(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     ).mean() * clip_guidance_scale

        snake_case__    							=				-torch.autograd.grad(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )[0]

        if isinstance(self.scheduler   ,					__SCREAMING_SNAKE_CASE     ):
         snake_case__    							=				latents.detach() + grads * (sigma**2)
         snake_case__    							=				noise_pred_original
        else:
         snake_case__    							=				noise_pred_original - torch.sqrt(__SCREAMING_SNAKE_CASE     ) * grads
        return noise_pred, latents

       @torch.no_grad()
       def __call__(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE = 512   ,					__SCREAMING_SNAKE_CASE = 512   ,					__SCREAMING_SNAKE_CASE = 50   ,					__SCREAMING_SNAKE_CASE = 7.5   ,					__SCREAMING_SNAKE_CASE = 1   ,					__SCREAMING_SNAKE_CASE = 0.0   ,					__SCREAMING_SNAKE_CASE = 100   ,					__SCREAMING_SNAKE_CASE = None   ,					__SCREAMING_SNAKE_CASE = 4   ,					__SCREAMING_SNAKE_CASE = True   ,					__SCREAMING_SNAKE_CASE = None   ,					__SCREAMING_SNAKE_CASE = None   ,					__SCREAMING_SNAKE_CASE = "pil"   ,					__SCREAMING_SNAKE_CASE = True   ,					):






        '''simple docstring'''

        if isinstance(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     ):
         snake_case__    							=				1
        elif isinstance(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     ):
         snake_case__    							=				len(__SCREAMING_SNAKE_CASE     )
        else:
         raise ValueError(F"`prompt` has to be of type `str` or `list` but is {type(__SCREAMING_SNAKE_CASE     )}"     )

        if height % 8 != 0 or width % 8 != 0:
         raise ValueError(F"`height` and `width` have to be divisible by 8 but are {height} and {width}."     )

        # get prompt text embeddings
        snake_case__    							=				self.tokenizer(
            __SCREAMING_SNAKE_CASE   ,					padding="max_length"   ,					max_length=self.tokenizer.model_max_length   ,					truncation=__SCREAMING_SNAKE_CASE   ,					return_tensors="pt"   ,					)
        snake_case__    							=				self.text_encoder(text_input.input_ids.to(self.device     )     )[0]
        # duplicate text embeddings for each generation per prompt
        snake_case__    							=				text_embeddings.repeat_interleave(__SCREAMING_SNAKE_CASE   ,					dim=0     )

        if clip_guidance_scale > 0:
         if clip_prompt is not None:
          snake_case__    							=				self.tokenizer(
              __SCREAMING_SNAKE_CASE   ,					padding="max_length"   ,					max_length=self.tokenizer.model_max_length   ,					truncation=__SCREAMING_SNAKE_CASE   ,					return_tensors="pt"   ,					).input_ids.to(self.device     )
         else:
          snake_case__    							=				text_input.input_ids.to(self.device     )
         snake_case__    							=				self.clip_model.get_text_features(__SCREAMING_SNAKE_CASE     )
         snake_case__    							=				text_embeddings_clip / text_embeddings_clip.norm(p=2   ,					dim=-1   ,					keepdim=__SCREAMING_SNAKE_CASE     )
         # duplicate text embeddings clip for each generation per prompt
         snake_case__    							=				text_embeddings_clip.repeat_interleave(__SCREAMING_SNAKE_CASE   ,					dim=0     )

        # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
        # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
        # corresponds to doing no classifier free guidance.
        snake_case__    							=				guidance_scale > 1.0
        # get unconditional embeddings for classifier free guidance
        if do_classifier_free_guidance:
         snake_case__    							=				text_input.input_ids.shape[-1]
         snake_case__    							=				self.tokenizer([""]   ,					padding="max_length"   ,					max_length=__SCREAMING_SNAKE_CASE   ,					return_tensors="pt"     )
         snake_case__    							=				self.text_encoder(uncond_input.input_ids.to(self.device     )     )[0]
         # duplicate unconditional embeddings for each generation per prompt
         snake_case__    							=				uncond_embeddings.repeat_interleave(__SCREAMING_SNAKE_CASE   ,					dim=0     )

         # For classifier free guidance, we need to do two forward passes.
         # Here we concatenate the unconditional and text embeddings into a single batch
         # to avoid doing two forward passes
         snake_case__    							=				torch.cat([uncond_embeddings, text_embeddings]     )

        # get the initial random noise unless the user supplied it

        # Unlike in other pipelines, latents need to be generated in the target device
        # for 1-to-1 results reproducibility with the CompVis implementation.
        # However this currently doesn't work in `mps`.
        snake_case__    							=				(batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
        snake_case__    							=				text_embeddings.dtype
        if latents is None:
         if self.device.type == "mps":
          # randn does not work reproducibly on mps
          snake_case__    							=				torch.randn(__SCREAMING_SNAKE_CASE   ,					generator=__SCREAMING_SNAKE_CASE   ,					device="cpu"   ,					dtype=__SCREAMING_SNAKE_CASE     ).to(
              self.device     )
         else:
          snake_case__    							=				torch.randn(__SCREAMING_SNAKE_CASE   ,					generator=__SCREAMING_SNAKE_CASE   ,					device=self.device   ,					dtype=__SCREAMING_SNAKE_CASE     )
        else:
         if latents.shape != latents_shape:
          raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {latents_shape}"     )
         snake_case__    							=				latents.to(self.device     )

        # set timesteps
        snake_case__    							=				"offset" in set(inspect.signature(self.scheduler.set_timesteps     ).parameters.keys()     )
        snake_case__    							=				{}
        if accepts_offset:
         snake_case__    							=				1

        self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE   ,					**__SCREAMING_SNAKE_CASE     )

        # Some schedulers like PNDM have timesteps as arrays
        # It's more optimized to move all timesteps to correct device beforehand
        snake_case__    							=				self.scheduler.timesteps.to(self.device     )

        # scale the initial noise by the standard deviation required by the scheduler
        snake_case__    							=				latents * self.scheduler.init_noise_sigma

        # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
        # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
        # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
        # and should be between [0, 1]
        snake_case__    							=				"eta" in set(inspect.signature(self.scheduler.step     ).parameters.keys()     )
        snake_case__    							=				{}
        if accepts_eta:
         snake_case__    							=				eta

        # check if the scheduler accepts generator
        snake_case__    							=				"generator" in set(inspect.signature(self.scheduler.step     ).parameters.keys()     )
        if accepts_generator:
         snake_case__    							=				generator

        for i, t in enumerate(self.progress_bar(__SCREAMING_SNAKE_CASE     )     ):
         # expand the latents if we are doing classifier free guidance
         snake_case__    							=				torch.cat([latents] * 2     ) if do_classifier_free_guidance else latents
         snake_case__    							=				self.scheduler.scale_model_input(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )

         # predict the noise residual
         snake_case__    							=				self.unet(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					encoder_hidden_states=__SCREAMING_SNAKE_CASE     ).sample

         # perform classifier free guidance
         if do_classifier_free_guidance:
          snake_case__							,   snake_case__    							=				noise_pred.chunk(2     )
          snake_case__    							=				noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

         # perform clip guidance
         if clip_guidance_scale > 0:
          snake_case__    							=				(
              text_embeddings.chunk(2     )[1] if do_classifier_free_guidance else text_embeddings
          )
          snake_case__							,   snake_case__    							=				self.cond_fn(
              __SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					)

         # compute the previous noisy sample x_t -> x_t-1
         snake_case__    							=				self.scheduler.step(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					**__SCREAMING_SNAKE_CASE     ).prev_sample

        # scale and decode the image latents with vae
        snake_case__    							=				1 / self.vae.config.scaling_factor * latents
        snake_case__    							=				self.vae.decode(__SCREAMING_SNAKE_CASE     ).sample

        snake_case__    							=				(image / 2 + 0.5).clamp(0   ,					1     )
        snake_case__    							=				image.cpu().permute(0   ,					2   ,					3   ,					1     ).numpy()

        if output_type == "pil":
         snake_case__    							=				self.numpy_to_pil(__SCREAMING_SNAKE_CASE     )

        if not return_dict:
         return (image, None)

        return StableDiffusionPipelineOutput(images=__SCREAMING_SNAKE_CASE   ,					nsfw_content_detected=__SCREAMING_SNAKE_CASE     )








"""simple docstring"""


import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
   from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import PoolFormerImageProcessor



class      lowercase_				(     __lowerCAmelCase							):

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				self.config_class(**self.inputs_dict     )
        self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,					"hidden_sizes"     )     )
        self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE   ,					"num_encoder_blocks"     )     )



class      lowercase_				:

       def __init__(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE=13   ,					__SCREAMING_SNAKE_CASE=64   ,					__SCREAMING_SNAKE_CASE=3   ,					__SCREAMING_SNAKE_CASE=4   ,					__SCREAMING_SNAKE_CASE=[2, 2, 2, 2]   ,					__SCREAMING_SNAKE_CASE=[8, 4, 2, 1]   ,					__SCREAMING_SNAKE_CASE=[16, 32, 64, 128]   ,					__SCREAMING_SNAKE_CASE=[1, 4, 8, 16]   ,					__SCREAMING_SNAKE_CASE=False   ,					__SCREAMING_SNAKE_CASE=True   ,					__SCREAMING_SNAKE_CASE="gelu"   ,					__SCREAMING_SNAKE_CASE=0.1   ,					__SCREAMING_SNAKE_CASE=0.02   ,					__SCREAMING_SNAKE_CASE=3   ,					__SCREAMING_SNAKE_CASE=None   ,					):






        '''simple docstring'''

        snake_case__    							=				parent
        snake_case__    							=				batch_size
        snake_case__    							=				image_size
        snake_case__    							=				num_channels
        snake_case__    							=				num_encoder_blocks
        snake_case__    							=				sr_ratios
        snake_case__    							=				depths
        snake_case__    							=				hidden_sizes
        snake_case__    							=				downsampling_rates
        snake_case__    							=				is_training
        snake_case__    							=				use_labels
        snake_case__    							=				hidden_act
        snake_case__    							=				hidden_dropout_prob
        snake_case__    							=				initializer_range
        snake_case__    							=				num_labels
        snake_case__    							=				scope

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

        snake_case__    							=				None
        if self.use_labels:
         snake_case__    							=				ids_tensor([self.batch_size, self.image_size, self.image_size]   ,					self.num_labels     )

        snake_case__    							=				PoolFormerConfig(
            image_size=self.image_size   ,					num_channels=self.num_channels   ,					num_encoder_blocks=self.num_encoder_blocks   ,					depths=self.depths   ,					hidden_sizes=self.hidden_sizes   ,					hidden_act=self.hidden_act   ,					hidden_dropout_prob=self.hidden_dropout_prob   ,					initializer_range=self.initializer_range   ,					)

        return config, pixel_values, labels

       def 		_SCREAMING_SNAKE_CASE(			self   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     ):






        '''simple docstring'''

        snake_case__    							=				PoolFormerModel(config=__SCREAMING_SNAKE_CASE     )
        model.to(__SCREAMING_SNAKE_CASE     )
        model.eval()
        snake_case__    							=				model(__SCREAMING_SNAKE_CASE     )
        snake_case__    							=				snake_case__    							=				self.image_size // 32.0
        self.parent.assertEqual(
            result.last_hidden_state.shape   ,					(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				self.prepare_config_and_inputs()
        snake_case__							,   snake_case__							,   snake_case__    							=				config_and_inputs
        snake_case__    							=				{"pixel_values": pixel_values}
        return config, inputs_dict



@require_torch
class      lowercase_				(     __lowerCAmelCase				,__lowerCAmelCase				,unittest.TestCase							):
       snake_case__				:							Optional[Any]											=     (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
       snake_case__				:							Dict											=     (
           {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       snake_case__				:							Optional[int]											=     False
       snake_case__				:							Optional[Any]											=     False
       snake_case__				:							List[Any]											=     False
       snake_case__				:							Any											=     False
       snake_case__				:							Tuple											=     False

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				PoolFormerModelTester(self     )
        snake_case__    							=				PoolFormerConfigTester(self   ,					config_class=__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        self.config_tester.run_common_tests()

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE     )

       @unittest.skip("PoolFormer does not use inputs_embeds"     )
       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        pass

       @unittest.skip("PoolFormer does not have get_input_embeddings method and get_output_embeddings methods"     )
       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        pass

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        def check_hidden_states_output(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     ):
         snake_case__    							=				model_class(__SCREAMING_SNAKE_CASE     )
         model.to(__SCREAMING_SNAKE_CASE     )
         model.eval()

         with torch.no_grad():
          snake_case__    							=				model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )     )

         snake_case__    							=				outputs.hidden_states

         snake_case__    							=				self.model_tester.num_encoder_blocks
         self.assertEqual(len(__SCREAMING_SNAKE_CASE     )   ,					__SCREAMING_SNAKE_CASE     )

         # verify the first hidden states (first block)
         self.assertListEqual(
             list(hidden_states[0].shape[-3:]     )   ,					[
                 self.model_tester.hidden_sizes[0],
                 self.model_tester.image_size // 4,
                 self.model_tester.image_size // 4,
             ]   ,					)

        snake_case__							,   snake_case__    							=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case__    							=				True
         check_hidden_states_output(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case__    							=				True

         check_hidden_states_output(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE     )

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        if not self.model_tester.is_training:
         return

        snake_case__							,   snake_case__    							=				self.model_tester.prepare_config_and_inputs_for_common()
        snake_case__    							=				True

        for model_class in self.all_model_classes:
         if model_class in get_values(__SCREAMING_SNAKE_CASE     ):
          continue
         snake_case__    							=				model_class(__SCREAMING_SNAKE_CASE     )
         model.to(__SCREAMING_SNAKE_CASE     )
         model.train()
         snake_case__    							=				self._prepare_for_class(__SCREAMING_SNAKE_CASE   ,					__SCREAMING_SNAKE_CASE   ,					return_labels=__SCREAMING_SNAKE_CASE     )
         snake_case__    							=				model(**__SCREAMING_SNAKE_CASE     ).loss
         loss.backward()

       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__							,   snake_case__    							=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case__    							=				model_class(__SCREAMING_SNAKE_CASE     )
         snake_case__    							=				inspect.signature(model.forward     )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         snake_case__    							=				[*signature.parameters.keys()]

         snake_case__    							=				["pixel_values"]
         self.assertListEqual(arg_names[:1]   ,					__SCREAMING_SNAKE_CASE     )

       @slow
       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case__    							=				PoolFormerModel.from_pretrained(__SCREAMING_SNAKE_CASE     )
         self.assertIsNotNone(__SCREAMING_SNAKE_CASE     )





def 							a__			(  )    ->  int:

 '''simple docstring'''






 snake_case__    							=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"	)
 return image



@require_torch
class      lowercase_				(     unittest.TestCase							):

       @slow
       def 		_SCREAMING_SNAKE_CASE(			self     ):






        '''simple docstring'''

        snake_case__    							=				PoolFormerImageProcessor()
        snake_case__    							=				PoolFormerForImageClassification.from_pretrained("sail/poolformer_s12"     ).to(__SCREAMING_SNAKE_CASE     )

        snake_case__    							=				image_processor(images=prepare_img()   ,					return_tensors="pt"     ).to(__SCREAMING_SNAKE_CASE     )

        # forward pass
        with torch.no_grad():
         snake_case__    							=				model(**__SCREAMING_SNAKE_CASE     )

        # verify the logits
        snake_case__    							=				torch.Size((1, 1000)     )
        self.assertEqual(outputs.logits.shape   ,					__SCREAMING_SNAKE_CASE     )

        snake_case__    							=				torch.tensor([-0.6113, 0.1685, -0.0492]     ).to(__SCREAMING_SNAKE_CASE     )
        self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,					__SCREAMING_SNAKE_CASE   ,					atol=1e-4     )     )

