




"""simple docstring"""




import gc
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel
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 					__magic_name__							( UpperCAmelCase__		,      unittest.TestCase       ):






				'''simple docstring'''
				__UpperCamelCase    						=		KandinskyVaaPipeline
				__UpperCamelCase    						=		[
				    "image_embeds",
				    "negative_image_embeds",
				]
				__UpperCamelCase    						=		["image_embeds", "negative_image_embeds"]
				__UpperCamelCase    						=		[
				    "generator",
				    "height",
				    "width",
				    "latents",
				    "guidance_scale",
				    "num_inference_steps",
				    "return_dict",
				    "guidance_scale",
				    "num_images_per_prompt",
				    "output_type",
				    "return_dict",
				]
				__UpperCamelCase    						=		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 100





				@property
				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							torch.manual_seed(0				)

							lowerCamelCase       =      {
							    """in_channels""": 4,
							    # Out channels is double in channels because predicts mean and variance
							    """out_channels""": 8,
							    """addition_embed_type""": """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""": """image_proj""",
							    """cross_attention_dim""": self.cross_attention_dim,
							    """attention_head_dim""": 4,
							    """resnet_time_scale_shift""": """scale_shift""",
							    """class_embed_type""": None,
							}

							lowerCamelCase       =      UNetaDConditionModel(**_a				)
							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				)
							lowerCamelCase       =      VQModel(**self.dummy_movq_kwargs				)
							return model





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      self.dummy_unet
							lowerCamelCase       =      self.dummy_movq

							lowerCamelCase       =      DDIMScheduler(
							    num_train_timesteps=1_000	,				beta_schedule="""linear"""	,				beta_start=0.00_085	,				beta_end=0.012	,				clip_sample=_a	,				set_alpha_to_one=_a	,				steps_offset=1	,				prediction_type="""epsilon"""	,				thresholding=_a	,				)

							lowerCamelCase       =      {
							    """unet""": unet,
							    """scheduler""": scheduler,
							    """movq""": movq,
							}
							return components





				def      _lowerCAmelCase       ( self	,				_a	,				_a=0				):





							"""simple docstring"""
							lowerCamelCase       =      floats_tensor((1, self.text_embedder_hidden_size)	,				rng=random.Random(_a				)				).to(_a				)
							lowerCamelCase       =      floats_tensor((1, self.text_embedder_hidden_size)	,				rng=random.Random(seed + 1				)				).to(
							    _a				)
							if str(_a				).startswith("""mps"""				):
										lowerCamelCase       =      torch.manual_seed(_a				)
							else:
										lowerCamelCase       =      torch.Generator(device=_a				).manual_seed(_a				)
							lowerCamelCase       =      {
							    """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"""
							lowerCamelCase       =      """cpu"""

							lowerCamelCase       =      self.get_dummy_components()

							lowerCamelCase       =      self.pipeline_class(**_a				)
							lowerCamelCase       =      pipe.to(_a				)

							pipe.set_progress_bar_config(disable=_a				)

							lowerCamelCase       =      pipe(**self.get_dummy_inputs(_a				)				)
							lowerCamelCase       =      output.images

							lowerCamelCase       =      pipe(
							    **self.get_dummy_inputs(_a				)	,				return_dict=_a	,				)[0]

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

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

							lowerCamelCase       =      np.array(
							    [0.6_237_976, 1.0, 0.36_441_332, 1.0, 0.70_639_634, 0.29_877_186, 0.85_652_125, 0.5_216_843, 0.54_454_046]				)

							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 					__magic_name__							( unittest.TestCase       ):






				'''simple docstring'''





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							# clean up the VRAM after each test
							super().tearDown()
							gc.collect()
							torch.cuda.empty_cache()





				def      _lowerCAmelCase       ( self				):





							"""simple docstring"""
							lowerCamelCase       =      load_numpy(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
							    """/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy"""				)

							lowerCamelCase       =      KandinskyVaaPriorPipeline.from_pretrained(
							    """kandinsky-community/kandinsky-2-2-prior"""	,				torch_dtype=torch.floataa				)
							pipe_prior.to(_a				)

							lowerCamelCase       =      KandinskyVaaPipeline.from_pretrained(
							    """kandinsky-community/kandinsky-2-2-decoder"""	,				torch_dtype=torch.floataa				)
							lowerCamelCase       =      pipeline.to(_a				)
							pipeline.set_progress_bar_config(disable=_a				)

							lowerCamelCase       =      """red cat, 4k photo"""

							lowerCamelCase       =      torch.Generator(device="""cuda"""				).manual_seed(0				)
							lowerCamelCase   ,     lowerCamelCase       =      pipe_prior(
							    _a	,				generator=_a	,				num_inference_steps=5	,				negative_prompt=""""""	,				).to_tuple()

							lowerCamelCase       =      torch.Generator(device="""cuda"""				).manual_seed(0				)
							lowerCamelCase       =      pipeline(
							    image_embeds=_a	,				negative_image_embeds=_a	,				generator=_a	,				num_inference_steps=100	,				output_type="""np"""	,				)

							lowerCamelCase       =      output.images[0]

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

							assert_mean_pixel_difference(_a	,				_a				)









"""simple docstring"""




from functools import lru_cache



def 					a__				(       snake_case__       )		->      set:
			lowerCamelCase       =      2
			lowerCamelCase       =      set()
			while i * i <= n:
						if n % i:
									i += 1
						else:
									n //= i
									factors.add(snake_case__       )
			if n > 1:
						factors.add(snake_case__       )
			return factors



@lru_cache
def 					a__				(       snake_case__       )		->      int:
			return len(unique_prime_factors(snake_case__       )       )



def 					a__				(       snake_case__       )		->      bool:
			return len(set(snake_case__       )       ) in (0, 1)



def 					a__				(       snake_case__       )		->      list:
			lowerCamelCase       =      2

			while True:
						# Increment each value of a generated range
						lowerCamelCase       =      [base + i for i in range(snake_case__       )]

						# Run elements through out unique_prime_factors function
						# Append our target number to the end.
						lowerCamelCase       =      [upf_len(snake_case__       ) for x in group]
						checker.append(snake_case__       )

						# If all numbers in the list are equal, return the group variable.
						if equality(snake_case__       ):
									return group

						# Increment our base variable by 1
						base += 1



def 					a__				(       snake_case__ = 4       )		->      int:
			lowerCamelCase       =      run(snake_case__       )
			return results[0] if len(snake_case__       ) else None


if __name__ == "__main__":
					print(solution())



