




import math

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from .attention_processor import Attention
from .embeddings import get_timestep_embedding
from .modeling_utils import ModelMixin







class   A(						UpperCamelCase							,      UpperCamelCase   ):



 '''simple docstring'''


 @register_to_config
 def __init__(				self					:  Optional[int]						,							A_					:  int = 128						,							A_					:  int = 256						,							A_					:  float = 2000.0						,							A_					:  int = 768						,							A_					:  int = 12						,							A_					:  int = 12						,							A_					:  int = 64						,							A_					:  int = 2048						,							A_					:  float = 0.1						,							)   ->	str:

       """simple docstring"""






       super().__init__()

       lowerCamelCase_		     =    nn.Sequential(
           nn.Linear(A_						,							d_model * 4						,							bias=A_  )						,							nn.SiLU()						,							nn.Linear(d_model * 4						,							d_model * 4						,							bias=A_  )						,							nn.SiLU()						,							)

       lowerCamelCase_		     =    nn.Embedding(A_						,							A_  )
       lowerCamelCase_		     =    False

       lowerCamelCase_		     =    nn.Linear(A_						,							A_						,							bias=A_  )

       lowerCamelCase_		     =    nn.Dropout(p=A_  )

       lowerCamelCase_		     =    nn.ModuleList()
       for lyr_num in range(A_  ):
             # FiLM conditional T5 decoder
             lowerCamelCase_		     =    DecoderLayer(d_model=A_						,							d_kv=A_						,							num_heads=A_						,							d_ff=A_						,							dropout_rate=A_  )
             self.decoders.append(A_  )

       lowerCamelCase_		     =    TaLayerNorm(A_  )

       lowerCamelCase_		     =    nn.Dropout(p=A_  )
       lowerCamelCase_		     =    nn.Linear(A_						,							A_						,							bias=A_  )

 def 				a__ (				self					:  Tuple						,							A_					:  int						,							A_					:  Dict  )   ->	Optional[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    torch.mul(query_input.unsqueeze(-1  )						,							key_input.unsqueeze(-2  )  )
       return mask.unsqueeze(-3  )





 def 				a__ (				self					:  Any						,							A_					:  Tuple						,							A_					:  int						,							A_					:  Union[str, Any]  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_		     =    decoder_input_tokens.shape
       assert decoder_noise_time.shape == (batch,)

       # decoder_noise_time is in [0, 1), so rescale to expected timing range.
       lowerCamelCase_		     =    get_timestep_embedding(
           decoder_noise_time * self.config.max_decoder_noise_time						,							embedding_dim=self.config.d_model						,							max_period=self.config.max_decoder_noise_time						,							).to(dtype=self.dtype  )

       lowerCamelCase_		     =    self.conditioning_emb(A_  ).unsqueeze(1  )

       assert conditioning_emb.shape == (batch, 1, self.config.d_model * 4)

       lowerCamelCase_		     =    decoder_input_tokens.shape[1]

       # If we want to use relative positions for audio context, we can just offset
       # this sequence by the length of encodings_and_masks.
       lowerCamelCase_		     =    torch.broadcast_to(
           torch.arange(A_						,							device=decoder_input_tokens.device  )						,							(batch, seq_length)						,							)

       lowerCamelCase_		     =    self.position_encoding(A_  )

       lowerCamelCase_		     =    self.continuous_inputs_projection(A_  )
       inputs += position_encodings
       lowerCamelCase_		     =    self.dropout(A_  )

       # decoder: No padding present.
       lowerCamelCase_		     =    torch.ones(
           decoder_input_tokens.shape[:2]						,							device=decoder_input_tokens.device						,							dtype=inputs.dtype  )

       # Translate encoding masks to encoder-decoder masks.
       lowerCamelCase_		     =    [(x, self.encoder_decoder_mask(A_						,							A_  )) for x, y in encodings_and_masks]

       # cross attend style: concat encodings
       lowerCamelCase_		     =    torch.cat([x[0] for x in encodings_and_encdec_masks]						,							dim=1  )
       lowerCamelCase_		     =    torch.cat([x[1] for x in encodings_and_encdec_masks]						,							dim=-1  )

       for lyr in self.decoders:
             lowerCamelCase_		     =    lyr(
                 A_						,							conditioning_emb=A_						,							encoder_hidden_states=A_						,							encoder_attention_mask=A_						,							)[0]

       lowerCamelCase_		     =    self.decoder_norm(A_  )
       lowerCamelCase_		     =    self.post_dropout(A_  )

       lowerCamelCase_		     =    self.spec_out(A_  )
       return spec_out







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  Optional[int]						,							A_					:  Tuple						,							A_					:  List[str]						,							A_					:  Dict						,							A_					:  Any						,							A_					:  List[str]						,							A_					:  Union[str, Any]=1E-6  )   ->	Dict:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    nn.ModuleList()

       # cond self attention: layer 0
       self.layer.append(
           TaLayerSelfAttentionCond(d_model=A_						,							d_kv=A_						,							num_heads=A_						,							dropout_rate=A_  )  )

       # cross attention: layer 1
       self.layer.append(
           TaLayerCrossAttention(
               d_model=A_						,							d_kv=A_						,							num_heads=A_						,							dropout_rate=A_						,							layer_norm_epsilon=A_						,							)  )

       # Film Cond MLP + dropout: last layer
       self.layer.append(
           TaLayerFFCond(d_model=A_						,							d_ff=A_						,							dropout_rate=A_						,							layer_norm_epsilon=A_  )  )





 def 				a__ (				self					:  Optional[Any]						,							A_					:  Dict						,							A_					:  Any=None						,							A_					:  Dict=None						,							A_					:  Optional[int]=None						,							A_					:  Dict=None						,							A_					:  int=None						,							)   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    self.layer[0](
           A_						,							conditioning_emb=A_						,							attention_mask=A_						,							)

       if encoder_hidden_states is not None:
             lowerCamelCase_		     =    torch.where(encoder_attention_mask > 0						,							0						,							-1E10  ).to(
                 encoder_hidden_states.dtype  )

             lowerCamelCase_		     =    self.layer[1](
                 A_						,							key_value_states=A_						,							attention_mask=A_						,							)

       # Apply Film Conditional Feed Forward layer
       lowerCamelCase_		     =    self.layer[-1](A_						,							A_  )

       return (hidden_states,)







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  List[str]						,							A_					:  Optional[Any]						,							A_					:  int						,							A_					:  Union[str, Any]						,							A_					:  Dict  )   ->	int:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    TaLayerNorm(A_  )
       lowerCamelCase_		     =    TaFiLMLayer(in_features=d_model * 4						,							out_features=A_  )
       lowerCamelCase_		     =    Attention(query_dim=A_						,							heads=A_						,							dim_head=A_						,							out_bias=A_						,							scale_qk=A_  )
       lowerCamelCase_		     =    nn.Dropout(A_  )





 def 				a__ (				self					:  Tuple						,							A_					:  List[str]						,							A_					:  Dict=None						,							A_					:  Union[str, Any]=None						,							)   ->	Dict:

       """simple docstring"""






       lowerCamelCase_		     =    self.layer_norm(A_  )

       if conditioning_emb is not None:
             lowerCamelCase_		     =    self.FiLMLayer(A_						,							A_  )

       # Self-attention block
       lowerCamelCase_		     =    self.attention(A_  )

       lowerCamelCase_		     =    hidden_states + self.dropout(A_  )

       return hidden_states







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  Union[str, Any]						,							A_					:  Union[str, Any]						,							A_					:  List[Any]						,							A_					:  int						,							A_					:  Any						,							A_					:  Optional[int]  )   ->	int:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    Attention(query_dim=A_						,							heads=A_						,							dim_head=A_						,							out_bias=A_						,							scale_qk=A_  )
       lowerCamelCase_		     =    TaLayerNorm(A_						,							eps=A_  )
       lowerCamelCase_		     =    nn.Dropout(A_  )





 def 				a__ (				self					:  Dict						,							A_					:  Optional[Any]						,							A_					:  Optional[int]=None						,							A_					:  Any=None						,							)   ->	Optional[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    self.layer_norm(A_  )
       lowerCamelCase_		     =    self.attention(
           A_						,							encoder_hidden_states=A_						,							attention_mask=attention_mask.squeeze(1  )						,							)
       lowerCamelCase_		     =    hidden_states + self.dropout(A_  )
       return layer_output







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  str						,							A_					:  Optional[int]						,							A_					:  Union[str, Any]						,							A_					:  str						,							A_					:  List[str]  )   ->	Optional[Any]:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    TaDenseGatedActDense(d_model=A_						,							d_ff=A_						,							dropout_rate=A_  )
       lowerCamelCase_		     =    TaFiLMLayer(in_features=d_model * 4						,							out_features=A_  )
       lowerCamelCase_		     =    TaLayerNorm(A_						,							eps=A_  )
       lowerCamelCase_		     =    nn.Dropout(A_  )





 def 				a__ (				self					:  List[str]						,							A_					:  Any						,							A_					:  Optional[Any]=None  )   ->	Optional[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    self.layer_norm(A_  )
       if conditioning_emb is not None:
             lowerCamelCase_		     =    self.film(A_						,							A_  )

       lowerCamelCase_		     =    self.DenseReluDense(A_  )
       lowerCamelCase_		     =    hidden_states + self.dropout(A_  )
       return hidden_states







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  Any						,							A_					:  str						,							A_					:  str						,							A_					:  Optional[int]  )   ->	Tuple:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    nn.Linear(A_						,							A_						,							bias=A_  )
       lowerCamelCase_		     =    nn.Linear(A_						,							A_						,							bias=A_  )
       lowerCamelCase_		     =    nn.Linear(A_						,							A_						,							bias=A_  )
       lowerCamelCase_		     =    nn.Dropout(A_  )
       lowerCamelCase_		     =    NewGELUActivation()





 def 				a__ (				self					:  Optional[int]						,							A_					:  Optional[int]  )   ->	str:

       """simple docstring"""






       lowerCamelCase_		     =    self.act(self.wi_a(A_  )  )
       lowerCamelCase_		     =    self.wi_a(A_  )
       lowerCamelCase_		     =    hidden_gelu * hidden_linear
       lowerCamelCase_		     =    self.dropout(A_  )

       lowerCamelCase_		     =    self.wo(A_  )
       return hidden_states







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  Any						,							A_					:  str						,							A_					:  List[str]=1E-6  )   ->	Optional[int]:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    nn.Parameter(torch.ones(A_  )  )
       lowerCamelCase_		     =    eps





 def 				a__ (				self					:  str						,							A_					:  List[Any]  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    hidden_states.to(torch.floataa  ).pow(2  ).mean(-1						,							keepdim=A_  )
       lowerCamelCase_		     =    hidden_states * torch.rsqrt(variance + self.variance_epsilon  )

       # convert into half-precision if necessary
       if self.weight.dtype in [torch.floataa, torch.bfloataa]:
             lowerCamelCase_		     =    hidden_states.to(self.weight.dtype  )

       return self.weight * hidden_states







class   A(						nn.Module   ):



 '''simple docstring'''


 def 				a__ (				self					:  List[str]						,							A_					:  torch.Tensor  )   ->	torch.Tensor:

       """simple docstring"""






       return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi  ) * (input + 0.044715 * torch.pow(A_						,							3.0  ))  ))







class   A(						nn.Module   ):



 '''simple docstring'''


 def __init__(				self					:  Any						,							A_					:  List[str]						,							A_					:  List[str]  )   ->	Dict:

       """simple docstring"""






       super().__init__()
       lowerCamelCase_		     =    nn.Linear(A_						,							out_features * 2						,							bias=A_  )





 def 				a__ (				self					:  Dict						,							A_					:  Tuple						,							A_					:  List[Any]  )   ->	Any:

       """simple docstring"""






       lowerCamelCase_		     =    self.scale_bias(A_  )
       lowerCamelCase_     ,				lowerCamelCase_		     =    torch.chunk(A_						,							2						,							-1  )
       lowerCamelCase_		     =    x * (1 + scale) + shift
       return x









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, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class   A(						unittest.TestCase   ):



 '''simple docstring'''


 def 				a__ (				self					:  Optional[Any]  )   ->	Optional[Any]:

       """simple docstring"""






       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

 @property
 def 				a__ (				self					:  int  )   ->	int:

       """simple docstring"""






       lowerCamelCase_		     =    1
       lowerCamelCase_		     =    3
       lowerCamelCase_		     =    (32, 32)

       lowerCamelCase_		     =    floats_tensor((batch_size, num_channels) + sizes						,							rng=random.Random(0  )  ).to(A_  )
       return image

 @property
 def 				a__ (				self					:  Any  )   ->	List[Any]:

       """simple docstring"""






       torch.manual_seed(0  )
       lowerCamelCase_		     =    UNetaDConditionModel(
           block_out_channels=(32, 32, 64)						,							layers_per_block=2						,							sample_size=32						,							in_channels=7						,							out_channels=4						,							down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D', 'CrossAttnDownBlock2D')						,							up_block_types=('CrossAttnUpBlock2D', 'CrossAttnUpBlock2D', 'UpBlock2D')						,							cross_attention_dim=32						,							attention_head_dim=8						,							use_linear_projection=A_						,							only_cross_attention=(True, True, False)						,							num_class_embeds=100						,							)
       return model

 @property
 def 				a__ (				self					:  Optional[int]  )   ->	Optional[int]:

       """simple docstring"""






       torch.manual_seed(0  )
       lowerCamelCase_		     =    AutoencoderKL(
           block_out_channels=[32, 32, 64]						,							in_channels=3						,							out_channels=3						,							down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D']						,							up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D']						,							latent_channels=4						,							)
       return model

 @property
 def 				a__ (				self					:  Union[str, Any]  )   ->	Union[str, Any]:

       """simple docstring"""






       torch.manual_seed(0  )
       lowerCamelCase_		     =    CLIPTextConfig(
           bos_token_id=0						,							eos_token_id=2						,							hidden_size=32						,							intermediate_size=37						,							layer_norm_eps=1E-05						,							num_attention_heads=4						,							num_hidden_layers=5						,							pad_token_id=1						,							vocab_size=1000						,							hidden_act='gelu'						,							projection_dim=512						,							)
       return CLIPTextModel(A_  )

 def 				a__ (				self					:  Optional[int]  )   ->	List[str]:

       """simple docstring"""






       lowerCamelCase_		     =    'cpu'  # ensure determinism for the device-dependent torch.Generator
       lowerCamelCase_		     =    self.dummy_cond_unet_upscale
       lowerCamelCase_		     =    DDPMScheduler()
       lowerCamelCase_		     =    DDIMScheduler(prediction_type='v_prediction'  )
       lowerCamelCase_		     =    self.dummy_vae
       lowerCamelCase_		     =    self.dummy_text_encoder
       lowerCamelCase_		     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

       lowerCamelCase_		     =    self.dummy_image.cpu().permute(0						,							2						,							3						,							1  )[0]
       lowerCamelCase_		     =    Image.fromarray(np.uinta(A_  )  ).convert('RGB'  ).resize((64, 64)  )

       # make sure here that pndm scheduler skips prk
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline(
           unet=A_						,							low_res_scheduler=A_						,							scheduler=A_						,							vae=A_						,							text_encoder=A_						,							tokenizer=A_						,							max_noise_level=350						,							)
       lowerCamelCase_		     =    sd_pipe.to(A_  )
       sd_pipe.set_progress_bar_config(disable=A_  )

       lowerCamelCase_		     =    'A painting of a squirrel eating a burger'
       lowerCamelCase_		     =    torch.Generator(device=A_  ).manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							guidance_scale=6.0						,							noise_level=20						,							num_inference_steps=2						,							output_type='np'						,							)

       lowerCamelCase_		     =    output.images

       lowerCamelCase_		     =    torch.Generator(device=A_  ).manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							guidance_scale=6.0						,							noise_level=20						,							num_inference_steps=2						,							output_type='np'						,							return_dict=A_						,							)[0]

       lowerCamelCase_		     =    image[0, -3:, -3:, -1]
       lowerCamelCase_		     =    image_from_tuple[0, -3:, -3:, -1]

       lowerCamelCase_		     =    low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)
       lowerCamelCase_		     =    np.array([0.3113, 0.3910, 0.4272, 0.4859, 0.5061, 0.4652, 0.5362, 0.5715, 0.5661]  )

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

 def 				a__ (				self					:  List[Any]  )   ->	int:

       """simple docstring"""






       lowerCamelCase_		     =    'cpu'  # ensure determinism for the device-dependent torch.Generator
       lowerCamelCase_		     =    self.dummy_cond_unet_upscale
       lowerCamelCase_		     =    DDPMScheduler()
       lowerCamelCase_		     =    DDIMScheduler(prediction_type='v_prediction'  )
       lowerCamelCase_		     =    self.dummy_vae
       lowerCamelCase_		     =    self.dummy_text_encoder
       lowerCamelCase_		     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

       lowerCamelCase_		     =    self.dummy_image.cpu().permute(0						,							2						,							3						,							1  )[0]
       lowerCamelCase_		     =    Image.fromarray(np.uinta(A_  )  ).convert('RGB'  ).resize((64, 64)  )

       # make sure here that pndm scheduler skips prk
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline(
           unet=A_						,							low_res_scheduler=A_						,							scheduler=A_						,							vae=A_						,							text_encoder=A_						,							tokenizer=A_						,							max_noise_level=350						,							)
       lowerCamelCase_		     =    sd_pipe.to(A_  )
       sd_pipe.set_progress_bar_config(disable=A_  )

       lowerCamelCase_		     =    'A painting of a squirrel eating a burger'
       lowerCamelCase_		     =    sd_pipe(
           2 * [prompt]						,							image=2 * [low_res_image]						,							guidance_scale=6.0						,							noise_level=20						,							num_inference_steps=2						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images
       assert image.shape[0] == 2

       lowerCamelCase_		     =    torch.Generator(device=A_  ).manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							num_images_per_prompt=2						,							guidance_scale=6.0						,							noise_level=20						,							num_inference_steps=2						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images
       assert image.shape[0] == 2





 @unittest.skipIf(torch_device != 'cuda'						,							'This test requires a GPU'  )
 def 				a__ (				self					:  List[Any]  )   ->	str:

       """simple docstring"""






       lowerCamelCase_		     =    self.dummy_cond_unet_upscale
       lowerCamelCase_		     =    DDPMScheduler()
       lowerCamelCase_		     =    DDIMScheduler(prediction_type='v_prediction'  )
       lowerCamelCase_		     =    self.dummy_vae
       lowerCamelCase_		     =    self.dummy_text_encoder
       lowerCamelCase_		     =    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

       lowerCamelCase_		     =    self.dummy_image.cpu().permute(0						,							2						,							3						,							1  )[0]
       lowerCamelCase_		     =    Image.fromarray(np.uinta(A_  )  ).convert('RGB'  ).resize((64, 64)  )

       # put models in fp16, except vae as it overflows in fp16
       lowerCamelCase_		     =    unet.half()
       lowerCamelCase_		     =    text_encoder.half()

       # make sure here that pndm scheduler skips prk
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline(
           unet=A_						,							low_res_scheduler=A_						,							scheduler=A_						,							vae=A_						,							text_encoder=A_						,							tokenizer=A_						,							max_noise_level=350						,							)
       lowerCamelCase_		     =    sd_pipe.to(A_  )
       sd_pipe.set_progress_bar_config(disable=A_  )

       lowerCamelCase_		     =    'A painting of a squirrel eating a burger'
       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    sd_pipe(
           [prompt]						,							image=A_						,							generator=A_						,							num_inference_steps=2						,							output_type='np'						,							).images

       lowerCamelCase_		     =    low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)







@slow
@require_torch_gpu
class   A(						unittest.TestCase   ):



 '''simple docstring'''


 def 				a__ (				self					:  Tuple  )   ->	str:

       """simple docstring"""






       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()

 def 				a__ (				self					:  int  )   ->	Optional[int]:

       """simple docstring"""






       lowerCamelCase_		     =    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/sd2-upscale/low_res_cat.png'  )
       lowerCamelCase_		     =    load_numpy(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
           '/upsampled_cat.npy'  )

       lowerCamelCase_		     =    'stabilityai/stable-diffusion-x4-upscaler'
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline.from_pretrained(A_  )
       pipe.to(A_  )
       pipe.set_progress_bar_config(disable=A_  )
       pipe.enable_attention_slicing()

       lowerCamelCase_		     =    'a cat sitting on a park bench'

       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    pipe(
           prompt=A_						,							image=A_						,							generator=A_						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image  ).max() < 1E-3

 def 				a__ (				self					:  Dict  )   ->	List[str]:

       """simple docstring"""






       lowerCamelCase_		     =    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/sd2-upscale/low_res_cat.png'  )
       lowerCamelCase_		     =    load_numpy(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'
           '/upsampled_cat_fp16.npy'  )

       lowerCamelCase_		     =    'stabilityai/stable-diffusion-x4-upscaler'
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline.from_pretrained(
           A_						,							torch_dtype=torch.floataa						,							)
       pipe.to(A_  )
       pipe.set_progress_bar_config(disable=A_  )
       pipe.enable_attention_slicing()

       lowerCamelCase_		     =    'a cat sitting on a park bench'

       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    pipe(
           prompt=A_						,							image=A_						,							generator=A_						,							output_type='np'						,							)
       lowerCamelCase_		     =    output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image  ).max() < 5E-1





 def 				a__ (				self					:  Union[str, Any]  )   ->	Union[str, Any]:

       """simple docstring"""






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

       lowerCamelCase_		     =    load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/sd2-upscale/low_res_cat.png'  )

       lowerCamelCase_		     =    'stabilityai/stable-diffusion-x4-upscaler'
       lowerCamelCase_		     =    StableDiffusionUpscalePipeline.from_pretrained(
           A_						,							torch_dtype=torch.floataa						,							)
       pipe.to(A_  )
       pipe.set_progress_bar_config(disable=A_  )
       pipe.enable_attention_slicing(1  )
       pipe.enable_sequential_cpu_offload()

       lowerCamelCase_		     =    'a cat sitting on a park bench'

       lowerCamelCase_		     =    torch.manual_seed(0  )
       lowerCamelCase_		     =    pipe(
           prompt=A_						,							image=A_						,							generator=A_						,							num_inference_steps=5						,							output_type='np'						,							)

       lowerCamelCase_		     =    torch.cuda.max_memory_allocated()
       # make sure that less than 2.9 GB is allocated
       assert mem_bytes < 2.9 * 10**9



