import gc
import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLImgaImgPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class       lowercase__		(     lowercase__			,							lowercase__			,							unittest.TestCase):
      UpperCamelCase_  =	StableDiffusionXLImgaImgPipeline
      UpperCamelCase_  =	TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width"""}
      UpperCamelCase_  =	PipelineTesterMixin.required_optional_params - {"""latents"""}
      UpperCamelCase_  =	TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
      UpperCamelCase_  =	IMAGE_TO_IMAGE_IMAGE_PARAMS
      UpperCamelCase_  =	IMAGE_TO_IMAGE_IMAGE_PARAMS


      def  __A ( self			:       Union[str, Any]		):





            '''simple docstring'''





            torch.manual_seed(0		)
            SCREAMING_SNAKE_CASE							:					List[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=UpperCamelCase__					,	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					,	)
            SCREAMING_SNAKE_CASE							:					str           =							EulerDiscreteScheduler(
                beta_start=0.0_0085					,	beta_end=0.012					,	steps_offset=1					,	beta_schedule='''scaled_linear'''					,	timestep_spacing='''leading'''					,	)
            torch.manual_seed(0		)
            SCREAMING_SNAKE_CASE							:					Tuple           =							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		)
            SCREAMING_SNAKE_CASE							:					int           =							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					,	hidden_act='''gelu'''					,	projection_dim=32					,	)
            SCREAMING_SNAKE_CASE							:					Tuple           =							CLIPTextModel(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					,	local_files_only=UpperCamelCase__		)

            SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							CLIPTextModelWithProjection(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					str           =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					,	local_files_only=UpperCamelCase__		)

            SCREAMING_SNAKE_CASE							:					str           =							{
                "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  __A ( self			:       Dict					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       Union[str, Any]=0		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					Optional[int]           =							floats_tensor((1, 3, 32, 32)					,	rng=random.Random(UpperCamelCase__		)		).to(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							image / 2 + 0.5
            if str(UpperCamelCase__		).startswith('''mps'''		):
                  SCREAMING_SNAKE_CASE							:					Tuple           =							torch.manual_seed(UpperCamelCase__		)
            else:
                  SCREAMING_SNAKE_CASE							:					Optional[Any]           =							torch.Generator(device=UpperCamelCase__		).manual_seed(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					int           =							{
                "prompt": "A painting of a squirrel eating a burger",
                "image": image,
                "generator": generator,
                "num_inference_steps": 2,
                "guidance_scale": 5.0,
                "output_type": "numpy",
                "strength": 0.75,
            }
            return inputs


      def  __A ( self			:       Any		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					Optional[int]           =							"cpu"  # ensure determinism for the device-dependent torch.Generator
            SCREAMING_SNAKE_CASE							:					int           =							self.get_dummy_components()
            SCREAMING_SNAKE_CASE							:					str           =							StableDiffusionXLImgaImgPipeline(**UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					str           =							sd_pipe.to(UpperCamelCase__		)
            sd_pipe.set_progress_bar_config(disable=UpperCamelCase__		)

            SCREAMING_SNAKE_CASE							:					Optional[int]           =							self.get_dummy_inputs(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							sd_pipe(**UpperCamelCase__		).images
            SCREAMING_SNAKE_CASE							:					Tuple           =							image[0, -3:, -3:, -1]

            assert image.shape == (1, 32, 32, 3)

            SCREAMING_SNAKE_CASE							:					List[Any]           =							np.array([0.4656, 0.4840, 0.4439, 0.6698, 0.5574, 0.4524, 0.5799, 0.5943, 0.5165]		)

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


      def  __A ( self			:       Tuple		):





            '''simple docstring'''





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


      def  __A ( self			:       Any		):





            '''simple docstring'''





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


      def  __A ( self			:       Dict		):





            '''simple docstring'''





            pass



      def  __A ( self			:       Optional[int]		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					List[Any]           =							self.get_dummy_components()
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							StableDiffusionXLImgaImgPipeline(**UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					str           =							sd_pipe.to(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					Optional[Any]           =							sd_pipe.to(UpperCamelCase__		)
            sd_pipe.set_progress_bar_config(disable=UpperCamelCase__		)

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

            SCREAMING_SNAKE_CASE							:					Optional[int]           =							sd_pipe(**UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							output.images[0, -3:, -3:, -1]

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

            (
                SCREAMING_SNAKE_CASE							
            )							:					Optional[Any]           =							sd_pipe.encode_prompt(UpperCamelCase__					,	negative_prompt=UpperCamelCase__		)

            SCREAMING_SNAKE_CASE							:					Tuple           =							sd_pipe(
                **UpperCamelCase__					,	prompt_embeds=UpperCamelCase__					,	negative_prompt_embeds=UpperCamelCase__					,	pooled_prompt_embeds=UpperCamelCase__					,	negative_pooled_prompt_embeds=UpperCamelCase__					,	)
            SCREAMING_SNAKE_CASE							:					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






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


      def  __A ( self			:       Optional[int]		):





            '''simple docstring'''





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


      def  __A ( self			:       int					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Dict="cpu"					,	UpperCamelCase__			:       Tuple=torch.floataa					,	UpperCamelCase__			:       List[str]=0		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					int           =							torch.Generator(device=UpperCamelCase__		).manual_seed(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					str           =							np.random.RandomState(UpperCamelCase__		).standard_normal((1, 4, 64, 64)		)
            SCREAMING_SNAKE_CASE							:					Optional[int]           =							torch.from_numpy(UpperCamelCase__		).to(device=UpperCamelCase__					,	dtype=UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					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  __A ( self			:       Any		):





            '''simple docstring'''





            SCREAMING_SNAKE_CASE							:					Dict           =							DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''		)
            pipe.to(UpperCamelCase__		)
            pipe.set_progress_bar_config(disable=UpperCamelCase__		)

            SCREAMING_SNAKE_CASE							:					List[str]           =							self.get_inputs(UpperCamelCase__		)
            SCREAMING_SNAKE_CASE							:					List[Any]           =							pipe(**UpperCamelCase__		).images
            SCREAMING_SNAKE_CASE							:					str           =							image[0, -3:, -3:, -1].flatten()

            assert image.shape == (1, 512, 512, 3)
            SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							np.array([0.4_9493, 0.4_7896, 0.4_0798, 0.5_4214, 0.5_3212, 0.4_8202, 0.4_7656, 0.4_6329, 0.4_8506]		)
            assert np.abs(image_slice - expected_slice		).max() < 7E-3






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

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


if TYPE_CHECKING:
					from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


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

__UpperCamelCase	: Optional[Any]    		=  {
    'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json'
    ),
    'microsoft/deberta-v2-xxlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json'
    ),
}





class       lowercase__		(     UpperCamelCase_):
						UpperCamelCase_  =	"""deberta-v2"""


						def __init__( self			:       Optional[Any]					,	UpperCamelCase__			:       Any=12_8100					,	UpperCamelCase__			:       Optional[int]=1536					,	UpperCamelCase__			:       Dict=24					,	UpperCamelCase__			:       List[str]=24					,	UpperCamelCase__			:       Tuple=6144					,	UpperCamelCase__			:       List[Any]="gelu"					,	UpperCamelCase__			:       Optional[int]=0.1					,	UpperCamelCase__			:       List[str]=0.1					,	UpperCamelCase__			:       Optional[int]=512					,	UpperCamelCase__			:       Optional[Any]=0					,	UpperCamelCase__			:       Union[str, Any]=0.02					,	UpperCamelCase__			:       List[Any]=1E-7					,	UpperCamelCase__			:       List[str]=False					,	UpperCamelCase__			:       str=-1					,	UpperCamelCase__			:       List[Any]=0					,	UpperCamelCase__			:       List[Any]=True					,	UpperCamelCase__			:       Optional[Any]=None					,	UpperCamelCase__			:       Tuple=0					,	UpperCamelCase__			:       str="gelu"					,	**UpperCamelCase__			:       Optional[int]					,	):





												'''simple docstring'''





												super().__init__(**UpperCamelCase__		)

												SCREAMING_SNAKE_CASE							:					List[Any]           =							hidden_size
												SCREAMING_SNAKE_CASE							:					int           =							num_hidden_layers
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							num_attention_heads
												SCREAMING_SNAKE_CASE							:					List[Any]           =							intermediate_size
												SCREAMING_SNAKE_CASE							:					Any           =							hidden_act
												SCREAMING_SNAKE_CASE							:					Dict           =							hidden_dropout_prob
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE							:					str           =							max_position_embeddings
												SCREAMING_SNAKE_CASE							:					Dict           =							type_vocab_size
												SCREAMING_SNAKE_CASE							:					str           =							initializer_range
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							relative_attention
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							max_relative_positions
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							pad_token_id
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							position_biased_input

												# Backwards compatibility
												if type(UpperCamelCase__		) == str:
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							[x.strip() for x in pos_att_type.lower().split('''|'''		)]

												SCREAMING_SNAKE_CASE							:					Any           =							pos_att_type
												SCREAMING_SNAKE_CASE							:					int           =							vocab_size
												SCREAMING_SNAKE_CASE							:					Dict           =							layer_norm_eps

												SCREAMING_SNAKE_CASE							:					Tuple           =							kwargs.get('''pooler_hidden_size'''					,	UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Any           =							pooler_dropout
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							pooler_hidden_act






class       lowercase__		(     UpperCamelCase_):


						@property
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												if self.task == "multiple-choice":
																		SCREAMING_SNAKE_CASE							:					Optional[int]           =							{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
												else:
																		SCREAMING_SNAKE_CASE							:					Optional[Any]           =							{0: '''batch''', 1: '''sequence'''}
												if self._config.type_vocab_size > 0:
																		return OrderedDict(
																		    [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)]		)
												else:
																		return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)]		)


						@property
						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												return 12



						def  __A ( self			:       Dict					,	UpperCamelCase__			:       Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]					,	UpperCamelCase__			:       int = -1					,	UpperCamelCase__			:       int = -1					,	UpperCamelCase__			:       int = -1					,	UpperCamelCase__			:       bool = False					,	UpperCamelCase__			:       Optional["TensorType"] = None					,	UpperCamelCase__			:       int = 3					,	UpperCamelCase__			:       int = 40					,	UpperCamelCase__			:       int = 40					,	UpperCamelCase__			:       "PreTrainedTokenizerBase" = None					,	):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							super().generate_dummy_inputs(preprocessor=UpperCamelCase__					,	framework=UpperCamelCase__		)
												if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
																		del dummy_inputs["token_type_ids"]
												return dummy_inputs





