
'''simple docstring'''
import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
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

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    snake_case_ :    Any     			=						[tensor.shape for tensor in tensor_list]
    return all(shape == shapes[0] for shape in shapes[1:]       )






class    UpperCAmelCase_				(	snake_case__    ,						snake_case__    ,						snake_case__    ,						unittest.TestCase					):
 UpperCAmelCase_		    =						StableDiffusionLatentUpscalePipeline
 UpperCAmelCase_		    =						TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
     """height""",
     """width""",
     """cross_attention_kwargs""",
     """negative_prompt_embeds""",
     """prompt_embeds""",
 }
 UpperCAmelCase_		    =						PipelineTesterMixin.required_optional_params - {"""num_images_per_prompt"""}
 UpperCAmelCase_		    =						TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
 UpperCAmelCase_		    =						frozenset(
     []					)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
 UpperCAmelCase_		    =						frozenset([]					)

 UpperCAmelCase_		    =						True





 @property
 def        snake_case__		(    self):
     snake_case_ :    Dict     			=						1
     snake_case_ :    int     			=						4
     snake_case_ :    int     			=						(16, 16)

     snake_case_ :    List[Any]     			=						floats_tensor((batch_size, num_channels) + sizes ,       rng=random.Random(0)).to(lowercase_)
     return image





 def        snake_case__		(    self):
     torch.manual_seed(0)
     snake_case_ :    str     			=						UNetaDConditionModel(
         act_fn="gelu" ,       attention_head_dim=8 ,       norm_num_groups=lowercase_ ,       block_out_channels=[32, 32, 64, 64] ,       time_cond_proj_dim=1_60 ,       conv_in_kernel=1 ,       conv_out_kernel=1 ,       cross_attention_dim=32 ,       down_block_types=(
             "KDownBlock2D",
             "KCrossAttnDownBlock2D",
             "KCrossAttnDownBlock2D",
             "KCrossAttnDownBlock2D",
         ) ,       in_channels=8 ,       mid_block_type=lowercase_ ,       only_cross_attention=lowercase_ ,       out_channels=5 ,       resnet_time_scale_shift="scale_shift" ,       time_embedding_type="fourier" ,       timestep_post_act="gelu" ,       up_block_types=("KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KUpBlock2D") ,       )
     snake_case_ :    Any     			=						AutoencoderKL(
         block_out_channels=[32, 32, 64, 64] ,       in_channels=3 ,       out_channels=3 ,       down_block_types=[
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
         ] ,       up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"] ,       latent_channels=4 ,       )
     snake_case_ :    List[str]     			=						EulerDiscreteScheduler(prediction_type="sample")
     snake_case_ :    Dict     			=						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=10_00 ,       hidden_act="quick_gelu" ,       projection_dim=5_12 ,       )
     snake_case_ :    List[str]     			=						CLIPTextModel(lowercase_)
     snake_case_ :    str     			=						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

     snake_case_ :    Optional[Any]     			=						{
         "unet": model.eval(),
         "vae": vae.eval(),
         "scheduler": scheduler,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
     }

     return components





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_=0):
     if str(lowercase_).startswith("mps"):
         snake_case_ :    str     			=						torch.manual_seed(lowercase_)
     else:
         snake_case_ :    Any     			=						torch.Generator(device=lowercase_).manual_seed(lowercase_)
     snake_case_ :    Tuple     			=						{
         "prompt": "A painting of a squirrel eating a burger",
         "image": self.dummy_image.cpu(),
         "generator": generator,
         "num_inference_steps": 2,
         "output_type": "numpy",
     }
     return inputs





 def        snake_case__		(    self):
     snake_case_ :    Tuple     			=						"cpu"

     snake_case_ :    Optional[int]     			=						self.get_dummy_components()
     snake_case_ :    Any     			=						self.pipeline_class(**lowercase_)
     pipe.to(lowercase_)
     pipe.set_progress_bar_config(disable=lowercase_)

     snake_case_ :    List[Any]     			=						self.get_dummy_inputs(lowercase_)
     snake_case_ :    int     			=						pipe(**lowercase_).images
     snake_case_ :    str     			=						image[0, -3:, -3:, -1]

     self.assertEqual(image.shape ,       (1, 2_56, 2_56, 3))
     snake_case_ :    int     			=						np.array(
         [0.47_222_412, 0.41_921_633, 0.44_717_434, 0.46_874_192, 0.42_588_258, 0.46_150_726, 0.4_677_534, 0.45_583_832, 0.48_579_055])
     snake_case_ :    Tuple     			=						np.abs(image_slice.flatten() - expected_slice).max()
     self.assertLessEqual(lowercase_ ,       1E-3)





 def        snake_case__		(    self):
     super().test_attention_slicing_forward_pass(expected_max_diff=7E-3)





 def        snake_case__		(    self):
     super().test_cpu_offload_forward_pass(expected_max_diff=3E-3)





 def        snake_case__		(    self):
     super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3)





 def        snake_case__		(    self):
     super().test_inference_batch_single_identical(expected_max_diff=7E-3)





 def        snake_case__		(    self):
     super().test_pt_np_pil_outputs_equivalent(expected_max_diff=3E-3)





 def        snake_case__		(    self):
     super().test_save_load_local(expected_max_difference=3E-3)





 def        snake_case__		(    self):
     super().test_save_load_optional_components(expected_max_difference=3E-3)





 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						[
         "DDIMScheduler",
         "DDPMScheduler",
         "PNDMScheduler",
         "HeunDiscreteScheduler",
         "EulerAncestralDiscreteScheduler",
         "KDPM2DiscreteScheduler",
         "KDPM2AncestralDiscreteScheduler",
         "DPMSolverSDEScheduler",
     ]
     snake_case_ :    List[str]     			=						self.get_dummy_components()
     snake_case_ :    List[str]     			=						self.pipeline_class(**lowercase_)

     # make sure that PNDM does not need warm-up
     pipe.scheduler.register_to_config(skip_prk_steps=lowercase_)

     pipe.to(lowercase_)
     pipe.set_progress_bar_config(disable=lowercase_)
     snake_case_ :    Optional[int]     			=						self.get_dummy_inputs(lowercase_)
     snake_case_ :    List[str]     			=						2

     snake_case_ :    int     			=						[]
     for scheduler_enum in KarrasDiffusionSchedulers:
         if scheduler_enum.name in skip_schedulers:
             # no sigma schedulers are not supported
             # no schedulers
             continue

         snake_case_ :    Optional[int]     			=						getattr(lowercase_ ,       scheduler_enum.name)
         snake_case_ :    Tuple     			=						scheduler_cls.from_config(pipe.scheduler.config)
         snake_case_ :    Dict     			=						pipe(**lowercase_)[0]
         outputs.append(lowercase_)

     assert check_same_shape(lowercase_)






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





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





 def        snake_case__		(    self):
     snake_case_ :    int     			=						torch.manual_seed(33)

     snake_case_ :    int     			=						StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4" ,       torch_dtype=torch.floataa)
     pipe.to("cuda")

     snake_case_ :    str     			=						StableDiffusionLatentUpscalePipeline.from_pretrained(
         "stabilityai/sd-x2-latent-upscaler" ,       torch_dtype=torch.floataa)
     upscaler.to("cuda")

     snake_case_ :    Dict     			=						"a photo of an astronaut high resolution, unreal engine, ultra realistic"

     snake_case_ :    Optional[int]     			=						pipe(lowercase_ ,       generator=lowercase_ ,       output_type="latent").images

     snake_case_ :    List[Any]     			=						upscaler(
         prompt=lowercase_ ,       image=lowercase_ ,       num_inference_steps=20 ,       guidance_scale=0 ,       generator=lowercase_ ,       output_type="np" ,       ).images[0]

     snake_case_ :    Optional[int]     			=						load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy")
     assert np.abs((expected_image - image).mean()) < 5E-2





 def        snake_case__		(    self):
     snake_case_ :    Union[str, Any]     			=						torch.manual_seed(33)

     snake_case_ :    Tuple     			=						StableDiffusionLatentUpscalePipeline.from_pretrained(
         "stabilityai/sd-x2-latent-upscaler" ,       torch_dtype=torch.floataa)
     upscaler.to("cuda")

     snake_case_ :    Union[str, Any]     			=						"the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"

     snake_case_ :    Optional[int]     			=						load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png")

     snake_case_ :    Optional[Any]     			=						upscaler(
         prompt=lowercase_ ,       image=lowercase_ ,       num_inference_steps=20 ,       guidance_scale=0 ,       generator=lowercase_ ,       output_type="np" ,       ).images[0]

     snake_case_ :    str     			=						load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy")
     assert np.abs((expected_image - image).max()) < 5E-2






'''simple docstring'''
import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


a_											=						logging.get_logger(__name__)

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    snake_case_ :    List[Any]     			=						r"\w+[.]\d+"
    snake_case_ :    Tuple     			=						re.findall(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       )
    for pat in pats:
        snake_case_ :    Union[str, Any]     			=						key.replace(__SCREAMING_SNAKE_CASE,		"_".join(pat.split("."       )       )       )
    return key

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    snake_case_ :    Tuple     			=						pt_tuple_key[:-1] + ("scale",)
    if (
        any("norm" in str_ for str_ in pt_tuple_key       )
        and (pt_tuple_key[-1] == "bias")
        and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
        and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
    ):
        snake_case_ :    int     			=						pt_tuple_key[:-1] + ("scale",)
        return renamed_pt_tuple_key, pt_tensor
    elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
        snake_case_ :    Dict     			=						pt_tuple_key[:-1] + ("scale",)
        return renamed_pt_tuple_key, pt_tensor

    # embedding
    if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
        snake_case_ :    List[Any]     			=						pt_tuple_key[:-1] + ("embedding",)
        return renamed_pt_tuple_key, pt_tensor

    # conv layer
    snake_case_ :    List[str]     			=						pt_tuple_key[:-1] + ("kernel",)
    if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
        snake_case_ :    str     			=						pt_tensor.transpose(2,		3,		1,		0       )
        return renamed_pt_tuple_key, pt_tensor

    # linear layer
    snake_case_ :    str     			=						pt_tuple_key[:-1] + ("kernel",)
    if pt_tuple_key[-1] == "weight":
        snake_case_ :    Any     			=						pt_tensor.T
        return renamed_pt_tuple_key, pt_tensor

    # old PyTorch layer norm weight
    snake_case_ :    Union[str, Any]     			=						pt_tuple_key[:-1] + ("weight",)
    if pt_tuple_key[-1] == "gamma":
        return renamed_pt_tuple_key, pt_tensor

    # old PyTorch layer norm bias
    snake_case_ :    Union[str, Any]     			=						pt_tuple_key[:-1] + ("bias",)
    if pt_tuple_key[-1] == "beta":
        return renamed_pt_tuple_key, pt_tensor

    return pt_tuple_key, pt_tensor


def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE=4_2       ):
    """simple docstring"""






    snake_case_ :    int     			=						{k: v.numpy() for k, v in pt_state_dict.items()}

    # Step 2: Since the model is stateless, get random Flax params
    snake_case_ :    Optional[int]     			=						flax_model.init_weights(PRNGKey(__SCREAMING_SNAKE_CASE       )       )

    snake_case_ :    Union[str, Any]     			=						flatten_dict(__SCREAMING_SNAKE_CASE       )
    snake_case_ :    str     			=						{}

    # Need to change some parameters name to match Flax names
    for pt_key, pt_tensor in pt_state_dict.items():
        snake_case_ :    List[str]     			=						rename_key(__SCREAMING_SNAKE_CASE       )
        snake_case_ :    Tuple     			=						tuple(renamed_pt_key.split("."       )       )

        # Correctly rename weight parameters
        snake_case_			, snake_case_ :    Dict     			=						rename_key_and_reshape_tensor(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       )

        if flax_key in random_flax_state_dict:
            if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
                raise ValueError(
                    f'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '
                    f'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.'       )

        # also add unexpected weight so that warning is thrown
        snake_case_ :    Any     			=						jnp.asarray(__SCREAMING_SNAKE_CASE       )

    return unflatten_dict(__SCREAMING_SNAKE_CASE       )




