




import unittest

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

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

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class  A		(  __lowercase      ,				unittest.TestCase     ):
		__snake_case									=   KandinskyVaaPriorPipeline
		__snake_case									=   ["prompt"]
		__snake_case									=   ["prompt", "negative_prompt"]
		__snake_case									=   [
		    "num_images_per_prompt",
		    "generator",
		    "num_inference_steps",
		    "latents",
		    "negative_prompt",
		    "guidance_scale",
		    "output_type",
		    "return_dict",
		]
		__snake_case									=   False



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return 32



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return 32



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return self.time_input_dim



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return self.time_input_dim * 4



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								return 100



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



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



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""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=37,	layer_norm_eps=1E-05,	num_attention_heads=4,	num_hidden_layers=5,	pad_token_id=1,	vocab_size=1000,	)
								return CLIPTextModelWithProjection(_a   )



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								torch.manual_seed(0   )

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

								lowerCAmelCase_      			=  PriorTransformer(**_a   )
								# 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        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								torch.manual_seed(0   )
								lowerCAmelCase_      			=  CLIPVisionConfig(
								    hidden_size=self.text_embedder_hidden_size,	image_size=224,	projection_dim=self.text_embedder_hidden_size,	intermediate_size=37,	num_attention_heads=4,	num_channels=3,	num_hidden_layers=5,	patch_size=14,	)

								lowerCAmelCase_      			=  CLIPVisionModelWithProjection(_a   )
								return model



		@property
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  CLIPImageProcessor(
								    crop_size=224,	do_center_crop=_a,	do_normalize=_a,	do_resize=_a,	image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073],	image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711],	resample=3,	size=224,	)

								return image_processor



		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""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=1000,	clip_sample=_a,	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        SCREAMING_SNAKE_CASE__			(     self,	UpperCamelCase__,	UpperCamelCase__=0   ):







								"""simple docstring"""



								if str(_a   ).startswith('''mps'''   ):
														lowerCAmelCase_      			=  torch.manual_seed(_a   )
								else:
														lowerCAmelCase_      			=  torch.Generator(device=_a   ).manual_seed(_a   )
								lowerCAmelCase_      			=  {
								    '''prompt''': '''horse''',
								    '''generator''': generator,
								    '''guidance_scale''': 4.0,
								    '''num_inference_steps''': 2,
								    '''output_type''': '''np''',
								}
								return inputs



		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



								lowerCAmelCase_      			=  '''cpu'''

								lowerCAmelCase_      			=  self.get_dummy_components()

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

								pipe.set_progress_bar_config(disable=_a   )

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

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

								lowerCAmelCase_      			=  image[0, -10:]
								lowerCAmelCase_      			=  image_from_tuple[0, -10:]

								assert image.shape == (1, 32)

								lowerCAmelCase_      			=  np.array(
								    [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]   )

								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        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



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

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







		@skip_mps
		def        SCREAMING_SNAKE_CASE__			(     self   ):







								"""simple docstring"""



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

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







def    __UpperCAmelCase (	__a      :			str	)			->     int:

					"""simple docstring"""

					assert column_title.isupper()
					_a	:    Optional[Any]         =		0
					_a	:    List[Any]         =		len(__a	) - 1
					_a	:    List[str]         =		0

					while index >= 0:
										_a	:    Dict         =		(ord(column_title[index]	) - 64) * pow(26		,__a	)
										answer += value
										power += 1
										index -= 1

					return answer


if __name__ == "__main__":
			from doctest import testmod

			testmod()



