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

 '''simple docstring'''
 lowercase_				:						str				=       KandinskyVaaPriorEmbaEmbPipeline
 lowercase_				:						Dict				=       ['prompt', 'image']
 lowercase_				:						List[Any]				=       ['prompt', 'image']
 lowercase_				:						List[Any]				=       [
     'num_images_per_prompt',
     'strength',
     'generator',
     'num_inference_steps',
     'latents',
     'negative_prompt',
     'guidance_scale',
     'output_type',
     'return_dict',
 ]
 lowercase_				:						List[str]				=       False







 @property
 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   return 32







 @property
 def        UpperCamelCase     (				self		:     int   ):



   '''simple docstring'''




   return 32







 @property
 def        UpperCamelCase     (				self		:     str   ):



   '''simple docstring'''




   return self.time_input_dim







 @property
 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   return self.time_input_dim * 4







 @property
 def        UpperCamelCase     (				self		:     int   ):



   '''simple docstring'''




   return 1_00







 @property
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




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







 @property
 def        UpperCamelCase     (				self		:     str   ):



   '''simple docstring'''




   torch.manual_seed(0   )
   UpperCAmelCase__			:				Optional[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-05 ,							num_attention_heads=4 ,							num_hidden_layers=5 ,							pad_token_id=1 ,							vocab_size=10_00 ,							)
   return CLIPTextModelWithProjection(_snake_case   )







 @property
 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   torch.manual_seed(0   )

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

   UpperCAmelCase__			:				Union[str, 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
   UpperCAmelCase__			:				Tuple												=       nn.Parameter(torch.ones(model.clip_std.shape   )   )
   return model







 @property
 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




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

   UpperCAmelCase__			:				str												=       CLIPVisionModelWithProjection(_snake_case   )
   return model







 @property
 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       CLIPImageProcessor(
       crop_size=2_24 ,							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=2_24 ,							)

   return image_processor







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       self.dummy_prior
   UpperCAmelCase__			:				List[str]												=       self.dummy_image_encoder
   UpperCAmelCase__			:				List[Any]												=       self.dummy_text_encoder
   UpperCAmelCase__			:				Tuple												=       self.dummy_tokenizer
   UpperCAmelCase__			:				List[Any]												=       self.dummy_image_processor

   UpperCAmelCase__			:				Optional[Any]												=       UnCLIPScheduler(
       variance_type="fixed_small_log" ,							prediction_type="sample" ,							num_train_timesteps=10_00 ,							clip_sample=_snake_case ,							clip_sample_range=10.0 ,							)

   UpperCAmelCase__			:				Union[str, Any]												=       {
       '''prior''': prior,
       '''image_encoder''': image_encoder,
       '''text_encoder''': text_encoder,
       '''tokenizer''': tokenizer,
       '''scheduler''': scheduler,
       '''image_processor''': image_processor,
   }

   return components







 def        UpperCamelCase     (				self		:     Any ,							snake_case__		:     Optional[Any] ,							snake_case__		:     List[str]=0   ):



   '''simple docstring'''




   if str(_snake_case   ).startswith("mps"   ):
     UpperCAmelCase__			:				str												=       torch.manual_seed(_snake_case   )
   else:
     UpperCAmelCase__			:				str												=       torch.Generator(device=_snake_case   ).manual_seed(_snake_case   )

   UpperCAmelCase__			:				List[str]												=       floats_tensor((1, 3, 64, 64) ,							rng=random.Random(_snake_case   )   ).to(_snake_case   )
   UpperCAmelCase__			:				int												=       image.cpu().permute(0 ,							2 ,							3 ,							1   )[0]
   UpperCAmelCase__			:				List[str]												=       Image.fromarray(np.uinta(_snake_case   )   ).convert("RGB"   ).resize((2_56, 2_56)   )

   UpperCAmelCase__			:				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        UpperCamelCase     (				self		:     Optional[Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       '''cpu'''

   UpperCAmelCase__			:				Union[str, Any]												=       self.get_dummy_components()

   UpperCAmelCase__			:				Optional[Any]												=       self.pipeline_class(**_snake_case   )
   UpperCAmelCase__			:				str												=       pipe.to(_snake_case   )

   pipe.set_progress_bar_config(disable=_snake_case   )

   UpperCAmelCase__			:				List[Any]												=       pipe(**self.get_dummy_inputs(_snake_case   )   )
   UpperCAmelCase__			:				Tuple												=       output.image_embeds

   UpperCAmelCase__			:				int												=       pipe(
       **self.get_dummy_inputs(_snake_case   ) ,							return_dict=_snake_case ,							)[0]

   UpperCAmelCase__			:				Union[str, Any]												=       image[0, -10:]
   UpperCAmelCase__			:				List[str]												=       image_from_tuple[0, -10:]

   assert image.shape == (1, 32)

   UpperCAmelCase__			:				int												=       np.array(
       [
           0.1071284,
           1.3330271,
           0.61260223,
           -0.6691065,
           -0.3846852,
           -1.0303661,
           0.22716111,
           0.03348901,
           0.30040675,
           -0.24805029,
       ]   )

   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        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Union[str, Any]												=       torch_device == '''cpu'''
   UpperCAmelCase__			:				Any												=       True
   UpperCAmelCase__			:				Optional[Any]												=       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        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       torch_device == '''cpu'''
   UpperCAmelCase__			:				Union[str, Any]												=       False

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








'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE    =					{"""configuration_visual_bert""": ["""VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """VisualBertConfig"""]}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE    =					[
          """VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """VisualBertForMultipleChoice""",
          """VisualBertForPreTraining""",
          """VisualBertForQuestionAnswering""",
          """VisualBertForRegionToPhraseAlignment""",
          """VisualBertForVisualReasoning""",
          """VisualBertLayer""",
          """VisualBertModel""",
          """VisualBertPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_visual_bert import (
                VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                VisualBertForMultipleChoice,
                VisualBertForPreTraining,
                VisualBertForQuestionAnswering,
                VisualBertForRegionToPhraseAlignment,
                VisualBertForVisualReasoning,
                VisualBertLayer,
                VisualBertModel,
                VisualBertPreTrainedModel,
            )


else:
      import sys

      SCREAMING_SNAKE_CASE    =					_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



