


"""simple docstring"""


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

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


if TYPE_CHECKING:
    from ...feature_extraction_utils import FeatureExtractionMixin
    from ...tokenization_utils_base import PreTrainedTokenizerBase
    from ...utils import TensorType

__lowerCamelCase      :Any	    =		logging.get_logger(__name__)

__lowerCamelCase      :List[str]	    =		{
    'openai/whisper-base': 'https://huggingface.co/openai/whisper-base/resolve/main/config.json',
}

# fmt: off
__lowerCamelCase      :Optional[Any]	    =		[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 357, 366, 438, 532, 685,
    705, 796, 930, 1_058, 1_220, 1_267, 1_279, 1_303, 1_343, 1_377,
    1_391, 1_635, 1_782, 1_875, 2_162, 2_361, 2_488, 3_467, 4_008, 4_211,
    4_600, 4_808, 5_299, 5_855, 6_329, 7_203, 9_609, 9_959, 10_563, 10_786,
    11_420, 11_709, 11_907, 13_163, 13_697, 13_700, 14_808, 15_306, 16_410, 16_791,
    17_992, 19_203, 19_510, 20_724, 22_305, 22_935, 27_007, 30_109, 30_420, 33_409,
    34_949, 40_283, 40_493, 40_549, 47_282, 49_146, 50_257, 50_359, 50_360, 50_361
]
__lowerCamelCase      :List[Any]	    =		[
    1, 2, 7, 8, 9, 10, 14, 25,
    26, 27, 28, 29, 31, 58, 59, 60, 61, 62,
    63, 90, 91, 92, 93, 359, 503, 522, 542, 873,
    893, 902, 918, 922, 931, 1_350, 1_853, 1_982, 2_460, 2_627,
    3_246, 3_253, 3_268, 3_536, 3_846, 3_961, 4_183, 4_667, 6_585, 6_647,
    7_273, 9_061, 9_383, 10_428, 10_929, 11_938, 12_033, 12_331, 12_562, 13_793,
    14_157, 14_635, 15_265, 15_618, 16_553, 16_604, 18_362, 18_956, 20_075, 21_675,
    22_520, 26_130, 26_161, 26_435, 28_279, 29_464, 31_650, 32_302, 32_470, 36_865,
    42_863, 47_425, 49_870, 50_254, 50_258, 50_360, 50_361, 50_362
]


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Optional[Any]													='''whisper'''
     snake_case__	:					Optional[Any]													=['''past_key_values''']
     snake_case__	:					Union[str, Any]													={'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}

     def __init__(	self:						Optional[int]     ,		__a:						int=51_865     ,		__a:						str=80     ,		__a:						int=6     ,		__a:						Union[str, Any]=4     ,		__a:						Union[str, Any]=6     ,		__a:						Union[str, Any]=4     ,		__a:						str=1_536     ,		__a:						Any=1_536     ,		__a:						int=0.0     ,		__a:						Optional[Any]=0.0     ,		__a:						Dict=50_257     ,		__a:						str=True     ,		__a:						Optional[Any]=True     ,		__a:						List[Any]="gelu"     ,		__a:						Optional[int]=256     ,		__a:						Tuple=0.0     ,		__a:						Union[str, Any]=0.0     ,		__a:						Union[str, Any]=0.0     ,		__a:						str=0.02     ,		__a:						Tuple=False     ,		__a:						Optional[Any]=1_500     ,		__a:						Optional[Any]=448     ,		__a:						List[str]=50_256     ,		__a:						int=50_256     ,		__a:						Tuple=50_256     ,		__a:						str=None     ,		__a:						Union[str, Any]=[220, 50_256]     ,		__a:						Optional[int]=False     ,		__a:						str=256     ,		__a:						str=False     ,		__a:						Tuple=0.05     ,		__a:						Union[str, Any]=10     ,		__a:						Tuple=2     ,		__a:						Optional[int]=0.0     ,		__a:						List[Any]=10     ,		__a:						Any=0     ,		__a:						Optional[int]=7     ,		**__a:						Tuple     ,		)->    str:
           lowerCamelCase					:					Optional[int]        =	vocab_size
           lowerCamelCase					:					List[str]        =	num_mel_bins
           lowerCamelCase					:					Dict        =	d_model
           lowerCamelCase					:					int        =	encoder_layers
           lowerCamelCase					:					Union[str, Any]        =	encoder_attention_heads
           lowerCamelCase					:					Any        =	decoder_layers
           lowerCamelCase					:					Optional[Any]        =	decoder_attention_heads
           lowerCamelCase					:					Dict        =	decoder_ffn_dim
           lowerCamelCase					:					List[Any]        =	encoder_ffn_dim
           lowerCamelCase					:					str        =	dropout
           lowerCamelCase					:					Tuple        =	attention_dropout
           lowerCamelCase					:					Tuple        =	activation_dropout
           lowerCamelCase					:					List[Any]        =	activation_function
           lowerCamelCase					:					List[str]        =	init_std
           lowerCamelCase					:					Union[str, Any]        =	encoder_layerdrop
           lowerCamelCase					:					List[Any]        =	decoder_layerdrop
           lowerCamelCase					:					List[str]        =	use_cache
           lowerCamelCase					:					int        =	encoder_layers
           lowerCamelCase					:					Any        =	scale_embedding  # scale factor will be sqrt(d_model) if True
           lowerCamelCase					:					Dict        =	max_source_positions
           lowerCamelCase					:					Any        =	max_target_positions

           # Audio Classification-specific parameters. Feel free to ignore for other classes.
           lowerCamelCase					:					int        =	classifier_proj_size
           lowerCamelCase					:					int        =	use_weighted_layer_sum

           # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
           lowerCamelCase					:					Any        =	apply_spec_augment
           lowerCamelCase					:					int        =	mask_time_prob
           lowerCamelCase					:					Optional[Any]        =	mask_time_length
           lowerCamelCase					:					Dict        =	mask_time_min_masks
           lowerCamelCase					:					Any        =	mask_feature_prob
           lowerCamelCase					:					Dict        =	mask_feature_length
           lowerCamelCase					:					List[str]        =	mask_feature_min_masks

           lowerCamelCase					:					int        =	median_filter_width

           super().__init__(
               pad_token_id=__a     ,		bos_token_id=__a     ,		eos_token_id=__a     ,		is_encoder_decoder=__a     ,		decoder_start_token_id=__a     ,		suppress_tokens=__a     ,		begin_suppress_tokens=__a     ,		**__a     ,		)


class 			A__	(						__lowercase):


     """simple docstring"""

     @property
     def 		a__						(	self:						Tuple	)->    Mapping[str, Mapping[int, str]]:
           lowerCamelCase					:					Tuple        =	OrderedDict(
               [
                   ("""input_features""", {0: """batch""", 1: """feature_size""", 2: """encoder_sequence"""}),
               ]	)
           if self.use_past:
                 lowerCamelCase					:					List[str]        =	{0: """batch"""}
           else:
                 lowerCamelCase					:					Tuple        =	{0: """batch""", 1: """decoder_sequence"""}

           if self.use_past:
                 self.fill_with_past_key_values_(__a     ,		direction="""inputs"""	)

           return common_inputs

     def 		a__						(	self:						int     ,		__a:						Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]     ,		__a:						int = -1     ,		__a:						int = -1     ,		__a:						bool = False     ,		__a:						Optional["TensorType"] = None     ,		__a:						int = 22_050     ,		__a:						float = 5.0     ,		__a:						int = 220     ,		)->    Mapping[str, Any]:
           lowerCamelCase					:					Dict        =	OrderedDict()
           lowerCamelCase					:					List[str]        =	OnnxConfig.generate_dummy_inputs(
               self     ,		preprocessor=preprocessor.feature_extractor     ,		batch_size=__a     ,		framework=__a     ,		sampling_rate=__a     ,		time_duration=__a     ,		frequency=__a     ,		)
           lowerCamelCase					:					Any        =	encoder_inputs["""input_features"""].shape[2]
           lowerCamelCase					:					str        =	encoder_sequence_length // 2 if self.use_past else seq_length

           lowerCamelCase					:					List[str]        =	super().generate_dummy_inputs(
               preprocessor.tokenizer     ,		__a     ,		__a     ,		__a     ,		__a	)

           lowerCamelCase					:					Union[str, Any]        =	encoder_inputs.pop("""input_features"""	)
           lowerCamelCase					:					List[Any]        =	decoder_inputs.pop("""decoder_input_ids"""	)

           if "past_key_values" in decoder_inputs:
                 lowerCamelCase					:					List[Any]        =	decoder_inputs.pop("""past_key_values"""	)

           return dummy_inputs







     @property
     def 		a__						(	self:						List[str]	)->    float:
           return 1e-3








"""simple docstring"""


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 			A__	(						__lowercase ,     __lowercase ,     unittest.TestCase):


     """simple docstring"""
     snake_case__	:					str													=StableDiffusionXLImgaImgPipeline
     snake_case__	:					Any													=TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {'''height''', '''width'''}
     snake_case__	:					Optional[int]													=PipelineTesterMixin.required_optional_params - {'''latents'''}
     snake_case__	:					Dict													=TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     snake_case__	:					Tuple													=IMAGE_TO_IMAGE_IMAGE_PARAMS
     snake_case__	:					List[str]													=IMAGE_TO_IMAGE_IMAGE_PARAMS

     def 		a__						(	self:						List[str]	)->    int:
           torch.manual_seed(0	)
           lowerCamelCase					:					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=__a     ,		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     ,		)
           lowerCamelCase					:					Any        =	EulerDiscreteScheduler(
               beta_start=0.0_00_85     ,		beta_end=0.0_12     ,		steps_offset=1     ,		beta_schedule="""scaled_linear"""     ,		timestep_spacing="""leading"""     ,		)
           torch.manual_seed(0	)
           lowerCamelCase					:					Any        =	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	)
           lowerCamelCase					:					Optional[Any]        =	CLIPTextConfig(
               bos_token_id=0     ,		eos_token_id=2     ,		hidden_size=32     ,		intermediate_size=37     ,		layer_norm_eps=1e-0_5     ,		num_attention_heads=4     ,		num_hidden_layers=5     ,		pad_token_id=1     ,		vocab_size=1_000     ,		hidden_act="""gelu"""     ,		projection_dim=32     ,		)
           lowerCamelCase					:					Dict        =	CLIPTextModel(__a	)
           lowerCamelCase					:					Union[str, Any]        =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""     ,		local_files_only=__a	)

           lowerCamelCase					:					Dict        =	CLIPTextModelWithProjection(__a	)
           lowerCamelCase					:					Optional[int]        =	CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""     ,		local_files_only=__a	)

           lowerCamelCase					:					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:						Any     ,		__a:						str     ,		__a:						Tuple=0	)->    Union[str, Any]:
           lowerCamelCase					:					List[Any]        =	floats_tensor((1, 3, 32, 32)     ,		rng=random.Random(__a	)	).to(__a	)
           lowerCamelCase					:					Any        =	image / 2 + 0.5
           if str(__a	).startswith("""mps"""	):
                 lowerCamelCase					:					Dict        =	torch.manual_seed(__a	)
           else:
                 lowerCamelCase					:					Tuple        =	torch.Generator(device=__a	).manual_seed(__a	)
           lowerCamelCase					:					Tuple        =	{
               """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:						Dict	)->    Optional[Any]:
           lowerCamelCase					:					Any        =	"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           lowerCamelCase					:					Union[str, Any]        =	self.get_dummy_components()
           lowerCamelCase					:					Optional[int]        =	StableDiffusionXLImgaImgPipeline(**__a	)
           lowerCamelCase					:					int        =	sd_pipe.to(__a	)
           sd_pipe.set_progress_bar_config(disable=__a	)

           lowerCamelCase					:					Optional[Any]        =	self.get_dummy_inputs(__a	)
           lowerCamelCase					:					Optional[int]        =	sd_pipe(**__a	).images
           lowerCamelCase					:					Tuple        =	image[0, -3:, -3:, -1]

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

           lowerCamelCase					:					Any        =	np.array([0.46_56, 0.48_40, 0.44_39, 0.66_98, 0.55_74, 0.45_24, 0.57_99, 0.59_43, 0.51_65]	)

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

     def 		a__						(	self:						Optional[int]	)->    Union[str, Any]:
           super().test_attention_slicing_forward_pass(expected_max_diff=3e-3	)

     def 		a__						(	self:						Optional[Any]	)->    str:
           super().test_inference_batch_single_identical(expected_max_diff=3e-3	)

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







     def 		a__						(	self:						List[Any]	)->    Union[str, Any]:
           lowerCamelCase					:					Tuple        =	self.get_dummy_components()
           lowerCamelCase					:					Union[str, Any]        =	StableDiffusionXLImgaImgPipeline(**__a	)
           lowerCamelCase					:					str        =	sd_pipe.to(__a	)
           lowerCamelCase					:					Any        =	sd_pipe.to(__a	)
           sd_pipe.set_progress_bar_config(disable=__a	)

           # forward without prompt embeds
           lowerCamelCase					:					Dict        =	self.get_dummy_inputs(__a	)
           lowerCamelCase					:					Any        =	3 * ["""this is a negative prompt"""]
           lowerCamelCase					:					Optional[int]        =	negative_prompt
           lowerCamelCase					:					Tuple        =	3 * [inputs["""prompt"""]]

           lowerCamelCase					:					List[Any]        =	sd_pipe(**__a	)
           lowerCamelCase					:					Optional[int]        =	output.images[0, -3:, -3:, -1]

           # forward with prompt embeds
           lowerCamelCase					:					Tuple        =	self.get_dummy_inputs(__a	)
           lowerCamelCase					:					List[Any]        =	3 * ["""this is a negative prompt"""]
           lowerCamelCase					:					Tuple        =	3 * [inputs.pop("""prompt"""	)]

           (
               (
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			(
               lowerCamelCase
           )     ,			
           )					:					Union[str, Any]        =	sd_pipe.encode_prompt(__a     ,		negative_prompt=__a	)

           lowerCamelCase					:					int        =	sd_pipe(
               **__a     ,		prompt_embeds=__a     ,		negative_prompt_embeds=__a     ,		pooled_prompt_embeds=__a     ,		negative_pooled_prompt_embeds=__a     ,		)
           lowerCamelCase					:					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 			A__	(						unittest.TestCase):


     """simple docstring"""

     def 		a__						(	self:						Dict	)->    str:
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()

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

           lowerCamelCase					:					Optional[int]        =	self.get_inputs(__a	)
           lowerCamelCase					:					Optional[Any]        =	pipe(**__a	).images
           lowerCamelCase					:					Dict        =	image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 512, 512, 3)
           lowerCamelCase					:					List[str]        =	np.array([0.4_94_93, 0.4_78_96, 0.4_07_98, 0.5_42_14, 0.5_32_12, 0.4_82_02, 0.4_76_56, 0.4_63_29, 0.4_85_06]	)
           assert np.abs(image_slice - expected_slice	).max() < 7e-3




