




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  lowerCamelCase_							(   _SCREAMING_SNAKE_CASE ,     unittest.TestCase      ):


 '''simple docstring'''







 lowercase_       					=					KandinskyVaaPriorEmbaEmbPipeline
 lowercase_       					=					["prompt", "image"]
 lowercase_       					=					["prompt", "image"]
 lowercase_       					=					[
     "num_images_per_prompt",
     "strength",
     "generator",
     "num_inference_steps",
     "latents",
     "negative_prompt",
     "guidance_scale",
     "output_type",
     "return_dict",
 ]
 lowercase_       					=					False



 @property
 def    lowerCAmelCase_						(							self						:	str		):
       return 32



 @property
 def    lowerCAmelCase_						(							self						:	Dict		):
       return 32



 @property
 def    lowerCAmelCase_						(							self						:	List[Any]		):
       return self.time_input_dim



 @property
 def    lowerCAmelCase_						(							self						:	List[str]		):
       return self.time_input_dim * 4



 @property
 def    lowerCAmelCase_						(							self						:	List[str]		):
       return 100



 @property
 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       SCREAMING_SNAKE_CASE_					=    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)
       return tokenizer



 @property
 def    lowerCAmelCase_						(							self						:	Dict		):
       torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    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=1_000       ,       )
       return CLIPTextModelWithProjection(_lowerCAmelCase		)



 @property
 def    lowerCAmelCase_						(							self						:	int		):
       torch.manual_seed(0		)

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

       SCREAMING_SNAKE_CASE_					=    PriorTransformer(**_lowerCAmelCase		)
       # 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
       SCREAMING_SNAKE_CASE_					=    nn.Parameter(torch.ones(model.clip_std.shape		)		)
       return model



 @property
 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    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       ,       )

       SCREAMING_SNAKE_CASE_					=    CLIPVisionModelWithProjection(_lowerCAmelCase		)
       return model



 @property
 def    lowerCAmelCase_						(							self						:	Union[str, Any]		):
       SCREAMING_SNAKE_CASE_					=    CLIPImageProcessor(
           crop_size=224       ,       do_center_crop=_lowerCAmelCase       ,       do_normalize=_lowerCAmelCase       ,       do_resize=_lowerCAmelCase       ,       image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]       ,       image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]       ,       resample=3       ,       size=224       ,       )

       return image_processor



 def    lowerCAmelCase_						(							self						:	str		):
       SCREAMING_SNAKE_CASE_					=    self.dummy_prior
       SCREAMING_SNAKE_CASE_					=    self.dummy_image_encoder
       SCREAMING_SNAKE_CASE_					=    self.dummy_text_encoder
       SCREAMING_SNAKE_CASE_					=    self.dummy_tokenizer
       SCREAMING_SNAKE_CASE_					=    self.dummy_image_processor

       SCREAMING_SNAKE_CASE_					=    UnCLIPScheduler(
           variance_type='fixed_small_log'       ,       prediction_type='sample'       ,       num_train_timesteps=1_000       ,       clip_sample=_lowerCAmelCase       ,       clip_sample_range=10.0       ,       )

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

       return components



 def    lowerCAmelCase_						(							self						:	List[str]       ,       _lowerCAmelCase						:	Dict       ,       _lowerCAmelCase						:	Tuple=0		):
       if str(_lowerCAmelCase		).startswith('mps'		):
             SCREAMING_SNAKE_CASE_					=    torch.manual_seed(_lowerCAmelCase		)
       else:
             SCREAMING_SNAKE_CASE_					=    torch.Generator(device=_lowerCAmelCase		).manual_seed(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    floats_tensor((1, 3, 64, 64)       ,       rng=random.Random(_lowerCAmelCase		)		).to(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    image.cpu().permute(0       ,       2       ,       3       ,       1		)[0]
       SCREAMING_SNAKE_CASE_					=    Image.fromarray(np.uinta(_lowerCAmelCase		)		).convert('RGB'		).resize((256, 256)		)

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



 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       SCREAMING_SNAKE_CASE_					=    'cpu'

       SCREAMING_SNAKE_CASE_					=    self.get_dummy_components()

       SCREAMING_SNAKE_CASE_					=    self.pipeline_class(**_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_					=    pipe.to(_lowerCAmelCase		)

       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    pipe(**self.get_dummy_inputs(_lowerCAmelCase		)		)
       SCREAMING_SNAKE_CASE_					=    output.image_embeds

       SCREAMING_SNAKE_CASE_					=    pipe(
           **self.get_dummy_inputs(_lowerCAmelCase		)       ,       return_dict=_lowerCAmelCase       ,       )[0]

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

       assert image.shape == (1, 32)

       SCREAMING_SNAKE_CASE_					=    np.array(
           [
               0.107_1284,
               1.333_0271,
               0.6126_0223,
               -0.669_1065,
               -0.384_6852,
               -1.030_3661,
               0.2271_6111,
               0.0334_8901,
               0.3004_0675,
               -0.2480_5029,
           ]		)

       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						:	Any		):
       SCREAMING_SNAKE_CASE_					=    torch_device == 'cpu'
       SCREAMING_SNAKE_CASE_					=    True
       SCREAMING_SNAKE_CASE_					=    False

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




 @skip_mps
 def    lowerCAmelCase_						(							self						:	Any		):
       SCREAMING_SNAKE_CASE_					=    torch_device == 'cpu'
       SCREAMING_SNAKE_CASE_					=    False

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





from collections import defaultdict

def       UpperCAmelCase_							(   __UpperCAmelCase		:			int = 5							)      ->				int:
      SCREAMING_SNAKE_CASE_					=    defaultdict(__UpperCAmelCase							)
      SCREAMING_SNAKE_CASE_					=    0

      while True:
            SCREAMING_SNAKE_CASE_					=    get_digits(__UpperCAmelCase							)
            freqs[digits].append(__UpperCAmelCase							)

            if len(freqs[digits]							) == max_base:
                  SCREAMING_SNAKE_CASE_					=    freqs[digits][0] ** 3
                  return base

            num += 1

def       UpperCAmelCase_							(   __UpperCAmelCase		:			int							)      ->				str:
      return "".join(sorted(str(num**3							)							)							)


if __name__ == "__main__":
   print(f'''{solution() = }''')