





'''simple docstring'''






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  (      _a      ,   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   _lowerCamelCase    (   self	):
							return 32



					@property
					def   _lowerCamelCase    (   self	):
							return 32



					@property
					def   _lowerCamelCase    (   self	):
							return self.time_input_dim



					@property
					def   _lowerCamelCase    (   self	):
							return self.time_input_dim * 4



					@property
					def   _lowerCamelCase    (   self	):
							return 100



					@property
					def   _lowerCamelCase    (   self	):
							lowerCAmelCase     :    str						=   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"	)
							return tokenizer



					@property
					def   _lowerCamelCase    (   self	):
							torch.manual_seed(0	)
							lowerCAmelCase     :    Any						=   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-0_5		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1_000		,      )
							return CLIPTextModelWithProjection(snake_case_	)



					@property
					def   _lowerCamelCase    (   self	):
							torch.manual_seed(0	)

							lowerCAmelCase     :    int						=   {
							    "num_attention_heads": 2,
							    "attention_head_dim": 12,
							    "embedding_dim": self.text_embedder_hidden_size,
							    "num_layers": 1,
							}

							lowerCAmelCase     :    Optional[Any]						=   PriorTransformer(**snake_case_	)
							# 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     :    Tuple						=   nn.Parameter(torch.ones(model.clip_std.shape	)	)
							return model



					@property
					def   _lowerCamelCase    (   self	):
							torch.manual_seed(0	)
							lowerCAmelCase     :    Dict						=   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     :    Optional[Any]						=   CLIPVisionModelWithProjection(snake_case_	)
							return model



					@property
					def   _lowerCamelCase    (   self	):
							lowerCAmelCase     :    Optional[int]						=   CLIPImageProcessor(
							    crop_size=224		,      do_center_crop=snake_case_		,      do_normalize=snake_case_		,      do_resize=snake_case_		,      image_mean=[0.48145466, 0.4578275, 0.40821073]		,      image_std=[0.26862954, 0.26130258, 0.27577711]		,      resample=3		,      size=224		,      )

							return image_processor



					def   _lowerCamelCase    (   self	):
							lowerCAmelCase     :    Optional[Any]						=   self.dummy_prior
							lowerCAmelCase     :    Optional[Any]						=   self.dummy_image_encoder
							lowerCAmelCase     :    Tuple						=   self.dummy_text_encoder
							lowerCAmelCase     :    Any						=   self.dummy_tokenizer
							lowerCAmelCase     :    List[Any]						=   self.dummy_image_processor

							lowerCAmelCase     :    Any						=   UnCLIPScheduler(
							    variance_type="fixed_small_log"		,      prediction_type="sample"		,      num_train_timesteps=1_000		,      clip_sample=snake_case_		,      clip_sample_range=10.0		,      )

							lowerCAmelCase     :    Dict						=   {
							    "prior": prior,
							    "image_encoder": image_encoder,
							    "text_encoder": text_encoder,
							    "tokenizer": tokenizer,
							    "scheduler": scheduler,
							    "image_processor": image_processor,
							}

							return components



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



					def   _lowerCamelCase    (   self	):
							lowerCAmelCase     :    Any						=   "cpu"

							lowerCAmelCase     :    Dict						=   self.get_dummy_components()

							lowerCAmelCase     :    int						=   self.pipeline_class(**snake_case_	)
							lowerCAmelCase     :    Any						=   pipe.to(snake_case_	)

							pipe.set_progress_bar_config(disable=snake_case_	)

							lowerCAmelCase     :    List[Any]						=   pipe(**self.get_dummy_inputs(snake_case_	)	)
							lowerCAmelCase     :    Any						=   output.image_embeds

							lowerCAmelCase     :    List[str]						=   pipe(
							    **self.get_dummy_inputs(snake_case_	)		,      return_dict=snake_case_		,      )[0]

							lowerCAmelCase     :    Optional[Any]						=   image[0, -10:]
							lowerCAmelCase     :    str						=   image_from_tuple[0, -10:]

							assert image.shape == (1, 32)

							lowerCAmelCase     :    Optional[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	):
							lowerCAmelCase     :    Union[str, Any]						=   torch_device == "cpu"
							lowerCAmelCase     :    int						=   True
							lowerCAmelCase     :    Tuple						=   False

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





					@skip_mps
					def   _lowerCamelCase    (   self	):
							lowerCAmelCase     :    Optional[int]						=   torch_device == "cpu"
							lowerCAmelCase     :    Dict						=   False

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










'''simple docstring'''






from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
					if not (is_transformers_available() and is_torch_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import KandinskyPipeline, KandinskyPriorPipeline
else:
					from .pipeline_kandinsky import KandinskyPipeline
					from .pipeline_kandinsky_imgaimg import KandinskyImgaImgPipeline
					from .pipeline_kandinsky_inpaint import KandinskyInpaintPipeline
					from .pipeline_kandinsky_prior import KandinskyPriorPipeline, KandinskyPriorPipelineOutput
					from .text_encoder import MultilingualCLIP



