


'''simple docstring'''



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      UpperCAmelCase_						(_UpperCAmelCase      ,	unittest.TestCase						):





    """simple docstring"""






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







    @property
    def    lowercase_       (							self			)       ->  Optional[Any]:
         return 32







    @property
    def    lowercase_       (							self			)       ->  Any:
         return 32







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







    @property
    def    lowercase_       (							self			)       ->  Any:
         return self.time_input_dim * 4







    @property
    def    lowercase_       (							self			)       ->  str:
         return 1_00







    @property
    def    lowercase_       (							self			)       ->  str:
         __lowerCamelCase     :			Dict              =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			)
         return tokenizer







    @property
    def    lowercase_       (							self			)       ->  Any:
         torch.manual_seed(0			)
         __lowerCamelCase     :			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=37    ,  layer_norm_eps=1E-05    ,  num_attention_heads=4    ,  num_hidden_layers=5    ,  pad_token_id=1    ,  vocab_size=10_00    ,  )
         return CLIPTextModelWithProjection(SCREAMING_SNAKE_CASE_			)







    @property
    def    lowercase_       (							self			)       ->  str:
         torch.manual_seed(0			)

         __lowerCamelCase     :			Optional[int]              =	{
             'num_attention_heads': 2,
             'attention_head_dim': 12,
             'embedding_dim': self.text_embedder_hidden_size,
             'num_layers': 1,
         }

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







    @property
    def    lowercase_       (							self			)       ->  Union[str, Any]:
         torch.manual_seed(0			)
         __lowerCamelCase     :			Dict              =	CLIPVisionConfig(
             hidden_size=self.text_embedder_hidden_size    ,  image_size=2_24    ,  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     :			Any              =	CLIPVisionModelWithProjection(SCREAMING_SNAKE_CASE_			)
         return model







    @property
    def    lowercase_       (							self			)       ->  List[str]:
         __lowerCamelCase     :			List[str]              =	CLIPImageProcessor(
             crop_size=2_24    ,  do_center_crop=SCREAMING_SNAKE_CASE_    ,  do_normalize=SCREAMING_SNAKE_CASE_    ,  do_resize=SCREAMING_SNAKE_CASE_    ,  image_mean=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]    ,  image_std=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]    ,  resample=3    ,  size=2_24    ,  )

         return image_processor







    def    lowercase_       (							self			)       ->  str:
         __lowerCamelCase     :			str              =	self.dummy_prior
         __lowerCamelCase     :			List[Any]              =	self.dummy_image_encoder
         __lowerCamelCase     :			List[Any]              =	self.dummy_text_encoder
         __lowerCamelCase     :			List[Any]              =	self.dummy_tokenizer
         __lowerCamelCase     :			List[Any]              =	self.dummy_image_processor

         __lowerCamelCase     :			str              =	UnCLIPScheduler(
             variance_type='fixed_small_log'    ,  prediction_type='sample'    ,  num_train_timesteps=10_00    ,  clip_sample=SCREAMING_SNAKE_CASE_    ,  clip_sample_range=10.0    ,  )

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

         return components







    def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_    ,  SCREAMING_SNAKE_CASE_=0			)       ->  List[str]:
         if str(SCREAMING_SNAKE_CASE_			).startswith('mps'			):
              __lowerCamelCase     :			Optional[int]              =	torch.manual_seed(SCREAMING_SNAKE_CASE_			)
         else:
              __lowerCamelCase     :			Optional[int]              =	torch.Generator(device=SCREAMING_SNAKE_CASE_			).manual_seed(SCREAMING_SNAKE_CASE_			)

         __lowerCamelCase     :			Union[str, Any]              =	floats_tensor((1, 3, 64, 64)    ,  rng=random.Random(SCREAMING_SNAKE_CASE_			)			).to(SCREAMING_SNAKE_CASE_			)
         __lowerCamelCase     :			str              =	image.cpu().permute(0    ,  2    ,  3    ,  1			)[0]
         __lowerCamelCase     :			Optional[Any]              =	Image.fromarray(np.uinta(SCREAMING_SNAKE_CASE_			)			).convert('RGB'			).resize((2_56, 2_56)			)

         __lowerCamelCase     :			List[str]              =	{
             '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			)       ->  List[Any]:
         __lowerCamelCase     :			Optional[Any]              =	'cpu'

         __lowerCamelCase     :			List[Any]              =	self.get_dummy_components()

         __lowerCamelCase     :			Optional[Any]              =	self.pipeline_class(**SCREAMING_SNAKE_CASE_			)
         __lowerCamelCase     :			int              =	pipe.to(SCREAMING_SNAKE_CASE_			)

         pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_			)

         __lowerCamelCase     :			Optional[Any]              =	pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE_			)			)
         __lowerCamelCase     :			List[str]              =	output.image_embeds

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

         __lowerCamelCase     :			int              =	image[0, -10:]
         __lowerCamelCase     :			Optional[int]              =	image_from_tuple[0, -10:]

         assert image.shape == (1, 32)

         __lowerCamelCase     :			Union[str, Any]              =	np.array(
             [
                 0.1_0_7_1_2_8_4,
                 1.3_3_3_0_2_7_1,
                 0.6_1_2_6_0_2_2_3,
                 -0.6_6_9_1_0_6_5,
                 -0.3_8_4_6_8_5_2,
                 -1.0_3_0_3_6_6_1,
                 0.2_2_7_1_6_1_1_1,
                 0.0_3_3_4_8_9_0_1,
                 0.3_0_0_4_0_6_7_5,
                 -0.2_4_8_0_5_0_2_9,
             ]			)

         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			)       ->  Dict:
         __lowerCamelCase     :			int              =	torch_device == 'cpu'
         __lowerCamelCase     :			Dict              =	True
         __lowerCamelCase     :			Dict              =	False

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







    @skip_mps
    def    lowercase_       (							self			)       ->  str:
         __lowerCamelCase     :			Tuple              =	torch_device == 'cpu'
         __lowerCamelCase     :			Union[str, Any]              =	False

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





'''simple docstring'''



from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__	:      Any				=						logging.get_logger(__name__)

A__	:      str				=						{
    """microsoft/prophetnet-large-uncased""": (
        """https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json"""
    ),
}





class      UpperCAmelCase_						(_UpperCAmelCase						):





    """simple docstring"""






    lowerCamelCase						:      Dict							  =    'prophetnet'
    lowerCamelCase						:      List[str]							  =    ['past_key_values']
    lowerCamelCase						:      List[str]							  =    {
        'num_attention_heads': 'num_encoder_attention_heads',
    }







    def __init__(							self    ,  SCREAMING_SNAKE_CASE_ = 0.1    ,  SCREAMING_SNAKE_CASE_ = "gelu"    ,  SCREAMING_SNAKE_CASE_ = 3_05_22    ,  SCREAMING_SNAKE_CASE_ = 10_24    ,  SCREAMING_SNAKE_CASE_ = 40_96    ,  SCREAMING_SNAKE_CASE_ = 12    ,  SCREAMING_SNAKE_CASE_ = 16    ,  SCREAMING_SNAKE_CASE_ = 40_96    ,  SCREAMING_SNAKE_CASE_ = 12    ,  SCREAMING_SNAKE_CASE_ = 16    ,  SCREAMING_SNAKE_CASE_ = 0.1    ,  SCREAMING_SNAKE_CASE_ = 0.1    ,  SCREAMING_SNAKE_CASE_ = 5_12    ,  SCREAMING_SNAKE_CASE_ = 0.0_2    ,  SCREAMING_SNAKE_CASE_ = True    ,  SCREAMING_SNAKE_CASE_ = True    ,  SCREAMING_SNAKE_CASE_ = 0    ,  SCREAMING_SNAKE_CASE_ = 2    ,  SCREAMING_SNAKE_CASE_ = 32    ,  SCREAMING_SNAKE_CASE_ = 1_28    ,  SCREAMING_SNAKE_CASE_ = False    ,  SCREAMING_SNAKE_CASE_ = 0.0    ,  SCREAMING_SNAKE_CASE_ = True    ,  SCREAMING_SNAKE_CASE_ = 0    ,  SCREAMING_SNAKE_CASE_ = 1    ,  SCREAMING_SNAKE_CASE_ = 2    ,  **SCREAMING_SNAKE_CASE_    ,  )       ->  List[Any]:
         __lowerCamelCase     :			List[Any]              =	vocab_size
         __lowerCamelCase     :			Union[str, Any]              =	hidden_size
         __lowerCamelCase     :			Tuple              =	encoder_ffn_dim
         __lowerCamelCase     :			Any              =	num_encoder_layers
         __lowerCamelCase     :			int              =	num_encoder_attention_heads
         __lowerCamelCase     :			Optional[Any]              =	decoder_ffn_dim
         __lowerCamelCase     :			str              =	num_decoder_layers
         __lowerCamelCase     :			int              =	num_decoder_attention_heads
         __lowerCamelCase     :			Union[str, Any]              =	max_position_embeddings
         __lowerCamelCase     :			Optional[Any]              =	init_std  # Normal(0, this parameter)
         __lowerCamelCase     :			Union[str, Any]              =	activation_function

         # parameters for prophetnet
         __lowerCamelCase     :			int              =	ngram
         __lowerCamelCase     :			Optional[int]              =	num_buckets
         __lowerCamelCase     :			str              =	relative_max_distance
         __lowerCamelCase     :			Optional[Any]              =	disable_ngram_loss
         __lowerCamelCase     :			str              =	eps

         # 3 Types of Dropout
         __lowerCamelCase     :			Optional[Any]              =	attention_dropout
         __lowerCamelCase     :			str              =	activation_dropout
         __lowerCamelCase     :			Optional[Any]              =	dropout

         __lowerCamelCase     :			Optional[Any]              =	use_cache

         super().__init__(
             pad_token_id=SCREAMING_SNAKE_CASE_    ,  bos_token_id=SCREAMING_SNAKE_CASE_    ,  eos_token_id=SCREAMING_SNAKE_CASE_    ,  is_encoder_decoder=SCREAMING_SNAKE_CASE_    ,  add_cross_attention=SCREAMING_SNAKE_CASE_    ,  decoder_start_token_id=SCREAMING_SNAKE_CASE_    ,  **SCREAMING_SNAKE_CASE_    ,  )







    @property
    def    lowercase_       (							self			)       ->  int:
         return self.num_encoder_layers + self.num_decoder_layers







    @num_hidden_layers.setter
    def    lowercase_       (							self    ,  SCREAMING_SNAKE_CASE_			)       ->  Union[str, Any]:
         raise NotImplementedError(
             'This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and'
             ' `num_decoder_layers`.'			)






