






import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class 							snake_case_       (    __UpperCamelCase		,							__UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      StableDiffusionSAGPipeline
						snake_case__        =      TEXT_TO_IMAGE_PARAMS
						snake_case__        =      TEXT_TO_IMAGE_BATCH_PARAMS
						snake_case__        =      TEXT_TO_IMAGE_IMAGE_PARAMS
						snake_case__        =      TEXT_TO_IMAGE_IMAGE_PARAMS
						snake_case__        =      False



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Dict:




											'''simple docstring'''



											torch.manual_seed(0   )
											__a					:						List[str]      =					UNetaDConditionModel(
											    block_out_channels=(32, 64) ,					layers_per_block=2 ,					sample_size=32 ,					in_channels=4 ,					out_channels=4 ,					down_block_types=("DownBlock2D", "CrossAttnDownBlock2D") ,					up_block_types=("CrossAttnUpBlock2D", "UpBlock2D") ,					cross_attention_dim=32 ,					)
											__a					:						Optional[Any]      =					DDIMScheduler(
											    beta_start=0.0_00_85 ,					beta_end=0.0_12 ,					beta_schedule="scaled_linear" ,					clip_sample=__UpperCAmelCase ,					set_alpha_to_one=__UpperCAmelCase ,					)
											torch.manual_seed(0   )
											__a					:						Dict      =					AutoencoderKL(
											    block_out_channels=[32, 64] ,					in_channels=3 ,					out_channels=3 ,					down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"] ,					up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"] ,					latent_channels=4 ,					)
											torch.manual_seed(0   )
											__a					:						List[Any]      =					CLIPTextConfig(
											    bos_token_id=0 ,					eos_token_id=2 ,					hidden_size=32 ,					intermediate_size=37 ,					layer_norm_eps=1E-05 ,					num_attention_heads=4 ,					num_hidden_layers=5 ,					pad_token_id=1 ,					vocab_size=1000 ,					)
											__a					:						Any      =					CLIPTextModel(__UpperCAmelCase   )
											__a					:						Tuple      =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"   )

											__a					:						Optional[Any]      =					{
											    "unet": unet,
											    "scheduler": scheduler,
											    "vae": vae,
											    "text_encoder": text_encoder,
											    "tokenizer": tokenizer,
											    "safety_checker": None,
											    "feature_extractor": None,
											}
											return components



						def        UpperCAmelCase__					(self:					Any ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					int=0   )    ->     Tuple:




											'''simple docstring'''



											if str(__UpperCAmelCase   ).startswith("mps"   ):
																__a					:						int      =					torch.manual_seed(__UpperCAmelCase   )
											else:
																__a					:						List[str]      =					torch.Generator(device=__UpperCAmelCase   ).manual_seed(__UpperCAmelCase   )
											__a					:						List[str]      =					{
											    "prompt": ".",
											    "generator": generator,
											    "num_inference_steps": 2,
											    "guidance_scale": 1.0,
											    "sag_scale": 1.0,
											    "output_type": "numpy",
											}
											return inputs



						def        UpperCAmelCase__					(self:					Dict   )    ->     Union[str, Any]:




											'''simple docstring'''



											super().test_inference_batch_single_identical(expected_max_diff=3E-3   )




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

						"""simple docstring"""



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Optional[int]:




											'''simple docstring'''



											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Tuple:




											'''simple docstring'''



											__a					:						List[str]      =					StableDiffusionSAGPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"   )
											__a					:						int      =					sag_pipe.to(__UpperCAmelCase   )
											sag_pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											__a					:						Dict      =					"."
											__a					:						int      =					torch.manual_seed(0   )
											__a					:						int      =					sag_pipe(
											    [prompt] ,					generator=__UpperCAmelCase ,					guidance_scale=7.5 ,					sag_scale=1.0 ,					num_inference_steps=20 ,					output_type="np"   )

											__a					:						Tuple      =					output.images

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

											assert image.shape == (1, 512, 512, 3)
											__a					:						Optional[Any]      =					np.array([0.15_68, 0.17_38, 0.16_95, 0.16_93, 0.15_07, 0.17_05, 0.15_47, 0.17_51, 0.19_49]   )

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



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     str:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"   )
											__a					:						Optional[Any]      =					sag_pipe.to(__UpperCAmelCase   )
											sag_pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											__a					:						Tuple      =					"."
											__a					:						str      =					torch.manual_seed(0   )
											__a					:						str      =					sag_pipe(
											    [prompt] ,					generator=__UpperCAmelCase ,					guidance_scale=7.5 ,					sag_scale=1.0 ,					num_inference_steps=20 ,					output_type="np"   )

											__a					:						int      =					output.images

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

											assert image.shape == (1, 512, 512, 3)
											__a					:						Tuple      =					np.array([0.34_59, 0.28_76, 0.25_37, 0.30_02, 0.26_71, 0.21_60, 0.30_26, 0.22_62, 0.23_71]   )

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



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Optional[int]      =					StableDiffusionSAGPipeline.from_pretrained("stabilityai/stable-diffusion-2-1-base"   )
											__a					:						str      =					sag_pipe.to(__UpperCAmelCase   )
											sag_pipe.set_progress_bar_config(disable=__UpperCAmelCase   )

											__a					:						Tuple      =					"."
											__a					:						Any      =					torch.manual_seed(0   )
											__a					:						str      =					sag_pipe(
											    [prompt] ,					width=768 ,					height=512 ,					generator=__UpperCAmelCase ,					guidance_scale=7.5 ,					sag_scale=1.0 ,					num_inference_steps=20 ,					output_type="np" ,					)

											__a					:						List[str]      =					output.images

											assert image.shape == (1, 512, 768, 3)













import copy
from typing import Dict, List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


UpperCAmelCase__      			=						{
    '''facebook/mask2former-swin-small-coco-instance''': (
        '''https://huggingface.co/facebook/mask2former-swin-small-coco-instance/blob/main/config.json'''
    )
    # See all Mask2Former models at https://huggingface.co/models?filter=mask2former
}

UpperCAmelCase__      			=						logging.get_logger(__name__)




class 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """mask2former"""
						snake_case__        =      ["""swin"""]
						snake_case__        =      {"""hidden_size""": """hidden_dim"""}



						def __init__(self:					Any ,					__UpperCAmelCase:					Optional[Dict] = None ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 256 ,					__UpperCAmelCase:					int = 1024 ,					__UpperCAmelCase:					str = "relu" ,					__UpperCAmelCase:					int = 6 ,					__UpperCAmelCase:					int = 10 ,					__UpperCAmelCase:					int = 8 ,					__UpperCAmelCase:					float = 0.0 ,					__UpperCAmelCase:					int = 2048 ,					__UpperCAmelCase:					bool = False ,					__UpperCAmelCase:					bool = False ,					__UpperCAmelCase:					int = 4 ,					__UpperCAmelCase:					int = 255 ,					__UpperCAmelCase:					int = 100 ,					__UpperCAmelCase:					float = 0.1 ,					__UpperCAmelCase:					float = 2.0 ,					__UpperCAmelCase:					float = 5.0 ,					__UpperCAmelCase:					float = 5.0 ,					__UpperCAmelCase:					int = 12544 ,					__UpperCAmelCase:					float = 3.0 ,					__UpperCAmelCase:					float = 0.75 ,					__UpperCAmelCase:					float = 0.02 ,					__UpperCAmelCase:					float = 1.0 ,					__UpperCAmelCase:					bool = True ,					__UpperCAmelCase:					List[int] = [4, 8, 16, 32] ,					__UpperCAmelCase:					bool = None ,					**__UpperCAmelCase:					Any ,					)    ->     int:




											'''simple docstring'''



											if backbone_config is None:
																logger.info("`backbone_config` is `None`. Initializing the config with the default `Swin` backbone."   )
																__a					:						str      =					CONFIG_MAPPING["swin"](
																    image_size=224 ,					in_channels=3 ,					patch_size=4 ,					embed_dim=96 ,					depths=[2, 2, 18, 2] ,					num_heads=[3, 6, 12, 24] ,					window_size=7 ,					drop_path_rate=0.3 ,					use_absolute_embeddings=__UpperCAmelCase ,					out_features=["stage1", "stage2", "stage3", "stage4"] ,					)

											if isinstance(__UpperCAmelCase ,					__UpperCAmelCase   ):
																__a					:						List[Any]      =					backbone_config.pop("model_type"   )
																__a					:						List[str]      =					CONFIG_MAPPING[backbone_model_type]
																__a					:						Dict      =					config_class.from_dict(__UpperCAmelCase   )

											# verify that the backbone is supported
											if backbone_config.model_type not in self.backbones_supported:
																logger.warning_once(
																    f'Backbone {backbone_config.model_type} is not a supported model and may not be compatible with Mask2Former. '
																    f'Supported model types: {",".join(self.backbones_supported   )}'   )

											__a					:						Tuple      =					backbone_config
											__a					:						Any      =					feature_size
											__a					:						int      =					mask_feature_size
											__a					:						Optional[int]      =					hidden_dim
											__a					:						int      =					encoder_feedforward_dim
											__a					:						List[str]      =					activation_function
											__a					:						Optional[Any]      =					encoder_layers
											__a					:						List[Any]      =					decoder_layers
											__a					:						Union[str, Any]      =					num_attention_heads
											__a					:						Optional[Any]      =					dropout
											__a					:						Tuple      =					dim_feedforward
											__a					:						List[str]      =					pre_norm
											__a					:						int      =					enforce_input_projection
											__a					:						List[str]      =					common_stride
											__a					:						Any      =					ignore_value
											__a					:						Union[str, Any]      =					num_queries
											__a					:						int      =					no_object_weight
											__a					:						Tuple      =					class_weight
											__a					:						int      =					mask_weight
											__a					:						Union[str, Any]      =					dice_weight
											__a					:						int      =					train_num_points
											__a					:						Any      =					oversample_ratio
											__a					:						Any      =					importance_sample_ratio
											__a					:						List[str]      =					init_std
											__a					:						List[str]      =					init_xavier_std
											__a					:						Dict      =					use_auxiliary_loss
											__a					:						List[Any]      =					feature_strides
											__a					:						Tuple      =					output_auxiliary_logits
											__a					:						List[Any]      =					decoder_layers

											super().__init__(**__UpperCAmelCase   )



						@classmethod
						def        UpperCAmelCase__					(cls:					Tuple ,					__UpperCAmelCase:					PretrainedConfig ,					**__UpperCAmelCase:					List[str]   )    ->     Any:




											'''simple docstring'''



											return cls(
											    backbone_config=__UpperCAmelCase ,					**__UpperCAmelCase ,					)



						def        UpperCAmelCase__					(self:					str   )    ->     Dict[str, any]:




											'''simple docstring'''



											__a					:						Tuple      =					copy.deepcopy(self.__dict__   )
											__a					:						List[str]      =					self.backbone_config.to_dict()
											__a					:						Any      =					self.__class__.model_type
											return output





