
"""simple docstring"""




import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()

def lowercase_						(     _snake_case		):
							SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     [tensor.shape for tensor in tensor_list]
							return all(shape == shapes[0] for shape in shapes[1:]		)



class 			lowerCAmelCase_		(a__				,      a__				,      a__				,      unittest.TestCase						):


				"""simple docstring"""



				__UpperCamelCase   :		Union[str, Any]       =     StableDiffusionLatentUpscalePipeline
				__UpperCamelCase   :		Optional[Any]       =     TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
				    '''height''',
				    '''width''',
				    '''cross_attention_kwargs''',
				    '''negative_prompt_embeds''',
				    '''prompt_embeds''',
				}
				__UpperCamelCase   :		Tuple       =     PipelineTesterMixin.required_optional_params - {'''num_images_per_prompt'''}
				__UpperCamelCase   :		Optional[Any]       =     TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
				__UpperCamelCase   :		Any       =     frozenset(
				    []						)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
				__UpperCamelCase   :		Tuple       =     frozenset([]						)

				__UpperCamelCase   :		int       =     True



				@property
				def 			__magic_name__					(self					)      ->	Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     1
											SCREAMING_SNAKE_CASE__		: List[Any]     =     4
											SCREAMING_SNAKE_CASE__		: str     =     (16, 16)

											SCREAMING_SNAKE_CASE__		: int     =     floats_tensor((batch_size, num_channels) + sizes     , rng=random.Random(0					)					).to(SCREAMING_SNAKE_CASE__					)
											return image



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											torch.manual_seed(0					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     UNetaDConditionModel(
											    act_fn="""gelu"""     , attention_head_dim=8     , norm_num_groups=SCREAMING_SNAKE_CASE__     , block_out_channels=[32, 32, 64, 64]     , time_cond_proj_dim=1_60     , conv_in_kernel=1     , conv_out_kernel=1     , cross_attention_dim=32     , down_block_types=(
											        """KDownBlock2D""",
											        """KCrossAttnDownBlock2D""",
											        """KCrossAttnDownBlock2D""",
											        """KCrossAttnDownBlock2D""",
											    )     , in_channels=8     , mid_block_type=SCREAMING_SNAKE_CASE__     , only_cross_attention=SCREAMING_SNAKE_CASE__     , out_channels=5     , resnet_time_scale_shift="""scale_shift"""     , time_embedding_type="""fourier"""     , timestep_post_act="""gelu"""     , up_block_types=("""KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KCrossAttnUpBlock2D""", """KUpBlock2D""")     , )
											SCREAMING_SNAKE_CASE__		: Any     =     AutoencoderKL(
											    block_out_channels=[32, 32, 64, 64]     , in_channels=3     , out_channels=3     , down_block_types=[
											        """DownEncoderBlock2D""",
											        """DownEncoderBlock2D""",
											        """DownEncoderBlock2D""",
											        """DownEncoderBlock2D""",
											    ]     , up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D""", """UpDecoderBlock2D"""]     , latent_channels=4     , )
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     EulerDiscreteScheduler(prediction_type="""sample"""					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     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=10_00     , hidden_act="""quick_gelu"""     , projection_dim=5_12     , )
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     CLIPTextModel(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					)

											SCREAMING_SNAKE_CASE__		: str     =     {
											    """unet""": model.eval(),
											    """vae""": vae.eval(),
											    """scheduler""": scheduler,
											    """text_encoder""": text_encoder,
											    """tokenizer""": tokenizer,
											}

											return components



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=0					)      ->	Dict:



											"""simple docstring"""




											if str(SCREAMING_SNAKE_CASE__					).startswith("""mps"""					):
																		SCREAMING_SNAKE_CASE__		: List[str]     =     torch.manual_seed(SCREAMING_SNAKE_CASE__					)
											else:
																		SCREAMING_SNAKE_CASE__		: Tuple     =     torch.Generator(device=SCREAMING_SNAKE_CASE__					).manual_seed(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: List[Any]     =     {
											    """prompt""": """A painting of a squirrel eating a burger""",
											    """image""": self.dummy_image.cpu(),
											    """generator""": generator,
											    """num_inference_steps""": 2,
											    """output_type""": """numpy""",
											}
											return inputs



				def 			__magic_name__					(self					)      ->	Optional[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     """cpu"""

											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.get_dummy_components()
											SCREAMING_SNAKE_CASE__		: List[str]     =     self.pipeline_class(**SCREAMING_SNAKE_CASE__					)
											pipe.to(SCREAMING_SNAKE_CASE__					)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: Dict     =     self.get_dummy_inputs(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     pipe(**SCREAMING_SNAKE_CASE__					).images
											SCREAMING_SNAKE_CASE__		: List[Any]     =     image[0, -3:, -3:, -1]

											self.assertEqual(image.shape     , (1, 2_56, 2_56, 3)					)
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     np.array(
											    [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055]					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     np.abs(image_slice.flatten() - expected_slice					).max()
											self.assertLessEqual(SCREAMING_SNAKE_CASE__     , 1E-3					)



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											super().test_attention_slicing_forward_pass(expected_max_diff=7E-3					)



				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3					)



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	List[str]:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											super().test_save_load_local(expected_max_difference=3E-3					)



				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											super().test_save_load_optional_components(expected_max_difference=3E-3					)



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     [
											    """DDIMScheduler""",
											    """DDPMScheduler""",
											    """PNDMScheduler""",
											    """HeunDiscreteScheduler""",
											    """EulerAncestralDiscreteScheduler""",
											    """KDPM2DiscreteScheduler""",
											    """KDPM2AncestralDiscreteScheduler""",
											    """DPMSolverSDEScheduler""",
											]
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     self.get_dummy_components()
											SCREAMING_SNAKE_CASE__		: List[str]     =     self.pipeline_class(**SCREAMING_SNAKE_CASE__					)

											# make sure that PNDM does not need warm-up
											pipe.scheduler.register_to_config(skip_prk_steps=SCREAMING_SNAKE_CASE__					)

											pipe.to(SCREAMING_SNAKE_CASE__					)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.get_dummy_inputs(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Dict     =     2

											SCREAMING_SNAKE_CASE__		: int     =     []
											for scheduler_enum in KarrasDiffusionSchedulers:
																		if scheduler_enum.name in skip_schedulers:
																									# no sigma schedulers are not supported
																									# no schedulers
																									continue

																		SCREAMING_SNAKE_CASE__		: List[str]     =     getattr(SCREAMING_SNAKE_CASE__     , scheduler_enum.name					)
																		SCREAMING_SNAKE_CASE__		: Any     =     scheduler_cls.from_config(pipe.scheduler.config					)
																		SCREAMING_SNAKE_CASE__		: int     =     pipe(**SCREAMING_SNAKE_CASE__					)[0]
																		outputs.append(SCREAMING_SNAKE_CASE__					)

											assert check_same_shape(SCREAMING_SNAKE_CASE__					)






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


				"""simple docstring"""



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



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     torch.manual_seed(33					)

											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     StableDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""     , torch_dtype=torch.floataa					)
											pipe.to("""cuda"""					)

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     StableDiffusionLatentUpscalePipeline.from_pretrained(
											    """stabilityai/sd-x2-latent-upscaler"""     , torch_dtype=torch.floataa					)
											upscaler.to("""cuda"""					)

											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     """a photo of an astronaut high resolution, unreal engine, ultra realistic"""

											SCREAMING_SNAKE_CASE__		: Dict     =     pipe(SCREAMING_SNAKE_CASE__     , generator=SCREAMING_SNAKE_CASE__     , output_type="""latent"""					).images

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     upscaler(
											    prompt=SCREAMING_SNAKE_CASE__     , image=SCREAMING_SNAKE_CASE__     , num_inference_steps=20     , guidance_scale=0     , generator=SCREAMING_SNAKE_CASE__     , output_type="""np"""     , ).images[0]

											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy"""					)
											assert np.abs((expected_image - image).mean()					) < 5E-2



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     torch.manual_seed(33					)

											SCREAMING_SNAKE_CASE__		: Tuple     =     StableDiffusionLatentUpscalePipeline.from_pretrained(
											    """stabilityai/sd-x2-latent-upscaler"""     , torch_dtype=torch.floataa					)
											upscaler.to("""cuda"""					)

											SCREAMING_SNAKE_CASE__		: Dict     =     """the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"""

											SCREAMING_SNAKE_CASE__		: int     =     load_image(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png"""					)

											SCREAMING_SNAKE_CASE__		: int     =     upscaler(
											    prompt=SCREAMING_SNAKE_CASE__     , image=SCREAMING_SNAKE_CASE__     , num_inference_steps=20     , guidance_scale=0     , generator=SCREAMING_SNAKE_CASE__     , output_type="""np"""     , ).images[0]

											SCREAMING_SNAKE_CASE__		: List[Any]     =     load_numpy(
											    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy"""					)
											assert np.abs((expected_image - image).max()					) < 5E-2






"""simple docstring"""




import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

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


UpperCAmelCase__   :					Optional[Any]												=				logging.get_logger(__name__)

UpperCAmelCase__   :					str												=				{
    'facebook/detr-resnet-50': 'https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json',
    # See all DETR models at https://huggingface.co/models?filter=detr
}



class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		List[Any]       =     '''detr'''
				__UpperCamelCase   :		List[Any]       =     ['''past_key_values''']
				__UpperCamelCase   :		List[Any]       =     {
				    '''hidden_size''': '''d_model''',
				    '''num_attention_heads''': '''encoder_attention_heads''',
				}



				def __init__(self     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=None     , SCREAMING_SNAKE_CASE__=3     , SCREAMING_SNAKE_CASE__=1_00     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=6     , SCREAMING_SNAKE_CASE__=20_48     , SCREAMING_SNAKE_CASE__=8     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__="relu"     , SCREAMING_SNAKE_CASE__=2_56     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.0     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=1.0     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__="sine"     , SCREAMING_SNAKE_CASE__="resnet50"     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=1     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.1     , **SCREAMING_SNAKE_CASE__     , )      ->	Optional[Any]:



											"""simple docstring"""




											if backbone_config is not None and use_timm_backbone:
																		raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""					)

											if not use_timm_backbone:
																		if backbone_config is None:
																									logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""					)
																									SCREAMING_SNAKE_CASE__		: Any     =     CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]					)
																		elif isinstance(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					):
																									SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     backbone_config.get("""model_type"""					)
																									SCREAMING_SNAKE_CASE__		: int     =     CONFIG_MAPPING[backbone_model_type]
																									SCREAMING_SNAKE_CASE__		: str     =     config_class.from_dict(SCREAMING_SNAKE_CASE__					)
																		# set timm attributes to None
																		SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__   ,   SCREAMING_SNAKE_CASE__		: List[str]     =     None, None, None

											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_timm_backbone
											SCREAMING_SNAKE_CASE__		: Tuple     =     backbone_config
											SCREAMING_SNAKE_CASE__		: List[Any]     =     num_channels
											SCREAMING_SNAKE_CASE__		: Tuple     =     num_queries
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     d_model
											SCREAMING_SNAKE_CASE__		: str     =     encoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: str     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     encoder_attention_heads
											SCREAMING_SNAKE_CASE__		: Any     =     decoder_ffn_dim
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     decoder_layers
											SCREAMING_SNAKE_CASE__		: Dict     =     decoder_attention_heads
											SCREAMING_SNAKE_CASE__		: List[str]     =     dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     attention_dropout
											SCREAMING_SNAKE_CASE__		: Tuple     =     activation_dropout
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     activation_function
											SCREAMING_SNAKE_CASE__		: Any     =     init_std
											SCREAMING_SNAKE_CASE__		: Dict     =     init_xavier_std
											SCREAMING_SNAKE_CASE__		: Any     =     encoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     decoder_layerdrop
											SCREAMING_SNAKE_CASE__		: Dict     =     encoder_layers
											SCREAMING_SNAKE_CASE__		: List[str]     =     auxiliary_loss
											SCREAMING_SNAKE_CASE__		: List[Any]     =     position_embedding_type
											SCREAMING_SNAKE_CASE__		: List[str]     =     backbone
											SCREAMING_SNAKE_CASE__		: Dict     =     use_pretrained_backbone
											SCREAMING_SNAKE_CASE__		: Any     =     dilation
											# Hungarian matcher
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     class_cost
											SCREAMING_SNAKE_CASE__		: Tuple     =     bbox_cost
											SCREAMING_SNAKE_CASE__		: List[Any]     =     giou_cost
											# Loss coefficients
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     mask_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     dice_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     bbox_loss_coefficient
											SCREAMING_SNAKE_CASE__		: Any     =     giou_loss_coefficient
											SCREAMING_SNAKE_CASE__		: List[str]     =     eos_coefficient
											super().__init__(is_encoder_decoder=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.encoder_attention_heads



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return self.d_model



				@classmethod
				def 			__magic_name__					(cls     , SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)      ->	Tuple:



											"""simple docstring"""




											return cls(backbone_config=SCREAMING_SNAKE_CASE__     , **SCREAMING_SNAKE_CASE__					)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     copy.deepcopy(self.__dict__					)
											if output["backbone_config"] is not None:
																		SCREAMING_SNAKE_CASE__		: Any     =     self.backbone_config.to_dict()
											SCREAMING_SNAKE_CASE__		: Dict     =     self.__class__.model_type
											return output






class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				__UpperCamelCase   :		Any       =     version.parse('''1.11'''						)



				@property
				def 			__magic_name__					(self					)      ->	Mapping[str, Mapping[int, str]]:



											"""simple docstring"""




											return OrderedDict(
											    [
											        ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
											        ("""pixel_mask""", {0: """batch"""}),
											    ]					)



				@property
				def 			__magic_name__					(self					)      ->	float:



											"""simple docstring"""




											return 1E-5



				@property
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											return 12




