

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





					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Any									= StableDiffusionKDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"						)
						UpperCAmelCase__:	List[Any]									= sd_pipe.to(SCREAMING_SNAKE_CASE__						)
						sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

						sd_pipe.set_scheduler("sample_euler"						)

						UpperCAmelCase__:	int									= "A painting of a squirrel eating a burger"
						UpperCAmelCase__:	Optional[int]									= torch.manual_seed(0						)
						UpperCAmelCase__:	Dict									= sd_pipe([prompt]				,      generator=SCREAMING_SNAKE_CASE__				,      guidance_scale=9.0				,      num_inference_steps=2_0				,      output_type="np"						)

						UpperCAmelCase__:	Optional[Any]									= output.images

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

						assert image.shape == (1, 5_1_2, 5_1_2, 3)
						UpperCAmelCase__:	Dict									= np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]						)

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





					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Any									= StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
						UpperCAmelCase__:	Dict									= sd_pipe.to(SCREAMING_SNAKE_CASE__						)
						sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

						sd_pipe.set_scheduler("sample_euler"						)

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

						UpperCAmelCase__:	Optional[int]									= output.images

						UpperCAmelCase__:	str									= image[0, -3:, -3:, -1]

						assert image.shape == (1, 5_1_2, 5_1_2, 3)
						UpperCAmelCase__:	Dict									= np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]						)

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





					def    _UpperCAmelCase    (				self						):
						UpperCAmelCase__:	Tuple									= StableDiffusionKDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"						)
						UpperCAmelCase__:	Dict									= sd_pipe.to(SCREAMING_SNAKE_CASE__						)
						sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__						)

						sd_pipe.set_scheduler("sample_dpmpp_2m"						)

						UpperCAmelCase__:	Union[str, Any]									= "A painting of a squirrel eating a burger"
						UpperCAmelCase__:	List[Any]									= torch.manual_seed(0						)
						UpperCAmelCase__:	List[str]									= sd_pipe(
						    [prompt]				,      generator=SCREAMING_SNAKE_CASE__				,      guidance_scale=7.5				,      num_inference_steps=1_5				,      output_type="np"				,      use_karras_sigmas=SCREAMING_SNAKE_CASE__				,      )

						UpperCAmelCase__:	Optional[Any]									= output.images

						UpperCAmelCase__:	Optional[int]									= image[0, -3:, -3:, -1]

						assert image.shape == (1, 5_1_2, 5_1_2, 3)
						UpperCAmelCase__:	Tuple									= np.array(
						    [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]						)

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




"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


UpperCamelCase     =							{
    """configuration_perceiver""": ["""PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """PerceiverConfig""", """PerceiverOnnxConfig"""],
    """tokenization_perceiver""": ["""PerceiverTokenizer"""],
}

try:
			if not is_vision_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCamelCase     =							["""PerceiverFeatureExtractor"""]
			UpperCamelCase     =							["""PerceiverImageProcessor"""]

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			UpperCamelCase     =							[
			    """PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST""",
			    """PerceiverForImageClassificationConvProcessing""",
			    """PerceiverForImageClassificationFourier""",
			    """PerceiverForImageClassificationLearned""",
			    """PerceiverForMaskedLM""",
			    """PerceiverForMultimodalAutoencoding""",
			    """PerceiverForOpticalFlow""",
			    """PerceiverForSequenceClassification""",
			    """PerceiverLayer""",
			    """PerceiverModel""",
			    """PerceiverPreTrainedModel""",
			]


if TYPE_CHECKING:
			from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig
			from .tokenization_perceiver import PerceiverTokenizer

			try:
						if not is_vision_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .feature_extraction_perceiver import PerceiverFeatureExtractor
						from .image_processing_perceiver import PerceiverImageProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_perceiver import (
						    PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST,
						    PerceiverForImageClassificationConvProcessing,
						    PerceiverForImageClassificationFourier,
						    PerceiverForImageClassificationLearned,
						    PerceiverForMaskedLM,
						    PerceiverForMultimodalAutoencoding,
						    PerceiverForOpticalFlow,
						    PerceiverForSequenceClassification,
						    PerceiverLayer,
						    PerceiverModel,
						    PerceiverPreTrainedModel,
						)

else:
			import sys

			UpperCamelCase     =							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




