
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




@skip_mps
class     _SCREAMING_SNAKE_CASE     (      snake_case_			,       snake_case_			,       unittest.TestCase						):
	lowerCAmelCase__         = StableDiffusionPanoramaPipeline
	lowerCAmelCase__         = TEXT_TO_IMAGE_PARAMS
	lowerCAmelCase__         = TEXT_TO_IMAGE_BATCH_PARAMS
	lowerCAmelCase__         = TEXT_TO_IMAGE_IMAGE_PARAMS
	lowerCAmelCase__         = TEXT_TO_IMAGE_IMAGE_PARAMS



	def     SCREAMING_SNAKE_CASE_(    self )				->       List[Any]:
					torch.manual_seed(0 )
					lowerCamelCase_						=			UNetaDConditionModel(
					    block_out_channels=(32, 64)						,  layers_per_block=1						,  sample_size=32						,  in_channels=4						,  out_channels=4						,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")						,  up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")						,  cross_attention_dim=32						,  )
					lowerCamelCase_						=			DDIMScheduler()
					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[str]:
					lowerCamelCase_						=			torch.manual_seed(lowercase )
					lowerCamelCase_						=			{
					    "prompt": "a photo of the dolomites",
					    "generator": generator,
					    # Setting height and width to None to prevent OOMs on CPU.
					    "height": None,
					    "width": None,
					    "num_inference_steps": 1,
					    "guidance_scale": 6.0,
					    "output_type": "numpy",
					}
					return inputs



	def     SCREAMING_SNAKE_CASE_(    self )				->       List[str]:
					lowerCamelCase_						=			"cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_						=			self.get_dummy_components()
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline(**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, 64, 64, 3)

					lowerCamelCase_						=			np.array([0.6_1_8_6, 0.5_3_7_4, 0.4_9_1_5, 0.4_1_3_5, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_7, 0.4_7_5_7] )

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       int:
					super().test_inference_batch_consistent(batch_sizes=[1, 2] )



	def     SCREAMING_SNAKE_CASE_(    self )				->       Tuple:
					super().test_inference_batch_single_identical(batch_size=2						,  expected_max_diff=3.25e-3 )



	def     SCREAMING_SNAKE_CASE_(    self )				->       Union[str, Any]:
					lowerCamelCase_						=			"cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_						=			self.get_dummy_components()
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline(**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, 64, 64, 3)

					lowerCamelCase_						=			np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7] )

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



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

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

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

					lowerCamelCase_						=			np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7] )

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       Union[str, 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_						=			StableDiffusionPanoramaPipeline(**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, 64, 64, 3)

					lowerCamelCase_						=			np.array([0.4_0_2_4, 0.6_5_1_0, 0.4_9_0_1, 0.5_3_7_8, 0.5_8_1_3, 0.5_6_2_2, 0.4_7_9_5, 0.4_4_6_7, 0.4_9_5_2] )

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       Union[str, Any]:
					lowerCamelCase_						=			"cpu"  # ensure determinism for the device-dependent torch.Generator
					lowerCamelCase_						=			self.get_dummy_components()
					lowerCamelCase_						=			PNDMScheduler(
					    beta_start=0.0_0_0_8_5						,  beta_end=0.0_1_2						,  beta_schedule="scaled_linear"						,  skip_prk_steps=lowercase )
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline(**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, 64, 64, 3)

					lowerCamelCase_						=			np.array([0.6_3_9_1, 0.6_2_9_1, 0.4_8_6_1, 0.5_1_3_4, 0.5_5_5_2, 0.4_5_7_8, 0.5_0_3_2, 0.5_0_2_3, 0.4_5_3_9] )

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




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



	def     SCREAMING_SNAKE_CASE_(    self )				->       Tuple:
					super().tearDown()
					gc.collect()
					torch.cuda.empty_cache()



	def     SCREAMING_SNAKE_CASE_(    self						,  lowercase=0 )				->       Any:
					lowerCamelCase_						=			torch.manual_seed(lowercase )
					lowerCamelCase_						=			{
					    "prompt": "a photo of the dolomites",
					    "generator": generator,
					    "num_inference_steps": 3,
					    "guidance_scale": 7.5,
					    "output_type": "numpy",
					}
					return inputs



	def     SCREAMING_SNAKE_CASE_(    self )				->       Dict:
					lowerCamelCase_						=			"stabilityai/stable-diffusion-2-base"
					lowerCamelCase_						=			DDIMScheduler.from_pretrained(lowercase						,  subfolder="scheduler" )
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline.from_pretrained(lowercase						,  scheduler=lowercase						,  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, 2048, 3)

					lowerCamelCase_						=			np.array(
					    [
					        0.3_6_9_6_8_3_9_2,
					        0.2_7_0_2_5_3_7_2,
					        0.3_2_4_4_6_7_6_6,
					        0.2_8_3_7_9_3_8_7,
					        0.3_6_3_6_3_2_7_4,
					        0.3_0_7_3_3_3_4_7,
					        0.2_7_1_0_0_0_2_7,
					        0.2_7_0_5_4_1_2_5,
					        0.2_5_5_3_6_0_9_6,
					    ] )

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       List[str]:
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline.from_pretrained(
					    "stabilityai/stable-diffusion-2-base"						,  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, 2048, 3)

					lowerCamelCase_						=			np.array(
					    [
					        [
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					            0.0,
					        ]
					    ] )

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



	def     SCREAMING_SNAKE_CASE_(    self )				->       Any:
					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, 256)
													lowerCamelCase_						=			latents[0, -3:, -3:, -1]

													lowerCamelCase_						=			np.array(
													    [
													        0.1_8_6_8_1_8_6_9,
													        0.3_3_9_0_7_8_1_6,
													        0.5_3_6_1_2_7_6,
													        0.1_4_4_3_2_8_6_5,
													        -0.0_2_8_5_6_6_1_1,
													        -0.7_3_9_4_1_1_2_3,
													        0.2_3_3_9_7_9_8_7,
													        0.4_7_3_2_2_6_8_2,
													        -0.3_7_8_2_3_1_6_4,
													    ] )
													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, 256)
													lowerCamelCase_						=			latents[0, -3:, -3:, -1]

													lowerCamelCase_						=			np.array(
													    [
													        0.1_8_5_3_9_6_4_5,
													        0.3_3_9_8_7_2_4_8,
													        0.5_3_7_8_5_5_9,
													        0.1_4_4_3_7_1_4_2,
													        -0.0_2_4_5_5_2_6_1,
													        -0.7_3_3_8_3_1_7,
													        0.2_3_9_9_0_7_5_5,
													        0.4_7_3_5_6_2_7_2,
													        -0.3_7_8_6_5_0_5,
													    ] )

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

					lowerCamelCase_						=			False

					lowerCamelCase_						=			"stabilityai/stable-diffusion-2-base"
					lowerCamelCase_						=			DDIMScheduler.from_pretrained(lowercase						,  subfolder="scheduler" )
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline.from_pretrained(lowercase						,  scheduler=lowercase						,  safety_checker=lowercase )
					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 )				->       Tuple:
					torch.cuda.empty_cache()
					torch.cuda.reset_max_memory_allocated()
					torch.cuda.reset_peak_memory_stats()

					lowerCamelCase_						=			"stabilityai/stable-diffusion-2-base"
					lowerCamelCase_						=			DDIMScheduler.from_pretrained(lowercase						,  subfolder="scheduler" )
					lowerCamelCase_						=			StableDiffusionPanoramaPipeline.from_pretrained(lowercase						,  scheduler=lowercase						,  safety_checker=lowercase )
					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 5.2 GB is allocated
					assert mem_bytes < 5.5 * 10**9




from typing import Callable, List, Optional, Union

import PIL
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPSegForImageSegmentation,
    CLIPSegProcessor,
    CLIPTextModel,
    CLIPTokenizer,
)

from diffusers import DiffusionPipeline
from diffusers.configuration_utils import FrozenDict
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import deprecate, is_accelerate_available, logging


__A				=logging.get_logger(__name__)  # pylint: disable=invalid-name




class     _SCREAMING_SNAKE_CASE     (      snake_case_						):



	def __init__(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  lowercase						,  )				->       List[Any]:
					super().__init__()

					if hasattr(scheduler.config						,  "steps_offset" ) and scheduler.config.steps_offset != 1:
									lowerCamelCase_						=			(
									    f'The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`'
									    f' should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure '
									    "to update the config accordingly as leaving `steps_offset` might led to incorrect results"
									    " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
									    " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
									    " file"
									)
									deprecate("steps_offset!=1"						,  "1.0.0"						,  lowercase						,  standard_warn=lowercase )
									lowerCamelCase_						=			dict(scheduler.config )
									lowerCamelCase_						=			1
									lowerCamelCase_						=			FrozenDict(lowercase )

					if hasattr(scheduler.config						,  "skip_prk_steps" ) and scheduler.config.skip_prk_steps is False:
									lowerCamelCase_						=			(
									    f'The configuration file of this scheduler: {scheduler} has not set the configuration'
									    " `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make"
									    " sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to"
									    " incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face"
									    " Hub, it would be very nice if you could open a Pull request for the"
									    " `scheduler/scheduler_config.json` file"
									)
									deprecate("skip_prk_steps not set"						,  "1.0.0"						,  lowercase						,  standard_warn=lowercase )
									lowerCamelCase_						=			dict(scheduler.config )
									lowerCamelCase_						=			True
									lowerCamelCase_						=			FrozenDict(lowercase )

					if safety_checker is None:
									logger.warning(
									    f'You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure'
									    " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered"
									    " results in services or applications open to the public. Both the diffusers team and Hugging Face"
									    " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling"
									    " it only for use-cases that involve analyzing network behavior or auditing its results. For more"
									    " information, please have a look at https://github.com/huggingface/diffusers/pull/254 ." )

					self.register_modules(
					    segmentation_model=lowercase						,  segmentation_processor=lowercase						,  vae=lowercase						,  text_encoder=lowercase						,  tokenizer=lowercase						,  unet=lowercase						,  scheduler=lowercase						,  safety_checker=lowercase						,  feature_extractor=lowercase						,  )



	def     SCREAMING_SNAKE_CASE_(    self						,  lowercase = "auto" )				->       Tuple:
					if slice_size == "auto":
									# half the attention head size is usually a good trade-off between
									# speed and memory
									lowerCamelCase_						=			self.unet.config.attention_head_dim // 2
					self.unet.set_attention_slice(lowercase )



	def     SCREAMING_SNAKE_CASE_(    self )				->       List[Any]:
					self.enable_attention_slicing(lowercase )



	def     SCREAMING_SNAKE_CASE_(    self )				->       str:
					if is_accelerate_available():
									from accelerate import cpu_offload
					else:
									raise ImportError("Please install accelerate via `pip install accelerate`" )

					lowerCamelCase_						=			torch.device("cuda" )

					for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]:
									if cpu_offloaded_model is not None:
													cpu_offload(lowercase						,  lowercase )



	@property
	# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
	def     SCREAMING_SNAKE_CASE_(    self )				->       Union[str, Any]:
					if self.device != torch.device("meta" ) or not hasattr(self.unet						,  "_hf_hook" ):
									return self.device
					for module in self.unet.modules():
									if (
									    hasattr(lowercase						,  "_hf_hook" )
									    and hasattr(module._hf_hook						,  "execution_device" )
									    and module._hf_hook.execution_device is not None
									):
													return torch.device(module._hf_hook.execution_device )
					return self.device



	@torch.no_grad()
	def __call__(    self						,  lowercase						,  lowercase						,  lowercase						,  lowercase = 512						,  lowercase = 512						,  lowercase = 50						,  lowercase = 7.5						,  lowercase = None						,  lowercase = 1						,  lowercase = 0.0						,  lowercase = None						,  lowercase = None						,  lowercase = "pil"						,  lowercase = True						,  lowercase = None						,  lowercase = 1						,  **lowercase						,  )				->       int:
					lowerCamelCase_						=			self.segmentation_processor(
					    text=[text]						,  images=[image]						,  padding="max_length"						,  return_tensors="pt" ).to(self.device )
					lowerCamelCase_						=			self.segmentation_model(**lowercase )
					lowerCamelCase_						=			torch.sigmoid(outputs.logits ).cpu().detach().unsqueeze(-1 ).numpy()
					lowerCamelCase_						=			self.numpy_to_pil(lowercase )[0].resize(image.size )

					# Run inpainting pipeline with the generated mask
					lowerCamelCase_						=			StableDiffusionInpaintPipeline(
					    vae=self.vae						,  text_encoder=self.text_encoder						,  tokenizer=self.tokenizer						,  unet=self.unet						,  scheduler=self.scheduler						,  safety_checker=self.safety_checker						,  feature_extractor=self.feature_extractor						,  )
					return inpainting_pipeline(
					    prompt=lowercase						,  image=lowercase						,  mask_image=lowercase						,  height=lowercase						,  width=lowercase						,  num_inference_steps=lowercase						,  guidance_scale=lowercase						,  negative_prompt=lowercase						,  num_images_per_prompt=lowercase						,  eta=lowercase						,  generator=lowercase						,  latents=lowercase						,  output_type=lowercase						,  return_dict=lowercase						,  callback=lowercase						,  callback_steps=lowercase						,  )


