






import random
import unittest

import numpy as np
import torch
from PIL import Image
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorEmbaEmbPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class 							a_ (						a_	,	unittest.TestCase		):


							'''simple docstring'''






							__a:	int     =    KandinskyVaaPriorEmbaEmbPipeline
							__a:	List[Any]     =    ['''prompt''', '''image''']
							__a:	List[str]     =    ['''prompt''', '''image''']
							__a:	int     =    [
							    '''num_images_per_prompt''',
							    '''strength''',
							    '''generator''',
							    '''num_inference_steps''',
							    '''latents''',
							    '''negative_prompt''',
							    '''guidance_scale''',
							    '''output_type''',
							    '''return_dict''',
							]
							__a:	Optional[int]     =    False

							@property
							def      _lowercase       (		self   )					->					Union[str, Any]:
														'''simple docstring'''

														return 3_2

							@property
							def      _lowercase       (		self   )					->					Union[str, Any]:
														'''simple docstring'''

														return 3_2

							@property
							def      _lowercase       (		self   )					->					Optional[Any]:
														'''simple docstring'''

														return self.time_input_dim

							@property
							def      _lowercase       (		self   )					->					Optional[Any]:
														'''simple docstring'''

														return self.time_input_dim * 4

							@property
							def      _lowercase       (		self   )					->					Optional[Any]:
														'''simple docstring'''

														return 1_0_0

							@property
							def      _lowercase       (		self   )					->					Union[str, Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'   )
														return tokenizer

							@property
							def      _lowercase       (		self   )					->					List[str]:
														'''simple docstring'''

														torch.manual_seed(0   )
														lowerCAmelCase_										=     CLIPTextConfig(
														    bos_token_id=0			,			eos_token_id=2			,			hidden_size=self.text_embedder_hidden_size			,			projection_dim=self.text_embedder_hidden_size			,			intermediate_size=3_7			,			layer_norm_eps=1e-05			,			num_attention_heads=4			,			num_hidden_layers=5			,			pad_token_id=1			,			vocab_size=1_0_0_0			,			)
														return CLIPTextModelWithProjection(lowercase_   )

							@property
							def      _lowercase       (		self   )					->					int:
														'''simple docstring'''

														torch.manual_seed(0   )

														lowerCAmelCase_										=     {
														    'num_attention_heads': 2,
														    'attention_head_dim': 1_2,
														    'embedding_dim': self.text_embedder_hidden_size,
														    'num_layers': 1,
														}

														lowerCAmelCase_										=     PriorTransformer(**lowercase_   )
														# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
														lowerCAmelCase_										=     nn.Parameter(torch.ones(model.clip_std.shape   )   )
														return model

							@property
							def      _lowercase       (		self   )					->					Dict:
														'''simple docstring'''

														torch.manual_seed(0   )
														lowerCAmelCase_										=     CLIPVisionConfig(
														    hidden_size=self.text_embedder_hidden_size			,			image_size=2_2_4			,			projection_dim=self.text_embedder_hidden_size			,			intermediate_size=3_7			,			num_attention_heads=4			,			num_channels=3			,			num_hidden_layers=5			,			patch_size=1_4			,			)

														lowerCAmelCase_										=     CLIPVisionModelWithProjection(lowercase_   )
														return model

							@property
							def      _lowercase       (		self   )					->					Tuple:
														'''simple docstring'''

														lowerCAmelCase_										=     CLIPImageProcessor(
														    crop_size=2_2_4			,			do_center_crop=lowercase_			,			do_normalize=lowercase_			,			do_resize=lowercase_			,			image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]			,			image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]			,			resample=3			,			size=2_2_4			,			)

														return image_processor

							def      _lowercase       (		self   )					->					int:
														'''simple docstring'''

														lowerCAmelCase_										=     self.dummy_prior
														lowerCAmelCase_										=     self.dummy_image_encoder
														lowerCAmelCase_										=     self.dummy_text_encoder
														lowerCAmelCase_										=     self.dummy_tokenizer
														lowerCAmelCase_										=     self.dummy_image_processor

														lowerCAmelCase_										=     UnCLIPScheduler(
														    variance_type='fixed_small_log'			,			prediction_type='sample'			,			num_train_timesteps=1_0_0_0			,			clip_sample=lowercase_			,			clip_sample_range=10.0			,			)

														lowerCAmelCase_										=     {
														    'prior': prior,
														    'image_encoder': image_encoder,
														    'text_encoder': text_encoder,
														    'tokenizer': tokenizer,
														    'scheduler': scheduler,
														    'image_processor': image_processor,
														}

														return components

							def      _lowercase       (		self			,			lowercase_			,			lowercase_=0   )					->					Tuple:
														'''simple docstring'''

														if str(lowercase_   ).startswith('mps'   ):
																					lowerCAmelCase_										=     torch.manual_seed(lowercase_   )
														else:
																					lowerCAmelCase_										=     torch.Generator(device=lowercase_   ).manual_seed(lowercase_   )

														lowerCAmelCase_										=     floats_tensor((1, 3, 6_4, 6_4)			,			rng=random.Random(lowercase_   )   ).to(lowercase_   )
														lowerCAmelCase_										=     image.cpu().permute(0			,			2			,			3			,			1   )[0]
														lowerCAmelCase_										=     Image.fromarray(np.uinta(lowercase_   )   ).convert('RGB'   ).resize((2_5_6, 2_5_6)   )

														lowerCAmelCase_										=     {
														    'prompt': 'horse',
														    'image': init_image,
														    'strength': 0.5,
														    'generator': generator,
														    'guidance_scale': 4.0,
														    'num_inference_steps': 2,
														    'output_type': 'np',
														}
														return inputs

							def      _lowercase       (		self   )					->					Optional[Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     'cpu'

														lowerCAmelCase_										=     self.get_dummy_components()

														lowerCAmelCase_										=     self.pipeline_class(**lowercase_   )
														lowerCAmelCase_										=     pipe.to(lowercase_   )

														pipe.set_progress_bar_config(disable=lowercase_   )

														lowerCAmelCase_										=     pipe(**self.get_dummy_inputs(lowercase_   )   )
														lowerCAmelCase_										=     output.image_embeds

														lowerCAmelCase_										=     pipe(
														    **self.get_dummy_inputs(lowercase_   )			,			return_dict=lowercase_			,			)[0]

														lowerCAmelCase_										=     image[0, -1_0:]
														lowerCAmelCase_										=     image_from_tuple[0, -1_0:]

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

														lowerCAmelCase_										=     np.array(
														    [
														        0.1_07_12_84,
														        1.3_33_02_71,
														        0.61_26_02_23,
														        -0.6_69_10_65,
														        -0.3_84_68_52,
														        -1.0_30_36_61,
														        0.22_71_61_11,
														        0.03_34_89_01,
														        0.30_04_06_75,
														        -0.24_80_50_29,
														    ]   )

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

							@skip_mps
							def      _lowercase       (		self   )					->					int:
														'''simple docstring'''

														lowerCAmelCase_										=     torch_device == 'cpu'
														lowerCAmelCase_										=     True
														lowerCAmelCase_										=     False

														self._test_inference_batch_single_identical(
														    test_max_difference=lowercase_			,			relax_max_difference=lowercase_			,			test_mean_pixel_difference=lowercase_			,			)






							@skip_mps
							def      _lowercase       (		self   )					->					int:
														'''simple docstring'''

														lowerCAmelCase_										=     torch_device == 'cpu'
														lowerCAmelCase_										=     False

														self._test_attention_slicing_forward_pass(
														    test_max_difference=lowercase_			,			test_mean_pixel_difference=lowercase_			,			)











import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


lowerCamelCase_						     =  logging.get_logger(__name__)




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self   )					->					Any:
														'''simple docstring'''

														super().__init__()
														if version.parse(torch.__version__   ) < version.parse('1.12.0'   ):
																					raise ImportError(
																					    f'''You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '''
																					    'PytorchGELUTanh. Please upgrade torch.'   )






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return nn.functional.gelu(lowercase_			,			approximate='tanh'   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi   ) * (input + 0.04_47_15 * torch.pow(lowercase_			,			3.0   ))   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self			,			lowercase_ = False   )					->					Optional[Any]:
														'''simple docstring'''

														super().__init__()
														if use_gelu_python:
																					lowerCAmelCase_										=     self._gelu_python
														else:
																					lowerCAmelCase_										=     nn.functional.gelu

							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0   )   ))






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return self.act(lowercase_   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return 0.5 * input * (1.0 + torch.tanh(input * 0.79_78_84_56_08 * (1.0 + 0.04_47_15 * input * input)   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input * torch.sigmoid(1.7_02 * input   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self			,			lowercase_			,			lowercase_   )					->					Optional[int]:
														'''simple docstring'''

														if min > max:
																					raise ValueError(f'''min should be < max (got min: {min}, max: {max})'''   )

														super().__init__()
														lowerCAmelCase_										=     min
														lowerCAmelCase_										=     max






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return torch.clip(gelu(lowercase_   )			,			self.min			,			self.max   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self   )					->					Union[str, Any]:
														'''simple docstring'''

														super().__init__()
														lowerCAmelCase_										=     math.sqrt(2 / math.pi   )






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_47_15 * torch.pow(lowercase_			,			3   ))   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return nn.functional.silu(lowercase_   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def __init__(		self   )					->					List[Any]:
														'''simple docstring'''

														super().__init__()
														if version.parse(torch.__version__   ) < version.parse('1.9.0'   ):
																					lowerCAmelCase_										=     self._mish_python
														else:
																					lowerCAmelCase_										=     nn.functional.mish

							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input * torch.tanh(nn.functional.softplus(lowercase_   )   )






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return self.act(lowercase_   )




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Tensor:
														'''simple docstring'''

														return input




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_			,			lowercase_=0.70_71_07			,			lowercase_=0.28_20_95   )					->					Optional[Any]:
														'''simple docstring'''

														lowerCAmelCase_										=     (input - mu).div(sigma * math.sqrt(2.0   )   )
														return 0.5 * (1.0 + torch.erf(lowercase_   ))




class 							a_ (						nn.Module		):


							'''simple docstring'''






							def      _lowercase       (		self			,			lowercase_   )					->					Any:
														'''simple docstring'''

														lowerCAmelCase_										=     nn.functional.relu(lowercase_   )
														lowerCAmelCase_										=     torch.square(lowercase_   )
														return squared




class 							a_ (						a_		):


							'''simple docstring'''






							def __getitem__(		self			,			lowercase_   )					->					Any:
														'''simple docstring'''

														lowerCAmelCase_										=     super().__getitem__(lowercase_   )
														lowerCAmelCase_						,   lowerCAmelCase_										=     content if isinstance(lowercase_			,			lowercase_   ) else (content, {})
														return cls(**lowercase_   )


lowerCamelCase_						     =  {
    """gelu""": GELUActivation,
    """gelu_10""": (ClippedGELUActivation, {"""min""": -1_0, """max""": 1_0}),
    """gelu_fast""": FastGELUActivation,
    """gelu_new""": NewGELUActivation,
    """gelu_python""": (GELUActivation, {"""use_gelu_python""": True}),
    """gelu_pytorch_tanh""": PytorchGELUTanh,
    """gelu_accurate""": AccurateGELUActivation,
    """laplace""": LaplaceActivation,
    """linear""": LinearActivation,
    """mish""": MishActivation,
    """quick_gelu""": QuickGELUActivation,
    """relu""": nn.ReLU,
    """relu2""": ReLUSquaredActivation,
    """relu6""": nn.ReLUa,
    """sigmoid""": nn.Sigmoid,
    """silu""": SiLUActivation,
    """swish""": SiLUActivation,
    """tanh""": nn.Tanh,
}
lowerCamelCase_						     =  ClassInstantier(ACTaCLS)
def      lowerCamelCase					(						a_	)	->			str:
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(F'''function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()	)}'''	)


# For backwards compatibility with: from activations import gelu_python
lowerCamelCase_						     =  get_activation("""gelu_python""")
lowerCamelCase_						     =  get_activation("""gelu_new""")
lowerCamelCase_						     =  get_activation("""gelu""")
lowerCamelCase_						     =  get_activation("""gelu_fast""")
lowerCamelCase_						     =  get_activation("""quick_gelu""")
lowerCamelCase_						     =  get_activation("""silu""")
lowerCamelCase_						     =  get_activation("""mish""")



lowerCamelCase_						     =  get_activation("""linear""")



