import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    LiltForQuestionAnswering,
					    LiltForSequenceClassification,
					    LiltForTokenClassification,
					    LiltModel,
					)
					from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST





class       lowercase__		:


						def __init__( self			:       List[str]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Union[str, Any]=13					,	UpperCamelCase__			:       List[Any]=7					,	UpperCamelCase__			:       int=True					,	UpperCamelCase__			:       Optional[int]=True					,	UpperCamelCase__			:       int=True					,	UpperCamelCase__			:       List[Any]=True					,	UpperCamelCase__			:       List[Any]=99					,	UpperCamelCase__			:       Union[str, Any]=24					,	UpperCamelCase__			:       Any=2					,	UpperCamelCase__			:       str=6					,	UpperCamelCase__			:       Optional[Any]=37					,	UpperCamelCase__			:       str="gelu"					,	UpperCamelCase__			:       Dict=0.1					,	UpperCamelCase__			:       int=0.1					,	UpperCamelCase__			:       int=512					,	UpperCamelCase__			:       Dict=16					,	UpperCamelCase__			:       List[Any]=2					,	UpperCamelCase__			:       Dict=0.02					,	UpperCamelCase__			:       Union[str, Any]=3					,	UpperCamelCase__			:       str=None					,	UpperCamelCase__			:       str=1000					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							parent
												SCREAMING_SNAKE_CASE							:					List[str]           =							batch_size
												SCREAMING_SNAKE_CASE							:					Dict           =							seq_length
												SCREAMING_SNAKE_CASE							:					Tuple           =							is_training
												SCREAMING_SNAKE_CASE							:					List[str]           =							use_input_mask
												SCREAMING_SNAKE_CASE							:					Dict           =							use_token_type_ids
												SCREAMING_SNAKE_CASE							:					List[str]           =							use_labels
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							vocab_size
												SCREAMING_SNAKE_CASE							:					str           =							hidden_size
												SCREAMING_SNAKE_CASE							:					Tuple           =							num_hidden_layers
												SCREAMING_SNAKE_CASE							:					int           =							num_attention_heads
												SCREAMING_SNAKE_CASE							:					List[str]           =							intermediate_size
												SCREAMING_SNAKE_CASE							:					Dict           =							hidden_act
												SCREAMING_SNAKE_CASE							:					Dict           =							hidden_dropout_prob
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE							:					int           =							max_position_embeddings
												SCREAMING_SNAKE_CASE							:					List[Any]           =							type_vocab_size
												SCREAMING_SNAKE_CASE							:					List[Any]           =							type_sequence_label_size
												SCREAMING_SNAKE_CASE							:					int           =							initializer_range
												SCREAMING_SNAKE_CASE							:					List[Any]           =							num_labels
												SCREAMING_SNAKE_CASE							:					Dict           =							scope
												SCREAMING_SNAKE_CASE							:					Dict           =							range_bbox


						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							ids_tensor([self.batch_size, self.seq_length]					,	self.vocab_size		)

												SCREAMING_SNAKE_CASE							:					List[str]           =							ids_tensor([self.batch_size, self.seq_length, 4]					,	self.range_bbox		)
												# Ensure that bbox is legal
												for i in range(bbox.shape[0]		):
																		for j in range(bbox.shape[1]		):
																								if bbox[i, j, 3] < bbox[i, j, 1]:
																														SCREAMING_SNAKE_CASE							:					Tuple           =							bbox[i, j, 3]
																														SCREAMING_SNAKE_CASE							:					str           =							bbox[i, j, 1]
																														SCREAMING_SNAKE_CASE							:					int           =							t
																								if bbox[i, j, 2] < bbox[i, j, 0]:
																														SCREAMING_SNAKE_CASE							:					Dict           =							bbox[i, j, 2]
																														SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							bbox[i, j, 0]
																														SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							t

												SCREAMING_SNAKE_CASE							:					List[str]           =							None
												if self.use_input_mask:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	vocab_size=2		)

												SCREAMING_SNAKE_CASE							:					Any           =							None
												if self.use_token_type_ids:
																		SCREAMING_SNAKE_CASE							:					List[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.type_vocab_size		)

												SCREAMING_SNAKE_CASE							:					Any           =							None
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							None
												if self.use_labels:
																		SCREAMING_SNAKE_CASE							:					Tuple           =							ids_tensor([self.batch_size]					,	self.type_sequence_label_size		)
																		SCREAMING_SNAKE_CASE							:					str           =							ids_tensor([self.batch_size, self.seq_length]					,	self.num_labels		)

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							self.get_config()

												return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												return LiltConfig(
												    vocab_size=self.vocab_size					,	hidden_size=self.hidden_size					,	num_hidden_layers=self.num_hidden_layers					,	num_attention_heads=self.num_attention_heads					,	intermediate_size=self.intermediate_size					,	hidden_act=self.hidden_act					,	hidden_dropout_prob=self.hidden_dropout_prob					,	attention_probs_dropout_prob=self.attention_probs_dropout_prob					,	max_position_embeddings=self.max_position_embeddings					,	type_vocab_size=self.type_vocab_size					,	initializer_range=self.initializer_range					,	)


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Dict					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Tuple           =							LiltModel(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							model(UpperCamelCase__					,	bbox=UpperCamelCase__					,	attention_mask=UpperCamelCase__					,	token_type_ids=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Any           =							model(UpperCamelCase__					,	bbox=UpperCamelCase__					,	token_type_ids=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Any           =							model(UpperCamelCase__					,	bbox=UpperCamelCase__		)

												self.parent.assertEqual(result.last_hidden_state.shape					,	(self.batch_size, self.seq_length, self.hidden_size)		)
												self.parent.assertEqual(result.pooler_output.shape					,	(self.batch_size, self.hidden_size)		)


						def  __A ( self			:       Any					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Any					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					Any           =							LiltForTokenClassification(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							model(
												    UpperCamelCase__					,	bbox=UpperCamelCase__					,	attention_mask=UpperCamelCase__					,	token_type_ids=UpperCamelCase__					,	labels=UpperCamelCase__		)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.seq_length, self.num_labels)		)


						def  __A ( self			:       Optional[Any]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Dict					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							LiltForQuestionAnswering(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					List[Any]           =							model(
												    UpperCamelCase__					,	bbox=UpperCamelCase__					,	attention_mask=UpperCamelCase__					,	token_type_ids=UpperCamelCase__					,	start_positions=UpperCamelCase__					,	end_positions=UpperCamelCase__					,	)
												self.parent.assertEqual(result.start_logits.shape					,	(self.batch_size, self.seq_length)		)
												self.parent.assertEqual(result.end_logits.shape					,	(self.batch_size, self.seq_length)		)



						def  __A ( self			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							self.prepare_config_and_inputs()
												(
												    SCREAMING_SNAKE_CASE						
												)							:					List[Any]           =							config_and_inputs
												SCREAMING_SNAKE_CASE							:					List[str]           =							{
												    '''input_ids''': input_ids,
												    '''bbox''': bbox,
												    '''token_type_ids''': token_type_ids,
												    '''attention_mask''': input_mask,
												}
												return config, inputs_dict






@require_torch
class       lowercase__		(     UpperCamelCase_			,							UpperCamelCase_			,							UpperCamelCase_			,							unittest.TestCase):
						UpperCamelCase_  =	(
						    (
						        LiltModel,
						        LiltForSequenceClassification,
						        LiltForTokenClassification,
						        LiltForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCamelCase_  =	(
						    {
						        """feature-extraction""": LiltModel,
						        """question-answering""": LiltForQuestionAnswering,
						        """text-classification""": LiltForSequenceClassification,
						        """token-classification""": LiltForTokenClassification,
						        """zero-shot""": LiltForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False


						def  __A ( self			:       List[Any]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       List[str]		):





												'''simple docstring'''





												return True


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Any           =							LiltModelTester(self		)
												SCREAMING_SNAKE_CASE							:					Dict           =							ConfigTester(self					,	config_class=UpperCamelCase__					,	hidden_size=37		)


						def  __A ( self			:       Any		):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*UpperCamelCase__		)


						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																		SCREAMING_SNAKE_CASE							:					Any           =							type
																		self.model_tester.create_and_check_model(*UpperCamelCase__		)


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__		)


						def  __A ( self			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[int]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__		)



						@slow
						def  __A ( self			:       str		):





												'''simple docstring'''





												for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							LiltModel.from_pretrained(UpperCamelCase__		)
																		self.assertIsNotNone(UpperCamelCase__		)






@require_torch
@slow
class       lowercase__		(     unittest.TestCase):


						def  __A ( self			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Dict           =							LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base'''		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Dict           =							torch.tensor([[1, 2]]					,	device=UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					str           =							torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]					,	device=UpperCamelCase__		)

												# forward pass
												with torch.no_grad():
																		SCREAMING_SNAKE_CASE							:					Dict           =							model(input_ids=UpperCamelCase__					,	bbox=UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					str           =							torch.Size([1, 2, 768]		)
												SCREAMING_SNAKE_CASE							:					Tuple           =							torch.tensor(
												    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]					,	device=UpperCamelCase__					,	)

												self.assertTrue(outputs.last_hidden_state.shape					,	UpperCamelCase__		)
												self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]					,	UpperCamelCase__					,	atol=1E-3		)		)






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


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												return self._get_dummy_components()


						def  __A ( self			:       int					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int=0		):





												'''simple docstring'''





												if str(UpperCamelCase__		).startswith('''mps'''		):
																		SCREAMING_SNAKE_CASE							:					Tuple           =							torch.manual_seed(UpperCamelCase__		)
												else:
																		SCREAMING_SNAKE_CASE							:					Dict           =							torch.Generator(device=UpperCamelCase__		).manual_seed(UpperCamelCase__		)

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

												return inputs


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												self._test_save_load_optional_components()


						@unittest.skipIf(torch_device != '''cuda'''					,	reason='''float16 requires CUDA'''		)
						def  __A ( self			:       Any		):





												'''simple docstring'''





												super().test_save_load_floataa(expected_max_diff=1E-1		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												self._test_attention_slicing_forward_pass(expected_max_diff=1E-2		)


						def  __A ( self			:       List[Any]		):





												'''simple docstring'''





												self._test_save_load_local()


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												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  __A ( self			:       Tuple		):





												'''simple docstring'''





												self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3		)






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


						def  __A ( self			:       Optional[Any]		):





												'''simple docstring'''





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


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							IFPipeline.from_pretrained('''DeepFloyd/IF-I-XL-v1.0'''					,	variant='''fp16'''					,	torch_dtype=torch.floataa		)

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

												# pre compute text embeddings and remove T5 to save memory

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

												SCREAMING_SNAKE_CASE       ,						SCREAMING_SNAKE_CASE							:					Dict           =							pipe_a.encode_prompt('''anime turtle'''					,	device='''cuda'''		)

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

												SCREAMING_SNAKE_CASE							:					Tuple           =							None
												SCREAMING_SNAKE_CASE							:					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(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# img2img

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							IFImgaImgPipeline(**pipe_a.components		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							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(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)

												pipe_a.remove_all_hooks()
												pipe_a.remove_all_hooks()

												# inpainting

												SCREAMING_SNAKE_CASE							:					Tuple           =							IFInpaintingPipeline(**pipe_a.components		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							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(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)


						def  __A ( self			:       Tuple					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Dict		):





												'''simple docstring'''





												_start_torch_memory_measurement()

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

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

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

												SCREAMING_SNAKE_CASE							:					Tuple           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 13 * 10**9

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

												# pipeline 2

												_start_torch_memory_measurement()

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

												SCREAMING_SNAKE_CASE							:					int           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Any           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	generator=UpperCamelCase__					,	num_inference_steps=2					,	output_type='''np'''					,	)

												SCREAMING_SNAKE_CASE							:					Tuple           =							output.images[0]

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

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 10**9

												SCREAMING_SNAKE_CASE							:					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(UpperCamelCase__					,	UpperCamelCase__		)


						def  __A ( self			:       Optional[int]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					Tuple           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Any           =							torch.Generator(device='''cpu'''		).manual_seed(0		)

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

												SCREAMING_SNAKE_CASE							:					int           =							output.images[0]

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

												SCREAMING_SNAKE_CASE							:					Optional[int]           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 10 * 10**9

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

												# pipeline 2

												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							torch.Generator(device='''cpu'''		).manual_seed(0		)

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							floats_tensor((1, 3, 256, 256)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					List[Any]           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					str           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	original_image=UpperCamelCase__					,	generator=UpperCamelCase__					,	num_inference_steps=2					,	output_type='''np'''					,	)

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

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

												SCREAMING_SNAKE_CASE							:					List[Any]           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 10**9

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							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(UpperCamelCase__					,	UpperCamelCase__		)



						def  __A ( self			:       List[str]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Optional[Any]		):





												'''simple docstring'''





												_start_torch_memory_measurement()

												SCREAMING_SNAKE_CASE							:					str           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(1		)		).to(UpperCamelCase__		)

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

												SCREAMING_SNAKE_CASE							:					Any           =							output.images[0]

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

												SCREAMING_SNAKE_CASE							:					Any           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 10 * 10**9

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

												# pipeline 2

												_start_torch_memory_measurement()

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

												SCREAMING_SNAKE_CASE							:					List[str]           =							floats_tensor((1, 3, 64, 64)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					int           =							floats_tensor((1, 3, 256, 256)					,	rng=random.Random(0		)		).to(UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							floats_tensor((1, 3, 256, 256)					,	rng=random.Random(1		)		).to(UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					Dict           =							pipe_a(
												    prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	image=UpperCamelCase__					,	mask_image=UpperCamelCase__					,	original_image=UpperCamelCase__					,	generator=UpperCamelCase__					,	num_inference_steps=2					,	output_type='''np'''					,	)

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

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

												SCREAMING_SNAKE_CASE							:					Any           =							torch.cuda.max_memory_allocated()
												assert mem_bytes < 4 * 10**9

												SCREAMING_SNAKE_CASE							:					Tuple           =							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(UpperCamelCase__					,	UpperCamelCase__		)






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





