



import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()





class 		__SCREAMING_SNAKE_CASE      (				lowercase__					,			unittest.TestCase      ):






				"""simple docstring"""

				SCREAMING_SNAKE_CASE_ 				=  KandinskyPipeline
				SCREAMING_SNAKE_CASE_ 				=  [
				    'prompt',
				    'image_embeds',
				    'negative_image_embeds',
				]
				SCREAMING_SNAKE_CASE_ 				=  ['prompt', 'negative_prompt', 'image_embeds', 'negative_image_embeds']
				SCREAMING_SNAKE_CASE_ 				=  [
				    'generator',
				    'height',
				    'width',
				    'latents',
				    'guidance_scale',
				    'negative_prompt',
				    'num_inference_steps',
				    'return_dict',
				    'guidance_scale',
				    'num_images_per_prompt',
				    'output_type',
				    'return_dict',
				]
				SCREAMING_SNAKE_CASE_ 				=  False







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					return 32







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					return 32







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					return self.time_input_dim







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					return self.time_input_dim * 4







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					return 1_00







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base"""      )
					return tokenizer







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					torch.manual_seed(0      )
					_snake_case					:							List[str]					     =   MCLIPConfig(
					    numDims=self.cross_attention_dim						,	transformerDimensions=self.text_embedder_hidden_size						,	hidden_size=self.text_embedder_hidden_size						,	intermediate_size=37						,	num_attention_heads=4						,	num_hidden_layers=5						,	vocab_size=10_05						,	)

					_snake_case					:							str					     =   MultilingualCLIP(SCREAMING_SNAKE_CASE__      )
					_snake_case					:							Optional[Any]					     =   text_encoder.eval()

					return text_encoder







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					torch.manual_seed(0      )

					_snake_case					:							Any					     =   {
					    """in_channels""": 4,
					    # Out channels is double in channels because predicts mean and variance
					    """out_channels""": 8,
					    """addition_embed_type""": """text_image""",
					    """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
					    """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
					    """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
					    """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
					    """layers_per_block""": 1,
					    """encoder_hid_dim""": self.text_embedder_hidden_size,
					    """encoder_hid_dim_type""": """text_image_proj""",
					    """cross_attention_dim""": self.cross_attention_dim,
					    """attention_head_dim""": 4,
					    """resnet_time_scale_shift""": """scale_shift""",
					    """class_embed_type""": None,
					}

					_snake_case					:							Optional[int]					     =   UNetaDConditionModel(**SCREAMING_SNAKE_CASE__      )
					return model







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					return {
					    "block_out_channels": [32, 64],
					    "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
					    "in_channels": 3,
					    "latent_channels": 4,
					    "layers_per_block": 1,
					    "norm_num_groups": 8,
					    "norm_type": "spatial",
					    "num_vq_embeddings": 12,
					    "out_channels": 3,
					    "up_block_types": [
					        "AttnUpDecoderBlock2D",
					        "UpDecoderBlock2D",
					    ],
					    "vq_embed_dim": 4,
					}







				@property
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					torch.manual_seed(0      )
					_snake_case					:							str					     =   VQModel(**self.dummy_movq_kwargs      )
					return model







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Optional[int]					     =   self.dummy_text_encoder
					_snake_case					:							str					     =   self.dummy_tokenizer
					_snake_case					:							int					     =   self.dummy_unet
					_snake_case					:							List[Any]					     =   self.dummy_movq

					_snake_case					:							Tuple					     =   DDIMScheduler(
					    num_train_timesteps=10_00						,	beta_schedule="""linear"""						,	beta_start=0.0_0085						,	beta_end=0.012						,	clip_sample=SCREAMING_SNAKE_CASE__						,	set_alpha_to_one=SCREAMING_SNAKE_CASE__						,	steps_offset=1						,	prediction_type="""epsilon"""						,	thresholding=SCREAMING_SNAKE_CASE__						,	)

					_snake_case					:							Optional[int]					     =   {
					    """text_encoder""": text_encoder,
					    """tokenizer""": tokenizer,
					    """unet""": unet,
					    """scheduler""": scheduler,
					    """movq""": movq,
					}
					return components







				def 		__lowerCamelCase(				self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=0      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   floats_tensor((1, self.cross_attention_dim)						,	rng=random.Random(SCREAMING_SNAKE_CASE__      )      ).to(SCREAMING_SNAKE_CASE__      )
					_snake_case					:							List[str]					     =   floats_tensor((1, self.cross_attention_dim)						,	rng=random.Random(seed + 1      )      ).to(SCREAMING_SNAKE_CASE__      )
					if str(SCREAMING_SNAKE_CASE__      ).startswith("""mps"""      ):
						_snake_case					:							Tuple					     =   torch.manual_seed(SCREAMING_SNAKE_CASE__      )
					else:
						_snake_case					:							Tuple					     =   torch.Generator(device=SCREAMING_SNAKE_CASE__      ).manual_seed(SCREAMING_SNAKE_CASE__      )
					_snake_case					:							List[str]					     =   {
					    """prompt""": """horse""",
					    """image_embeds""": image_embeds,
					    """negative_image_embeds""": negative_image_embeds,
					    """generator""": generator,
					    """height""": 64,
					    """width""": 64,
					    """guidance_scale""": 4.0,
					    """num_inference_steps""": 2,
					    """output_type""": """np""",
					}
					return inputs







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Optional[int]					     =   """cpu"""

					_snake_case					:							Optional[Any]					     =   self.get_dummy_components()

					_snake_case					:							Optional[Any]					     =   self.pipeline_class(**SCREAMING_SNAKE_CASE__      )
					_snake_case					:							Dict					     =   pipe.to(SCREAMING_SNAKE_CASE__      )

					pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Any					     =   pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE__      )      )
					_snake_case					:							List[str]					     =   output.images

					_snake_case					:							List[str]					     =   pipe(
					    **self.get_dummy_inputs(SCREAMING_SNAKE_CASE__      )						,	return_dict=SCREAMING_SNAKE_CASE__						,	)[0]

					_snake_case					:							List[Any]					     =   image[0, -3:, -3:, -1]
					_snake_case					:							Any					     =   image_from_tuple[0, -3:, -3:, -1]

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

					_snake_case					:							List[str]					     =   np.array(
					    [0.32_8663, 1.0, 0.2321_6873, 1.0, 0.9271_7564, 0.463_9046, 0.9689_4777, 0.3171_3378, 0.629_3953]      )

					assert (
					    np.abs(image_slice.flatten() - expected_slice      ).max() < 1e-2
					), f''' expected_slice {expected_slice}, but got {image_slice.flatten()}'''
					assert (
					    np.abs(image_from_tuple_slice.flatten() - expected_slice      ).max() < 1e-2
					), f''' expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}'''





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






				"""simple docstring"""

				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

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







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Dict					     =   load_numpy(
					    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
					    """/kandinsky/kandinsky_text2img_cat_fp16.npy"""      )

					_snake_case					:							Optional[int]					     =   KandinskyPriorPipeline.from_pretrained(
					    """kandinsky-community/kandinsky-2-1-prior"""						,	torch_dtype=torch.floataa      )
					pipe_prior.to(SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Tuple					     =   KandinskyPipeline.from_pretrained("""kandinsky-community/kandinsky-2-1"""						,	torch_dtype=torch.floataa      )
					_snake_case					:							Dict					     =   pipeline.to(SCREAMING_SNAKE_CASE__      )
					pipeline.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Any					     =   """red cat, 4k photo"""

					_snake_case					:							Dict					     =   torch.Generator(device="""cuda"""      ).manual_seed(0      )
					_snake_case     , _snake_case					:							List[Any]					     =   pipe_prior(
					    SCREAMING_SNAKE_CASE__						,	generator=SCREAMING_SNAKE_CASE__						,	num_inference_steps=5						,	negative_prompt=""""""						,	).to_tuple()

					_snake_case					:							List[Any]					     =   torch.Generator(device="""cuda"""      ).manual_seed(0      )
					_snake_case					:							str					     =   pipeline(
					    SCREAMING_SNAKE_CASE__						,	image_embeds=SCREAMING_SNAKE_CASE__						,	negative_image_embeds=SCREAMING_SNAKE_CASE__						,	generator=SCREAMING_SNAKE_CASE__						,	num_inference_steps=1_00						,	output_type="""np"""						,	)

					_snake_case					:							List[Any]					     =   output.images[0]

					assert image.shape == (5_12, 5_12, 3)

					assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__      )










import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor





class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				def 		__lowerCamelCase(				self						,	SCREAMING_SNAKE_CASE__ = False      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   10

					if only_cross_attention:
						_snake_case					:							Tuple					     =   12
					else:
						# when only cross attention is not set, the cross attention dim must be the same as the query dim
						_snake_case					:							Any					     =   query_dim

					return {
					    "query_dim": query_dim,
					    "cross_attention_dim": cross_attention_dim,
					    "heads": 2,
					    "dim_head": 4,
					    "added_kv_proj_dim": 6,
					    "norm_num_groups": 1,
					    "only_cross_attention": only_cross_attention,
					    "processor": AttnAddedKVProcessor(),
					}







				def 		__lowerCamelCase(				self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   2

					_snake_case					:							List[str]					     =   torch.rand(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	3						,	2      )
					_snake_case					:							List[str]					     =   torch.rand(SCREAMING_SNAKE_CASE__						,	4						,	SCREAMING_SNAKE_CASE__      )
					_snake_case					:							Optional[int]					     =   None

					return {
					    "hidden_states": hidden_states,
					    "encoder_hidden_states": encoder_hidden_states,
					    "attention_mask": attention_mask,
					}







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					torch.manual_seed(0      )

					_snake_case					:							Optional[int]					     =   self.get_constructor_arguments(only_cross_attention=SCREAMING_SNAKE_CASE__      )
					_snake_case					:							List[str]					     =   Attention(**SCREAMING_SNAKE_CASE__      )

					self.assertTrue(attn.to_k is not None      )
					self.assertTrue(attn.to_v is not None      )

					_snake_case					:							Dict					     =   self.get_forward_arguments(
					    query_dim=constructor_args["""query_dim"""]						,	added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

					_snake_case					:							Any					     =   attn(**SCREAMING_SNAKE_CASE__      )

					# only self attention

					torch.manual_seed(0      )

					_snake_case					:							Any					     =   self.get_constructor_arguments(only_cross_attention=SCREAMING_SNAKE_CASE__      )
					_snake_case					:							List[str]					     =   Attention(**SCREAMING_SNAKE_CASE__      )

					self.assertTrue(attn.to_k is None      )
					self.assertTrue(attn.to_v is None      )

					_snake_case					:							str					     =   self.get_forward_arguments(
					    query_dim=constructor_args["""query_dim"""]						,	added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

					_snake_case					:							Any					     =   attn(**SCREAMING_SNAKE_CASE__      )

					self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )





class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							str					     =   DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""						,	safety_checker=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Tuple					     =   pipe(
					    """foo"""						,	num_inference_steps=2						,	generator=torch.Generator("""cpu"""      ).manual_seed(0      )						,	output_type="""np"""						,	).images

					# the initial conversion succeeds
					_snake_case					:							List[str]					     =   DiffusionPipeline.from_pretrained(
					    """hf-internal-testing/tiny-stable-diffusion-pipe"""						,	device_map="""sequential"""						,	safety_checker=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Union[str, Any]					     =   pipe(
					    """foo"""						,	num_inference_steps=2						,	generator=torch.Generator("""cpu"""      ).manual_seed(0      )						,	output_type="""np"""						,	).images

					with tempfile.TemporaryDirectory() as tmpdir:
						# save the converted model
						pipe.save_pretrained(SCREAMING_SNAKE_CASE__      )

						# can also load the converted weights
						_snake_case					:							str					     =   DiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__						,	device_map="""sequential"""						,	safety_checker=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							int					     =   pipe(
					    """foo"""						,	num_inference_steps=2						,	generator=torch.Generator("""cpu"""      ).manual_seed(0      )						,	output_type="""np"""						,	).images

					self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	atol=1e-5      )      )
					self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	atol=1e-5      )      )





