






'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






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




 def      __magic_name__	(      self	:							Any      )	->						Tuple:
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()




 def      __magic_name__	(      self	:							Optional[Any]      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=StableDiffusionKDiffusionPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4'''      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=sd_pipe.to(__lowercase      )
       sd_pipe.set_progress_bar_config(disable=__lowercase      )

       sd_pipe.set_scheduler('''sample_euler'''      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__     :				List[Any]									=torch.manual_seed(0      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=sd_pipe([prompt]					,      generator=__lowercase					,      guidance_scale=9.0					,      num_inference_steps=20					,      output_type='''np'''      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=output.images

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__     :				Any									=np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]      )

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




 def      __magic_name__	(      self	:							Dict      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''      )
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=sd_pipe.to(__lowercase      )
       sd_pipe.set_progress_bar_config(disable=__lowercase      )

       sd_pipe.set_scheduler('''sample_euler'''      )

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=torch.manual_seed(0      )
       SCREAMING_SNAKE_CASE__     :				int									=sd_pipe([prompt]					,      generator=__lowercase					,      guidance_scale=9.0					,      num_inference_steps=20					,      output_type='''np'''      )

       SCREAMING_SNAKE_CASE__     :				str									=output.images

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__     :				int									=np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]      )

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




 def      __magic_name__	(      self	:							str      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=StableDiffusionKDiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base'''      )
       SCREAMING_SNAKE_CASE__     :				Dict									=sd_pipe.to(__lowercase      )
       sd_pipe.set_progress_bar_config(disable=__lowercase      )

       sd_pipe.set_scheduler('''sample_dpmpp_2m'''      )

       SCREAMING_SNAKE_CASE__     :				Dict									='''A painting of a squirrel eating a burger'''
       SCREAMING_SNAKE_CASE__     :				int									=torch.manual_seed(0      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=sd_pipe(
           [prompt]					,      generator=__lowercase					,      guidance_scale=7.5					,      num_inference_steps=15					,      output_type='''np'''					,      use_karras_sigmas=__lowercase					,      )

       SCREAMING_SNAKE_CASE__     :				Optional[int]									=output.images

       SCREAMING_SNAKE_CASE__     :				List[Any]									=image[0, -3:, -3:, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       SCREAMING_SNAKE_CASE__     :				str									=np.array(
           [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048]      )

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







'''simple docstring'''
import inspect
import os
import sys
import unittest

import accelerate
from accelerate.test_utils import execute_subprocess_async, require_tpu






class      __SCREAMING_SNAKE_CASE      (			unittest.TestCase				):




 def      __magic_name__	(      self	:							int      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=inspect.getfile(accelerate.test_utils      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ['''scripts''', '''test_script.py''']      )
       SCREAMING_SNAKE_CASE__     :				Dict									=os.path.sep.join(inspect.getfile(self.__class__      ).split(os.path.sep      )[:-1]      )




 @require_tpu
 def      __magic_name__	(      self	:							int      )	->						Tuple:
       SCREAMING_SNAKE_CASE__     :				Any									=F"\n            {self.test_dir}/xla_spawn.py\n            --num_cores 8\n            {self.test_file_path}\n        ".split()
       SCREAMING_SNAKE_CASE__     :				List[str]									=[sys.executable] + distributed_args
       execute_subprocess_async(__lowercase					,      env=os.environ.copy()      )