
'''simple docstring'''







import gc
import unittest

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

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

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


enable_full_determinism()


class 						__UpperCAmelCase      (					_lowerCamelCase						,   unittest.TestCase						):
							__lowercase      		=       LDMTextToImagePipeline
							__lowercase      		=       TEXT_TO_IMAGE_PARAMS - {
							    """negative_prompt""",
							    """negative_prompt_embeds""",
							    """cross_attention_kwargs""",
							    """prompt_embeds""",
							}
							__lowercase      		=       PipelineTesterMixin.required_optional_params - {
							    """num_images_per_prompt""",
							    """callback""",
							    """callback_steps""",
							}
							__lowercase      		=       TEXT_TO_IMAGE_BATCH_PARAMS
							__lowercase      		=       False


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								torch.manual_seed(0  )
								_snake_case   						=		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    ,   )
								_snake_case   						=		DDIMScheduler(
								    beta_start=0.00085    ,   beta_end=0.012    ,   beta_schedule='scaled_linear'    ,   clip_sample=lowerCAmelCase_    ,   set_alpha_to_one=lowerCAmelCase_    ,   )
								torch.manual_seed(0  )
								_snake_case   						=		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  )
								_snake_case   						=		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    ,   )
								_snake_case   						=		CLIPTextModel(lowerCAmelCase_  )
								_snake_case   						=		CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'  )

								_snake_case   						=		{
								    'unet': unet,
								    'scheduler': scheduler,
								    'vqvae': vae,
								    'bert': text_encoder,
								    'tokenizer': tokenizer,
								}
								return components


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=0  ):





								"""simple docstring"""






								if str(lowerCAmelCase_  ).startswith('mps'  ):
									_snake_case   						=		torch.manual_seed(lowerCAmelCase_  )
								else:
									_snake_case   						=		torch.Generator(device=lowerCAmelCase_  ).manual_seed(lowerCAmelCase_  )
								_snake_case   						=		{
								    'prompt': 'A painting of a squirrel eating a burger',
								    'generator': generator,
								    'num_inference_steps': 2,
								    'guidance_scale': 6.0,
								    'output_type': 'numpy',
								}
								return inputs




							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		'cpu'  # ensure determinism for the device-dependent torch.Generator

								_snake_case   						=		self.get_dummy_components()
								_snake_case   						=		LDMTextToImagePipeline(**lowerCAmelCase_  )
								pipe.to(lowerCAmelCase_  )
								pipe.set_progress_bar_config(disable=lowerCAmelCase_  )

								_snake_case   						=		self.get_dummy_inputs(lowerCAmelCase_  )
								_snake_case   						=		pipe(**lowerCAmelCase_  ).images
								_snake_case   						=		image[0, -3:, -3:, -1]

								assert image.shape == (1, 16, 16, 3)
								_snake_case   						=		np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]  )

								assert np.abs(image_slice.flatten() - expected_slice  ).max() < 1E-3


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


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=torch.floataa    ,   lowerCAmelCase_=0  ):





								"""simple docstring"""






								_snake_case   						=		torch.manual_seed(lowerCAmelCase_  )
								_snake_case   						=		np.random.RandomState(lowerCAmelCase_  ).standard_normal((1, 4, 32, 32)  )
								_snake_case   						=		torch.from_numpy(lowerCAmelCase_  ).to(device=lowerCAmelCase_    ,   dtype=lowerCAmelCase_  )
								_snake_case   						=		{
								    'prompt': 'A painting of a squirrel eating a burger',
								    'latents': latents,
								    'generator': generator,
								    'num_inference_steps': 3,
								    'guidance_scale': 6.0,
								    'output_type': 'numpy',
								}
								return inputs




							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'  ).to(lowerCAmelCase_  )
								pipe.set_progress_bar_config(disable=lowerCAmelCase_  )

								_snake_case   						=		self.get_inputs(lowerCAmelCase_  )
								_snake_case   						=		pipe(**lowerCAmelCase_  ).images
								_snake_case   						=		image[0, -3:, -3:, -1].flatten()

								assert image.shape == (1, 2_56, 2_56, 3)
								_snake_case   						=		np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]  )
								_snake_case   						=		np.abs(expected_slice - image_slice  ).max()
								assert max_diff < 1E-3


@nightly
@require_torch_gpu
class 						__UpperCAmelCase      (					unittest.TestCase						):


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_=torch.floataa    ,   lowerCAmelCase_=0  ):





								"""simple docstring"""






								_snake_case   						=		torch.manual_seed(lowerCAmelCase_  )
								_snake_case   						=		np.random.RandomState(lowerCAmelCase_  ).standard_normal((1, 4, 32, 32)  )
								_snake_case   						=		torch.from_numpy(lowerCAmelCase_  ).to(device=lowerCAmelCase_    ,   dtype=lowerCAmelCase_  )
								_snake_case   						=		{
								    'prompt': 'A painting of a squirrel eating a burger',
								    'latents': latents,
								    'generator': generator,
								    'num_inference_steps': 50,
								    'guidance_scale': 6.0,
								    'output_type': 'numpy',
								}
								return inputs




							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		LDMTextToImagePipeline.from_pretrained('CompVis/ldm-text2im-large-256'  ).to(lowerCAmelCase_  )
								pipe.set_progress_bar_config(disable=lowerCAmelCase_  )

								_snake_case   						=		self.get_inputs(lowerCAmelCase_  )
								_snake_case   						=		pipe(**lowerCAmelCase_  ).images[0]

								_snake_case   						=		load_numpy(
								    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy'  )
								_snake_case   						=		np.abs(expected_image - image  ).max()
								assert max_diff < 1E-3









"""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


lowerCamelCase_					=      logging.get_logger(__name__)

lowerCamelCase_					=      {
    '''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 			UpperCamelCase_       (__A     ):
	__magic_name__	      =				'''detr'''
	__magic_name__	      =				['''past_key_values''']
	__magic_name__	      =				{
	    '''hidden_size''': '''d_model''',
	    '''num_attention_heads''': '''encoder_attention_heads''',
	}





	def __init__(      self					: Dict	,	lowerCAmelCase_					: str=True	,	lowerCAmelCase_					: Union[str, Any]=None	,	lowerCAmelCase_					: Dict=3	,	lowerCAmelCase_					: List[Any]=100	,	lowerCAmelCase_					: int=6	,	lowerCAmelCase_					: Union[str, Any]=2_048	,	lowerCAmelCase_					: int=8	,	lowerCAmelCase_					: int=6	,	lowerCAmelCase_					: Union[str, Any]=2_048	,	lowerCAmelCase_					: Any=8	,	lowerCAmelCase_					: Dict=0.0	,	lowerCAmelCase_					: Union[str, Any]=0.0	,	lowerCAmelCase_					: Dict=True	,	lowerCAmelCase_					: List[Any]="relu"	,	lowerCAmelCase_					: List[Any]=256	,	lowerCAmelCase_					: Tuple=0.1	,	lowerCAmelCase_					: Optional[Any]=0.0	,	lowerCAmelCase_					: List[Any]=0.0	,	lowerCAmelCase_					: List[Any]=0.0_2	,	lowerCAmelCase_					: Optional[int]=1.0	,	lowerCAmelCase_					: str=False	,	lowerCAmelCase_					: Tuple="sine"	,	lowerCAmelCase_					: str="resnet50"	,	lowerCAmelCase_					: List[Any]=True	,	lowerCAmelCase_					: int=False	,	lowerCAmelCase_					: Optional[Any]=1	,	lowerCAmelCase_					: int=5	,	lowerCAmelCase_					: Optional[Any]=2	,	lowerCAmelCase_					: List[Any]=1	,	lowerCAmelCase_					: List[Any]=1	,	lowerCAmelCase_					: Dict=5	,	lowerCAmelCase_					: Optional[Any]=2	,	lowerCAmelCase_					: Union[str, Any]=0.1	,	**lowerCAmelCase_					: Dict	,	)						->			int:
				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."						)
										UpperCAmelCase_     : Tuple			     =					CONFIG_MAPPING["resnet"](out_features=["stage4"]						)
							elif isinstance(lowerCAmelCase_	,	lowerCAmelCase_						):
										UpperCAmelCase_     : Dict			     =					backbone_config.get("model_type"						)
										UpperCAmelCase_     : Dict			     =					CONFIG_MAPPING[backbone_model_type]
										UpperCAmelCase_     : Tuple			     =					config_class.from_dict(lowerCAmelCase_						)
							# set timm attributes to None
							UpperCAmelCase_     ,	UpperCAmelCase_     ,	UpperCAmelCase_     : Optional[Any]			     =					None, None, None

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





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			int:
				return self.encoder_attention_heads





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			int:
				return self.d_model





	@classmethod
	def      _SCREAMING_SNAKE_CASE      (      cls					: Union[str, Any]	,	lowerCAmelCase_					: PretrainedConfig	,	**lowerCAmelCase_					: Tuple						)						->			List[Any]:
				return cls(backbone_config=lowerCAmelCase_	,	**lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Dict[str, any]:
				UpperCAmelCase_     : Tuple			     =					copy.deepcopy(self.__dict__						)
				if output["backbone_config"] is not None:
							UpperCAmelCase_     : Union[str, Any]			     =					self.backbone_config.to_dict()
				UpperCAmelCase_     : Any			     =					self.__class__.model_type
				return output







class 			UpperCamelCase_       (__A     ):
	__magic_name__	      =				version.parse('''1.11'''     )





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Mapping[str, Mapping[int, str]]:
				return OrderedDict(
				    [
				        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
				        ("pixel_mask", {0: "batch"}),
				    ]						)





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			float:
				return 1e-5





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			int:
				return 12



