
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        _SCREAMING_SNAKE_CASE	( snake_case_     ,			snake_case_     ,			snake_case_     ,			unittest.TestCase		):
	lowerCAmelCase__   =					StableDiffusionInstructPixaPixPipeline
	lowerCAmelCase__   =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'height', 'width', 'cross_attention_kwargs'}
	lowerCAmelCase__   =					TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
	lowerCAmelCase__   =					IMAGE_TO_IMAGE_IMAGE_PARAMS
	lowerCAmelCase__   =					IMAGE_TO_IMAGE_IMAGE_PARAMS



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					torch.manual_seed(0					)
					lowerCamelCase_         =     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		,    )
					lowerCamelCase_         =     PNDMScheduler(skip_prk_steps=lowercase					)
					torch.manual_seed(0					)
					lowerCamelCase_         =     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					)
					lowerCamelCase_         =     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=1000		,    )
					lowerCamelCase_         =     CLIPTextModel(lowercase					)
					lowerCamelCase_         =     CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"					)

					lowerCamelCase_         =     {
					    "unet": unet,
					    "scheduler": scheduler,
					    "vae": vae,
					    "text_encoder": text_encoder,
					    "tokenizer": tokenizer,
					    "safety_checker": None,
					    "feature_extractor": None,
					}
					return components



	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase=0					)							->			List[Any]:
					lowerCamelCase_         =     floats_tensor((1, 3, 32, 32)		,    rng=random.Random(lowercase					)					).to(lowercase					)
					lowerCamelCase_         =     image.cpu().permute(0		,    2		,    3		,    1					)[0]
					lowerCamelCase_         =     Image.fromarray(np.uinta(lowercase					)					).convert("RGB"					)
					if str(lowercase					).startswith("mps"					):
									lowerCamelCase_         =     torch.manual_seed(lowercase					)
					else:
									lowerCamelCase_         =     torch.Generator(device=lowercase					).manual_seed(lowercase					)
					lowerCamelCase_         =     {
					    "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        SCREAMING_SNAKE_CASE_(       self					)							->			Tuple:
					lowerCamelCase_         =     "cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_         =     self.get_dummy_components()
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline(**lowercase					)
					lowerCamelCase_         =     sd_pipe.to(lowercase					)
					sd_pipe.set_progress_bar_config(disable=lowercase					)

					lowerCamelCase_         =     self.get_dummy_inputs(lowercase					)
					lowerCamelCase_         =     sd_pipe(**lowercase					).images
					lowerCamelCase_         =     image[0, -3:, -3:, -1]
					assert image.shape == (1, 32, 32, 3)
					lowerCamelCase_         =     np.array([0.7_5_2_6, 0.3_7_5_0, 0.4_5_4_7, 0.6_1_1_7, 0.5_8_6_6, 0.5_0_1_6, 0.4_3_2_7, 0.5_6_4_2, 0.4_8_1_5]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			List[str]:
					lowerCamelCase_         =     "cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_         =     self.get_dummy_components()
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline(**lowercase					)
					lowerCamelCase_         =     sd_pipe.to(lowercase					)
					sd_pipe.set_progress_bar_config(disable=lowercase					)

					lowerCamelCase_         =     self.get_dummy_inputs(lowercase					)
					lowerCamelCase_         =     "french fries"
					lowerCamelCase_         =     sd_pipe(**lowercase		,    negative_prompt=lowercase					)
					lowerCamelCase_         =     output.images
					lowerCamelCase_         =     image[0, -3:, -3:, -1]

					assert image.shape == (1, 32, 32, 3)
					lowerCamelCase_         =     np.array([0.7_5_1_1, 0.3_6_4_2, 0.4_5_5_3, 0.6_2_3_6, 0.5_7_9_7, 0.5_0_1_3, 0.4_3_4_3, 0.5_6_1_1, 0.4_8_3_1]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					lowerCamelCase_         =     "cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_         =     self.get_dummy_components()
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline(**lowercase					)
					lowerCamelCase_         =     sd_pipe.to(lowercase					)
					sd_pipe.set_progress_bar_config(disable=lowercase					)

					lowerCamelCase_         =     self.get_dummy_inputs(lowercase					)
					lowerCamelCase_         =     [inputs["prompt"]] * 2

					lowerCamelCase_         =     np.array(inputs["image"]					).astype(np.floataa					) / 2_5_5.0
					lowerCamelCase_         =     torch.from_numpy(lowercase					).unsqueeze(0					).to(lowercase					)
					lowerCamelCase_         =     image / 2 + 0.5
					lowerCamelCase_         =     image.permute(0		,    3		,    1		,    2					)
					lowerCamelCase_         =     image.repeat(2		,    1		,    1		,    1					)

					lowerCamelCase_         =     sd_pipe(**lowercase					).images
					lowerCamelCase_         =     image[-1, -3:, -3:, -1]

					assert image.shape == (2, 32, 32, 3)
					lowerCamelCase_         =     np.array([0.5_8_1_2, 0.5_7_4_8, 0.5_2_2_2, 0.5_9_0_8, 0.5_6_9_5, 0.7_1_7_4, 0.6_8_0_4, 0.5_5_2_3, 0.5_5_7_9]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			List[Any]:
					lowerCamelCase_         =     "cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_         =     self.get_dummy_components()
					lowerCamelCase_         =     EulerAncestralDiscreteScheduler(
					    beta_start=0.0_0_0_8_5		,    beta_end=0.0_1_2		,    beta_schedule="scaled_linear"					)
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline(**lowercase					)
					lowerCamelCase_         =     sd_pipe.to(lowercase					)
					sd_pipe.set_progress_bar_config(disable=lowercase					)

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

					lowerCamelCase_         =     [round(lowercase		,    4					) for x in image_slice.flatten().tolist()]
					print(",".join([str(lowercase					) for x in slice]					)					)

					assert image.shape == (1, 32, 32, 3)
					lowerCamelCase_         =     np.array([0.7_4_1_7, 0.3_8_4_2, 0.4_7_3_2, 0.5_7_7_6, 0.5_8_9_1, 0.5_1_3_9, 0.4_0_5_2, 0.5_6_7_3, 0.4_9_8_6]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[int]:
					super().test_inference_batch_single_identical(expected_max_diff=3e-3					)



	def        SCREAMING_SNAKE_CASE_(       self					)							->			str:
					lowerCamelCase_         =     self.get_dummy_components()
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline(**lowercase					)
					lowerCamelCase_         =     VaeImageProcessor(do_resize=lowercase		,    do_normalize=lowercase					)
					lowerCamelCase_         =     pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)

					lowerCamelCase_         =     pipe(**self.get_dummy_inputs_by_type(lowercase		,    input_image_type="pt"					)					)[0]

					lowerCamelCase_         =     components["vae"]
					lowerCamelCase_         =     self.get_dummy_inputs_by_type(lowercase		,    input_image_type="pt"					)

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

					lowerCamelCase_         =     pipe(**lowercase					)[0]

					lowerCamelCase_         =     np.abs(out - out_latents_inputs					).max()
					self.assertLess(lowercase		,    1e-4		,    "passing latents as image input generate different result from passing image"					)




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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			List[Any]:
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()



	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase=0					)							->			Any:
					lowerCamelCase_         =     torch.manual_seed(lowercase					)
					lowerCamelCase_         =     load_image(
					    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg"					)
					lowerCamelCase_         =     {
					    "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        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[int]:
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
					    "timbrooks/instruct-pix2pix"		,    safety_checker=lowercase					)
					pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)
					pipe.enable_attention_slicing()

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

					assert image.shape == (1, 512, 512, 3)
					lowerCamelCase_         =     np.array([0.5_9_0_2, 0.6_0_1_5, 0.6_0_2_7, 0.5_9_8_3, 0.6_0_9_2, 0.6_0_6_1, 0.5_7_6_5, 0.5_7_8_5, 0.5_5_5_5]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[int]:
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
					    "timbrooks/instruct-pix2pix"		,    safety_checker=lowercase					)
					lowerCamelCase_         =     LMSDiscreteScheduler.from_config(pipe.scheduler.config					)
					pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)
					pipe.enable_attention_slicing()

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

					assert image.shape == (1, 512, 512, 3)
					lowerCamelCase_         =     np.array([0.6_5_7_8, 0.6_8_1_7, 0.6_9_7_2, 0.6_7_6_1, 0.6_8_5_6, 0.6_9_1_6, 0.6_4_2_8, 0.6_5_1_6, 0.6_3_0_1]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Any:
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
					    "timbrooks/instruct-pix2pix"		,    safety_checker=lowercase					)
					lowerCamelCase_         =     DDIMScheduler.from_config(pipe.scheduler.config					)
					pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)
					pipe.enable_attention_slicing()

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

					assert image.shape == (1, 512, 512, 3)
					lowerCamelCase_         =     np.array([0.3_8_2_8, 0.3_8_3_4, 0.3_8_1_8, 0.3_7_9_2, 0.3_8_6_5, 0.3_7_5_2, 0.3_7_9_2, 0.3_8_4_7, 0.3_7_5_3]					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			List[str]:
					lowerCamelCase_         =     0

					def callback_fn(lowercase		,    lowercase		,    lowercase					) -> None:
									lowerCamelCase_         =     True
									nonlocal number_of_steps
									number_of_steps += 1
									if step == 1:
													lowerCamelCase_         =     latents.detach().cpu().numpy()
													assert latents.shape == (1, 4, 64, 64)
													lowerCamelCase_         =     latents[0, -3:, -3:, -1]
													lowerCamelCase_         =     np.array([-0.2_4_6_3, -0.4_6_4_4, -0.9_7_5_6, 1.5_1_7_6, 1.4_4_1_4, 0.7_8_6_6, 0.9_8_9_7, 0.8_5_2_1, 0.7_9_8_3]					)

													assert np.abs(latents_slice.flatten() - expected_slice					).max() < 5e-2
									elif step == 2:
													lowerCamelCase_         =     latents.detach().cpu().numpy()
													assert latents.shape == (1, 4, 64, 64)
													lowerCamelCase_         =     latents[0, -3:, -3:, -1]
													lowerCamelCase_         =     np.array([-0.2_6_4_4, -0.4_6_2_6, -0.9_6_5_3, 1.5_1_7_6, 1.4_5_5_1, 0.7_6_8_6, 0.9_8_0_5, 0.8_4_5_2, 0.8_1_1_5]					)

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

					lowerCamelCase_         =     False

					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
					    "timbrooks/instruct-pix2pix"		,    safety_checker=lowercase		,    torch_dtype=torch.floataa					)
					lowerCamelCase_         =     pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)
					pipe.enable_attention_slicing()

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Dict:
					torch.cuda.empty_cache()
					torch.cuda.reset_max_memory_allocated()
					torch.cuda.reset_peak_memory_stats()

					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
					    "timbrooks/instruct-pix2pix"		,    safety_checker=lowercase		,    torch_dtype=torch.floataa					)
					lowerCamelCase_         =     pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)
					pipe.enable_attention_slicing(1					)
					pipe.enable_sequential_cpu_offload()

					lowerCamelCase_         =     self.get_inputs()
					lowerCamelCase_         =     pipe(**lowercase					)

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



	def        SCREAMING_SNAKE_CASE_(       self					)							->			int:
					lowerCamelCase_         =     self.get_inputs()
					# resize to resolution that is divisible by 8 but not 16 or 32
					lowerCamelCase_         =     inputs["image"].resize((504, 504)					)

					lowerCamelCase_         =     "timbrooks/instruct-pix2pix"
					lowerCamelCase_         =     StableDiffusionInstructPixaPixPipeline.from_pretrained(
					    lowercase		,    safety_checker=lowercase		,    )
					pipe.to(lowercase					)
					pipe.set_progress_bar_config(disable=lowercase					)
					pipe.enable_attention_slicing()

					lowerCamelCase_         =     pipe(**lowercase					)
					lowerCamelCase_         =     output.images[0]

					lowerCamelCase_         =     image[255:258, 383:386, -1]

					assert image.shape == (504, 504, 3)
					lowerCamelCase_         =     np.array([0.2_7_2_6, 0.2_5_2_9, 0.2_6_6_4, 0.2_6_5_5, 0.2_6_4_1, 0.2_6_4_2, 0.2_5_9_1, 0.2_6_4_9, 0.2_5_9_0]					)

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




import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin




class        _SCREAMING_SNAKE_CASE	( snake_case_		):
	lowerCAmelCase__   =					'MCTCTFeatureExtractor'
	lowerCAmelCase__   =					'AutoTokenizer'



	def __init__(       self		,    lowercase		,    lowercase					)							->			str:
					super().__init__(lowercase		,    lowercase					)
					lowerCamelCase_         =     self.feature_extractor
					lowerCamelCase_         =     False



	def __call__(       self		,    *lowercase		,    **lowercase					)							->			List[str]:
					# For backward compatibility
					if self._in_target_context_manager:
									return self.current_processor(*lowercase		,    **lowercase					)

					if "raw_speech" in kwargs:
									warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."					)
									lowerCamelCase_         =     kwargs.pop("raw_speech"					)
					else:
									lowerCamelCase_         =     kwargs.pop("audio"		,    lowercase					)
					lowerCamelCase_         =     kwargs.pop("sampling_rate"		,    lowercase					)
					lowerCamelCase_         =     kwargs.pop("text"		,    lowercase					)
					if len(lowercase					) > 0:
									lowerCamelCase_         =     args[0]
									lowerCamelCase_         =     args[1:]

					if audio is None and text is None:
									raise ValueError("You need to specify either an `audio` or `text` input to process."					)

					if audio is not None:
									lowerCamelCase_         =     self.feature_extractor(lowercase		,    *lowercase		,    sampling_rate=lowercase		,    **lowercase					)
					if text is not None:
									lowerCamelCase_         =     self.tokenizer(lowercase		,    **lowercase					)

					if text is None:
									return inputs
					elif audio is None:
									return encodings
					else:
									lowerCamelCase_         =     encodings["input_ids"]
									return inputs



	def        SCREAMING_SNAKE_CASE_(       self		,    *lowercase		,    **lowercase					)							->			Optional[Any]:
					return self.tokenizer.batch_decode(*lowercase		,    **lowercase					)



	def        SCREAMING_SNAKE_CASE_(       self		,    *lowercase		,    **lowercase					)							->			Optional[Any]:
					# For backward compatibility
					if self._in_target_context_manager:
									return self.current_processor.pad(*lowercase		,    **lowercase					)

					lowerCamelCase_         =     kwargs.pop("input_features"		,    lowercase					)
					lowerCamelCase_         =     kwargs.pop("labels"		,    lowercase					)
					if len(lowercase					) > 0:
									lowerCamelCase_         =     args[0]
									lowerCamelCase_         =     args[1:]

					if input_features is not None:
									lowerCamelCase_         =     self.feature_extractor.pad(lowercase		,    *lowercase		,    **lowercase					)
					if labels is not None:
									lowerCamelCase_         =     self.tokenizer.pad(lowercase		,    **lowercase					)

					if labels is None:
									return input_features
					elif input_features is None:
									return labels
					else:
									lowerCamelCase_         =     labels["input_ids"]
									return input_features



	def        SCREAMING_SNAKE_CASE_(       self		,    *lowercase		,    **lowercase					)							->			List[str]:
					return self.tokenizer.decode(*lowercase		,    **lowercase					)



	@contextmanager
	def        SCREAMING_SNAKE_CASE_(       self					)							->			Optional[int]:
					warnings.warn(
					    "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
					    "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
					    "your audio inputs, or in a separate call."					)
					lowerCamelCase_         =     True
					lowerCamelCase_         =     self.tokenizer
					yield
					lowerCamelCase_         =     self.feature_extractor
					lowerCamelCase_         =     False


