

import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin






@skip_mps
class 	A_		(	SCREAMING_SNAKE_CASE      ,							SCREAMING_SNAKE_CASE      ,							unittest.TestCase    ):
							_UpperCAmelCase   :  Optional[int]										=				IFPipeline
							_UpperCAmelCase   :  Dict										=				TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
							_UpperCAmelCase   :  int										=				TEXT_TO_IMAGE_BATCH_PARAMS
							_UpperCAmelCase   :  Union[str, Any]										=				PipelineTesterMixin.required_optional_params - {'''latents'''}
							def    lowerCAmelCase					(		self		:      Any):
								return self._get_dummy_components()
							def    lowerCAmelCase					(		self		:      List[Any]       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=0):
								if str(SCREAMING_SNAKE_CASE__).startswith('mps'):
									__lowerCamelCase    :    Tuple 	=			torch.manual_seed(SCREAMING_SNAKE_CASE__)
								else:
									__lowerCamelCase    :    Optional[int] 	=			torch.Generator(device=SCREAMING_SNAKE_CASE__).manual_seed(SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    Tuple 	=			{
								    'prompt': 'A painting of a squirrel eating a burger',
								    'generator': generator,
								    'num_inference_steps': 2,
								    'output_type': 'numpy',
								}

								return inputs
							def    lowerCAmelCase					(		self		:      Dict):
								self._test_save_load_optional_components()
							@unittest.skipIf(torch_device != 'cuda'       ,reason='float16 requires CUDA')
							def    lowerCAmelCase					(		self		:      Tuple):
								# Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
								super().test_save_load_floataa(expected_max_diff=1E-1)
							def    lowerCAmelCase					(		self		:      Optional[int]):
								self._test_attention_slicing_forward_pass(expected_max_diff=1E-2)
							def    lowerCAmelCase					(		self		:      str):
								self._test_save_load_local()
							def    lowerCAmelCase					(		self		:      Optional[int]):
								self._test_inference_batch_single_identical(
								    expected_max_diff=1E-2       ,)


							@unittest.skipIf(
							    torch_device != 'cuda' or not is_xformers_available()       ,reason='XFormers attention is only available with CUDA and `xformers` installed'       ,)
							def    lowerCAmelCase					(		self		:      List[str]):
								self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3)






@slow
@require_torch_gpu
class 	A_		(	unittest.TestCase    ):
							def    lowerCAmelCase					(		self		:      List[str]):
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()
							def    lowerCAmelCase					(		self		:      int):
								# if

								__lowerCamelCase    :    Tuple 	=			IFPipeline.from_pretrained('DeepFloyd/IF-I-XL-v1.0'       ,variant='fp16'       ,torch_dtype=torch.floataa)

								__lowerCamelCase    :    List[Any] 	=			IFSuperResolutionPipeline.from_pretrained(
								    'DeepFloyd/IF-II-L-v1.0'       ,variant='fp16'       ,torch_dtype=torch.floataa       ,text_encoder=SCREAMING_SNAKE_CASE__       ,tokenizer=SCREAMING_SNAKE_CASE__)

								# pre compute text embeddings and remove T5 to save memory

								pipe_a.text_encoder.to('cuda')

								__lowerCamelCase	,			__lowerCamelCase    :    Any 	=			pipe_a.encode_prompt('anime turtle'       ,device='cuda')

								del pipe_a.tokenizer
								del pipe_a.text_encoder
								gc.collect()

								__lowerCamelCase    :    Any 	=			None
								__lowerCamelCase    :    Union[str, Any] 	=			None

								pipe_a.enable_model_cpu_offload()
								pipe_a.enable_model_cpu_offload()

								pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())
								pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())

								self._test_if(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)

								pipe_a.remove_all_hooks()
								pipe_a.remove_all_hooks()

								# img2img

								__lowerCamelCase    :    Union[str, Any] 	=			IFImgaImgPipeline(**pipe_a.components)
								__lowerCamelCase    :    Optional[Any] 	=			IFImgaImgSuperResolutionPipeline(**pipe_a.components)

								pipe_a.enable_model_cpu_offload()
								pipe_a.enable_model_cpu_offload()

								pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())
								pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())

								self._test_if_imgaimg(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)

								pipe_a.remove_all_hooks()
								pipe_a.remove_all_hooks()

								# inpainting

								__lowerCamelCase    :    List[str] 	=			IFInpaintingPipeline(**pipe_a.components)
								__lowerCamelCase    :    Dict 	=			IFInpaintingSuperResolutionPipeline(**pipe_a.components)

								pipe_a.enable_model_cpu_offload()
								pipe_a.enable_model_cpu_offload()

								pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())
								pipe_a.unet.set_attn_processor(AttnAddedKVProcessor())

								self._test_if_inpainting(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
							def    lowerCAmelCase					(		self		:      Tuple       ,SCREAMING_SNAKE_CASE__		:      int       ,SCREAMING_SNAKE_CASE__		:      List[str]       ,SCREAMING_SNAKE_CASE__		:      Any       ,SCREAMING_SNAKE_CASE__		:      int):
								# pipeline 1

								_start_torch_memory_measurement()

								__lowerCamelCase    :    Optional[int] 	=			torch.Generator(device='cpu').manual_seed(0)
								__lowerCamelCase    :    List[str] 	=			pipe_a(
								    prompt_embeds=SCREAMING_SNAKE_CASE__       ,negative_prompt_embeds=SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,generator=SCREAMING_SNAKE_CASE__       ,output_type='np'       ,)

								__lowerCamelCase    :    Union[str, Any] 	=			output.images[0]

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

								__lowerCamelCase    :    str 	=			torch.cuda.max_memory_allocated()
								assert mem_bytes < 1_3 * 1_0**9

								__lowerCamelCase    :    List[Any] 	=			load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy')
								assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)

								# pipeline 2

								_start_torch_memory_measurement()

								__lowerCamelCase    :    Tuple 	=			torch.Generator(device='cpu').manual_seed(0)

								__lowerCamelCase    :    str 	=			floats_tensor((1, 3, 6_4, 6_4)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    List[Any] 	=			pipe_a(
								    prompt_embeds=SCREAMING_SNAKE_CASE__       ,negative_prompt_embeds=SCREAMING_SNAKE_CASE__       ,image=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,output_type='np'       ,)

								__lowerCamelCase    :    Any 	=			output.images[0]

								assert image.shape == (2_5_6, 2_5_6, 3)

								__lowerCamelCase    :    Any 	=			torch.cuda.max_memory_allocated()
								assert mem_bytes < 4 * 1_0**9

								__lowerCamelCase    :    Tuple 	=			load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy')
								assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
							def    lowerCAmelCase					(		self		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      Any       ,SCREAMING_SNAKE_CASE__		:      str       ,SCREAMING_SNAKE_CASE__		:      List[Any]):
								# pipeline 1

								_start_torch_memory_measurement()

								__lowerCamelCase    :    str 	=			floats_tensor((1, 3, 6_4, 6_4)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    List[str] 	=			torch.Generator(device='cpu').manual_seed(0)

								__lowerCamelCase    :    int 	=			pipe_a(
								    prompt_embeds=SCREAMING_SNAKE_CASE__       ,negative_prompt_embeds=SCREAMING_SNAKE_CASE__       ,image=SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,generator=SCREAMING_SNAKE_CASE__       ,output_type='np'       ,)

								__lowerCamelCase    :    Optional[Any] 	=			output.images[0]

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

								__lowerCamelCase    :    List[str] 	=			torch.cuda.max_memory_allocated()
								assert mem_bytes < 1_0 * 1_0**9

								__lowerCamelCase    :    Any 	=			load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy')
								assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)

								# pipeline 2

								_start_torch_memory_measurement()

								__lowerCamelCase    :    str 	=			torch.Generator(device='cpu').manual_seed(0)

								__lowerCamelCase    :    Optional[int] 	=			floats_tensor((1, 3, 2_5_6, 2_5_6)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    int 	=			floats_tensor((1, 3, 6_4, 6_4)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    int 	=			pipe_a(
								    prompt_embeds=SCREAMING_SNAKE_CASE__       ,negative_prompt_embeds=SCREAMING_SNAKE_CASE__       ,image=SCREAMING_SNAKE_CASE__       ,original_image=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,output_type='np'       ,)

								__lowerCamelCase    :    Optional[int] 	=			output.images[0]

								assert image.shape == (2_5_6, 2_5_6, 3)

								__lowerCamelCase    :    str 	=			torch.cuda.max_memory_allocated()
								assert mem_bytes < 4 * 1_0**9

								__lowerCamelCase    :    List[str] 	=			load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy')
								assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)


							def    lowerCAmelCase					(		self		:      Dict       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      List[str]       ,SCREAMING_SNAKE_CASE__		:      str):
								# pipeline 1

								_start_torch_memory_measurement()

								__lowerCamelCase    :    str 	=			floats_tensor((1, 3, 6_4, 6_4)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    str 	=			floats_tensor((1, 3, 6_4, 6_4)       ,rng=random.Random(1)).to(SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    Union[str, Any] 	=			torch.Generator(device='cpu').manual_seed(0)
								__lowerCamelCase    :    Tuple 	=			pipe_a(
								    prompt_embeds=SCREAMING_SNAKE_CASE__       ,negative_prompt_embeds=SCREAMING_SNAKE_CASE__       ,image=SCREAMING_SNAKE_CASE__       ,mask_image=SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,generator=SCREAMING_SNAKE_CASE__       ,output_type='np'       ,)

								__lowerCamelCase    :    int 	=			output.images[0]

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

								__lowerCamelCase    :    Tuple 	=			torch.cuda.max_memory_allocated()
								assert mem_bytes < 1_0 * 1_0**9

								__lowerCamelCase    :    Union[str, Any] 	=			load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy')
								assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)

								# pipeline 2

								_start_torch_memory_measurement()

								__lowerCamelCase    :    int 	=			torch.Generator(device='cpu').manual_seed(0)

								__lowerCamelCase    :    Any 	=			floats_tensor((1, 3, 6_4, 6_4)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    Optional[Any] 	=			floats_tensor((1, 3, 2_5_6, 2_5_6)       ,rng=random.Random(0)).to(SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    List[Any] 	=			floats_tensor((1, 3, 2_5_6, 2_5_6)       ,rng=random.Random(1)).to(SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    Tuple 	=			pipe_a(
								    prompt_embeds=SCREAMING_SNAKE_CASE__       ,negative_prompt_embeds=SCREAMING_SNAKE_CASE__       ,image=SCREAMING_SNAKE_CASE__       ,mask_image=SCREAMING_SNAKE_CASE__       ,original_image=SCREAMING_SNAKE_CASE__       ,generator=SCREAMING_SNAKE_CASE__       ,num_inference_steps=2       ,output_type='np'       ,)

								__lowerCamelCase    :    List[str] 	=			output.images[0]

								assert image.shape == (2_5_6, 2_5_6, 3)

								__lowerCamelCase    :    List[Any] 	=			torch.cuda.max_memory_allocated()
								assert mem_bytes < 4 * 1_0**9

								__lowerCamelCase    :    Optional[Any] 	=			load_numpy(
								    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy')
								assert_mean_pixel_difference(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)






def        SCREAMING_SNAKE_CASE__	(  )  ->					str:
	torch.cuda.empty_cache()
	torch.cuda.reset_max_memory_allocated()
	torch.cuda.reset_peak_memory_stats()







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


a		=logging.get_logger(__name__)

a		={
    """sayakpaul/vit-msn-base""": """https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json""",
    # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn
}






class 	A_		(	SCREAMING_SNAKE_CASE    ):
							_UpperCAmelCase   :  str										=				'''vit_msn'''
							def __init__(		self		:      Any       ,SCREAMING_SNAKE_CASE__		:      int=7_6_8       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=1_2       ,SCREAMING_SNAKE_CASE__		:      Optional[Any]=1_2       ,SCREAMING_SNAKE_CASE__		:      Tuple=3_0_7_2       ,SCREAMING_SNAKE_CASE__		:      str="gelu"       ,SCREAMING_SNAKE_CASE__		:      Any=0.0       ,SCREAMING_SNAKE_CASE__		:      str=0.0       ,SCREAMING_SNAKE_CASE__		:      Any=0.02       ,SCREAMING_SNAKE_CASE__		:      int=1E-06       ,SCREAMING_SNAKE_CASE__		:      Optional[int]=2_2_4       ,SCREAMING_SNAKE_CASE__		:      Dict=1_6       ,SCREAMING_SNAKE_CASE__		:      int=3       ,SCREAMING_SNAKE_CASE__		:      List[str]=True       ,**SCREAMING_SNAKE_CASE__		:      str       ,):
								super().__init__(**SCREAMING_SNAKE_CASE__)

								__lowerCamelCase    :    List[Any] 	=			hidden_size
								__lowerCamelCase    :    Tuple 	=			num_hidden_layers
								__lowerCamelCase    :    Union[str, Any] 	=			num_attention_heads
								__lowerCamelCase    :    Dict 	=			intermediate_size
								__lowerCamelCase    :    List[Any] 	=			hidden_act
								__lowerCamelCase    :    Optional[Any] 	=			hidden_dropout_prob
								__lowerCamelCase    :    str 	=			attention_probs_dropout_prob
								__lowerCamelCase    :    List[str] 	=			initializer_range
								__lowerCamelCase    :    Optional[int] 	=			layer_norm_eps
								__lowerCamelCase    :    Tuple 	=			image_size
								__lowerCamelCase    :    Union[str, Any] 	=			patch_size
								__lowerCamelCase    :    Optional[Any] 	=			num_channels
								__lowerCamelCase    :    List[str] 	=			qkv_bias




