


'''simple docstring'''


import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    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 						lowerCAmelCase__      (      UpperCAmelCase__     ,							UpperCAmelCase__     ,							unittest.TestCase						):
			lowerCAmelCase			:						int							 =  StableDiffusionXLPipeline
			lowerCAmelCase			:						Optional[int]							 =  TEXT_TO_IMAGE_PARAMS
			lowerCAmelCase			:						Optional[Any]							 =  TEXT_TO_IMAGE_BATCH_PARAMS
			lowerCAmelCase			:						Optional[int]							 =  TEXT_TO_IMAGE_IMAGE_PARAMS
			lowerCAmelCase			:						str							 =  TEXT_TO_IMAGE_IMAGE_PARAMS




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



					'''simple docstring'''







					torch.manual_seed(0			)
					_UpperCAmelCase		:							Dict             =   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")						,	attention_head_dim=(2, 4)						,	use_linear_projection=lowerCamelCase__						,	addition_embed_type="text_time"						,	addition_time_embed_dim=8						,	transformer_layers_per_block=(1, 2)						,	projection_class_embeddings_input_dim=80						,	cross_attention_dim=64						,	)
					_UpperCAmelCase		:							Optional[Any]             =   EulerDiscreteScheduler(
					    beta_start=0.0_0_0_8_5						,	beta_end=0.0_1_2						,	steps_offset=1						,	beta_schedule="scaled_linear"						,	timestep_spacing="leading"						,	)
					torch.manual_seed(0			)
					_UpperCAmelCase		:							str             =   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						,	sample_size=1_28						,	)
					torch.manual_seed(0			)
					_UpperCAmelCase		:							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="gelu"						,	projection_dim=32						,	)
					_UpperCAmelCase		:							Optional[int]             =   CLIPTextModel(lowerCamelCase__			)
					_UpperCAmelCase		:							List[str]             =   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"						,	local_files_only=lowerCamelCase__			)

					_UpperCAmelCase		:							Union[str, Any]             =   CLIPTextModelWithProjection(lowerCamelCase__			)
					_UpperCAmelCase		:							Any             =   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"						,	local_files_only=lowerCamelCase__			)

					_UpperCAmelCase		:							int             =   {
					    "unet": unet,
					    "scheduler": scheduler,
					    "vae": vae,
					    "text_encoder": text_encoder,
					    "tokenizer": tokenizer,
					    "text_encoder_2": text_encoder_a,
					    "tokenizer_2": tokenizer_a,
					    # "safety_checker": None,
					    # "feature_extractor": None,
					}
					return components




			def 				lowerCAmelCase__     (		self	:  Dict						,	lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  List[str]=0			) ->Union[str, Any]:



					'''simple docstring'''







					if str(lowerCamelCase__			).startswith("mps"			):
							_UpperCAmelCase		:							Optional[Any]             =   torch.manual_seed(lowerCamelCase__			)
					else:
							_UpperCAmelCase		:							Union[str, Any]             =   torch.Generator(device=lowerCamelCase__			).manual_seed(lowerCamelCase__			)
					_UpperCAmelCase		:							Optional[Any]             =   {
					    "prompt": "A painting of a squirrel eating a burger",
					    "generator": generator,
					    "num_inference_steps": 2,
					    "guidance_scale": 5.0,
					    "output_type": "numpy",
					}
					return inputs




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



					'''simple docstring'''







					_UpperCAmelCase		:							Any             =   "cpu"  # ensure determinism for the device-dependent torch.Generator
					_UpperCAmelCase		:							Any             =   self.get_dummy_components()
					_UpperCAmelCase		:							Union[str, Any]             =   StableDiffusionXLPipeline(**lowerCamelCase__			)
					_UpperCAmelCase		:							Union[str, Any]             =   sd_pipe.to(lowerCamelCase__			)
					sd_pipe.set_progress_bar_config(disable=lowerCamelCase__			)

					_UpperCAmelCase		:							List[str]             =   self.get_dummy_inputs(lowerCamelCase__			)
					_UpperCAmelCase		:							int             =   sd_pipe(**lowerCamelCase__			).images
					_UpperCAmelCase		:							List[str]             =   image[0, -3:, -3:, -1]

					assert image.shape == (1, 64, 64, 3)
					_UpperCAmelCase		:							Optional[int]             =   np.array([0.5_8_7_3, 0.6_1_2_8, 0.4_7_9_7, 0.5_1_2_2, 0.5_6_7_4, 0.4_6_3_9, 0.5_2_2_7, 0.5_1_4_9, 0.4_7_4_7]			)

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




			def 				lowerCAmelCase__     (		self	:  Any			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							Any             =   self.get_dummy_components()
					_UpperCAmelCase		:							Dict             =   StableDiffusionXLPipeline(**lowerCamelCase__			)
					_UpperCAmelCase		:							Union[str, Any]             =   sd_pipe.to(lowerCamelCase__			)
					_UpperCAmelCase		:							Optional[int]             =   sd_pipe.to(lowerCamelCase__			)
					sd_pipe.set_progress_bar_config(disable=lowerCamelCase__			)

					# forward without prompt embeds
					_UpperCAmelCase		:							List[Any]             =   self.get_dummy_inputs(lowerCamelCase__			)
					_UpperCAmelCase		:							Union[str, Any]             =   3 * ["this is a negative prompt"]
					_UpperCAmelCase		:							Optional[int]             =   negative_prompt
					_UpperCAmelCase		:							Dict             =   3 * [inputs["prompt"]]

					_UpperCAmelCase		:							Tuple             =   sd_pipe(**lowerCamelCase__			)
					_UpperCAmelCase		:							Optional[Any]             =   output.images[0, -3:, -3:, -1]

					# forward with prompt embeds
					_UpperCAmelCase		:							Union[str, Any]             =   self.get_dummy_inputs(lowerCamelCase__			)
					_UpperCAmelCase		:							str             =   3 * ["this is a negative prompt"]
					_UpperCAmelCase		:							int             =   3 * [inputs.pop("prompt"			)]

					(
					    (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     (
					    _UpperCAmelCase
					)       ,     
					)		:							int             =   sd_pipe.encode_prompt(lowerCamelCase__						,	negative_prompt=lowerCamelCase__			)

					_UpperCAmelCase		:							int             =   sd_pipe(
					    **lowerCamelCase__						,	prompt_embeds=lowerCamelCase__						,	negative_prompt_embeds=lowerCamelCase__						,	pooled_prompt_embeds=lowerCamelCase__						,	negative_pooled_prompt_embeds=lowerCamelCase__						,	)
					_UpperCAmelCase		:							Union[str, Any]             =   output.images[0, -3:, -3:, -1]

					# make sure that it's equal
					assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()			).max() < 1E-4




			def 				lowerCAmelCase__     (		self	:  Any			) ->Dict:



					'''simple docstring'''







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






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



					'''simple docstring'''







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






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




			def 				lowerCAmelCase__     (		self	:  Dict			) ->List[str]:



					'''simple docstring'''







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




			def 				lowerCAmelCase__     (		self	:  List[Any]						,	lowerCamelCase__	:  List[Any]						,	lowerCamelCase__	:  Tuple="cpu"						,	lowerCamelCase__	:  int=torch.floataa						,	lowerCamelCase__	:  int=0			) ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase		:							List[Any]             =   torch.Generator(device=lowerCamelCase__			).manual_seed(lowerCamelCase__			)
					_UpperCAmelCase		:							List[str]             =   np.random.RandomState(lowerCamelCase__			).standard_normal((1, 4, 64, 64)			)
					_UpperCAmelCase		:							str             =   torch.from_numpy(lowerCamelCase__			).to(device=lowerCamelCase__						,	dtype=lowerCamelCase__			)
					_UpperCAmelCase		:							Dict             =   {
					    "prompt": "a photograph of an astronaut riding a horse",
					    "latents": latents,
					    "generator": generator,
					    "num_inference_steps": 3,
					    "guidance_scale": 7.5,
					    "output_type": "numpy",
					}
					return inputs






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



					'''simple docstring'''







					_UpperCAmelCase		:							Tuple             =   DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-base"			)
					pipe.to(lowerCamelCase__			)
					pipe.set_progress_bar_config(disable=lowerCamelCase__			)

					_UpperCAmelCase		:							List[Any]             =   self.get_inputs(lowerCamelCase__			)
					_UpperCAmelCase		:							List[Any]             =   pipe(**lowerCamelCase__			).images
					_UpperCAmelCase		:							Tuple             =   image[0, -3:, -3:, -1].flatten()

					assert image.shape == (1, 5_12, 5_12, 3)
					_UpperCAmelCase		:							List[str]             =   np.array([0.4_9_4_9_3, 0.4_7_8_9_6, 0.4_0_7_9_8, 0.5_4_2_1_4, 0.5_3_2_1_2, 0.4_8_2_0_2, 0.4_7_6_5_6, 0.4_6_3_2_9, 0.4_8_5_0_6]			)
					assert np.abs(image_slice - expected_slice			).max() < 7E-3







'''simple docstring'''


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
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


lowerCamelCase__     						=  logging.get_logger(__name__)

lowerCamelCase__     						=  {
    'facebook/convnext-tiny-224': 'https://huggingface.co/facebook/convnext-tiny-224/resolve/main/config.json',
    # See all ConvNeXT models at https://huggingface.co/models?filter=convnext
}






class 						lowerCAmelCase__      (      UpperCAmelCase__     ,							UpperCAmelCase__						):
			lowerCAmelCase			:						Optional[Any]							 =  "convnext"




			def __init__(		self	:  Optional[int]						,	lowerCamelCase__	:  List[str]=3						,	lowerCamelCase__	:  List[str]=4						,	lowerCamelCase__	:  Optional[int]=4						,	lowerCamelCase__	:  Dict=None						,	lowerCamelCase__	:  Tuple=None						,	lowerCamelCase__	:  List[str]="gelu"						,	lowerCamelCase__	:  List[Any]=0.0_2						,	lowerCamelCase__	:  Dict=1E-12						,	lowerCamelCase__	:  Tuple=1E-6						,	lowerCamelCase__	:  Dict=0.0						,	lowerCamelCase__	:  Optional[int]=2_24						,	lowerCamelCase__	:  List[Any]=None						,	lowerCamelCase__	:  Any=None						,	**lowerCamelCase__	:  int						,	) ->str:



					'''simple docstring'''







					super().__init__(**lowerCamelCase__			)

					_UpperCAmelCase		:							Optional[int]             =   num_channels
					_UpperCAmelCase		:							Tuple             =   patch_size
					_UpperCAmelCase		:							Dict             =   num_stages
					_UpperCAmelCase		:							Optional[int]             =   [96, 1_92, 3_84, 7_68] if hidden_sizes is None else hidden_sizes
					_UpperCAmelCase		:							Dict             =   [3, 3, 9, 3] if depths is None else depths
					_UpperCAmelCase		:							Union[str, Any]             =   hidden_act
					_UpperCAmelCase		:							Tuple             =   initializer_range
					_UpperCAmelCase		:							Tuple             =   layer_norm_eps
					_UpperCAmelCase		:							Tuple             =   layer_scale_init_value
					_UpperCAmelCase		:							str             =   drop_path_rate
					_UpperCAmelCase		:							str             =   image_size
					_UpperCAmelCase		:							Tuple             =   ["stem"] + [F"""stage{idx}""" for idx in range(1						,	len(self.depths			) + 1			)]
					_UpperCAmelCase       ,     _UpperCAmelCase		:							str             =   get_aligned_output_features_output_indices(
					    out_features=lowerCamelCase__						,	out_indices=lowerCamelCase__						,	stage_names=self.stage_names			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):
			lowerCAmelCase			:						str							 =  version.parse("1.11"						)




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



					'''simple docstring'''







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






			@property
			def 				lowerCAmelCase__     (		self	:  str			) ->float:



					'''simple docstring'''







					return 1E-5



