


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 	lowerCamelCase_       (				a__     ,     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 A      (				self						)   ->			Optional[int]:







								'''simple docstring'''

								return 3_2






					@property
					def A      (				self						)   ->			int:







								'''simple docstring'''

								return 3_2






					@property
					def A      (				self						)   ->			List[str]:







								'''simple docstring'''

								return self.time_input_dim






					@property
					def A      (				self						)   ->			Union[str, Any]:







								'''simple docstring'''

								return self.time_input_dim * 4






					@property
					def A      (				self						)   ->			int:







								'''simple docstring'''

								return 1_0_0






					@property
					def A      (				self						)   ->			str:







								'''simple docstring'''

								torch.manual_seed(0						)

								__lowercase 					=   {
								    '''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,
								}

								__lowercase 					=   UNetaDConditionModel(**lowerCamelCase_						)
								return model






					@property
					def A      (				self						)   ->			Optional[int]:







								'''simple docstring'''

								return {
								    "block_out_channels": [3_2, 6_4],
								    "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": 1_2,
								    "out_channels": 3,
								    "up_block_types": [
								        "AttnUpDecoderBlock2D",
								        "UpDecoderBlock2D",
								    ],
								    "vq_embed_dim": 4,
								}






					@property
					def A      (				self						)   ->			List[str]:







								'''simple docstring'''

								torch.manual_seed(0						)
								__lowercase 					=   VQModel(**self.dummy_movq_kwargs						)
								return model






					def A      (				self						)   ->			Tuple:







								'''simple docstring'''

								__lowercase 					=   self.dummy_unet
								__lowercase 					=   self.dummy_movq

								__lowercase 					=   DDIMScheduler(
								    num_train_timesteps=1_0_0_0 ,       beta_schedule='''linear''' ,       beta_start=0.0_0_0_8_5 ,       beta_end=0.0_1_2 ,       clip_sample=lowerCamelCase_ ,       set_alpha_to_one=lowerCamelCase_ ,       steps_offset=1 ,       prediction_type='''epsilon''' ,       thresholding=lowerCamelCase_ ,       )

								__lowercase 					=   {
								    '''unet''': unet,
								    '''scheduler''': scheduler,
								    '''movq''': movq,
								}
								return components






					def A      (				self ,       snake_case_ ,       snake_case_=0						)   ->			Dict:







								'''simple docstring'''

								__lowercase 					=   floats_tensor((1, self.text_embedder_hidden_size) ,       rng=random.Random(lowerCamelCase_						)						).to(lowerCamelCase_						)
								__lowercase 					=   floats_tensor((1, self.text_embedder_hidden_size) ,       rng=random.Random(seed + 1						)						).to(
								    lowerCamelCase_						)
								if str(lowerCamelCase_						).startswith('''mps'''						):
											__lowercase 					=   torch.manual_seed(lowerCamelCase_						)
								else:
											__lowercase 					=   torch.Generator(device=lowerCamelCase_						).manual_seed(lowerCamelCase_						)
								__lowercase 					=   {
								    '''image_embeds''': image_embeds,
								    '''negative_image_embeds''': negative_image_embeds,
								    '''generator''': generator,
								    '''height''': 6_4,
								    '''width''': 6_4,
								    '''guidance_scale''': 4.0,
								    '''num_inference_steps''': 2,
								    '''output_type''': '''np''',
								}
								return inputs






					def A      (				self						)   ->			Optional[Any]:







								'''simple docstring'''

								__lowercase 					=   '''cpu'''

								__lowercase 					=   self.get_dummy_components()

								__lowercase 					=   self.pipeline_class(**lowerCamelCase_						)
								__lowercase 					=   pipe.to(lowerCamelCase_						)

								pipe.set_progress_bar_config(disable=lowerCamelCase_						)

								__lowercase 					=   pipe(**self.get_dummy_inputs(lowerCamelCase_						)						)
								__lowercase 					=   output.images

								__lowercase 					=   pipe(
								    **self.get_dummy_inputs(lowerCamelCase_						) ,       return_dict=lowerCamelCase_ ,       )[0]

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

								assert image.shape == (1, 6_4, 6_4, 3)

								__lowercase 					=   np.array(
								    [0.6_2_3_7_9_7_6, 1.0, 0.3_6_4_4_1_3_3_2, 1.0, 0.7_0_6_3_9_6_3_4, 0.2_9_8_7_7_1_8_6, 0.8_5_6_5_2_1_2_5, 0.5_2_1_6_8_4_3, 0.5_4_4_5_4_0_4_6]						)

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



					'''simple docstring'''







					def A      (				self						)   ->			List[Any]:







								'''simple docstring'''

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






					def A      (				self						)   ->			str:







								'''simple docstring'''

								__lowercase 					=   load_numpy(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
								    '''/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy'''						)

								__lowercase 					=   KandinskyVaaPriorPipeline.from_pretrained(
								    '''kandinsky-community/kandinsky-2-2-prior''' ,       torch_dtype=torch.floataa						)
								pipe_prior.to(lowerCamelCase_						)

								__lowercase 					=   KandinskyVaaPipeline.from_pretrained(
								    '''kandinsky-community/kandinsky-2-2-decoder''' ,       torch_dtype=torch.floataa						)
								__lowercase 					=   pipeline.to(lowerCamelCase_						)
								pipeline.set_progress_bar_config(disable=lowerCamelCase_						)

								__lowercase 					=   '''red cat, 4k photo'''

								__lowercase 					=   torch.Generator(device='''cuda'''						).manual_seed(0						)
								__lowercase					, __lowercase 					=   pipe_prior(
								    lowerCamelCase_ ,       generator=lowerCamelCase_ ,       num_inference_steps=5 ,       negative_prompt='''''' ,       ).to_tuple()

								__lowercase 					=   torch.Generator(device='''cuda'''						).manual_seed(0						)
								__lowercase 					=   pipeline(
								    image_embeds=lowerCamelCase_ ,       negative_image_embeds=lowerCamelCase_ ,       generator=lowerCamelCase_ ,       num_inference_steps=1_0_0 ,       output_type='''np''' ,       )

								__lowercase 					=   output.images[0]

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

								assert_mean_pixel_difference(lowerCamelCase_ ,       lowerCamelCase_						)






from typing import List

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


a : Any		=				logging.get_logger(__name__)

a : int		=				{
    '''snap-research/efficientformer-l1-300''': (
        '''https://huggingface.co/snap-research/efficientformer-l1-300/resolve/main/config.json'''
    ),
}
class 	lowerCamelCase_       (				lowerCAmelCase__				):



					'''simple docstring'''







					__UpperCAmelCase            =				"efficientformer"






					def __init__(				self ,       snake_case_ = [3, 2, 6, 4] ,       snake_case_ = [4_8, 9_6, 2_2_4, 4_4_8] ,       snake_case_ = [True, True, True, True] ,       snake_case_ = 4_4_8 ,       snake_case_ = 3_2 ,       snake_case_ = 4 ,       snake_case_ = 7 ,       snake_case_ = 5 ,       snake_case_ = 8 ,       snake_case_ = 4 ,       snake_case_ = 0.0 ,       snake_case_ = 1_6 ,       snake_case_ = 3 ,       snake_case_ = 3 ,       snake_case_ = 3 ,       snake_case_ = 2 ,       snake_case_ = 1 ,       snake_case_ = 0.0 ,       snake_case_ = 1 ,       snake_case_ = True ,       snake_case_ = True ,       snake_case_ = 1e-5 ,       snake_case_ = "gelu" ,       snake_case_ = 0.0_2 ,       snake_case_ = 1e-1_2 ,       snake_case_ = 2_2_4 ,       snake_case_ = 1e-0_5 ,       **snake_case_ ,       )   ->			None:







								'''simple docstring'''

								super().__init__(**snake_case_						)

								__lowercase 					=   hidden_act
								__lowercase 					=   hidden_dropout_prob
								__lowercase 					=   hidden_sizes
								__lowercase 					=   num_hidden_layers
								__lowercase 					=   num_attention_heads
								__lowercase 					=   initializer_range
								__lowercase 					=   layer_norm_eps
								__lowercase 					=   patch_size
								__lowercase 					=   num_channels
								__lowercase 					=   depths
								__lowercase 					=   mlp_expansion_ratio
								__lowercase 					=   downsamples
								__lowercase 					=   dim
								__lowercase 					=   key_dim
								__lowercase 					=   attention_ratio
								__lowercase 					=   resolution
								__lowercase 					=   pool_size
								__lowercase 					=   downsample_patch_size
								__lowercase 					=   downsample_stride
								__lowercase 					=   downsample_pad
								__lowercase 					=   drop_path_rate
								__lowercase 					=   num_metaad_blocks
								__lowercase 					=   distillation
								__lowercase 					=   use_layer_scale
								__lowercase 					=   layer_scale_init_value
								__lowercase 					=   image_size
								__lowercase 					=   batch_norm_eps


