




"""simple docstring"""





import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyInpaintPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
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 ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()



class      a__				(	__magic_name__							,    unittest.TestCase   ):
			lowercase_    =	KandinskyInpaintPipeline
			lowercase_    =	["prompt", "image_embeds", "negative_image_embeds", "image", "mask_image"]
			lowercase_    =	[
			    "prompt",
			    "negative_prompt",
			    "image_embeds",
			    "negative_image_embeds",
			    "image",
			    "mask_image",
			]
			lowercase_    =	[
			    "generator",
			    "height",
			    "width",
			    "latents",
			    "guidance_scale",
			    "negative_prompt",
			    "num_inference_steps",
			    "return_dict",
			    "guidance_scale",
			    "num_images_per_prompt",
			    "output_type",
			    "return_dict",
			]
			lowercase_    =	False


			@property
			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						return 32


			@property
			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						return 32


			@property
			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						return self.time_input_dim


			@property
			def       a_     (      self      :    str):


						"""simple docstring"""




						return self.time_input_dim * 4


			@property
			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						return 100


			@property
			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base")
						return tokenizer


			@property
			def       a_     (      self      :    str):


						"""simple docstring"""




						torch.manual_seed(0)
						__UpperCAmelCase	: int		=					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=1005      ,  )

						__UpperCAmelCase	: int		=					MultilingualCLIP(UpperCamelCase_)
						__UpperCAmelCase	: List[Any]		=					text_encoder.eval()

						return text_encoder


			@property
			def       a_     (      self      :    str):


						"""simple docstring"""




						torch.manual_seed(0)

						__UpperCAmelCase	: str		=					{
						    "in_channels": 9,
						    # 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,
						}

						__UpperCAmelCase	: str		=					UNetaDConditionModel(**UpperCamelCase_)
						return model


			@property
			def       a_     (      self      :    List[Any]):


						"""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       a_     (      self      :    Dict):


						"""simple docstring"""




						torch.manual_seed(0)
						__UpperCAmelCase	: Any		=					VQModel(**self.dummy_movq_kwargs)
						return model


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					self.dummy_text_encoder
						__UpperCAmelCase	: List[Any]		=					self.dummy_tokenizer
						__UpperCAmelCase	: Optional[int]		=					self.dummy_unet
						__UpperCAmelCase	: Optional[Any]		=					self.dummy_movq

						__UpperCAmelCase	: Optional[int]		=					DDIMScheduler(
						    num_train_timesteps=1000      ,  beta_schedule="linear"      ,  beta_start=0.00085      ,  beta_end=0.012      ,  clip_sample=UpperCamelCase_      ,  set_alpha_to_one=UpperCamelCase_      ,  steps_offset=1      ,  prediction_type="epsilon"      ,  thresholding=UpperCamelCase_      ,  )

						__UpperCAmelCase	: List[str]		=					{
						    "text_encoder": text_encoder,
						    "tokenizer": tokenizer,
						    "unet": unet,
						    "scheduler": scheduler,
						    "movq": movq,
						}

						return components


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    List[Any]      ,  UpperCamelCase_      :    str=0):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					floats_tensor((1, self.cross_attention_dim)      ,  rng=random.Random(UpperCamelCase_)).to(UpperCamelCase_)
						__UpperCAmelCase	: Union[str, Any]		=					floats_tensor((1, self.cross_attention_dim)      ,  rng=random.Random(seed + 1)).to(UpperCamelCase_)
						# create init_image
						__UpperCAmelCase	: List[Any]		=					floats_tensor((1, 3, 64, 64)      ,  rng=random.Random(UpperCamelCase_)).to(UpperCamelCase_)
						__UpperCAmelCase	: str		=					image.cpu().permute(0      ,  2      ,  3      ,  1)[0]
						__UpperCAmelCase	: List[Any]		=					Image.fromarray(np.uinta(UpperCamelCase_)).convert("RGB").resize((256, 256))
						# create mask
						__UpperCAmelCase	: Tuple		=					np.ones((64, 64)      ,  dtype=np.floataa)
						__UpperCAmelCase	: Any		=					0

						if str(UpperCamelCase_).startswith("mps"):
									__UpperCAmelCase	: List[str]		=					torch.manual_seed(UpperCamelCase_)
						else:
									__UpperCAmelCase	: Union[str, Any]		=					torch.Generator(device=UpperCamelCase_).manual_seed(UpperCamelCase_)
						__UpperCAmelCase	: List[Any]		=					{
						    "prompt": "horse",
						    "image": init_image,
						    "mask_image": mask,
						    "image_embeds": image_embeds,
						    "negative_image_embeds": negative_image_embeds,
						    "generator": generator,
						    "height": 64,
						    "width": 64,
						    "num_inference_steps": 2,
						    "guidance_scale": 4.0,
						    "output_type": "np",
						}
						return inputs


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					"cpu"

						__UpperCAmelCase	: List[str]		=					self.get_dummy_components()

						__UpperCAmelCase	: Union[str, Any]		=					self.pipeline_class(**UpperCamelCase_)
						__UpperCAmelCase	: Optional[int]		=					pipe.to(UpperCamelCase_)

						pipe.set_progress_bar_config(disable=UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					pipe(**self.get_dummy_inputs(UpperCamelCase_))
						__UpperCAmelCase	: Optional[int]		=					output.images

						__UpperCAmelCase	: str		=					pipe(
						    **self.get_dummy_inputs(UpperCamelCase_)      ,  return_dict=UpperCamelCase_      ,  )[0]

						__UpperCAmelCase	: int		=					image[0, -3:, -3:, -1]
						__UpperCAmelCase	: Any		=					image_from_tuple[0, -3:, -3:, -1]

						print(F"image.shape {image.shape}")

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

						__UpperCAmelCase	: str		=					np.array(
						    [0.8326919, 0.73790467, 0.20918581, 0.9309612, 0.5511791, 0.43713328, 0.5513321, 0.49922934, 0.59497786])

						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()}"


			def       a_     (      self      :    str):


						"""simple docstring"""




						super().test_inference_batch_single_identical(expected_max_diff=3e-3)






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


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




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


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					load_numpy(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
						    "/kandinsky/kandinsky_inpaint_cat_with_hat_fp16.npy")

						__UpperCAmelCase	: Optional[int]		=					load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png")
						__UpperCAmelCase	: Union[str, Any]		=					np.ones((768, 768)      ,  dtype=np.floataa)
						__UpperCAmelCase	: List[str]		=					0

						__UpperCAmelCase	: Any		=					"a hat"

						__UpperCAmelCase	: Union[str, Any]		=					KandinskyPriorPipeline.from_pretrained(
						    "kandinsky-community/kandinsky-2-1-prior"      ,  torch_dtype=torch.floataa)
						pipe_prior.to(UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					KandinskyInpaintPipeline.from_pretrained(
						    "kandinsky-community/kandinsky-2-1-inpaint"      ,  torch_dtype=torch.floataa)
						__UpperCAmelCase	: Optional[int]		=					pipeline.to(UpperCamelCase_)
						pipeline.set_progress_bar_config(disable=UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					torch.Generator(device="cpu").manual_seed(0)
						__UpperCAmelCase	,     __UpperCAmelCase	: Optional[int]		=					pipe_prior(
						    UpperCamelCase_      ,  generator=UpperCamelCase_      ,  num_inference_steps=5      ,  negative_prompt=""      ,  ).to_tuple()

						__UpperCAmelCase	: List[Any]		=					pipeline(
						    UpperCamelCase_      ,  image=UpperCamelCase_      ,  mask_image=UpperCamelCase_      ,  image_embeds=UpperCamelCase_      ,  negative_image_embeds=UpperCamelCase_      ,  generator=UpperCamelCase_      ,  num_inference_steps=100      ,  height=768      ,  width=768      ,  output_type="np"      ,  )

						__UpperCAmelCase	: Tuple		=					output.images[0]

						assert image.shape == (768, 768, 3)

						assert_mean_pixel_difference(UpperCamelCase_      ,  UpperCamelCase_)








"""simple docstring"""





from manim import *



class      a__				(	__magic_name__   ):


			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					Rectangle(height=0.5      ,  width=0.5)
						__UpperCAmelCase	: List[str]		=					Rectangle(height=0.25      ,  width=0.25)
						__UpperCAmelCase	: Dict		=					Rectangle(height=0.46      ,  width=0.46).set_stroke(width=0)

						__UpperCAmelCase	: List[str]		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: Optional[int]		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: List[str]		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: List[Any]		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Optional[int]		=					VGroup(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Dict		=					Text("CPU"      ,  font_size=24)
						__UpperCAmelCase	: int		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						cpu.move_to([-2.5, -0.5, 0])
						self.add(UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					[mem.copy() for i in range(4)]
						__UpperCAmelCase	: List[Any]		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Tuple		=					Text("GPU"      ,  font_size=24)
						__UpperCAmelCase	: Tuple		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						gpu.move_to([-1, -1, 0])
						self.add(UpperCamelCase_)

						__UpperCAmelCase	: Any		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: int		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)

						__UpperCAmelCase	: Union[str, Any]		=					Text("Model"      ,  font_size=24)
						__UpperCAmelCase	: Optional[Any]		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						model.move_to([3, -1.0, 0])
						self.add(UpperCamelCase_)

						__UpperCAmelCase	: str		=					[]
						__UpperCAmelCase	: Tuple		=					[]
						__UpperCAmelCase	: Any		=					[]

						for i, rect in enumerate(UpperCamelCase_):
									rect.set_stroke(UpperCamelCase_)

									__UpperCAmelCase	: Optional[int]		=					Rectangle(height=0.46 / 4      ,  width=0.46 / 3).set_stroke(width=0.0).set_fill(UpperCamelCase_      ,  opacity=0.7)

									if i == 0:
												cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT)      ,  buff=0.02      ,  direction=UpperCamelCase_)
												cpu_target.set_x(cpu_target.get_x() + 0.1)
									elif i == 3:
												cpu_target.next_to(model_cpu_arr[0]      ,  direction=UpperCamelCase_      ,  buff=0.0)
									else:
												cpu_target.next_to(model_cpu_arr[i - 1]      ,  direction=UpperCamelCase_      ,  buff=0.0)
									self.add(UpperCamelCase_)
									model_cpu_arr.append(UpperCamelCase_)

						self.add(*UpperCamelCase_      ,  *UpperCamelCase_      ,  *UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: Dict		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)

						__UpperCAmelCase	: Tuple		=					Text("Loaded Checkpoint"      ,  font_size=24)
						__UpperCAmelCase	: Any		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						checkpoint.move_to([3, 0.5, 0])

						self.add(UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					[]
						__UpperCAmelCase	: int		=					[]

						for i, rect in enumerate(UpperCamelCase_):
									__UpperCAmelCase	: Optional[Any]		=					fill.copy().set_fill(UpperCamelCase_      ,  opacity=0.7)
									target.move_to(UpperCamelCase_)
									ckpt_arr.append(UpperCamelCase_)

									__UpperCAmelCase	: Optional[Any]		=					target.copy()
									if i < 5:
												cpu_target.move_to(cpu_left_col_base[i + 1])
									else:
												cpu_target.move_to(cpu_right_col_base[i - 5])
									ckpt_cpu_arr.append(UpperCamelCase_)
						self.add(*UpperCamelCase_      ,  *UpperCamelCase_)

						__UpperCAmelCase	: str		=					Square(side_length=2.2)
						key.move_to([-5, 2, 0])

						__UpperCAmelCase	: int		=					MarkupText(
						    F"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"      ,  font_size=18      ,  )

						key_text.move_to([-5, 2.4, 0])

						self.add(UpperCamelCase_      ,  UpperCamelCase_)

						__UpperCAmelCase	: Optional[Any]		=					MarkupText(
						    F"<span fgcolor='{BLUE}'>●</span> Checkpoint"      ,  font_size=18      ,  )

						blue_text.next_to(UpperCamelCase_      ,  DOWN * 2.4      ,  aligned_edge=key_text.get_left())
						self.add(UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					MarkupText(
						    F"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device."      ,  font_size=24      ,  )
						step_a.move_to([2, 2, 0])

						__UpperCAmelCase	: List[Any]		=					[meta_mem.copy() for i in range(6)]
						__UpperCAmelCase	: Optional[int]		=					[meta_mem.copy() for i in range(6)]
						__UpperCAmelCase	: int		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Union[str, Any]		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Tuple		=					VGroup(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Optional[int]		=					Text("Disk"      ,  font_size=24)
						__UpperCAmelCase	: str		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						disk.move_to([-4.0, -1.25, 0])
						self.play(Write(UpperCamelCase_      ,  run_time=3)      ,  Write(UpperCamelCase_      ,  run_time=1)      ,  Create(UpperCamelCase_      ,  run_time=1))

						__UpperCAmelCase	: str		=					[]
						for i, rect in enumerate(UpperCamelCase_):
									__UpperCAmelCase	: Union[str, Any]		=					rect.copy()
									target.generate_target()
									target.target.move_to(disk_left_col_base[i]).scale(0.5)
									animations.append(MoveToTarget(UpperCamelCase_      ,  run_time=1.5))
						self.play(*UpperCamelCase_)

						self.play(FadeOut(UpperCamelCase_))

						__UpperCAmelCase	: Union[str, Any]		=					MarkupText(F"Then, the checkpoint is removed from memory\nthrough garbage collection."      ,  font_size=24)
						step_a.move_to([2, 2, 0])

						self.play(Write(UpperCamelCase_      ,  run_time=3))

						self.play(
						    FadeOut(UpperCamelCase_      ,  UpperCamelCase_      ,  *UpperCamelCase_      ,  *UpperCamelCase_)      ,  )

						self.wait()


