



import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
						import jax
						import jax.numpy as jnp

						from transformers.generation import (
						    FlaxForcedBOSTokenLogitsProcessor,
						    FlaxForcedEOSTokenLogitsProcessor,
						    FlaxLogitsProcessorList,
						    FlaxMinLengthLogitsProcessor,
						    FlaxTemperatureLogitsWarper,
						    FlaxTopKLogitsWarper,
						    FlaxTopPLogitsWarper,
						)



@require_flax
class 				__snake_case						(     unittest.TestCase						):




							def    UpperCAmelCase__			(       self     :       Optional[Any]				,				A_     :       int				,				A_     :       int):
									lowerCAmelCase_			:     List[str]									= jnp.ones((batch_size, length)) / length
									return scores




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									lowerCAmelCase_			:     Union[str, Any]									= None
									lowerCAmelCase_			:     Optional[Any]									= 2_0

									lowerCAmelCase_			:     Optional[int]									= self._get_uniform_logits(batch_size=2				,				length=A_)

									# tweak scores to not be uniform anymore
									lowerCAmelCase_			:     Optional[int]									= scores.at[1, 5].set((1 / length) + 0.1)  # peak, 1st batch
									lowerCAmelCase_			:     Any									= scores.at[1, 1_0].set((1 / length) - 0.4)  # valley, 1st batch

									# compute softmax
									lowerCAmelCase_			:     Any									= jax.nn.softmax(A_				,				axis=-1)

									lowerCAmelCase_			:     List[Any]									= FlaxTemperatureLogitsWarper(temperature=0.5)
									lowerCAmelCase_			:     Optional[Any]									= FlaxTemperatureLogitsWarper(temperature=1.3)

									lowerCAmelCase_			:     List[str]									= jax.nn.softmax(temp_dist_warper_sharper(A_				,				scores.copy()				,				cur_len=A_)				,				axis=-1)
									lowerCAmelCase_			:     Tuple									= jax.nn.softmax(temp_dist_warper_smoother(A_				,				scores.copy()				,				cur_len=A_)				,				axis=-1)

									# uniform distribution stays uniform
									self.assertTrue(jnp.allclose(probs[0, :]				,				warped_prob_sharp[0, :]				,				atol=1e-3))
									self.assertTrue(jnp.allclose(probs[0, :]				,				warped_prob_smooth[0, :]				,				atol=1e-3))

									# sharp peaks get higher, valleys get lower
									self.assertLess(probs[1, :].max()				,				warped_prob_sharp[1, :].max())
									self.assertGreater(probs[1, :].min()				,				warped_prob_sharp[1, :].min())

									# smooth peaks get lower, valleys get higher
									self.assertGreater(probs[1, :].max()				,				warped_prob_smooth[1, :].max())
									self.assertLess(probs[1, :].min()				,				warped_prob_smooth[1, :].min())




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									lowerCAmelCase_			:     str									= None
									lowerCAmelCase_			:     Optional[Any]									= 1_0
									lowerCAmelCase_			:     int									= 2

									# create ramp distribution
									lowerCAmelCase_			:     Optional[int]									= np.broadcast_to(np.arange(A_)[None, :]				,				(batch_size, vocab_size)).copy()
									lowerCAmelCase_			:     Dict									= ramp_logits[1:, : vocab_size // 2] + vocab_size

									lowerCAmelCase_			:     Union[str, Any]									= FlaxTopKLogitsWarper(3)

									lowerCAmelCase_			:     List[str]									= top_k_warp(A_				,				A_				,				cur_len=A_)

									# check that correct tokens are filtered
									self.assertListEqual(jnp.isinf(scores[0]).tolist()				,				7 * [True] + 3 * [False])
									self.assertListEqual(jnp.isinf(scores[1]).tolist()				,				2 * [True] + 3 * [False] + 5 * [True])

									# check special case
									lowerCAmelCase_			:     int									= 5
									lowerCAmelCase_			:     Optional[Any]									= FlaxTopKLogitsWarper(top_k=1				,				filter_value=0.0				,				min_tokens_to_keep=3)

									lowerCAmelCase_			:     int									= np.broadcast_to(np.arange(A_)[None, :]				,				(batch_size, length)).copy()
									lowerCAmelCase_			:     Dict									= top_k_warp_safety_check(A_				,				A_				,				cur_len=A_)

									# min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
									self.assertListEqual((scores == 0.0).sum(axis=-1).tolist()				,				[2, 2])




							def    UpperCAmelCase__			(       self     :       Tuple):
									lowerCAmelCase_			:     str									= None
									lowerCAmelCase_			:     Any									= 1_0
									lowerCAmelCase_			:     int									= 2

									# create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
									lowerCAmelCase_			:     List[str]									= np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]]))

									lowerCAmelCase_			:     List[str]									= FlaxTopPLogitsWarper(0.8)
									lowerCAmelCase_			:     Optional[Any]									= np.exp(top_p_warp(A_				,				A_				,				cur_len=A_))

									# dist should be filtered to keep min num values so that sum is >= top_p
									# exp (-inf) => 0
									lowerCAmelCase_			:     int									= np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]])
									self.assertTrue(np.allclose(A_				,				A_				,				atol=1e-3))

									# check edge cases with negative and extreme logits
									lowerCAmelCase_			:     int									= np.broadcast_to(np.arange(A_)[None, :]				,				(batch_size, vocab_size)).copy() - (
									    vocab_size // 2
									)

									# make ramp_logits more extreme
									lowerCAmelCase_			:     Any									= ramp_logits[1] * 100.0

									# make sure at least 2 tokens are kept
									lowerCAmelCase_			:     Dict									= FlaxTopPLogitsWarper(0.9				,				min_tokens_to_keep=2				,				filter_value=0.0)
									lowerCAmelCase_			:     str									= top_p_warp(A_				,				A_				,				cur_len=A_)

									# first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
									self.assertListEqual((filtered_dist != 0.0).sum(axis=-1).tolist()				,				[3, 2])




							def    UpperCAmelCase__			(       self     :       str):
									lowerCAmelCase_			:     str									= 2_0
									lowerCAmelCase_			:     int									= 4
									lowerCAmelCase_			:     Tuple									= 0

									lowerCAmelCase_			:     Any									= FlaxMinLengthLogitsProcessor(min_length=1_0				,				eos_token_id=A_)

									# check that min length is applied at length 5
									lowerCAmelCase_			:     Union[str, Any]									= ids_tensor((batch_size, 2_0)				,				vocab_size=2_0)
									lowerCAmelCase_			:     str									= 5
									lowerCAmelCase_			:     Any									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Optional[Any]									= min_dist_processor(A_				,				A_				,				cur_len=A_)
									self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()				,				4 * [-float('''inf''')])

									# check that min length is not applied anymore at length 15
									lowerCAmelCase_			:     Dict									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Optional[int]									= 1_5
									lowerCAmelCase_			:     str									= min_dist_processor(A_				,				A_				,				cur_len=A_)
									self.assertFalse(jnp.isinf(A_).any())




							def    UpperCAmelCase__			(       self     :       List[Any]):
									lowerCAmelCase_			:     Union[str, Any]									= 2_0
									lowerCAmelCase_			:     List[Any]									= 4
									lowerCAmelCase_			:     List[str]									= 0

									lowerCAmelCase_			:     Optional[Any]									= FlaxForcedBOSTokenLogitsProcessor(bos_token_id=A_)

									# check that all scores are -inf except the bos_token_id score
									lowerCAmelCase_			:     Tuple									= ids_tensor((batch_size, 1)				,				vocab_size=2_0)
									lowerCAmelCase_			:     Optional[int]									= 1
									lowerCAmelCase_			:     List[Any]									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Any									= logits_processor(A_				,				A_				,				cur_len=A_)
									self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :]).all())
									self.assertListEqual(scores[:, bos_token_id].tolist()				,				4 * [0])  # score for bos_token_id shold be zero

									# check that bos_token_id is not forced if current length is greater than 1
									lowerCAmelCase_			:     List[Any]									= 3
									lowerCAmelCase_			:     Any									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Tuple									= logits_processor(A_				,				A_				,				cur_len=A_)
									self.assertFalse(jnp.isinf(A_).any())




							def    UpperCAmelCase__			(       self     :       Tuple):
									lowerCAmelCase_			:     Any									= 2_0
									lowerCAmelCase_			:     str									= 4
									lowerCAmelCase_			:     List[str]									= 0
									lowerCAmelCase_			:     Optional[int]									= 5

									lowerCAmelCase_			:     List[Any]									= FlaxForcedEOSTokenLogitsProcessor(max_length=A_				,				eos_token_id=A_)

									# check that all scores are -inf except the eos_token_id when max_length is reached
									lowerCAmelCase_			:     List[str]									= ids_tensor((batch_size, 4)				,				vocab_size=2_0)
									lowerCAmelCase_			:     Dict									= 4
									lowerCAmelCase_			:     List[str]									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Dict									= logits_processor(A_				,				A_				,				cur_len=A_)
									self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :]).all())
									self.assertListEqual(scores[:, eos_token_id].tolist()				,				4 * [0])  # score for eos_token_id should be zero

									# check that eos_token_id is not forced if max_length is not reached
									lowerCAmelCase_			:     Optional[int]									= 3
									lowerCAmelCase_			:     Optional[int]									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Optional[Any]									= logits_processor(A_				,				A_				,				cur_len=A_)
									self.assertFalse(jnp.isinf(A_).any())




							def    UpperCAmelCase__			(       self     :       List[str]):
									lowerCAmelCase_			:     List[Any]									= 4
									lowerCAmelCase_			:     List[Any]									= 1_0
									lowerCAmelCase_			:     Tuple									= 1_5
									lowerCAmelCase_			:     List[str]									= 2
									lowerCAmelCase_			:     Dict									= 1
									lowerCAmelCase_			:     List[Any]									= 1_5

									# dummy input_ids and scores
									lowerCAmelCase_			:     str									= ids_tensor((batch_size, sequence_length)				,				A_)
									lowerCAmelCase_			:     str									= input_ids.copy()

									lowerCAmelCase_			:     Union[str, Any]									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     List[str]									= scores.copy()

									# instantiate all dist processors
									lowerCAmelCase_			:     int									= FlaxTemperatureLogitsWarper(temperature=0.5)
									lowerCAmelCase_			:     str									= FlaxTopKLogitsWarper(3)
									lowerCAmelCase_			:     Optional[int]									= FlaxTopPLogitsWarper(0.8)

									# instantiate all logits processors
									lowerCAmelCase_			:     List[Any]									= FlaxMinLengthLogitsProcessor(min_length=1_0				,				eos_token_id=A_)
									lowerCAmelCase_			:     Tuple									= FlaxForcedBOSTokenLogitsProcessor(bos_token_id=A_)
									lowerCAmelCase_			:     Any									= FlaxForcedEOSTokenLogitsProcessor(max_length=A_				,				eos_token_id=A_)

									lowerCAmelCase_			:     Optional[Any]									= 1_0

									# no processor list
									lowerCAmelCase_			:     Tuple									= temp_dist_warp(A_				,				A_				,				cur_len=A_)
									lowerCAmelCase_			:     List[str]									= top_k_warp(A_				,				A_				,				cur_len=A_)
									lowerCAmelCase_			:     Optional[Any]									= top_p_warp(A_				,				A_				,				cur_len=A_)
									lowerCAmelCase_			:     int									= min_dist_proc(A_				,				A_				,				cur_len=A_)
									lowerCAmelCase_			:     Optional[int]									= bos_dist_proc(A_				,				A_				,				cur_len=A_)
									lowerCAmelCase_			:     str									= eos_dist_proc(A_				,				A_				,				cur_len=A_)

									# with processor list
									lowerCAmelCase_			:     Tuple									= FlaxLogitsProcessorList(
									    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc])
									lowerCAmelCase_			:     Optional[Any]									= processor(A_				,				A_				,				cur_len=A_)

									# scores should be equal
									self.assertTrue(jnp.allclose(A_				,				A_				,				atol=1e-3))

									# input_ids should never be changed
									self.assertListEqual(input_ids.tolist()				,				input_ids_comp.tolist())







							def    UpperCAmelCase__			(       self     :       Tuple):
									lowerCAmelCase_			:     Dict									= 4
									lowerCAmelCase_			:     List[Any]									= 1_0
									lowerCAmelCase_			:     Tuple									= 1_5
									lowerCAmelCase_			:     str									= 2
									lowerCAmelCase_			:     List[str]									= 1
									lowerCAmelCase_			:     List[Any]									= 1_5

									# dummy input_ids and scores
									lowerCAmelCase_			:     Any									= ids_tensor((batch_size, sequence_length)				,				A_)
									lowerCAmelCase_			:     Dict									= input_ids.copy()

									lowerCAmelCase_			:     Union[str, Any]									= self._get_uniform_logits(A_				,				A_)
									lowerCAmelCase_			:     Dict									= scores.copy()

									# instantiate all dist processors
									lowerCAmelCase_			:     Optional[int]									= FlaxTemperatureLogitsWarper(temperature=0.5)
									lowerCAmelCase_			:     Union[str, Any]									= FlaxTopKLogitsWarper(3)
									lowerCAmelCase_			:     Optional[int]									= FlaxTopPLogitsWarper(0.8)

									# instantiate all logits processors
									lowerCAmelCase_			:     Dict									= FlaxMinLengthLogitsProcessor(min_length=1_0				,				eos_token_id=A_)
									lowerCAmelCase_			:     Optional[Any]									= FlaxForcedBOSTokenLogitsProcessor(bos_token_id=A_)
									lowerCAmelCase_			:     Dict									= FlaxForcedEOSTokenLogitsProcessor(max_length=A_				,				eos_token_id=A_)

									lowerCAmelCase_			:     List[Any]									= 1_0

									# no processor list
									def run_no_processor_list(A_     :       str				,				A_     :       Tuple				,				A_     :       Tuple):
											lowerCAmelCase_			:     Optional[Any]									= temp_dist_warp(A_				,				A_				,				cur_len=A_)
											lowerCAmelCase_			:     Optional[Any]									= top_k_warp(A_				,				A_				,				cur_len=A_)
											lowerCAmelCase_			:     str									= top_p_warp(A_				,				A_				,				cur_len=A_)
											lowerCAmelCase_			:     Any									= min_dist_proc(A_				,				A_				,				cur_len=A_)
											lowerCAmelCase_			:     Union[str, Any]									= bos_dist_proc(A_				,				A_				,				cur_len=A_)
											lowerCAmelCase_			:     List[Any]									= eos_dist_proc(A_				,				A_				,				cur_len=A_)
											return scores

									# with processor list
									def run_processor_list(A_     :       Any				,				A_     :       int				,				A_     :       Tuple):
											lowerCAmelCase_			:     List[str]									= FlaxLogitsProcessorList(
											    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc])
											lowerCAmelCase_			:     Optional[int]									= processor(A_				,				A_				,				cur_len=A_)
											return scores

									lowerCAmelCase_			:     List[Any]									= jax.jit(A_)
									lowerCAmelCase_			:     int									= jax.jit(A_)

									lowerCAmelCase_			:     Any									= jitted_run_no_processor_list(A_				,				A_				,				A_)
									lowerCAmelCase_			:     Any									= jitted_run_processor_list(A_				,				A_				,				A_)

									# scores should be equal
									self.assertTrue(jnp.allclose(A_				,				A_				,				atol=1e-3))

									# input_ids should never be changed
									self.assertListEqual(input_ids.tolist()				,				input_ids_comp.tolist())










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 				__snake_case						(     UpperCamelCase_						,UpperCamelCase_						,unittest.TestCase						):
							_a			      = IFPipeline
							_a			      = TEXT_TO_IMAGE_PARAMS - {'''width''', '''height''', '''latents'''}
							_a			      = TEXT_TO_IMAGE_BATCH_PARAMS
							_a			      = PipelineTesterMixin.required_optional_params - {'''latents'''}




							def    UpperCAmelCase__			(       self     :       List[str]):
									return self._get_dummy_components()




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       List[Any]				,				A_     :       Any=0):
									if str(A_).startswith('''mps'''):
											lowerCAmelCase_			:     List[Any]									= torch.manual_seed(A_)
									else:
											lowerCAmelCase_			:     List[str]									= torch.Generator(device=A_).manual_seed(A_)

									lowerCAmelCase_			:     Optional[Any]									= {
									    '''prompt''': '''A painting of a squirrel eating a burger''',
									    '''generator''': generator,
									    '''num_inference_steps''': 2,
									    '''output_type''': '''numpy''',
									}

									return inputs




							def    UpperCAmelCase__			(       self     :       int):
									self._test_save_load_optional_components()




							@unittest.skipIf(torch_device != '''cuda'''				,				reason='''float16 requires CUDA''')
							def    UpperCAmelCase__			(       self     :       str):
									# 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    UpperCAmelCase__			(       self     :       str):
									self._test_attention_slicing_forward_pass(expected_max_diff=1e-2)




							def    UpperCAmelCase__			(       self     :       int):
									self._test_save_load_local()




							def    UpperCAmelCase__			(       self     :       str):
									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    UpperCAmelCase__			(       self     :       Optional[int]):
									self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3)





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




							def    UpperCAmelCase__			(       self     :       Optional[Any]):
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()




							def    UpperCAmelCase__			(       self     :       List[str]):
									# if

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

									lowerCAmelCase_			:     Dict									= IFSuperResolutionPipeline.from_pretrained(
									    '''DeepFloyd/IF-II-L-v1.0'''				,				variant='''fp16'''				,				torch_dtype=torch.floataa				,				text_encoder=A_				,				tokenizer=A_)

									# pre compute text embeddings and remove T5 to save memory

									pipe_a.text_encoder.to('''cuda''')

									lowerCAmelCase_					,  lowerCAmelCase_			:     List[str]									= pipe_a.encode_prompt('''anime turtle'''				,				device='''cuda''')

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

									lowerCAmelCase_			:     Tuple									= None
									lowerCAmelCase_			:     str									= 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(A_				,				A_				,				A_				,				A_)

									pipe_a.remove_all_hooks()
									pipe_a.remove_all_hooks()

									# img2img

									lowerCAmelCase_			:     List[str]									= IFImgaImgPipeline(**pipe_a.components)
									lowerCAmelCase_			:     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(A_				,				A_				,				A_				,				A_)

									pipe_a.remove_all_hooks()
									pipe_a.remove_all_hooks()

									# inpainting

									lowerCAmelCase_			:     int									= IFInpaintingPipeline(**pipe_a.components)
									lowerCAmelCase_			:     Union[str, Any]									= 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(A_				,				A_				,				A_				,				A_)




							def    UpperCAmelCase__			(       self     :       int				,				A_     :       Optional[int]				,				A_     :       Any				,				A_     :       str				,				A_     :       Union[str, Any]):
									# pipeline 1

									_start_torch_memory_measurement()

									lowerCAmelCase_			:     Optional[Any]									= torch.Generator(device='''cpu''').manual_seed(0)
									lowerCAmelCase_			:     Optional[Any]									= pipe_a(
									    prompt_embeds=A_				,				negative_prompt_embeds=A_				,				num_inference_steps=2				,				generator=A_				,				output_type='''np'''				,				)

									lowerCAmelCase_			:     Dict									= output.images[0]

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

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

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

									# pipeline 2

									_start_torch_memory_measurement()

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

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

									lowerCAmelCase_			:     Optional[int]									= pipe_a(
									    prompt_embeds=A_				,				negative_prompt_embeds=A_				,				image=A_				,				generator=A_				,				num_inference_steps=2				,				output_type='''np'''				,				)

									lowerCAmelCase_			:     Union[str, 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_			:     int									= load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy''')
									assert_mean_pixel_difference(A_				,				A_)




							def    UpperCAmelCase__			(       self     :       int				,				A_     :       Optional[int]				,				A_     :       Any				,				A_     :       List[str]				,				A_     :       List[str]):
									# pipeline 1

									_start_torch_memory_measurement()

									lowerCAmelCase_			:     Any									= floats_tensor((1, 3, 6_4, 6_4)				,				rng=random.Random(0)).to(A_)

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

									lowerCAmelCase_			:     Any									= pipe_a(
									    prompt_embeds=A_				,				negative_prompt_embeds=A_				,				image=A_				,				num_inference_steps=2				,				generator=A_				,				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_0 * 1_0**9

									lowerCAmelCase_			:     Optional[int]									= load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy''')
									assert_mean_pixel_difference(A_				,				A_)

									# pipeline 2

									_start_torch_memory_measurement()

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

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

									lowerCAmelCase_			:     List[str]									= pipe_a(
									    prompt_embeds=A_				,				negative_prompt_embeds=A_				,				image=A_				,				original_image=A_				,				generator=A_				,				num_inference_steps=2				,				output_type='''np'''				,				)

									lowerCAmelCase_			:     Union[str, Any]									= 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_			:     Tuple									= 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(A_				,				A_)







							def    UpperCAmelCase__			(       self     :       str				,				A_     :       Optional[Any]				,				A_     :       Optional[Any]				,				A_     :       Dict				,				A_     :       List[str]):
									# pipeline 1

									_start_torch_memory_measurement()

									lowerCAmelCase_			:     Union[str, Any]									= floats_tensor((1, 3, 6_4, 6_4)				,				rng=random.Random(0)).to(A_)
									lowerCAmelCase_			:     int									= floats_tensor((1, 3, 6_4, 6_4)				,				rng=random.Random(1)).to(A_)

									lowerCAmelCase_			:     Optional[Any]									= torch.Generator(device='''cpu''').manual_seed(0)
									lowerCAmelCase_			:     Any									= pipe_a(
									    prompt_embeds=A_				,				negative_prompt_embeds=A_				,				image=A_				,				mask_image=A_				,				num_inference_steps=2				,				generator=A_				,				output_type='''np'''				,				)

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

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

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

									lowerCAmelCase_			:     int									= load_numpy(
									    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy''')
									assert_mean_pixel_difference(A_				,				A_)

									# pipeline 2

									_start_torch_memory_measurement()

									lowerCAmelCase_			:     Optional[Any]									= torch.Generator(device='''cpu''').manual_seed(0)

									lowerCAmelCase_			:     Optional[Any]									= floats_tensor((1, 3, 6_4, 6_4)				,				rng=random.Random(0)).to(A_)
									lowerCAmelCase_			:     Union[str, Any]									= floats_tensor((1, 3, 2_5_6, 2_5_6)				,				rng=random.Random(0)).to(A_)
									lowerCAmelCase_			:     Optional[Any]									= floats_tensor((1, 3, 2_5_6, 2_5_6)				,				rng=random.Random(1)).to(A_)

									lowerCAmelCase_			:     int									= pipe_a(
									    prompt_embeds=A_				,				negative_prompt_embeds=A_				,				image=A_				,				mask_image=A_				,				original_image=A_				,				generator=A_				,				num_inference_steps=2				,				output_type='''np'''				,				)

									lowerCAmelCase_			:     Tuple									= output.images[0]

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

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

									lowerCAmelCase_			:     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(A_				,				A_)





def 			UpperCamelCase(  ):
		torch.cuda.empty_cache()
		torch.cuda.reset_max_memory_allocated()
		torch.cuda.reset_peak_memory_stats()





