


"""simple docstring"""


# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
A	:  Optional[Any]            =     abspath(join(dirname(__file__), "src"))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="ignore", category=FutureWarning)


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			config.addinivalue_line(
			    "markers"       ,					"is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested" )
			config.addinivalue_line(
			    "markers"       ,					"is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested" )
			config.addinivalue_line("markers"       ,					"is_pipeline_test: mark test to run only when pipelines are tested" )
			config.addinivalue_line("markers"       ,					"is_staging_test: mark test to run only in the staging environment" )
			config.addinivalue_line("markers"       ,					"accelerate_tests: mark test that require accelerate" )
			config.addinivalue_line("markers"       ,					"tool_tests: mark the tool tests that are run on their specific schedule" )


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			from transformers.testing_utils import pytest_addoption_shared

			pytest_addoption_shared(_UpperCamelCase )


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			from transformers.testing_utils import pytest_terminal_summary_main

			__lowerCAmelCase							       =     terminalreporter.config.getoption("--make-reports" )
			if make_reports:
						pytest_terminal_summary_main(_UpperCamelCase       ,					id=_UpperCamelCase )


def _lowerCamelCase      (						_UpperCamelCase       ,					_UpperCamelCase ):

			'''simple docstring'''




			if exitstatus == 5:
						__lowerCAmelCase							       =     0


# Doctest custom flag to ignore output.
A	:  str            =     doctest.register_optionflag("IGNORE_RESULT")

A	:  Union[str, Any]            =     doctest.OutputChecker
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										if IGNORE_RESULT & optionflags:
													return True
										return OutputChecker.check_output(self       ,  __a       ,  __a       ,  __a      )
A	:  Union[str, Any]            =     CustomOutputChecker
A	:  Any            =     HfDoctestModule


A	:  Any            =     HfDocTestParser








"""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      snake_case							(	self      ):
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

										sd_pipe.set_scheduler("sample_euler"      )

										__lowerCAmelCase							       =     "A painting of a squirrel eating a burger"
										__lowerCAmelCase							       =     torch.manual_seed(0      )
										__lowerCAmelCase							       =     sd_pipe([prompt]       ,  generator=__a       ,  guidance_scale=9.0       ,  num_inference_steps=20       ,  output_type="np"      )

										__lowerCAmelCase							       =     output.images

										__lowerCAmelCase							       =     image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_12, 5_12, 3)
										__lowerCAmelCase							       =     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      snake_case							(	self      ):
										__lowerCAmelCase							       =     StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

										sd_pipe.set_scheduler("sample_euler"      )

										__lowerCAmelCase							       =     "A painting of a squirrel eating a burger"
										__lowerCAmelCase							       =     torch.manual_seed(0      )
										__lowerCAmelCase							       =     sd_pipe([prompt]       ,  generator=__a       ,  guidance_scale=9.0       ,  num_inference_steps=20       ,  output_type="np"      )

										__lowerCAmelCase							       =     output.images

										__lowerCAmelCase							       =     image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_12, 5_12, 3)
										__lowerCAmelCase							       =     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      snake_case							(	self      ):
										__lowerCAmelCase							       =     StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"      )
										__lowerCAmelCase							       =     sd_pipe.to(__a      )
										sd_pipe.set_progress_bar_config(disable=__a      )

										sd_pipe.set_scheduler("sample_dpmpp_2m"      )

										__lowerCAmelCase							       =     "A painting of a squirrel eating a burger"
										__lowerCAmelCase							       =     torch.manual_seed(0      )
										__lowerCAmelCase							       =     sd_pipe(
										    [prompt]       ,  generator=__a       ,  guidance_scale=7.5       ,  num_inference_steps=15       ,  output_type="np"       ,  use_karras_sigmas=__a       ,  )

										__lowerCAmelCase							       =     output.images

										__lowerCAmelCase							       =     image[0, -3:, -3:, -1]

										assert image.shape == (1, 5_12, 5_12, 3)
										__lowerCAmelCase							       =     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




