



import gc
import tempfile
import unittest

import numpy as np
import torch

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


a__   :					Tuple 						=      False



class    UpperCAmelCase__(				unittest.TestCase						):

   '''simple docstring'''




   pass



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

   '''simple docstring'''




   def       UpperCAmelCase		(    self	:      Optional[Any])						->					List[Any]:




         """simple docstring"""

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


   def       UpperCAmelCase		(    self	:      Optional[Any])						->					List[Any]:




         """simple docstring"""

         lowercase__  =	VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'      ,					torch_dtype=torch.floataa)
         pipe.to(lowerCAmelCase)
         pipe.set_progress_bar_config(disable=lowerCAmelCase)

         lowercase__  =	load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg')

         lowercase__  =	torch.manual_seed(0)
         lowercase__  =	pipe.dual_guided(
             prompt='first prompt'      ,					image=lowerCAmelCase      ,					text_to_image_strength=0.75      ,					generator=lowerCAmelCase      ,					guidance_scale=7.5      ,					num_inference_steps=2      ,					output_type='numpy'      ,					).images

         with tempfile.TemporaryDirectory() as tmpdirname:
               pipe.save_pretrained(lowerCAmelCase)
               lowercase__  =	VersatileDiffusionPipeline.from_pretrained(lowerCAmelCase      ,					torch_dtype=torch.floataa)
         pipe.to(lowerCAmelCase)
         pipe.set_progress_bar_config(disable=lowerCAmelCase)

         lowercase__  =	generator.manual_seed(0)
         lowercase__  =	pipe.dual_guided(
             prompt='first prompt'      ,					image=lowerCAmelCase      ,					text_to_image_strength=0.75      ,					generator=lowerCAmelCase      ,					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	:      int)						->					Optional[int]:




         """simple docstring"""

         lowercase__  =	VersatileDiffusionPipeline.from_pretrained('shi-labs/versatile-diffusion'      ,					torch_dtype=torch.floataa)
         pipe.to(lowerCAmelCase)
         pipe.set_progress_bar_config(disable=lowerCAmelCase)

         lowercase__  =	'cyberpunk 2077'
         lowercase__  =	load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg')
         lowercase__  =	torch.manual_seed(0)
         lowercase__  =	pipe.dual_guided(
             prompt=lowerCAmelCase      ,					image=lowerCAmelCase      ,					text_to_image_strength=0.75      ,					generator=lowerCAmelCase      ,					guidance_scale=7.5      ,					num_inference_steps=50      ,					output_type='numpy'      ,					).images

         lowercase__  =	image[0, 2_53:2_56, 2_53:2_56, -1]

         assert image.shape == (1, 5_12, 5_12, 3)
         lowercase__  =	np.array([0.14_48, 0.16_19, 0.17_41, 0.10_86, 0.11_47, 0.11_28, 0.11_99, 0.11_65, 0.10_01])

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

         lowercase__  =	'A painting of a squirrel eating a burger '
         lowercase__  =	torch.manual_seed(0)
         lowercase__  =	pipe.text_to_image(
             prompt=lowerCAmelCase      ,					generator=lowerCAmelCase      ,					guidance_scale=7.5      ,					num_inference_steps=50      ,					output_type='numpy').images

         lowercase__  =	image[0, 2_53:2_56, 2_53:2_56, -1]

         assert image.shape == (1, 5_12, 5_12, 3)
         lowercase__  =	np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78])

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

         lowercase__  =	pipe.image_variation(lowerCAmelCase      ,					generator=lowerCAmelCase      ,					output_type='numpy').images

         lowercase__  =	image[0, 2_53:2_56, 2_53:2_56, -1]

         assert image.shape == (1, 5_12, 5_12, 3)
         lowercase__  =	np.array([0.30_76, 0.31_23, 0.32_84, 0.37_82, 0.37_70, 0.38_94, 0.42_97, 0.43_31, 0.44_56])

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





import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
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    UpperCAmelCase__(				lowerCamelCase  ,				lowerCamelCase  ,				unittest.TestCase						):

   '''simple docstring'''




   A     :	str												=     IFImgaImgSuperResolutionPipeline
   A     :	Dict												=     TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
   A     :	Any												=     TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"}						)
   A     :	str												=     PipelineTesterMixin.required_optional_params - {"latents"}


   def       UpperCAmelCase		(    self	:      Dict)						->					Dict:




         """simple docstring"""

         return self._get_superresolution_dummy_components()


   def       UpperCAmelCase		(    self	:      Optional[Any]      ,					lowerCAmelCase	:      Tuple      ,					lowerCAmelCase	:      Union[str, Any]=0)						->					str:




         """simple docstring"""

         if str(lowerCAmelCase).startswith('mps'):
               lowercase__  =	torch.manual_seed(lowerCAmelCase)
         else:
               lowercase__  =	torch.Generator(device=lowerCAmelCase).manual_seed(lowerCAmelCase)

         lowercase__  =	floats_tensor((1, 3, 32, 32)      ,					rng=random.Random(lowerCAmelCase)).to(lowerCAmelCase)
         lowercase__  =	floats_tensor((1, 3, 16, 16)      ,					rng=random.Random(lowerCAmelCase)).to(lowerCAmelCase)

         lowercase__  =	{
             'prompt': 'A painting of a squirrel eating a burger',
             'image': image,
             'original_image': original_image,
             'generator': generator,
             'num_inference_steps': 2,
             'output_type': 'numpy',
         }

         return inputs


   @unittest.skipIf(
       torch_device != 'cuda' or not is_xformers_available()      ,					reason='XFormers attention is only available with CUDA and `xformers` installed'      ,					)
   def       UpperCAmelCase		(    self	:      str)						->					Dict:




         """simple docstring"""

         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3)


   def       UpperCAmelCase		(    self	:      List[Any])						->					Dict:




         """simple docstring"""

         self._test_save_load_optional_components()


   @unittest.skipIf(torch_device != 'cuda'      ,					reason='float16 requires CUDA')
   def       UpperCAmelCase		(    self	:      Optional[Any])						->					List[Any]:




         """simple docstring"""

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


   def       UpperCAmelCase		(    self	:      str)						->					Union[str, Any]:




         """simple docstring"""

         self._test_attention_slicing_forward_pass(expected_max_diff=1E-2)


   def       UpperCAmelCase		(    self	:      Tuple)						->					Tuple:




         """simple docstring"""

         self._test_save_load_local()



   def       UpperCAmelCase		(    self	:      List[str])						->					Tuple:




         """simple docstring"""

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