



'''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 	UpperCAmelCase	(		__a					,	unittest.TestCase):


 '''simple docstring'''




 __magic_name__					: Optional[int]  =      KandinskyVaaPriorPipeline
 __magic_name__					: Dict  =      ["prompt"]
 __magic_name__					: List[str]  =      ["prompt", "negative_prompt"]
 __magic_name__					: Union[str, Any]  =      [
     "num_images_per_prompt",
     "generator",
     "num_inference_steps",
     "latents",
     "negative_prompt",
     "guidance_scale",
     "output_type",
     "return_dict",
 ]
 __magic_name__					: Union[str, Any]  =      False




 @property
 def       lowercase_  (   self) ->      str:



      """simple docstring"""







      return 3_2




 @property
 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







      return 3_2




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



      """simple docstring"""







      return self.time_input_dim




 @property
 def       lowercase_  (   self) ->      List[str]:



      """simple docstring"""







      return self.time_input_dim * 4




 @property
 def       lowercase_  (   self) ->      Optional[int]:



      """simple docstring"""







      return 1_0_0




 @property
 def       lowercase_  (   self) ->      int:



      """simple docstring"""







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




 @property
 def       lowercase_  (   self) ->      Tuple:



      """simple docstring"""







      torch.manual_seed(0)
      a_       		=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-05							,  num_attention_heads=4							,  num_hidden_layers=5							,  pad_token_id=1							,  vocab_size=1_0_0_0							,  )
      return CLIPTextModelWithProjection(__A)




 @property
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      torch.manual_seed(0)

      a_       		={
          "num_attention_heads": 2,
          "attention_head_dim": 1_2,
          "embedding_dim": self.text_embedder_hidden_size,
          "num_layers": 1,
      }

      a_       		=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
      a_       		=nn.Parameter(torch.ones(model.clip_std.shape))
      return model




 @property
 def       lowercase_  (   self) ->      List[Any]:



      """simple docstring"""







      torch.manual_seed(0)
      a_       		=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							,  )

      a_       		=CLIPVisionModelWithProjection(__A)
      return model




 @property
 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      a_       		=CLIPImageProcessor(
          crop_size=2_2_4							,  do_center_crop=__A							,  do_normalize=__A							,  do_resize=__A							,  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_2_4							,  )

      return image_processor




 def       lowercase_  (   self) ->      str:



      """simple docstring"""







      a_       		=self.dummy_prior
      a_       		=self.dummy_image_encoder
      a_       		=self.dummy_text_encoder
      a_       		=self.dummy_tokenizer
      a_       		=self.dummy_image_processor

      a_       		=UnCLIPScheduler(
          variance_type="fixed_small_log"							,  prediction_type="sample"							,  num_train_timesteps=1_0_0_0							,  clip_sample=__A							,  clip_sample_range=1_0.0							,  )

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

      return components




 def       lowercase_  (   self							,  lowerCAmelCase_							,  lowerCAmelCase_=0) ->      Tuple:



      """simple docstring"""







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




 def       lowercase_  (   self) ->      Any:



      """simple docstring"""







      a_       		="cpu"

      a_       		=self.get_dummy_components()

      a_       		=self.pipeline_class(**__A)
      a_       		=pipe.to(__A)

      pipe.set_progress_bar_config(disable=__A)

      a_       		=pipe(**self.get_dummy_inputs(__A))
      a_       		=output.image_embeds

      a_       		=pipe(
          **self.get_dummy_inputs(__A)							,  return_dict=__A							,  )[0]

      a_       		=image[0, -1_0:]
      a_       		=image_from_tuple[0, -1_0:]

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

      a_       		=np.array(
          [-0.0_5_3_2, 1.7_1_2_0, 0.3_6_5_6, -1.0_8_5_2, -0.8_9_4_6, -1.1_7_5_6, 0.4_3_4_8, 0.2_4_8_2, 0.5_1_4_6, -0.1_1_5_6])

      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) ->      int:



      """simple docstring"""







      a_       		=torch_device == "cpu"
      a_       		=True
      a_       		=False

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




 @skip_mps
 def       lowercase_  (   self) ->      Optional[Any]:



      """simple docstring"""







      a_       		=torch_device == "cpu"
      a_       		=False

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







'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


lowercase							       =			{
    '''albert-base-v1''': '''https://huggingface.co/albert-base-v1/resolve/main/config.json''',
    '''albert-large-v1''': '''https://huggingface.co/albert-large-v1/resolve/main/config.json''',
    '''albert-xlarge-v1''': '''https://huggingface.co/albert-xlarge-v1/resolve/main/config.json''',
    '''albert-xxlarge-v1''': '''https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json''',
    '''albert-base-v2''': '''https://huggingface.co/albert-base-v2/resolve/main/config.json''',
    '''albert-large-v2''': '''https://huggingface.co/albert-large-v2/resolve/main/config.json''',
    '''albert-xlarge-v2''': '''https://huggingface.co/albert-xlarge-v2/resolve/main/config.json''',
    '''albert-xxlarge-v2''': '''https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json''',
}







class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 __magic_name__					: int  =      "albert"




 def __init__(   self							,  lowerCAmelCase_=3_0_0_0_0							,  lowerCAmelCase_=1_2_8							,  lowerCAmelCase_=4_0_9_6							,  lowerCAmelCase_=1_2							,  lowerCAmelCase_=1							,  lowerCAmelCase_=6_4							,  lowerCAmelCase_=1_6_3_8_4							,  lowerCAmelCase_=1							,  lowerCAmelCase_="gelu_new"							,  lowerCAmelCase_=0							,  lowerCAmelCase_=0							,  lowerCAmelCase_=5_1_2							,  lowerCAmelCase_=2							,  lowerCAmelCase_=0.0_2							,  lowerCAmelCase_=1e-12							,  lowerCAmelCase_=0.1							,  lowerCAmelCase_="absolute"							,  lowerCAmelCase_=0							,  lowerCAmelCase_=2							,  lowerCAmelCase_=3							,  **lowerCAmelCase_							,  ) ->      Any:



      """simple docstring"""







      super().__init__(pad_token_id=lowerCAmelCase_							,  bos_token_id=lowerCAmelCase_							,  eos_token_id=lowerCAmelCase_							,  **lowerCAmelCase_)

      a_       		=vocab_size
      a_       		=embedding_size
      a_       		=hidden_size
      a_       		=num_hidden_layers
      a_       		=num_hidden_groups
      a_       		=num_attention_heads
      a_       		=inner_group_num
      a_       		=hidden_act
      a_       		=intermediate_size
      a_       		=hidden_dropout_prob
      a_       		=attention_probs_dropout_prob
      a_       		=max_position_embeddings
      a_       		=type_vocab_size
      a_       		=initializer_range
      a_       		=layer_norm_eps
      a_       		=classifier_dropout_prob
      a_       		=position_embedding_type







class 	UpperCAmelCase	(		__a):


 '''simple docstring'''




 @property
 def       lowercase_  (   self) ->      Mapping[str, Mapping[int, str]]:



      """simple docstring"""







      if self.task == "multiple-choice":
           a_       		={0: "batch", 1: "choice", 2: "sequence"}
      else:
           a_       		={0: "batch", 1: "sequence"}
      return OrderedDict(
          [
              ("input_ids", dynamic_axis),
              ("attention_mask", dynamic_axis),
              ("token_type_ids", dynamic_axis),
          ])


