





import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import (
        MODEL_MAPPING,
        BeitForImageClassification,
        BeitForMaskedImageModeling,
        BeitForSemanticSegmentation,
        BeitModel,
    )
    from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    import PIL
    from PIL import Image

    from transformers import BeitImageProcessor



class       A__     :

      '''simple docstring'''





      def __init__(				self ,							A_ ,							A_=100 ,							A_=13 ,							A_=30 ,							A_=2 ,							A_=3 ,							A_=True ,							A_=True ,							A_=32 ,							A_=4 ,							A_=4 ,							A_=37 ,							A_="gelu" ,							A_=0.1 ,							A_=0.1 ,							A_=10 ,							A_=0.02 ,							A_=3 ,							A_=None ,							A_=[0, 1, 2, 3] ,							):
       '''simple docstring'''




       UpperCamelCase			:      int 			=     parent
       UpperCamelCase			:      Optional[Any] 			=     100
       UpperCamelCase			:      List[str] 			=     batch_size
       UpperCamelCase			:      int 			=     image_size
       UpperCamelCase			:      str 			=     patch_size
       UpperCamelCase			:      Optional[Any] 			=     num_channels
       UpperCamelCase			:      str 			=     is_training
       UpperCamelCase			:      Tuple 			=     use_labels
       UpperCamelCase			:      int 			=     hidden_size
       UpperCamelCase			:      Any 			=     num_hidden_layers
       UpperCamelCase			:      Union[str, Any] 			=     num_attention_heads
       UpperCamelCase			:      Dict 			=     intermediate_size
       UpperCamelCase			:      List[Any] 			=     hidden_act
       UpperCamelCase			:      Union[str, Any] 			=     hidden_dropout_prob
       UpperCamelCase			:      List[str] 			=     attention_probs_dropout_prob
       UpperCamelCase			:      Dict 			=     type_sequence_label_size
       UpperCamelCase			:      Union[str, Any] 			=     initializer_range
       UpperCamelCase			:      int 			=     scope
       UpperCamelCase			:      Optional[Any] 			=     out_indices
       UpperCamelCase			:      Tuple 			=     num_labels

       # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
       UpperCamelCase			:      List[str] 			=     (image_size // patch_size) ** 2
       UpperCamelCase			:      List[Any] 			=     num_patches + 1





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

       UpperCamelCase			:      int 			=     None
       UpperCamelCase			:      Optional[int] 			=     None
       if self.use_labels:
        UpperCamelCase			:      str 			=     ids_tensor([self.batch_size] ,							self.type_sequence_label_size     )
        UpperCamelCase			:      List[Any] 			=     ids_tensor([self.batch_size, self.image_size, self.image_size] ,							self.num_labels     )

       UpperCamelCase			:      int 			=     self.get_config()

       return config, pixel_values, labels, pixel_labels





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return BeitConfig(
           vocab_size=self.vocab_size ,							image_size=self.image_size ,							patch_size=self.patch_size ,							num_channels=self.num_channels ,							hidden_size=self.hidden_size ,							num_hidden_layers=self.num_hidden_layers ,							num_attention_heads=self.num_attention_heads ,							intermediate_size=self.intermediate_size ,							hidden_act=self.hidden_act ,							hidden_dropout_prob=self.hidden_dropout_prob ,							attention_probs_dropout_prob=self.attention_probs_dropout_prob ,							is_decoder=A_ ,							initializer_range=self.initializer_range ,							out_indices=self.out_indices ,							)





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     BeitModel(config=A_     )
       model.to(A_     )
       model.eval()
       UpperCamelCase			:      Optional[Any] 			=     model(A_     )
       self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.hidden_size)     )





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Any 			=     BeitForMaskedImageModeling(config=A_     )
       model.to(A_     )
       model.eval()
       UpperCamelCase			:      Dict 			=     model(A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length - 1, self.vocab_size)     )





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     self.type_sequence_label_size
       UpperCamelCase			:      List[Any] 			=     BeitForImageClassification(A_     )
       model.to(A_     )
       model.eval()
       UpperCamelCase			:      Tuple 			=     model(A_ ,							labels=A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.type_sequence_label_size)     )

       # test greyscale images
       UpperCamelCase			:      List[Any] 			=     1
       UpperCamelCase			:      Any 			=     BeitForImageClassification(A_     )
       model.to(A_     )
       model.eval()

       UpperCamelCase			:      int 			=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
       UpperCamelCase			:      Tuple 			=     model(A_ ,							labels=A_     )
       self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.type_sequence_label_size)     )





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_ ,							A_     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     self.num_labels
       UpperCamelCase			:      List[str] 			=     BeitForSemanticSegmentation(A_     )
       model.to(A_     )
       model.eval()
       UpperCamelCase			:      int 			=     model(A_     )
       self.parent.assertEqual(
           result.logits.shape ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )
       UpperCamelCase			:      Tuple 			=     model(A_ ,							labels=A_     )
       self.parent.assertEqual(
           result.logits.shape ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Any 			=     self.prepare_config_and_inputs()
       UpperCamelCase			:      Dict 			=     config_and_inputs
       UpperCamelCase			:      Optional[int] 			=     {"pixel_values": pixel_values}
       return config, inputs_dict



@require_torch
class       A__     ( __snake_case ,     __snake_case ,     unittest.TestCase    ):

      '''simple docstring'''





      _UpperCAmelCase			:List[str]							=     (
          (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      _UpperCAmelCase			:List[str]							=     (
          {
              'feature-extraction': BeitModel,
              'image-classification': BeitForImageClassification,
              'image-segmentation': BeitForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _UpperCAmelCase			:Optional[Any]							=     False
      _UpperCAmelCase			:str							=     False
      _UpperCAmelCase			:Optional[int]							=     False





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     BeitModelTester(self     )
       UpperCamelCase			:      List[Any] 			=     ConfigTester(self ,							config_class=A_ ,							has_text_modality=A_ ,							hidden_size=37     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      @unittest.skip(reason="BEiT does not use inputs_embeds"     )
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass





      @require_torch_multi_gpu
      @unittest.skip(reason="BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`"     )
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       pass





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      str 			=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCamelCase			:      Tuple 			=     model_class(A_     )
        self.assertIsInstance(model.get_input_embeddings() ,							(nn.Module)     )
        UpperCamelCase			:      Dict 			=     model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(A_ ,							nn.Linear     )     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        UpperCamelCase			:      Optional[Any] 			=     model_class(A_     )
        UpperCamelCase			:      int 			=     inspect.signature(model.forward     )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        UpperCamelCase			:      Optional[int] 			=     [*signature.parameters.keys()]

        UpperCamelCase			:      List[Any] 			=     ["pixel_values"]
        self.assertListEqual(arg_names[:1] ,							A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Union[str, Any] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[Any] 			=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*A_     )





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       if not self.model_tester.is_training:
        return

       UpperCamelCase			:      Optional[Any] 			=     self.model_tester.prepare_config_and_inputs_for_common()
       UpperCamelCase			:      Dict 			=     True

       for model_class in self.all_model_classes:
        # we don't test BeitForMaskedImageModeling
        if model_class in [*get_values(A_     ), BeitForMaskedImageModeling]:
         continue

        UpperCamelCase			:      Dict 			=     model_class(A_     )
        model.to(A_     )
        model.train()
        UpperCamelCase			:      List[str] 			=     self._prepare_for_class(A_ ,							A_ ,							return_labels=A_     )
        UpperCamelCase			:      int 			=     model(**A_     ).loss
        loss.backward()





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Tuple 			=     self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
        return

       UpperCamelCase			:      List[Any] 			=     False
       UpperCamelCase			:      str 			=     True

       for model_class in self.all_model_classes:
        # we don't test BeitForMaskedImageModeling
        if (
            model_class in [*get_values(A_     ), BeitForMaskedImageModeling]
            or not model_class.supports_gradient_checkpointing
        ):
         continue

        UpperCamelCase			:      Union[str, Any] 			=     model_class(A_     )
        model.gradient_checkpointing_enable()
        model.to(A_     )
        model.train()
        UpperCamelCase			:      Optional[int] 			=     self._prepare_for_class(A_ ,							A_ ,							return_labels=A_     )
        UpperCamelCase			:      int 			=     model(**A_     ).loss
        loss.backward()





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     self.model_tester.prepare_config_and_inputs_for_common()

       UpperCamelCase			:      List[str] 			=     _config_zero_init(A_     )
       for model_class in self.all_model_classes:
        UpperCamelCase			:      Tuple 			=     model_class(config=A_     )
        for name, param in model.named_parameters():
         # we skip lambda parameters as these require special initial values
         # determined by config.layer_scale_init_value
         if "lambda" in name:
          continue
         if param.requires_grad:
          self.assertIn(
              ((param.data.mean() * 1e9).round() / 1e9).item() ,							[0.0, 1.0] ,							msg=F"""Parameter {name} of model {model_class} seems not properly initialized""" ,							)





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCamelCase			:      Dict 			=     BeitModel.from_pretrained(A_     )
        self.assertIsNotNone(A_     )



def 	A_       (  )		->				Optional[int]:
 UpperCamelCase			:      str 			=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
 return image



@require_torch
@require_vision
class       A__     ( unittest.TestCase    ):

      '''simple docstring'''





      @cached_property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224"     ) if is_vision_available() else None





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      str 			=     BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"     ).to(A_     )

       UpperCamelCase			:      int 			=     self.default_image_processor
       UpperCamelCase			:      int 			=     prepare_img()
       UpperCamelCase			:      Optional[int] 			=     image_processor(images=A_ ,							return_tensors="pt"     ).pixel_values.to(A_     )

       # prepare bool_masked_pos
       UpperCamelCase			:      Union[str, Any] 			=     torch.ones((1, 196) ,							dtype=torch.bool     ).to(A_     )

       # forward pass
       with torch.no_grad():
        UpperCamelCase			:      Optional[int] 			=     model(pixel_values=A_ ,							bool_masked_pos=A_     )
       UpperCamelCase			:      Any 			=     outputs.logits

       # verify the logits
       UpperCamelCase			:      List[str] 			=     torch.Size((1, 196, 8192)     )
       self.assertEqual(logits.shape ,							A_     )

       UpperCamelCase			:      List[Any] 			=     torch.tensor(
           [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]     ).to(A_     )

       self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3] ,							A_ ,							atol=1e-2     )     )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Any 			=     BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"     ).to(A_     )

       UpperCamelCase			:      List[Any] 			=     self.default_image_processor
       UpperCamelCase			:      Optional[Any] 			=     prepare_img()
       UpperCamelCase			:      int 			=     image_processor(images=A_ ,							return_tensors="pt"     ).to(A_     )

       # forward pass
       with torch.no_grad():
        UpperCamelCase			:      Any 			=     model(**A_     )
       UpperCamelCase			:      List[str] 			=     outputs.logits

       # verify the logits
       UpperCamelCase			:      Optional[int] 			=     torch.Size((1, 1000)     )
       self.assertEqual(logits.shape ,							A_     )

       UpperCamelCase			:      Optional[int] 			=     torch.tensor([-1.23_85, -1.09_87, -1.01_08]     ).to(A_     )

       self.assertTrue(torch.allclose(logits[0, :3] ,							A_ ,							atol=1e-4     )     )

       UpperCamelCase			:      Optional[int] 			=     281
       self.assertEqual(logits.argmax(-1     ).item() ,							A_     )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      List[str] 			=     BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"     ).to(
           A_     )

       UpperCamelCase			:      List[Any] 			=     self.default_image_processor
       UpperCamelCase			:      Union[str, Any] 			=     prepare_img()
       UpperCamelCase			:      Union[str, Any] 			=     image_processor(images=A_ ,							return_tensors="pt"     ).to(A_     )

       # forward pass
       with torch.no_grad():
        UpperCamelCase			:      Any 			=     model(**A_     )
       UpperCamelCase			:      Tuple 			=     outputs.logits

       # verify the logits
       UpperCamelCase			:      str 			=     torch.Size((1, 2_1841)     )
       self.assertEqual(logits.shape ,							A_     )

       UpperCamelCase			:      List[Any] 			=     torch.tensor([1.68_81, -0.27_87, 0.59_01]     ).to(A_     )

       self.assertTrue(torch.allclose(logits[0, :3] ,							A_ ,							atol=1e-4     )     )

       UpperCamelCase			:      Optional[int] 			=     2396
       self.assertEqual(logits.argmax(-1     ).item() ,							A_     )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640"     )
       UpperCamelCase			:      Optional[Any] 			=     model.to(A_     )

       UpperCamelCase			:      Dict 			=     BeitImageProcessor(do_resize=A_ ,							size=640 ,							do_center_crop=A_     )

       UpperCamelCase			:      List[str] 			=     load_dataset("hf-internal-testing/fixtures_ade20k" ,							split="test"     )
       UpperCamelCase			:      int 			=     Image.open(ds[0]["file"]     )
       UpperCamelCase			:      Any 			=     image_processor(images=A_ ,							return_tensors="pt"     ).to(A_     )

       # forward pass
       with torch.no_grad():
        UpperCamelCase			:      int 			=     model(**A_     )
       UpperCamelCase			:      Optional[int] 			=     outputs.logits

       # verify the logits
       UpperCamelCase			:      int 			=     torch.Size((1, 150, 160, 160)     )
       self.assertEqual(logits.shape ,							A_     )

       UpperCamelCase			:      Optional[Any] 			=     version.parse(PIL.__version__     ) < version.parse("9.0.0"     )

       if is_pillow_less_than_a:
        UpperCamelCase			:      Tuple 			=     torch.tensor(
            [
                [[-4.92_25, -2.39_54, -3.05_22], [-2.88_22, -1.00_46, -1.75_61], [-2.95_49, -1.32_28, -2.13_47]],
                [[-5.81_68, -3.41_29, -4.07_78], [-3.86_51, -2.22_14, -3.02_77], [-3.83_56, -2.46_43, -3.35_35]],
                [[-0.00_78, 3.99_52, 4.07_54], [2.98_56, 4.69_44, 5.00_35], [3.24_13, 4.78_13, 4.99_69]],
            ] ,							device=A_ ,							)
       else:
        UpperCamelCase			:      Optional[int] 			=     torch.tensor(
            [
                [[-4.89_60, -2.36_88, -3.03_55], [-2.84_78, -0.98_36, -1.74_18], [-2.94_49, -1.33_32, -2.14_56]],
                [[-5.80_81, -3.41_24, -4.10_06], [-3.85_61, -2.20_81, -3.03_23], [-3.83_65, -2.46_01, -3.36_69]],
                [[-0.03_09, 3.98_68, 4.05_40], [2.96_40, 4.68_77, 4.99_76], [3.20_81, 4.76_90, 4.99_42]],
            ] ,							device=A_ ,							)

       self.assertTrue(torch.allclose(logits[0, :3, :3, :3] ,							A_ ,							atol=1e-4     )     )





      @slow
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640"     )
       UpperCamelCase			:      List[Any] 			=     model.to(A_     )

       UpperCamelCase			:      str 			=     BeitImageProcessor(do_resize=A_ ,							size=640 ,							do_center_crop=A_     )

       UpperCamelCase			:      Optional[Any] 			=     load_dataset("hf-internal-testing/fixtures_ade20k" ,							split="test"     )
       UpperCamelCase			:      List[Any] 			=     Image.open(ds[0]["file"]     )
       UpperCamelCase			:      List[Any] 			=     image_processor(images=A_ ,							return_tensors="pt"     ).to(A_     )

       # forward pass
       with torch.no_grad():
        UpperCamelCase			:      str 			=     model(**A_     )

       UpperCamelCase			:      List[Any] 			=     outputs.logits.detach().cpu()

       UpperCamelCase			:      Optional[Any] 			=     image_processor.post_process_semantic_segmentation(outputs=A_ ,							target_sizes=[(500, 300)]     )
       UpperCamelCase			:      int 			=     torch.Size((500, 300)     )
       self.assertEqual(segmentation[0].shape ,							A_     )

       UpperCamelCase			:      Any 			=     image_processor.post_process_semantic_segmentation(outputs=A_     )
       UpperCamelCase			:      List[Any] 			=     torch.Size((160, 160)     )
       self.assertEqual(segmentation[0].shape ,							A_     )







import gc
import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, KandinskyVaaPipeline, KandinskyVaaPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.utils import floats_tensor, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()



class       A__     ( __snake_case ,     unittest.TestCase    ):
      _UpperCAmelCase			:str							=     KandinskyVaaPipeline
      _UpperCAmelCase			:str							=     [
          'image_embeds',
          'negative_image_embeds',
      ]
      _UpperCAmelCase			:str							=     ['image_embeds', 'negative_image_embeds']
      _UpperCAmelCase			:List[str]							=     [
          'generator',
          'height',
          'width',
          'latents',
          'guidance_scale',
          'num_inference_steps',
          'return_dict',
          'guidance_scale',
          'num_images_per_prompt',
          'output_type',
          'return_dict',
      ]
      _UpperCAmelCase			:List[str]							=     False





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return 32





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return 32





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return self.time_input_dim





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return self.time_input_dim * 4





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return 100





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       torch.manual_seed(0     )

       UpperCamelCase			:      List[str] 			=     {
           "in_channels": 4,
           # Out channels is double in channels because predicts mean and variance
           "out_channels": 8,
           "addition_embed_type": "image",
           "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"),
           "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"),
           "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
           "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
           "layers_per_block": 1,
           "encoder_hid_dim": self.text_embedder_hidden_size,
           "encoder_hid_dim_type": "image_proj",
           "cross_attention_dim": self.cross_attention_dim,
           "attention_head_dim": 4,
           "resnet_time_scale_shift": "scale_shift",
           "class_embed_type": None,
       }

       UpperCamelCase			:      Dict 			=     UNetaDConditionModel(**A_     )
       return model





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       return {
           "block_out_channels": [32, 64],
           "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
           "in_channels": 3,
           "latent_channels": 4,
           "layers_per_block": 1,
           "norm_num_groups": 8,
           "norm_type": "spatial",
           "num_vq_embeddings": 12,
           "out_channels": 3,
           "up_block_types": [
               "AttnUpDecoderBlock2D",
               "UpDecoderBlock2D",
           ],
           "vq_embed_dim": 4,
       }





      @property
      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       torch.manual_seed(0     )
       UpperCamelCase			:      Optional[Any] 			=     VQModel(**self.dummy_movq_kwargs     )
       return model





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Tuple 			=     self.dummy_unet
       UpperCamelCase			:      Optional[Any] 			=     self.dummy_movq

       UpperCamelCase			:      Dict 			=     DDIMScheduler(
           num_train_timesteps=1000 ,							beta_schedule="linear" ,							beta_start=0.0_00_85 ,							beta_end=0.0_12 ,							clip_sample=A_ ,							set_alpha_to_one=A_ ,							steps_offset=1 ,							prediction_type="epsilon" ,							thresholding=A_ ,							)

       UpperCamelCase			:      Tuple 			=     {
           "unet": unet,
           "scheduler": scheduler,
           "movq": movq,
       }
       return components





      def  __UpperCamelCase(				self ,							A_ ,							A_=0     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     floats_tensor((1, self.text_embedder_hidden_size) ,							rng=random.Random(A_     )     ).to(A_     )
       UpperCamelCase			:      Optional[Any] 			=     floats_tensor((1, self.text_embedder_hidden_size) ,							rng=random.Random(seed + 1     )     ).to(
           A_     )
       if str(A_     ).startswith("mps"     ):
        UpperCamelCase			:      Optional[Any] 			=     torch.manual_seed(A_     )
       else:
        UpperCamelCase			:      List[Any] 			=     torch.Generator(device=A_     ).manual_seed(A_     )
       UpperCamelCase			:      Optional[int] 			=     {
           "image_embeds": image_embeds,
           "negative_image_embeds": negative_image_embeds,
           "generator": generator,
           "height": 64,
           "width": 64,
           "guidance_scale": 4.0,
           "num_inference_steps": 2,
           "output_type": "np",
       }
       return inputs





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     "cpu"

       UpperCamelCase			:      List[str] 			=     self.get_dummy_components()

       UpperCamelCase			:      Tuple 			=     self.pipeline_class(**A_     )
       UpperCamelCase			:      List[str] 			=     pipe.to(A_     )

       pipe.set_progress_bar_config(disable=A_     )

       UpperCamelCase			:      Dict 			=     pipe(**self.get_dummy_inputs(A_     )     )
       UpperCamelCase			:      Optional[int] 			=     output.images

       UpperCamelCase			:      int 			=     pipe(
           **self.get_dummy_inputs(A_     ) ,							return_dict=A_ ,							)[0]

       UpperCamelCase			:      Tuple 			=     image[0, -3:, -3:, -1]
       UpperCamelCase			:      List[Any] 			=     image_from_tuple[0, -3:, -3:, -1]

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

       UpperCamelCase			:      int 			=     np.array(
           [0.6_23_79_76, 1.0, 0.36_44_13_32, 1.0, 0.70_63_96_34, 0.29_87_71_86, 0.85_65_21_25, 0.5_21_68_43, 0.54_45_40_46]     )

       assert (
           np.abs(image_slice.flatten() - expected_slice     ).max() < 1e-2
       ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}"""

       assert (
           np.abs(image_from_tuple_slice.flatten() - expected_slice     ).max() < 1e-2
       ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"""



@slow
@require_torch_gpu
class       A__     ( unittest.TestCase    ):





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()





      def  __UpperCamelCase(				self     ):
       '''simple docstring'''




       UpperCamelCase			:      Dict 			=     load_numpy(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy"     )

       UpperCamelCase			:      Dict 			=     KandinskyVaaPriorPipeline.from_pretrained(
           "kandinsky-community/kandinsky-2-2-prior" ,							torch_dtype=torch.floataa     )
       pipe_prior.to(A_     )

       UpperCamelCase			:      Dict 			=     KandinskyVaaPipeline.from_pretrained(
           "kandinsky-community/kandinsky-2-2-decoder" ,							torch_dtype=torch.floataa     )
       UpperCamelCase			:      Tuple 			=     pipeline.to(A_     )
       pipeline.set_progress_bar_config(disable=A_     )

       UpperCamelCase			:      str 			=     "red cat, 4k photo"

       UpperCamelCase			:      str 			=     torch.Generator(device="cuda"     ).manual_seed(0     )
       UpperCamelCase			,					UpperCamelCase			:      Tuple 			=     pipe_prior(
           A_ ,							generator=A_ ,							num_inference_steps=5 ,							negative_prompt="" ,							).to_tuple()

       UpperCamelCase			:      int 			=     torch.Generator(device="cuda"     ).manual_seed(0     )
       UpperCamelCase			:      Tuple 			=     pipeline(
           image_embeds=A_ ,							negative_image_embeds=A_ ,							generator=A_ ,							num_inference_steps=100 ,							output_type="np" ,							)

       UpperCamelCase			:      Union[str, Any] 			=     output.images[0]

       assert image.shape == (512, 512, 3)

       assert_mean_pixel_difference(A_ ,							A_     )
