





import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, 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_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class      lowercase__    (							__A     ,					__A     ,					unittest.TestCase  ):
  __UpperCamelCase	     =  StableDiffusionXLImgaImgPipeline
  __UpperCamelCase	     =  TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
  __UpperCamelCase	     =  PipelineTesterMixin.required_optional_params - {"""latents"""}
  __UpperCamelCase	     =  TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
  __UpperCamelCase	     =  IMAGE_TO_IMAGE_IMAGE_PARAMS
  __UpperCamelCase	     =  IMAGE_TO_IMAGE_IMAGE_PARAMS







  def 	UpperCAmelCase__		(	self						):
       torch.manual_seed(0						)
       lowerCAmelCase_					:       List[Any]    =		UNetaDConditionModel(
           block_out_channels=(32, 64)    ,						layers_per_block=2    ,						sample_size=32    ,						in_channels=4    ,						out_channels=4    ,						down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")    ,						up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")    ,						attention_head_dim=(2, 4)    ,						use_linear_projection=_lowercase    ,						addition_embed_type="""text_time"""    ,						addition_time_embed_dim=8    ,						transformer_layers_per_block=(1, 2)    ,						projection_class_embeddings_input_dim=80    ,						cross_attention_dim=64    ,						)
       lowerCAmelCase_					:       List[Any]    =		EulerDiscreteScheduler(
           beta_start=0.0_0085    ,						beta_end=0.012    ,						steps_offset=1    ,						beta_schedule="""scaled_linear"""    ,						timestep_spacing="""leading"""    ,						)
       torch.manual_seed(0						)
       lowerCAmelCase_					:       Any    =		AutoencoderKL(
           block_out_channels=[32, 64]    ,						in_channels=3    ,						out_channels=3    ,						down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]    ,						up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]    ,						latent_channels=4    ,						sample_size=128    ,						)
       torch.manual_seed(0						)
       lowerCAmelCase_					:       Optional[Any]    =		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=1_000    ,						hidden_act="""gelu"""    ,						projection_dim=32    ,						)
       lowerCAmelCase_					:       Dict    =		CLIPTextModel(_lowercase						)
       lowerCAmelCase_					:       Optional[Any]    =		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    ,						local_files_only=_lowercase						)

       lowerCAmelCase_					:       Optional[int]    =		CLIPTextModelWithProjection(_lowercase						)
       lowerCAmelCase_					:       List[Any]    =		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    ,						local_files_only=_lowercase						)

       lowerCAmelCase_					:       List[Any]    =		{
           """unet""": unet,
           """scheduler""": scheduler,
           """vae""": vae,
           """text_encoder""": text_encoder,
           """tokenizer""": tokenizer,
           """text_encoder_2""": text_encoder_a,
           """tokenizer_2""": tokenizer_a,
           # "safety_checker": None,
           # "feature_extractor": None,
       }
       return components







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase=0						):
       lowerCAmelCase_					:       List[Any]    =		floats_tensor((1, 3, 32, 32)    ,						rng=random.Random(_lowercase						)						).to(_lowercase						)
       lowerCAmelCase_					:       Optional[int]    =		image / 2 + 0.5
       if str(_lowercase						).startswith("""mps"""						):
            lowerCAmelCase_					:       Any    =		torch.manual_seed(_lowercase						)
       else:
            lowerCAmelCase_					:       str    =		torch.Generator(device=_lowercase						).manual_seed(_lowercase						)
       lowerCAmelCase_					:       Optional[Any]    =		{
           """prompt""": """A painting of a squirrel eating a burger""",
           """image""": image,
           """generator""": generator,
           """num_inference_steps""": 2,
           """guidance_scale""": 5.0,
           """output_type""": """numpy""",
           """strength""": 0.75,
       }
       return inputs







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Union[str, Any]    =		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
       lowerCAmelCase_					:       Any    =		self.get_dummy_components()
       lowerCAmelCase_					:       Any    =		StableDiffusionXLImgaImgPipeline(**_lowercase						)
       lowerCAmelCase_					:       List[str]    =		sd_pipe.to(_lowercase						)
       sd_pipe.set_progress_bar_config(disable=_lowercase						)

       lowerCAmelCase_					:       Dict    =		self.get_dummy_inputs(_lowercase						)
       lowerCAmelCase_					:       List[str]    =		sd_pipe(**_lowercase						).images
       lowerCAmelCase_					:       List[Any]    =		image[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)

       lowerCAmelCase_					:       Optional[Any]    =		np.array([0.4656, 0.4840, 0.4439, 0.6698, 0.5574, 0.4524, 0.5799, 0.5943, 0.5165]						)

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







  def 	UpperCAmelCase__		(	self						):
       super().test_attention_slicing_forward_pass(expected_max_diff=3e-3						)







  def 	UpperCAmelCase__		(	self						):
       super().test_inference_batch_single_identical(expected_max_diff=3e-3						)







  def 	UpperCAmelCase__		(	self						):
       pass







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       Optional[Any]    =		self.get_dummy_components()
       lowerCAmelCase_					:       Optional[Any]    =		StableDiffusionXLImgaImgPipeline(**_lowercase						)
       lowerCAmelCase_					:       Union[str, Any]    =		sd_pipe.to(_lowercase						)
       lowerCAmelCase_					:       Union[str, Any]    =		sd_pipe.to(_lowercase						)
       sd_pipe.set_progress_bar_config(disable=_lowercase						)

       # forward without prompt embeds
       lowerCAmelCase_					:       str    =		self.get_dummy_inputs(_lowercase						)
       lowerCAmelCase_					:       Union[str, Any]    =		3 * ["""this is a negative prompt"""]
       lowerCAmelCase_					:       Tuple    =		negative_prompt
       lowerCAmelCase_					:       Dict    =		3 * [inputs["""prompt"""]]

       lowerCAmelCase_					:       Optional[int]    =		sd_pipe(**_lowercase						)
       lowerCAmelCase_					:       Tuple    =		output.images[0, -3:, -3:, -1]

       # forward with prompt embeds
       lowerCAmelCase_					:       Optional[int]    =		self.get_dummy_inputs(_lowercase						)
       lowerCAmelCase_					:       str    =		3 * ["""this is a negative prompt"""]
       lowerCAmelCase_					:       Any    =		3 * [inputs.pop("""prompt"""						)]

       (
           lowerCAmelCase_  
       )					:       Dict    =		sd_pipe.encode_prompt(_lowercase    ,						negative_prompt=_lowercase						)

       lowerCAmelCase_					:       Union[str, Any]    =		sd_pipe(
           **_lowercase    ,						prompt_embeds=_lowercase    ,						negative_prompt_embeds=_lowercase    ,						pooled_prompt_embeds=_lowercase    ,						negative_pooled_prompt_embeds=_lowercase    ,						)
       lowerCAmelCase_					:       Dict    =		output.images[0, -3:, -3:, -1]

       # make sure that it's equal
       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()						).max() < 1e-4





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







  def 	UpperCAmelCase__		(	self						):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()







  def 	UpperCAmelCase__		(	self    ,						_lowercase    ,						_lowercase="cpu"    ,						_lowercase=torch.floataa    ,						_lowercase=0						):
       lowerCAmelCase_					:       str    =		torch.Generator(device=_lowercase						).manual_seed(_lowercase						)
       lowerCAmelCase_					:       Optional[int]    =		np.random.RandomState(_lowercase						).standard_normal((1, 4, 64, 64)						)
       lowerCAmelCase_					:       List[str]    =		torch.from_numpy(_lowercase						).to(device=_lowercase    ,						dtype=_lowercase						)
       lowerCAmelCase_					:       Any    =		{
           """prompt""": """a photograph of an astronaut riding a horse""",
           """latents""": latents,
           """generator""": generator,
           """num_inference_steps""": 3,
           """guidance_scale""": 7.5,
           """output_type""": """numpy""",
       }
       return inputs







  def 	UpperCAmelCase__		(	self						):
       lowerCAmelCase_					:       str    =		DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""						)
       pipe.to(_lowercase						)
       pipe.set_progress_bar_config(disable=_lowercase						)

       lowerCAmelCase_					:       Dict    =		self.get_inputs(_lowercase						)
       lowerCAmelCase_					:       List[str]    =		pipe(**_lowercase						).images
       lowerCAmelCase_					:       Optional[Any]    =		image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 512, 512, 3)
       lowerCAmelCase_					:       int    =		np.array([0.4_9493, 0.4_7896, 0.4_0798, 0.5_4214, 0.5_3212, 0.4_8202, 0.4_7656, 0.4_6329, 0.4_8506]						)
       assert np.abs(image_slice - expected_slice						).max() < 7e-3







from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_					:   Tuple 							=						logging.get_logger(__name__)

UpperCAmelCase_					:   List[str] 							=						{
    """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/config.json""",
    """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/config.json"""
    # See all FNet models at https://huggingface.co/models?filter=fnet
}





class      lowercase__    (							__A  ):
  __UpperCamelCase	     =  """fnet"""







  def __init__(	self    ,						_lowercase=32_000    ,						_lowercase=768    ,						_lowercase=12    ,						_lowercase=3_072    ,						_lowercase="gelu_new"    ,						_lowercase=0.1    ,						_lowercase=512    ,						_lowercase=4    ,						_lowercase=0.02    ,						_lowercase=1e-12    ,						_lowercase=False    ,						_lowercase=512    ,						_lowercase=3    ,						_lowercase=1    ,						_lowercase=2    ,						**_lowercase    ,						):
       super().__init__(pad_token_id=_lowercase    ,						bos_token_id=_lowercase    ,						eos_token_id=_lowercase    ,						**_lowercase						)

       lowerCAmelCase_					:       Optional[Any]    =		vocab_size
       lowerCAmelCase_					:       List[str]    =		max_position_embeddings
       lowerCAmelCase_					:       str    =		hidden_size
       lowerCAmelCase_					:       Any    =		num_hidden_layers
       lowerCAmelCase_					:       Optional[int]    =		intermediate_size
       lowerCAmelCase_					:       int    =		hidden_act
       lowerCAmelCase_					:       List[str]    =		hidden_dropout_prob
       lowerCAmelCase_					:       Tuple    =		initializer_range
       lowerCAmelCase_					:       int    =		type_vocab_size
       lowerCAmelCase_					:       str    =		layer_norm_eps
       lowerCAmelCase_					:       Optional[Any]    =		use_tpu_fourier_optimizations
       lowerCAmelCase_					:       Union[str, Any]    =		tpu_short_seq_length
