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    lowercase_    (     __snake_case						, __snake_case						, unittest.TestCase		):
							_lowerCamelCase						=		StableDiffusionXLPipeline
							_lowerCamelCase						=		TEXT_TO_IMAGE_PARAMS
							_lowerCamelCase						=		TEXT_TO_IMAGE_BATCH_PARAMS
							_lowerCamelCase						=		TEXT_TO_IMAGE_IMAGE_PARAMS
							_lowerCamelCase						=		TEXT_TO_IMAGE_IMAGE_PARAMS



							def        UpperCamelCase       (						self				):
													torch.manual_seed(0				)
													_snake_case						:     Any				=    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=lowercase_      ,				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      ,				)
													_snake_case						:     List[Any]				=    EulerDiscreteScheduler(
													    beta_start=0.00_085      ,				beta_end=0.012      ,				steps_offset=1      ,				beta_schedule="scaled_linear"      ,				timestep_spacing="leading"      ,				)
													torch.manual_seed(0				)
													_snake_case						:     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=128      ,				)
													torch.manual_seed(0				)
													_snake_case						:     Optional[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=1_000      ,				hidden_act="gelu"      ,				projection_dim=32      ,				)
													_snake_case						:     List[str]				=    CLIPTextModel(lowercase_				)
													_snake_case						:     List[Any]				=    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"      ,				local_files_only=lowercase_				)

													_snake_case						:     Dict				=    CLIPTextModelWithProjection(lowercase_				)
													_snake_case						:     int				=    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"      ,				local_files_only=lowercase_				)

													_snake_case						:     Any				=    {
													    "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        UpperCamelCase       (						self      ,				lowercase_      ,				lowercase_=0				):
													if str(lowercase_				).startswith("mps"				):
																			_snake_case						:     Union[str, Any]				=    torch.manual_seed(lowercase_				)
													else:
																			_snake_case						:     List[Any]				=    torch.Generator(device=lowercase_				).manual_seed(lowercase_				)
													_snake_case						:     Dict				=    {
													    "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        UpperCamelCase       (						self				):
													_snake_case						:     Any				=    "cpu"  # ensure determinism for the device-dependent torch.Generator
													_snake_case						:     List[Any]				=    self.get_dummy_components()
													_snake_case						:     Tuple				=    StableDiffusionXLPipeline(**lowercase_				)
													_snake_case						:     Optional[int]				=    sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

													_snake_case						:     Optional[Any]				=    self.get_dummy_inputs(lowercase_				)
													_snake_case						:     List[Any]				=    sd_pipe(**lowercase_				).images
													_snake_case						:     Optional[Any]				=    image[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)
													_snake_case						:     int				=    np.array([0.5_873, 0.6_128, 0.4_797, 0.5_122, 0.5_674, 0.4_639, 0.5_227, 0.5_149, 0.4_747]				)

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



							def        UpperCamelCase       (						self				):
													_snake_case						:     int				=    self.get_dummy_components()
													_snake_case						:     List[str]				=    StableDiffusionXLPipeline(**lowercase_				)
													_snake_case						:     Optional[int]				=    sd_pipe.to(lowercase_				)
													_snake_case						:     List[Any]				=    sd_pipe.to(lowercase_				)
													sd_pipe.set_progress_bar_config(disable=lowercase_				)

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

													_snake_case						:     Any				=    sd_pipe(**lowercase_				)
													_snake_case						:     Any				=    output.images[0, -3:, -3:, -1]

													# forward with prompt embeds
													_snake_case						:     str				=    self.get_dummy_inputs(lowercase_				)
													_snake_case						:     List[Any]				=    3 * ["this is a negative prompt"]
													_snake_case						:     int				=    3 * [inputs.pop("prompt"				)]

													(
													    (
													    _snake_case
													)				,(
													    _snake_case
													)				,(
													    _snake_case
													)				,(
													    _snake_case
													)				,
													)						:     List[str]				=    sd_pipe.encode_prompt(lowercase_      ,				negative_prompt=lowercase_				)

													_snake_case						:     Any				=    sd_pipe(
													    **lowercase_      ,				prompt_embeds=lowercase_      ,				negative_prompt_embeds=lowercase_      ,				pooled_prompt_embeds=lowercase_      ,				negative_pooled_prompt_embeds=lowercase_      ,				)
													_snake_case						:     str				=    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        UpperCamelCase       (						self				):
													super().test_attention_slicing_forward_pass(expected_max_diff=3e-3				)



							def        UpperCamelCase       (						self				):
													super().test_inference_batch_single_identical(expected_max_diff=3e-3				)




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



							def        UpperCamelCase       (						self				):
													super().tearDown()
													gc.collect()
													torch.cuda.empty_cache()



							def        UpperCamelCase       (						self      ,				lowercase_      ,				lowercase_="cpu"      ,				lowercase_=torch.floataa      ,				lowercase_=0				):
													_snake_case						:     str				=    torch.Generator(device=lowercase_				).manual_seed(lowercase_				)
													_snake_case						:     Dict				=    np.random.RandomState(lowercase_				).standard_normal((1, 4, 64, 64)				)
													_snake_case						:     Tuple				=    torch.from_numpy(lowercase_				).to(device=lowercase_      ,				dtype=lowercase_				)
													_snake_case						:     Union[str, Any]				=    {
													    "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        UpperCamelCase       (						self				):
													_snake_case						:     str				=    DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-base"				)
													pipe.to(lowercase_				)
													pipe.set_progress_bar_config(disable=lowercase_				)

													_snake_case						:     Optional[int]				=    self.get_inputs(lowercase_				)
													_snake_case						:     str				=    pipe(**lowercase_				).images
													_snake_case						:     Union[str, Any]				=    image[0, -3:, -3:, -1].flatten()

													assert image.shape == (1, 512, 512, 3)
													_snake_case						:     Optional[Any]				=    np.array([0.49_493, 0.47_896, 0.40_798, 0.54_214, 0.53_212, 0.48_202, 0.47_656, 0.46_329, 0.48_506]				)
													assert np.abs(image_slice - expected_slice				).max() < 7e-3
from collections import OrderedDict
from typing import Mapping

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


__SCREAMING_SNAKE_CASE						:    str     = logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE						:    Union[str, Any]     = {
    'nghuyong/ernie-1.0-base-zh': 'https://huggingface.co/nghuyong/ernie-1.0-base-zh/resolve/main/config.json',
    'nghuyong/ernie-2.0-base-en': 'https://huggingface.co/nghuyong/ernie-2.0-base-en/resolve/main/config.json',
    'nghuyong/ernie-2.0-large-en': 'https://huggingface.co/nghuyong/ernie-2.0-large-en/resolve/main/config.json',
    'nghuyong/ernie-3.0-base-zh': 'https://huggingface.co/nghuyong/ernie-3.0-base-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-medium-zh': 'https://huggingface.co/nghuyong/ernie-3.0-medium-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-mini-zh': 'https://huggingface.co/nghuyong/ernie-3.0-mini-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-micro-zh': 'https://huggingface.co/nghuyong/ernie-3.0-micro-zh/resolve/main/config.json',
    'nghuyong/ernie-3.0-nano-zh': 'https://huggingface.co/nghuyong/ernie-3.0-nano-zh/resolve/main/config.json',
    'nghuyong/ernie-gram-zh': 'https://huggingface.co/nghuyong/ernie-gram-zh/resolve/main/config.json',
    'nghuyong/ernie-health-zh': 'https://huggingface.co/nghuyong/ernie-health-zh/resolve/main/config.json',
}



class    lowercase_    (     __snake_case		):
							_lowerCamelCase						=		'ernie'



							def __init__(						self      ,				lowercase_=30_522      ,				lowercase_=768      ,				lowercase_=12      ,				lowercase_=12      ,				lowercase_=3_072      ,				lowercase_="gelu"      ,				lowercase_=0.1      ,				lowercase_=0.1      ,				lowercase_=512      ,				lowercase_=2      ,				lowercase_=3      ,				lowercase_=False      ,				lowercase_=0.02      ,				lowercase_=1e-12      ,				lowercase_=0      ,				lowercase_="absolute"      ,				lowercase_=True      ,				lowercase_=None      ,				**lowercase_      ,				):
													super().__init__(pad_token_id=lowercase_      ,				**lowercase_				)

													_snake_case						:     List[Any]				=    vocab_size
													_snake_case						:     Dict				=    hidden_size
													_snake_case						:     Any				=    num_hidden_layers
													_snake_case						:     List[str]				=    num_attention_heads
													_snake_case						:     int				=    hidden_act
													_snake_case						:     Tuple				=    intermediate_size
													_snake_case						:     Tuple				=    hidden_dropout_prob
													_snake_case						:     Optional[int]				=    attention_probs_dropout_prob
													_snake_case						:     Any				=    max_position_embeddings
													_snake_case						:     List[str]				=    type_vocab_size
													_snake_case						:     List[Any]				=    task_type_vocab_size
													_snake_case						:     Optional[int]				=    use_task_id
													_snake_case						:     Tuple				=    initializer_range
													_snake_case						:     Union[str, Any]				=    layer_norm_eps
													_snake_case						:     Optional[Any]				=    position_embedding_type
													_snake_case						:     int				=    use_cache
													_snake_case						:     Optional[Any]				=    classifier_dropout




class    lowercase_    (     __snake_case		):



							@property
							def        UpperCamelCase       (						self				):
													if self.task == "multiple-choice":
																			_snake_case						:     str				=    {0: "batch", 1: "choice", 2: "sequence"}
													else:
																			_snake_case						:     Optional[int]				=    {0: "batch", 1: "sequence"}
													return OrderedDict(
													    [
													        ("input_ids", dynamic_axis),
													        ("attention_mask", dynamic_axis),
													        ("token_type_ids", dynamic_axis),
													        ("task_type_ids", dynamic_axis),
													    ]				)