



# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import sys
import warnings
from os.path import abspath, dirname, join


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
_snake_case		=  abspath(join(dirname(dirname(dirname(__file__))), "src"))
sys.path.insert(1, git_repo_path)


# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="ignore", category=FutureWarning)






def 				A				(					_lowerCamelCase  ):





							'''simple docstring'''




							from transformers.testing_utils import pytest_addoption_shared

							pytest_addoption_shared(_lowerCamelCase  )






def 				A				(					_lowerCamelCase  ):





							'''simple docstring'''




							from transformers.testing_utils import pytest_terminal_summary_main

							_lowerCAmelCase     :							int    =    terminalreporter.config.getoption("--make-reports"  )
							if make_reports:
														pytest_terminal_summary_main(_lowerCamelCase				, id=_lowerCamelCase  )


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




	'''simple docstring'''




	_A								=     KandinskyVaaPriorPipeline
	_A								=     ['prompt']
	_A								=     ['prompt', 'negative_prompt']
	_A								=     [
	    'num_images_per_prompt',
	    'generator',
	    'num_inference_steps',
	    'latents',
	    'negative_prompt',
	    'guidance_scale',
	    'output_type',
	    'return_dict',
	]
	_A								=     False







	@property
	def    _lowerCamelCase				(					self		:Tuple						)		-> Optional[Any]:
								return 3_2







	@property
	def    _lowerCamelCase				(					self		:List[str]						)		-> List[str]:
								return 3_2







	@property
	def    _lowerCamelCase				(					self		:Union[str, Any]						)		-> Tuple:
								return self.time_input_dim







	@property
	def    _lowerCamelCase				(					self		:Union[str, Any]						)		-> Any:
								return self.time_input_dim * 4







	@property
	def    _lowerCamelCase				(					self		:Tuple						)		-> str:
								return 1_0_0







	@property
	def    _lowerCamelCase				(					self		:Union[str, Any]						)		-> Any:
								__UpperCamelCase	:   int			   =					CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"						)
								return tokenizer







	@property
	def    _lowerCamelCase				(					self		:str						)		-> int:
								torch.manual_seed(0						)
								__UpperCamelCase	:   Optional[int]			   =					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-0_5     ,   num_attention_heads=4     ,   num_hidden_layers=5     ,   pad_token_id=1     ,   vocab_size=1_0_0_0     ,   )
								return CLIPTextModelWithProjection(a						)







	@property
	def    _lowerCamelCase				(					self		:int						)		-> Optional[Any]:
								torch.manual_seed(0						)

								__UpperCamelCase	:   Optional[int]			   =					{
								    "num_attention_heads": 2,
								    "attention_head_dim": 1_2,
								    "embedding_dim": self.text_embedder_hidden_size,
								    "num_layers": 1,
								}

								__UpperCamelCase	:   List[Any]			   =					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
								__UpperCamelCase	:   Optional[int]			   =					nn.Parameter(torch.ones(model.clip_std.shape						)						)
								return model







	@property
	def    _lowerCamelCase				(					self		:Dict						)		-> Union[str, Any]:
								torch.manual_seed(0						)
								__UpperCamelCase	:   Union[str, Any]			   =					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     ,   )

								__UpperCamelCase	:   int			   =					CLIPVisionModelWithProjection(a						)
								return model







	@property
	def    _lowerCamelCase				(					self		:Dict						)		-> Optional[Any]:
								__UpperCamelCase	:   List[str]			   =					CLIPImageProcessor(
								    crop_size=2_2_4     ,   do_center_crop=a     ,   do_normalize=a     ,   do_resize=a     ,   image_mean=[0.48145466, 0.4578275, 0.40821073]     ,   image_std=[0.26862954, 0.26130258, 0.27577711]     ,   resample=3     ,   size=2_2_4     ,   )

								return image_processor







	def    _lowerCamelCase				(					self		:str						)		-> Optional[int]:
								__UpperCamelCase	:   str			   =					self.dummy_prior
								__UpperCamelCase	:   int			   =					self.dummy_image_encoder
								__UpperCamelCase	:   Tuple			   =					self.dummy_text_encoder
								__UpperCamelCase	:   int			   =					self.dummy_tokenizer
								__UpperCamelCase	:   Optional[Any]			   =					self.dummy_image_processor

								__UpperCamelCase	:   int			   =					UnCLIPScheduler(
								    variance_type="fixed_small_log"     ,   prediction_type="sample"     ,   num_train_timesteps=1_0_0_0     ,   clip_sample=a     ,   clip_sample_range=10.0     ,   )

								__UpperCamelCase	:   List[Any]			   =					{
								    "prior": prior,
								    "image_encoder": image_encoder,
								    "text_encoder": text_encoder,
								    "tokenizer": tokenizer,
								    "scheduler": scheduler,
								    "image_processor": image_processor,
								}

								return components







	def    _lowerCamelCase				(					self		:Optional[Any]     ,   a		:int     ,   a		:Union[str, Any]=0						)		-> Any:
								if str(a						).startswith("mps"						):
															__UpperCamelCase	:   int			   =					torch.manual_seed(a						)
								else:
															__UpperCamelCase	:   List[Any]			   =					torch.Generator(device=a						).manual_seed(a						)
								__UpperCamelCase	:   int			   =					{
								    "prompt": "horse",
								    "generator": generator,
								    "guidance_scale": 4.0,
								    "num_inference_steps": 2,
								    "output_type": "np",
								}
								return inputs







	def    _lowerCamelCase				(					self		:List[Any]						)		-> Dict:
								__UpperCamelCase	:   int			   =					"cpu"

								__UpperCamelCase	:   List[str]			   =					self.get_dummy_components()

								__UpperCamelCase	:   List[str]			   =					self.pipeline_class(**a						)
								__UpperCamelCase	:   Tuple			   =					pipe.to(a						)

								pipe.set_progress_bar_config(disable=a						)

								__UpperCamelCase	:   int			   =					pipe(**self.get_dummy_inputs(a						)						)
								__UpperCamelCase	:   int			   =					output.image_embeds

								__UpperCamelCase	:   Optional[int]			   =					pipe(
								    **self.get_dummy_inputs(a						)     ,   return_dict=a     ,   )[0]

								__UpperCamelCase	:   Union[str, Any]			   =					image[0, -1_0:]
								__UpperCamelCase	:   List[str]			   =					image_from_tuple[0, -1_0:]

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

								__UpperCamelCase	:   List[Any]			   =					np.array(
								    [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]						)

								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    _lowerCamelCase				(					self		:int						)		-> Union[str, Any]:
								__UpperCamelCase	:   str			   =					torch_device == "cpu"
								__UpperCamelCase	:   List[str]			   =					True
								__UpperCamelCase	:   List[Any]			   =					False

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







	@skip_mps
	def    _lowerCamelCase				(					self		:Any						)		-> int:
								__UpperCamelCase	:   Optional[Any]			   =					torch_device == "cpu"
								__UpperCamelCase	:   Dict			   =					False

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