
'''simple docstring'''







from __future__ import annotations

import copy
import inspect
import json
import math
import os
import tempfile
import unittest
from importlib import import_module

import numpy as np

from transformers import ViTMAEConfig
from transformers.file_utils import cached_property, is_tf_available, is_vision_available
from transformers.testing_utils import require_tf, require_vision, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFViTMAEForPreTraining, TFViTMAEModel


if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor


class 		__UpperCAmelCase   :


							def __init__( self					,     lowerCAmelCase_					,     lowerCAmelCase_=13					,     lowerCAmelCase_=30					,     lowerCAmelCase_=2					,     lowerCAmelCase_=3					,     lowerCAmelCase_=True					,     lowerCAmelCase_=True					,     lowerCAmelCase_=32					,     lowerCAmelCase_=2					,     lowerCAmelCase_=4					,     lowerCAmelCase_=37					,     lowerCAmelCase_="gelu"					,     lowerCAmelCase_=0.1					,     lowerCAmelCase_=0.1					,     lowerCAmelCase_=10					,     lowerCAmelCase_=0.02					,     lowerCAmelCase_=3					,     lowerCAmelCase_=0.6					,     lowerCAmelCase_=None					,     ):





								"""simple docstring"""






								_snake_case     		=	parent
								_snake_case     		=	batch_size
								_snake_case     		=	image_size
								_snake_case     		=	patch_size
								_snake_case     		=	num_channels
								_snake_case     		=	is_training
								_snake_case     		=	use_labels
								_snake_case     		=	hidden_size
								_snake_case     		=	num_hidden_layers
								_snake_case     		=	num_attention_heads
								_snake_case     		=	intermediate_size
								_snake_case     		=	hidden_act
								_snake_case     		=	hidden_dropout_prob
								_snake_case     		=	attention_probs_dropout_prob
								_snake_case     		=	type_sequence_label_size
								_snake_case     		=	initializer_range
								_snake_case     		=	mask_ratio
								_snake_case     		=	scope

								# in ViTMAE, the expected sequence length = (num_patches + 1) * (1 - config.mask_ratio), rounded above
								# (we add 1 for the [CLS] token)
								_snake_case     		=	(image_size // patch_size) ** 2
								_snake_case     		=	int(math.ceil((1 - mask_ratio) * (num_patches + 1)						)						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

								_snake_case     		=	None
								if self.use_labels:
									_snake_case     		=	ids_tensor([self.batch_size]					,     self.type_sequence_label_size						)

								_snake_case     		=	self.get_config()

								return config, pixel_values, labels


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return ViTMAEConfig(
								    image_size=self.image_size					,     patch_size=self.patch_size					,     num_channels=self.num_channels					,     hidden_size=self.hidden_size					,     num_hidden_layers=self.num_hidden_layers					,     num_attention_heads=self.num_attention_heads					,     intermediate_size=self.intermediate_size					,     decoder_hidden_size=self.hidden_size					,     decoder_num_hidden_layers=self.num_hidden_layers					,     decoder_num_attention_heads=self.num_attention_heads					,     decoder_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					,     is_decoder=lowerCAmelCase_					,     initializer_range=self.initializer_range					,     mask_ratio=self.mask_ratio					,     )


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	TFViTMAEModel(config=lowerCAmelCase_						)
								_snake_case     		=	model(lowerCAmelCase_					,     training=lowerCAmelCase_						)
								self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								_snake_case     		=	TFViTMAEForPreTraining(lowerCAmelCase_						)
								_snake_case     		=	model(lowerCAmelCase_					,     training=lowerCAmelCase_						)
								# expected sequence length = num_patches
								_snake_case     		=	(self.image_size // self.patch_size) ** 2
								_snake_case     		=	self.patch_size**2 * self.num_channels
								self.parent.assertEqual(result.logits.shape					,     (self.batch_size, num_patches, expected_num_channels)						)

								# test greyscale images
								_snake_case     		=	1
								_snake_case     		=	TFViTMAEForPreTraining(lowerCAmelCase_						)

								_snake_case     		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
								_snake_case     		=	model(lowerCAmelCase_					,     training=lowerCAmelCase_						)
								_snake_case     		=	self.patch_size**2
								self.parent.assertEqual(result.logits.shape					,     (self.batch_size, num_patches, expected_num_channels)						)




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.prepare_config_and_inputs()
								((_snake_case)		,				(_snake_case)		,				(_snake_case))     		=	config_and_inputs
								_snake_case     		=	{'pixel_values': pixel_values}
								return config, inputs_dict


@require_tf
class 		__UpperCAmelCase   (      _lowerCamelCase		,     _lowerCamelCase		,     unittest.TestCase		):
							__lowercase   	= (TFViTMAEModel, TFViTMAEForPreTraining) if is_tf_available() else ()
							__lowercase   	= {"""feature-extraction""": TFViTMAEModel} if is_tf_available() else {}

							__lowercase   	= False
							__lowercase   	= False
							__lowercase   	= False
							__lowercase   	= False


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	TFViTMAEModelTester(self						)
								_snake_case     		=	ConfigTester(self					,     config_class=lowerCAmelCase_					,     has_text_modality=lowerCAmelCase_					,     hidden_size=37						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								self.config_tester.run_common_tests()


							@unittest.skip(reason='ViTMAE does not use inputs_embeds'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(lowerCAmelCase_						)
									self.assertIsInstance(model.get_input_embeddings()					,     (tf.keras.layers.Layer)						)
									_snake_case     		=	model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(lowerCAmelCase_					,     tf.keras.layers.Layer						)						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(lowerCAmelCase_						)
									_snake_case     		=	inspect.signature(model.call						)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									_snake_case     		=	[*signature.parameters.keys()]

									_snake_case     		=	['pixel_values']
									self.assertListEqual(arg_names[:1]					,     lowerCAmelCase_						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCAmelCase_						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase_						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								_snake_case     		=	int((config.image_size // config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(lowerCAmelCase_						)
									_snake_case     		=	self._prepare_for_class(lowerCAmelCase_					,     lowerCAmelCase_						)

									_snake_case     		=	model(lowerCAmelCase_					,     noise=lowerCAmelCase_						)

									_snake_case     		=	copy.deepcopy(self._prepare_for_class(lowerCAmelCase_					,     lowerCAmelCase_						)						)
									_snake_case     		=	model(**lowerCAmelCase_					,     noise=lowerCAmelCase_						)
									_snake_case     		=	outputs_dict[0].numpy()
									_snake_case     		=	outputs_keywords[0].numpy()

									self.assertLess(np.sum(np.abs(output_dict - output_keywords						)						)					,     1E-6						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								_snake_case     		=	int((config.image_size // config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)

								def prepare_numpy_arrays(lowerCAmelCase_						):
									_snake_case     		=	{}
									for k, v in inputs_dict.items():
										if tf.is_tensor(lowerCAmelCase_						):
											_snake_case     		=	v.numpy()
										else:
											_snake_case     		=	np.array(lowerCAmelCase_						)

									return inputs_np_dict

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(lowerCAmelCase_						)

									_snake_case     		=	self._prepare_for_class(lowerCAmelCase_					,     lowerCAmelCase_						)
									_snake_case     		=	prepare_numpy_arrays(lowerCAmelCase_						)

									_snake_case     		=	model(lowerCAmelCase_					,     noise=lowerCAmelCase_						)
									_snake_case     		=	model(**lowerCAmelCase_					,     noise=lowerCAmelCase_						)
									self.assert_outputs_same(lowerCAmelCase_					,     lowerCAmelCase_						)


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case     		=	int((tf_model.config.image_size // tf_model.config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)
								_snake_case     		=	tf.constant(lowerCAmelCase_						)

								# Add `noise` argument.
								# PT inputs will be prepared in `super().check_pt_tf_models()` with this added `noise` argument
								_snake_case     		=	tf_noise

								super().check_pt_tf_models(lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								_snake_case     		=	{
								    module_member
								    for model_class in self.all_model_classes
								    for module in (import_module(model_class.__module__						),)
								    for module_member_name in dir(lowerCAmelCase_						)
								    if module_member_name.endswith('MainLayer'						)
								    # This condition is required, since `modeling_tf_clip.py` has 3 classes whose names end with `MainLayer`.
								    and module_member_name[: -len('MainLayer'						)] == model_class.__name__[: -len('Model'						)]
								    for module_member in (getattr(lowerCAmelCase_					,     lowerCAmelCase_						),)
								    if isinstance(lowerCAmelCase_					,     lowerCAmelCase_						)
								    and tf.keras.layers.Layer in module_member.__bases__
								    and getattr(lowerCAmelCase_					,     '_keras_serializable'					,     lowerCAmelCase_						)
								}

								_snake_case     		=	int((config.image_size // config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)
								_snake_case     		=	tf.convert_to_tensor(lowerCAmelCase_						)
								inputs_dict.update({'noise': noise}						)

								for main_layer_class in tf_main_layer_classes:
									_snake_case     		=	main_layer_class(lowerCAmelCase_						)

									_snake_case     		=	{
									    name: tf.keras.Input(tensor.shape[1:]					,     dtype=tensor.dtype						) for name, tensor in inputs_dict.items()
									}

									_snake_case     		=	tf.keras.Model(lowerCAmelCase_					,     outputs=main_layer(lowerCAmelCase_						)						)
									_snake_case     		=	model(lowerCAmelCase_						)

									with tempfile.TemporaryDirectory() as tmpdirname:
										_snake_case     		=	os.path.join(lowerCAmelCase_					,     'keras_model.h5'						)
										model.save(lowerCAmelCase_						)
										_snake_case     		=	tf.keras.models.load_model(
										    lowerCAmelCase_					,     custom_objects={main_layer_class.__name__: main_layer_class}						)
										assert isinstance(lowerCAmelCase_					,     tf.keras.Model						)
										_snake_case     		=	model(lowerCAmelCase_						)
										self.assert_outputs_same(lowerCAmelCase_					,     lowerCAmelCase_						)


							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								_snake_case     		=	int((config.image_size // config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(lowerCAmelCase_						)
									_snake_case     		=	self._prepare_for_class(lowerCAmelCase_					,     lowerCAmelCase_						)
									_snake_case     		=	model(lowerCAmelCase_					,     noise=lowerCAmelCase_						)

									if model_class.__name__ == "TFViTMAEModel":
										_snake_case     		=	outputs.last_hidden_state.numpy()
										_snake_case     		=	0
									else:
										_snake_case     		=	outputs.logits.numpy()
										_snake_case     		=	0

									with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(lowerCAmelCase_					,     saved_model=lowerCAmelCase_						)
										_snake_case     		=	model_class.from_pretrained(lowerCAmelCase_						)
										_snake_case     		=	model(lowerCAmelCase_					,     noise=lowerCAmelCase_						)

										if model_class.__name__ == "TFViTMAEModel":
											_snake_case     		=	after_outputs['last_hidden_state'].numpy()
											_snake_case     		=	0
										else:
											_snake_case     		=	after_outputs['logits'].numpy()
											_snake_case     		=	0

										_snake_case     		=	np.amax(np.abs(out_a - out_a						)						)
										self.assertLessEqual(lowerCAmelCase_					,     1E-5						)


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case		,				_snake_case     		=	self.model_tester.prepare_config_and_inputs_for_common()

								_snake_case     		=	int((config.image_size // config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(self.model_tester.batch_size, num_patches)						)

								for model_class in self.all_model_classes:
									_snake_case     		=	model_class(lowerCAmelCase_						)
									_snake_case     		=	self._prepare_for_class(lowerCAmelCase_					,     lowerCAmelCase_						)

									_snake_case     		=	model(lowerCAmelCase_					,     noise=lowerCAmelCase_						)
									_snake_case     		=	model.get_config()
									# make sure that returned config is jsonifiable, which is required by keras
									json.dumps(lowerCAmelCase_						)
									_snake_case     		=	model_class.from_config(model.get_config()						)
									# make sure it also accepts a normal config
									_snake_case     		=	model_class.from_config(model.config						)
									_snake_case     		=	new_model(lowerCAmelCase_						)  # Build model
									new_model.set_weights(model.get_weights()						)
									_snake_case     		=	new_model(lowerCAmelCase_					,     noise=lowerCAmelCase_						)

									self.assert_outputs_same(lowerCAmelCase_					,     lowerCAmelCase_						)


							@unittest.skip(
							    reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load\n    to get deterministic results.'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass


							@unittest.skip(reason='ViTMAE returns a random mask + ids_restore in each forward pass. See test_save_load'						)
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								pass




							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	TFViTMAEModel.from_pretrained('google/vit-base-patch16-224'						)
								self.assertIsNotNone(lowerCAmelCase_						)







def        SCREAMING_SNAKE_CASE__			(     ) ->			Optional[Any]:
	_snake_case     		=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
	return image





@require_tf
@require_vision
class 		__UpperCAmelCase   (      unittest.TestCase		):


							@cached_property
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								return ViTImageProcessor.from_pretrained('facebook/vit-mae-base'						) if is_vision_available() else None




							@slow
							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								np.random.seed(2						)

								_snake_case     		=	TFViTMAEForPreTraining.from_pretrained('facebook/vit-mae-base'						)

								_snake_case     		=	self.default_image_processor
								_snake_case     		=	prepare_img()
								_snake_case     		=	image_processor(images=lowerCAmelCase_					,     return_tensors='tf'						)

								# prepare a noise vector that will be also used for testing the TF model
								# (this way we can ensure that the PT and TF models operate on the same inputs)
								_snake_case     		=	ViTMAEConfig()
								_snake_case     		=	int((vit_mae_config.image_size // vit_mae_config.patch_size) ** 2						)
								_snake_case     		=	np.random.uniform(size=(1, num_patches)						)

								# forward pass
								_snake_case     		=	model(**lowerCAmelCase_					,     noise=lowerCAmelCase_						)

								# verify the logits
								_snake_case     		=	tf.convert_to_tensor([1, 1_96, 7_68]						)
								self.assertEqual(outputs.logits.shape					,     lowerCAmelCase_						)

								_snake_case     		=	tf.convert_to_tensor(
								    [[-0.0548, -1.7023, -0.9325], [0.3721, -0.5670, -0.2233], [0.8235, -1.3878, -0.3524]]						)

								tf.debugging.assert_near(outputs.logits[0, :3, :3]					,     lowerCAmelCase_					,     atol=1E-4						)



'''simple docstring'''







import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionInstructPixaPixPipeline,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


class 		__UpperCAmelCase   (      _lowerCamelCase		,     _lowerCamelCase		,     _lowerCamelCase		,     unittest.TestCase		):
							__lowercase   	= StableDiffusionInstructPixaPixPipeline
							__lowercase   	= TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""}
							__lowercase   	= TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
							__lowercase   	= IMAGE_TO_IMAGE_IMAGE_PARAMS
							__lowercase   	= IMAGE_TO_IMAGE_IMAGE_PARAMS


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								torch.manual_seed(0						)
								_snake_case     		=	UNetaDConditionModel(
								    block_out_channels=(32, 64)					,     layers_per_block=2					,     sample_size=32					,     in_channels=8					,     out_channels=4					,     down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')					,     up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')					,     cross_attention_dim=32					,     )
								_snake_case     		=	PNDMScheduler(skip_prk_steps=lowerCAmelCase_						)
								torch.manual_seed(0						)
								_snake_case     		=	AutoencoderKL(
								    block_out_channels=[32, 64]					,     in_channels=3					,     out_channels=3					,     down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']					,     up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']					,     latent_channels=4					,     )
								torch.manual_seed(0						)
								_snake_case     		=	CLIPTextConfig(
								    bos_token_id=0					,     eos_token_id=2					,     hidden_size=32					,     intermediate_size=37					,     layer_norm_eps=1E-05					,     num_attention_heads=4					,     num_hidden_layers=5					,     pad_token_id=1					,     vocab_size=10_00					,     )
								_snake_case     		=	CLIPTextModel(lowerCAmelCase_						)
								_snake_case     		=	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'						)

								_snake_case     		=	{
								    'unet': unet,
								    'scheduler': scheduler,
								    'vae': vae,
								    'text_encoder': text_encoder,
								    'tokenizer': tokenizer,
								    'safety_checker': None,
								    'feature_extractor': None,
								}
								return components


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_					,     lowerCAmelCase_=0						):





								"""simple docstring"""






								_snake_case     		=	floats_tensor((1, 3, 32, 32)					,     rng=random.Random(lowerCAmelCase_						)						).to(lowerCAmelCase_						)
								_snake_case     		=	image.cpu().permute(0					,     2					,     3					,     1						)[0]
								_snake_case     		=	Image.fromarray(np.uinta(lowerCAmelCase_						)						).convert('RGB'						)
								if str(lowerCAmelCase_						).startswith('mps'						):
									_snake_case     		=	torch.manual_seed(lowerCAmelCase_						)
								else:
									_snake_case     		=	torch.Generator(device=lowerCAmelCase_						).manual_seed(lowerCAmelCase_						)
								_snake_case     		=	{
								    'prompt': 'A painting of a squirrel eating a burger',
								    'image': image,
								    'generator': generator,
								    'num_inference_steps': 2,
								    'guidance_scale': 6.0,
								    'image_guidance_scale': 1,
								    'output_type': 'numpy',
								}
								return inputs


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	'cpu'  # ensure determinism for the device-dependent torch.Generator
								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	StableDiffusionInstructPixaPixPipeline(**lowerCAmelCase_						)
								_snake_case     		=	sd_pipe.to(lowerCAmelCase_						)
								sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	self.get_dummy_inputs(lowerCAmelCase_						)
								_snake_case     		=	sd_pipe(**lowerCAmelCase_						).images
								_snake_case     		=	image[0, -3:, -3:, -1]
								assert image.shape == (1, 32, 32, 3)
								_snake_case     		=	np.array([0.7526, 0.3750, 0.4547, 0.6117, 0.5866, 0.5016, 0.4327, 0.5642, 0.4815]						)

								assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	'cpu'  # ensure determinism for the device-dependent torch.Generator
								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	StableDiffusionInstructPixaPixPipeline(**lowerCAmelCase_						)
								_snake_case     		=	sd_pipe.to(lowerCAmelCase_						)
								sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	self.get_dummy_inputs(lowerCAmelCase_						)
								_snake_case     		=	'french fries'
								_snake_case     		=	sd_pipe(**lowerCAmelCase_					,     negative_prompt=lowerCAmelCase_						)
								_snake_case     		=	output.images
								_snake_case     		=	image[0, -3:, -3:, -1]

								assert image.shape == (1, 32, 32, 3)
								_snake_case     		=	np.array([0.7511, 0.3642, 0.4553, 0.6236, 0.5797, 0.5013, 0.4343, 0.5611, 0.4831]						)

								assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	'cpu'  # ensure determinism for the device-dependent torch.Generator
								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	StableDiffusionInstructPixaPixPipeline(**lowerCAmelCase_						)
								_snake_case     		=	sd_pipe.to(lowerCAmelCase_						)
								sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	self.get_dummy_inputs(lowerCAmelCase_						)
								_snake_case     		=	[inputs['prompt']] * 2

								_snake_case     		=	np.array(inputs['image']						).astype(np.floataa						) / 255.0
								_snake_case     		=	torch.from_numpy(lowerCAmelCase_						).unsqueeze(0						).to(lowerCAmelCase_						)
								_snake_case     		=	image / 2 + 0.5
								_snake_case     		=	image.permute(0					,     3					,     1					,     2						)
								_snake_case     		=	image.repeat(2					,     1					,     1					,     1						)

								_snake_case     		=	sd_pipe(**lowerCAmelCase_						).images
								_snake_case     		=	image[-1, -3:, -3:, -1]

								assert image.shape == (2, 32, 32, 3)
								_snake_case     		=	np.array([0.5812, 0.5748, 0.5222, 0.5908, 0.5695, 0.7174, 0.6804, 0.5523, 0.5579]						)

								assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	'cpu'  # ensure determinism for the device-dependent torch.Generator
								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	EulerAncestralDiscreteScheduler(
								    beta_start=0.00085					,     beta_end=0.012					,     beta_schedule='scaled_linear'						)
								_snake_case     		=	StableDiffusionInstructPixaPixPipeline(**lowerCAmelCase_						)
								_snake_case     		=	sd_pipe.to(lowerCAmelCase_						)
								sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	self.get_dummy_inputs(lowerCAmelCase_						)
								_snake_case     		=	sd_pipe(**lowerCAmelCase_						).images
								_snake_case     		=	image[0, -3:, -3:, -1]

								_snake_case     		=	[round(lowerCAmelCase_					,     4						) for x in image_slice.flatten().tolist()]
								print(','.join([str(lowerCAmelCase_						) for x in slice]						)						)

								assert image.shape == (1, 32, 32, 3)
								_snake_case     		=	np.array([0.7417, 0.3842, 0.4732, 0.5776, 0.5891, 0.5139, 0.4052, 0.5673, 0.4986]						)

								assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								super().test_inference_batch_single_identical(expected_max_diff=3E-3						)




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.get_dummy_components()
								_snake_case     		=	StableDiffusionInstructPixaPixPipeline(**lowerCAmelCase_						)
								_snake_case     		=	VaeImageProcessor(do_resize=lowerCAmelCase_					,     do_normalize=lowerCAmelCase_						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)

								_snake_case     		=	pipe(**self.get_dummy_inputs_by_type(lowerCAmelCase_					,     input_image_type='pt'						)						)[0]

								_snake_case     		=	components['vae']
								_snake_case     		=	self.get_dummy_inputs_by_type(lowerCAmelCase_					,     input_image_type='pt'						)

								for image_param in self.image_latents_params:
									if image_param in inputs.keys():
										_snake_case     		=	vae.encode(inputs[image_param]						).latent_dist.mode()

								_snake_case     		=	pipe(**lowerCAmelCase_						)[0]

								_snake_case     		=	np.abs(out - out_latents_inputs						).max()
								self.assertLess(lowerCAmelCase_					,     1E-4					,     'passing latents as image input generate different result from passing image'						)


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


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






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


							def 				lowerCamelCase       ( self					,     lowerCAmelCase_=0						):





								"""simple docstring"""






								_snake_case     		=	torch.manual_seed(lowerCAmelCase_						)
								_snake_case     		=	load_image(
								    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg'						)
								_snake_case     		=	{
								    'prompt': 'turn him into a cyborg',
								    'image': image,
								    'generator': generator,
								    'num_inference_steps': 3,
								    'guidance_scale': 7.5,
								    'image_guidance_scale': 1.0,
								    'output_type': 'numpy',
								}
								return inputs


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	StableDiffusionInstructPixaPixPipeline.from_pretrained(
								    'timbrooks/instruct-pix2pix'					,     safety_checker=lowerCAmelCase_						)
								pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)
								pipe.enable_attention_slicing()

								_snake_case     		=	self.get_inputs()
								_snake_case     		=	pipe(**lowerCAmelCase_						).images
								_snake_case     		=	image[0, -3:, -3:, -1].flatten()

								assert image.shape == (1, 5_12, 5_12, 3)
								_snake_case     		=	np.array([0.5902, 0.6015, 0.6027, 0.5983, 0.6092, 0.6061, 0.5765, 0.5785, 0.5555]						)

								assert np.abs(expected_slice - image_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	StableDiffusionInstructPixaPixPipeline.from_pretrained(
								    'timbrooks/instruct-pix2pix'					,     safety_checker=lowerCAmelCase_						)
								_snake_case     		=	LMSDiscreteScheduler.from_config(pipe.scheduler.config						)
								pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)
								pipe.enable_attention_slicing()

								_snake_case     		=	self.get_inputs()
								_snake_case     		=	pipe(**lowerCAmelCase_						).images
								_snake_case     		=	image[0, -3:, -3:, -1].flatten()

								assert image.shape == (1, 5_12, 5_12, 3)
								_snake_case     		=	np.array([0.6578, 0.6817, 0.6972, 0.6761, 0.6856, 0.6916, 0.6428, 0.6516, 0.6301]						)

								assert np.abs(expected_slice - image_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	StableDiffusionInstructPixaPixPipeline.from_pretrained(
								    'timbrooks/instruct-pix2pix'					,     safety_checker=lowerCAmelCase_						)
								_snake_case     		=	DDIMScheduler.from_config(pipe.scheduler.config						)
								pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)
								pipe.enable_attention_slicing()

								_snake_case     		=	self.get_inputs()
								_snake_case     		=	pipe(**lowerCAmelCase_						).images
								_snake_case     		=	image[0, -3:, -3:, -1].flatten()

								assert image.shape == (1, 5_12, 5_12, 3)
								_snake_case     		=	np.array([0.3828, 0.3834, 0.3818, 0.3792, 0.3865, 0.3752, 0.3792, 0.3847, 0.3753]						)

								assert np.abs(expected_slice - image_slice						).max() < 1E-3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	0

								def callback_fn(lowerCAmelCase_					,     lowerCAmelCase_					,     lowerCAmelCase_						) -> None:
									_snake_case     		=	True
									nonlocal number_of_steps
									number_of_steps += 1
									if step == 1:
										_snake_case     		=	latents.detach().cpu().numpy()
										assert latents.shape == (1, 4, 64, 64)
										_snake_case     		=	latents[0, -3:, -3:, -1]
										_snake_case     		=	np.array([-0.2463, -0.4644, -0.9756, 1.5176, 1.4414, 0.7866, 0.9897, 0.8521, 0.7983]						)

										assert np.abs(latents_slice.flatten() - expected_slice						).max() < 5E-2
									elif step == 2:
										_snake_case     		=	latents.detach().cpu().numpy()
										assert latents.shape == (1, 4, 64, 64)
										_snake_case     		=	latents[0, -3:, -3:, -1]
										_snake_case     		=	np.array([-0.2644, -0.4626, -0.9653, 1.5176, 1.4551, 0.7686, 0.9805, 0.8452, 0.8115]						)

										assert np.abs(latents_slice.flatten() - expected_slice						).max() < 5E-2

								_snake_case     		=	False

								_snake_case     		=	StableDiffusionInstructPixaPixPipeline.from_pretrained(
								    'timbrooks/instruct-pix2pix'					,     safety_checker=lowerCAmelCase_					,     torch_dtype=torch.floataa						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)
								pipe.enable_attention_slicing()

								_snake_case     		=	self.get_inputs()
								pipe(**lowerCAmelCase_					,     callback=lowerCAmelCase_					,     callback_steps=1						)
								assert callback_fn.has_been_called
								assert number_of_steps == 3


							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								torch.cuda.empty_cache()
								torch.cuda.reset_max_memory_allocated()
								torch.cuda.reset_peak_memory_stats()

								_snake_case     		=	StableDiffusionInstructPixaPixPipeline.from_pretrained(
								    'timbrooks/instruct-pix2pix'					,     safety_checker=lowerCAmelCase_					,     torch_dtype=torch.floataa						)
								_snake_case     		=	pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)
								pipe.enable_attention_slicing(1						)
								pipe.enable_sequential_cpu_offload()

								_snake_case     		=	self.get_inputs()
								_snake_case     		=	pipe(**lowerCAmelCase_						)

								_snake_case     		=	torch.cuda.max_memory_allocated()
								# make sure that less than 2.2 GB is allocated
								assert mem_bytes < 2.2 * 10**9




							def 				lowerCamelCase       ( self						):





								"""simple docstring"""






								_snake_case     		=	self.get_inputs()
								# resize to resolution that is divisible by 8 but not 16 or 32
								_snake_case     		=	inputs['image'].resize((5_04, 5_04)						)

								_snake_case     		=	'timbrooks/instruct-pix2pix'
								_snake_case     		=	StableDiffusionInstructPixaPixPipeline.from_pretrained(
								    lowerCAmelCase_					,     safety_checker=lowerCAmelCase_					,     )
								pipe.to(lowerCAmelCase_						)
								pipe.set_progress_bar_config(disable=lowerCAmelCase_						)
								pipe.enable_attention_slicing()

								_snake_case     		=	pipe(**lowerCAmelCase_						)
								_snake_case     		=	output.images[0]

								_snake_case     		=	image[2_55:2_58, 3_83:3_86, -1]

								assert image.shape == (5_04, 5_04, 3)
								_snake_case     		=	np.array([0.2726, 0.2529, 0.2664, 0.2655, 0.2641, 0.2642, 0.2591, 0.2649, 0.2590]						)

								assert np.abs(image_slice.flatten() - expected_slice						).max() < 5E-3

