import random
import unittest

import torch

from diffusers import IFImgaImgPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin






@skip_mps
class 		a__						( A__   ,		A__   ,		unittest.TestCase ):
     A     	=							IFImgaImgPipeline
     A     	=							TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'width', 'height'}
     A     	=							TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     A     	=							PipelineTesterMixin.required_optional_params - {'latents'}

     def     __UpperCamelCase       (   self				:	List[Any]   ):







       """simple docstring"""






       return self._get_dummy_components()

     def     __UpperCamelCase       (   self				:	List[str],_A				:	List[str],_A				:	List[Any]=0   ):







       """simple docstring"""






       if str(_A   ).startswith("mps"   ):
         SCREAMING_SNAKE_CASE_  :		List[Any]    				=   torch.manual_seed(_A   )
       else:
         SCREAMING_SNAKE_CASE_  :		Any    				=   torch.Generator(device=_A   ).manual_seed(_A   )

       SCREAMING_SNAKE_CASE_  :		int    				=   floats_tensor((1, 3, 32, 32),rng=random.Random(_A   )   ).to(_A   )

       SCREAMING_SNAKE_CASE_  :		Tuple    				=   {
           "prompt": "A painting of a squirrel eating a burger",
           "image": image,
           "generator": generator,
           "num_inference_steps": 2,
           "output_type": "numpy",
       }

       return inputs

     def     __UpperCamelCase       (   self				:	Any   ):







       """simple docstring"""






       self._test_save_load_optional_components()

     @unittest.skipIf(
         torch_device != "cuda" or not is_xformers_available(),reason="XFormers attention is only available with CUDA and `xformers` installed",)
     def     __UpperCamelCase       (   self				:	Any   ):







       """simple docstring"""






       self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )

     @unittest.skipIf(torch_device != "cuda",reason="float16 requires CUDA"   )
     def     __UpperCamelCase       (   self				:	Tuple   ):







       """simple docstring"""






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

     @unittest.skipIf(torch_device != "cuda",reason="float16 requires CUDA"   )
     def     __UpperCamelCase       (   self				:	Optional[Any]   ):







       """simple docstring"""






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

     def     __UpperCamelCase       (   self				:	int   ):







       """simple docstring"""






       self._test_attention_slicing_forward_pass(expected_max_diff=1E-2   )

     def     __UpperCamelCase       (   self				:	str   ):







       """simple docstring"""






       self._test_save_load_local()






     def     __UpperCamelCase       (   self				:	Any   ):







       """simple docstring"""






       self._test_inference_batch_single_identical(
           expected_max_diff=1E-2,)

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


__lowerCamelCase :     List[Any]          =  False






@nightly
@require_torch_gpu
class 		a__						( unittest.TestCase ):

					def     __UpperCamelCase       (   self				:	Optional[int]   ):







							"""simple docstring"""






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

					def     __UpperCamelCase       (   self				:	Optional[Any]   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int    				=   VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"   )
							# remove text_unet
							pipe.remove_unused_weights()
							pipe.to(_A   )
							pipe.set_progress_bar_config(disable=_A   )

							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"   )

							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   torch.manual_seed(0   )
							SCREAMING_SNAKE_CASE_  :		int    				=   pipe(
							    prompt="first prompt",image=_A,text_to_image_strength=0.75,generator=_A,guidance_scale=7.5,num_inference_steps=2,output_type="numpy",).images

							with tempfile.TemporaryDirectory() as tmpdirname:
									pipe.save_pretrained(_A   )
									SCREAMING_SNAKE_CASE_  :		Dict    				=   VersatileDiffusionDualGuidedPipeline.from_pretrained(_A   )

							pipe.to(_A   )
							pipe.set_progress_bar_config(disable=_A   )

							SCREAMING_SNAKE_CASE_  :		List[str]    				=   generator.manual_seed(0   )
							SCREAMING_SNAKE_CASE_  :		str    				=   pipe(
							    prompt="first prompt",image=_A,text_to_image_strength=0.75,generator=_A,guidance_scale=7.5,num_inference_steps=2,output_type="numpy",).images

							assert np.abs(image - new_image   ).sum() < 1E-5, "Models don't have the same forward pass"






					def     __UpperCamelCase       (   self				:	Any   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any    				=   VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"   )
							pipe.remove_unused_weights()
							pipe.to(_A   )
							pipe.set_progress_bar_config(disable=_A   )

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   "cyberpunk 2077"
							SCREAMING_SNAKE_CASE_  :		Dict    				=   load_image(
							    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"   )
							SCREAMING_SNAKE_CASE_  :		Tuple    				=   torch.manual_seed(0   )
							SCREAMING_SNAKE_CASE_  :		Dict    				=   pipe(
							    prompt=_A,image=_A,text_to_image_strength=0.75,generator=_A,guidance_scale=7.5,num_inference_steps=50,output_type="numpy",).images

							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   image[0, 253:256, 253:256, -1]

							assert image.shape == (1, 512, 512, 3)
							SCREAMING_SNAKE_CASE_  :		Optional[Any]    				=   np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]   )

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