






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

	def      __UpperCamelCase							(      self   :						Optional[Any]):
			# clean up the VRAM after each test
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()

	def      __UpperCamelCase							(      self   :						Tuple):
			UpperCamelCase__				:					Optional[int]					=					StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4')
			UpperCamelCase__				:					Optional[int]					=					sd_pipe.to(UpperCAmelCase_)
			sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_)

			sd_pipe.set_scheduler('sample_euler')

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

			UpperCamelCase__				:					Dict					=					output.images

			UpperCamelCase__				:					Union[str, Any]					=					image[0, -3:, -3:, -1]

			assert image.shape == (1, 512, 512, 3)
			UpperCamelCase__				:					List[str]					=					np.array([0.04_47, 0.04_92, 0.04_68, 0.04_08, 0.03_83, 0.04_08, 0.03_54, 0.03_80, 0.03_39])

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

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

			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__				:					Any					=					sd_pipe([prompt]  ,					generator=UpperCAmelCase_  ,					guidance_scale=9.0  ,					num_inference_steps=20  ,					output_type='np')

			UpperCamelCase__				:					Optional[int]					=					output.images

			UpperCamelCase__				:					Tuple					=					image[0, -3:, -3:, -1]

			assert image.shape == (1, 512, 512, 3)
			UpperCamelCase__				:					str					=					np.array([0.12_37, 0.13_20, 0.14_38, 0.13_59, 0.13_90, 0.11_32, 0.12_77, 0.11_75, 0.11_12])

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





	def      __UpperCamelCase							(      self   :						Optional[int]):
			UpperCamelCase__				:					Tuple					=					StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base')
			UpperCamelCase__				:					Tuple					=					sd_pipe.to(UpperCAmelCase_)
			sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_)

			sd_pipe.set_scheduler('sample_dpmpp_2m')

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

			UpperCamelCase__				:					List[Any]					=					output.images

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

			assert image.shape == (1, 512, 512, 3)
			UpperCamelCase__				:					Union[str, Any]					=					np.array(
			    [0.11_38_16_89, 0.12_11_29_21, 0.1_38_94_57, 0.12_54_96_06, 0.1_24_49_64, 0.10_83_15_17, 0.11_56_28_66, 0.10_86_78_16, 0.10_49_90_48])

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












'''simple docstring'''




import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__          =						logging.get_logger(__name__)

lowerCAmelCase__          =						{
    'nvidia/segformer-b0-finetuned-ade-512-512': (
        'https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''segformer'''

	def __init__(      self   :						Tuple  ,					UpperCAmelCase_   :						Optional[Any]=3  ,					UpperCAmelCase_   :						Optional[int]=4  ,					UpperCAmelCase_   :						Tuple=[2, 2, 2, 2]  ,					UpperCAmelCase_   :						List[str]=[8, 4, 2, 1]  ,					UpperCAmelCase_   :						Union[str, Any]=[32, 64, 160, 256]  ,					UpperCAmelCase_   :						Any=[7, 3, 3, 3]  ,					UpperCAmelCase_   :						Any=[4, 2, 2, 2]  ,					UpperCAmelCase_   :						Union[str, Any]=[1, 2, 5, 8]  ,					UpperCAmelCase_   :						Tuple=[4, 4, 4, 4]  ,					UpperCAmelCase_   :						str="gelu"  ,					UpperCAmelCase_   :						List[Any]=0.0  ,					UpperCAmelCase_   :						int=0.0  ,					UpperCAmelCase_   :						int=0.1  ,					UpperCAmelCase_   :						List[str]=0.02  ,					UpperCAmelCase_   :						Dict=0.1  ,					UpperCAmelCase_   :						Dict=1e-6  ,					UpperCAmelCase_   :						int=256  ,					UpperCAmelCase_   :						Optional[int]=255  ,					**UpperCAmelCase_   :						Tuple  ,					):
			super().__init__(**UpperCAmelCase_)

			if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
					warnings.warn(
					    'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
					    ' removed, as the behaviour will default to that of reshape_last_stage = True.'  ,					UpperCAmelCase_  ,					)

			UpperCamelCase__				:					List[Any]					=					num_channels
			UpperCamelCase__				:					Any					=					num_encoder_blocks
			UpperCamelCase__				:					Dict					=					depths
			UpperCamelCase__				:					int					=					sr_ratios
			UpperCamelCase__				:					str					=					hidden_sizes
			UpperCamelCase__				:					List[str]					=					patch_sizes
			UpperCamelCase__				:					Optional[int]					=					strides
			UpperCamelCase__				:					Dict					=					mlp_ratios
			UpperCamelCase__				:					List[str]					=					num_attention_heads
			UpperCamelCase__				:					int					=					hidden_act
			UpperCamelCase__				:					Any					=					hidden_dropout_prob
			UpperCamelCase__				:					str					=					attention_probs_dropout_prob
			UpperCamelCase__				:					List[str]					=					classifier_dropout_prob
			UpperCamelCase__				:					List[Any]					=					initializer_range
			UpperCamelCase__				:					Union[str, Any]					=					drop_path_rate
			UpperCamelCase__				:					int					=					layer_norm_eps
			UpperCamelCase__				:					Dict					=					decoder_hidden_size
			UpperCamelCase__				:					List[Any]					=					kwargs.get('reshape_last_stage'  ,					UpperCAmelCase_)
			UpperCamelCase__				:					List[str]					=					semantic_loss_ignore_index







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  version.parse('''1.11'''    )

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return OrderedDict(
			    [
			        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
			    ])

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return 1e-4





	@property
	def      __UpperCamelCase							(      self   :						Any):
			return 12




