


'''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   UpperCAmelCase_    (unittest.TestCase    ):





    """simple docstring"""






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







    def  lowercase_							(				self      )							->	Optional[Any]:
         __lowerCamelCase  :      str														=						StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4'      )
         __lowerCamelCase  :      Dict														=						sd_pipe.to(SCREAMING_SNAKE_CASE_      )
         sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_      )

         sd_pipe.set_scheduler('sample_euler'      )

         __lowerCamelCase  :      int														=						'A painting of a squirrel eating a burger'
         __lowerCamelCase  :      Tuple														=						torch.manual_seed(0      )
         __lowerCamelCase  :      Tuple														=						sd_pipe([prompt]     ,	generator=SCREAMING_SNAKE_CASE_     ,	guidance_scale=9.0     ,	num_inference_steps=20     ,	output_type='np'      )

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

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

         assert image.shape == (1, 5_12, 5_12, 3)
         __lowerCamelCase  :      List[str]														=						np.array([0.0_4_4_7, 0.0_4_9_2, 0.0_4_6_8, 0.0_4_0_8, 0.0_3_8_3, 0.0_4_0_8, 0.0_3_5_4, 0.0_3_8_0, 0.0_3_3_9]      )

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







    def  lowercase_							(				self      )							->	Dict:
         __lowerCamelCase  :      List[Any]														=						StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'      )
         __lowerCamelCase  :      List[Any]														=						sd_pipe.to(SCREAMING_SNAKE_CASE_      )
         sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_      )

         sd_pipe.set_scheduler('sample_euler'      )

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

         __lowerCamelCase  :      List[str]														=						output.images

         __lowerCamelCase  :      List[str]														=						image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_12, 5_12, 3)
         __lowerCamelCase  :      Tuple														=						np.array([0.1_2_3_7, 0.1_3_2_0, 0.1_4_3_8, 0.1_3_5_9, 0.1_3_9_0, 0.1_1_3_2, 0.1_2_7_7, 0.1_1_7_5, 0.1_1_1_2]      )

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







    def  lowercase_							(				self      )							->	List[Any]:
         __lowerCamelCase  :      List[Any]														=						StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base'      )
         __lowerCamelCase  :      Dict														=						sd_pipe.to(SCREAMING_SNAKE_CASE_      )
         sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_      )

         sd_pipe.set_scheduler('sample_dpmpp_2m'      )

         __lowerCamelCase  :      Any														=						'A painting of a squirrel eating a burger'
         __lowerCamelCase  :      Optional[Any]														=						torch.manual_seed(0      )
         __lowerCamelCase  :      Optional[int]														=						sd_pipe(
             [prompt]     ,	generator=SCREAMING_SNAKE_CASE_     ,	guidance_scale=7.5     ,	num_inference_steps=15     ,	output_type='np'     ,	use_karras_sigmas=SCREAMING_SNAKE_CASE_     ,	)

         __lowerCamelCase  :      Optional[Any]														=						output.images

         __lowerCamelCase  :      Any														=						image[0, -3:, -3:, -1]

         assert image.shape == (1, 5_12, 5_12, 3)
         __lowerCamelCase  :      List[Any]														=						np.array(
             [0.1_1_3_8_1_6_8_9, 0.1_2_1_1_2_9_2_1, 0.1_3_8_9_4_5_7, 0.1_2_5_4_9_6_0_6, 0.1_2_4_4_9_6_4, 0.1_0_8_3_1_5_1_7, 0.1_1_5_6_2_8_6_6, 0.1_0_8_6_7_8_1_6, 0.1_0_4_9_9_0_4_8]      )

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










'''simple docstring'''



from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

A__						:   Dict				=    [
    """python""",
    """tqdm""",
    """regex""",
    """requests""",
    """packaging""",
    """filelock""",
    """numpy""",
    """tokenizers""",
    """huggingface-hub""",
    """safetensors""",
    """accelerate""",
    """pyyaml""",
]

for pkg in pkgs_to_check_at_runtime:
      if pkg in deps:
            if pkg == "tokenizers":
                  # must be loaded here, or else tqdm check may fail
                  from .utils import is_tokenizers_available

                  if not is_tokenizers_available():
                        continue  # not required, check version only if installed
            elif pkg == "accelerate":
                  # must be loaded here, or else tqdm check may fail
                  from .utils import is_accelerate_available

                  # Maybe switch to is_torch_available in the future here so that Accelerate is hard dep of
                  # Transformers with PyTorch
                  if not is_accelerate_available():
                        continue  # not required, check version only if installed

            require_version_core(deps[pkg])
      else:
            raise ValueError(f'''can\'t find {pkg} in {deps.keys()}, check dependency_versions_table.py''')



def      UpperCAmelCase__   (      UpperCAmelCase_       :							List[Any] ,       UpperCAmelCase_       :							List[Any]=None				)	->						List[Any]:
     require_version(deps[pkg] ,       UpperCAmelCase_				)






