





'''simple docstring'''






import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
      import torch

      from transformers import (
          MODEL_MAPPING,
          SegformerForImageClassification,
          SegformerForSemanticSegmentation,
          SegformerModel,
      )
      from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import SegformerImageProcessor




class  UpperCAmelCase     (			snake_case_   ):




      def 				lowercase__    (			self			: List[Any]  )				->      Union[str, Any]:
            _lowerCAmelCase							     =							self.config_class(**self.inputs_dict  )
            self.parent.assertTrue(hasattr(__snake_case			,			"""hidden_sizes"""  )  )
            self.parent.assertTrue(hasattr(__snake_case			,			"""num_attention_heads"""  )  )
            self.parent.assertTrue(hasattr(__snake_case			,			"""num_encoder_blocks"""  )  )




class  UpperCAmelCase     :




      def __init__(			self			: Optional[int]			,			__snake_case			: str			,			__snake_case			: Dict=13			,			__snake_case			: str=64			,			__snake_case			: Dict=3			,			__snake_case			: Dict=4			,			__snake_case			: Tuple=[2, 2, 2, 2]			,			__snake_case			: int=[8, 4, 2, 1]			,			__snake_case			: List[str]=[16, 32, 64, 1_28]			,			__snake_case			: Optional[Any]=[1, 4, 8, 16]			,			__snake_case			: Dict=[1, 2, 4, 8]			,			__snake_case			: Optional[Any]=True			,			__snake_case			: List[str]=True			,			__snake_case			: int="gelu"			,			__snake_case			: Optional[Any]=0.1			,			__snake_case			: Any=0.1			,			__snake_case			: Tuple=0.02			,			__snake_case			: Union[str, Any]=3			,			__snake_case			: Tuple=None			,			)				->      List[str]:
            _lowerCAmelCase							     =							parent
            _lowerCAmelCase							     =							batch_size
            _lowerCAmelCase							     =							image_size
            _lowerCAmelCase							     =							num_channels
            _lowerCAmelCase							     =							num_encoder_blocks
            _lowerCAmelCase							     =							sr_ratios
            _lowerCAmelCase							     =							depths
            _lowerCAmelCase							     =							hidden_sizes
            _lowerCAmelCase							     =							downsampling_rates
            _lowerCAmelCase							     =							num_attention_heads
            _lowerCAmelCase							     =							is_training
            _lowerCAmelCase							     =							use_labels
            _lowerCAmelCase							     =							hidden_act
            _lowerCAmelCase							     =							hidden_dropout_prob
            _lowerCAmelCase							     =							attention_probs_dropout_prob
            _lowerCAmelCase							     =							initializer_range
            _lowerCAmelCase							     =							num_labels
            _lowerCAmelCase							     =							scope




      def 				lowercase__    (			self			: int  )				->      Union[str, Any]:
            _lowerCAmelCase							     =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

            _lowerCAmelCase							     =							None
            if self.use_labels:
                  _lowerCAmelCase							     =							ids_tensor([self.batch_size, self.image_size, self.image_size]			,			self.num_labels  )

            _lowerCAmelCase							     =							self.get_config()
            return config, pixel_values, labels




      def 				lowercase__    (			self			: List[Any]  )				->      List[str]:
            return SegformerConfig(
                image_size=self.image_size			,			num_channels=self.num_channels			,			num_encoder_blocks=self.num_encoder_blocks			,			depths=self.depths			,			hidden_sizes=self.hidden_sizes			,			num_attention_heads=self.num_attention_heads			,			hidden_act=self.hidden_act			,			hidden_dropout_prob=self.hidden_dropout_prob			,			attention_probs_dropout_prob=self.attention_probs_dropout_prob			,			initializer_range=self.initializer_range			,			)




      def 				lowercase__    (			self			: Tuple			,			__snake_case			: Optional[Any]			,			__snake_case			: Union[str, Any]			,			__snake_case			: Optional[int]  )				->      Tuple:
            _lowerCAmelCase							     =							SegformerModel(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase							     =							model(__snake_case  )
            _lowerCAmelCase							     =							_lowerCAmelCase							     =							self.image_size // (self.downsampling_rates[-1] * 2)
            self.parent.assertEqual(
                result.last_hidden_state.shape			,			(self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)  )




      def 				lowercase__    (			self			: List[str]			,			__snake_case			: List[Any]			,			__snake_case			: Optional[Any]			,			__snake_case			: Optional[int]  )				->      List[str]:
            _lowerCAmelCase							     =							self.num_labels
            _lowerCAmelCase							     =							SegformerForSemanticSegmentation(__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase							     =							model(__snake_case  )
            self.parent.assertEqual(
                result.logits.shape			,			(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)  )
            _lowerCAmelCase							     =							model(__snake_case			,			labels=__snake_case  )
            self.parent.assertEqual(
                result.logits.shape			,			(self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)  )
            self.parent.assertGreater(result.loss			,			0.0  )




      def 				lowercase__    (			self			: str			,			__snake_case			: Union[str, Any]			,			__snake_case			: Optional[int]			,			__snake_case			: Dict  )				->      List[str]:
            _lowerCAmelCase							     =							1
            _lowerCAmelCase							     =							SegformerForSemanticSegmentation(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase							     =							torch.randint(0			,			1			,			(self.batch_size, self.image_size, self.image_size)  ).to(__snake_case  )
            _lowerCAmelCase							     =							model(__snake_case			,			labels=__snake_case  )
            self.parent.assertGreater(result.loss			,			0.0  )




      def 				lowercase__    (			self			: Optional[int]  )				->      int:
            _lowerCAmelCase							     =							self.prepare_config_and_inputs()
            _lowerCAmelCase     ,							_lowerCAmelCase     ,							_lowerCAmelCase							     =							config_and_inputs
            _lowerCAmelCase							     =							{"""pixel_values""": pixel_values}
            return config, inputs_dict




@require_torch
class  UpperCAmelCase     (			snake_case_							,		snake_case_							,		unittest.TestCase   ):
      _lowercase:			Any						     =   (
          (
              SegformerModel,
              SegformerForSemanticSegmentation,
              SegformerForImageClassification,
          )
          if is_torch_available()
          else ()
      )
      _lowercase:			Tuple						     =   (
          {
              '''feature-extraction''': SegformerModel,
              '''image-classification''': SegformerForImageClassification,
              '''image-segmentation''': SegformerForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      _lowercase:			Tuple						     =   True
      _lowercase:			Union[str, Any]						     =   False
      _lowercase:			Dict						     =   False
      _lowercase:			Optional[Any]						     =   False




      def 				lowercase__    (			self			: Tuple  )				->      Any:
            _lowerCAmelCase							     =							SegformerModelTester(self  )
            _lowerCAmelCase							     =							SegformerConfigTester(self			,			config_class=__snake_case  )




      def 				lowercase__    (			self			: Optional[Any]  )				->      Dict:
            self.config_tester.run_common_tests()




      def 				lowercase__    (			self			: int  )				->      Union[str, Any]:
            _lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__snake_case  )




      def 				lowercase__    (			self			: Dict  )				->      int:
            _lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_binary_image_segmentation(*__snake_case  )




      def 				lowercase__    (			self			: Dict  )				->      Dict:
            _lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_segmentation(*__snake_case  )




      @unittest.skip("""SegFormer does not use inputs_embeds"""  )
      def 				lowercase__    (			self			: int  )				->      Union[str, Any]:
            pass




      @unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""  )
      def 				lowercase__    (			self			: Optional[int]  )				->      int:
            pass




      def 				lowercase__    (			self			: Union[str, Any]  )				->      Optional[Any]:
            _lowerCAmelCase     ,							_lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs_for_common()

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

                  _lowerCAmelCase							     =							["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]			,			__snake_case  )




      def 				lowercase__    (			self			: Tuple  )				->      Tuple:
            _lowerCAmelCase     ,							_lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase							     =							True

            for model_class in self.all_model_classes:
                  _lowerCAmelCase							     =							True
                  _lowerCAmelCase							     =							False
                  _lowerCAmelCase							     =							True
                  _lowerCAmelCase							     =							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()
                  with torch.no_grad():
                        _lowerCAmelCase							     =							model(**self._prepare_for_class(__snake_case			,			__snake_case  )  )
                  _lowerCAmelCase							     =							outputs.attentions

                  _lowerCAmelCase							     =							sum(self.model_tester.depths  )
                  self.assertEqual(len(__snake_case  )			,			__snake_case  )

                  # check that output_attentions also work using config
                  del inputs_dict["output_attentions"]
                  _lowerCAmelCase							     =							True
                  _lowerCAmelCase							     =							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()
                  with torch.no_grad():
                        _lowerCAmelCase							     =							model(**self._prepare_for_class(__snake_case			,			__snake_case  )  )
                  _lowerCAmelCase							     =							outputs.attentions

                  self.assertEqual(len(__snake_case  )			,			__snake_case  )

                  # verify the first attentions (first block, first layer)
                  _lowerCAmelCase							     =							(self.model_tester.image_size // 4) ** 2
                  _lowerCAmelCase							     =							(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                  self.assertListEqual(
                      list(attentions[0].shape[-3:]  )			,			[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]			,			)

                  # verify the last attentions (last block, last layer)
                  _lowerCAmelCase							     =							(self.model_tester.image_size // 32) ** 2
                  _lowerCAmelCase							     =							(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
                  self.assertListEqual(
                      list(attentions[-1].shape[-3:]  )			,			[self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]			,			)
                  _lowerCAmelCase							     =							len(__snake_case  )

                  # Check attention is always last and order is fine
                  _lowerCAmelCase							     =							True
                  _lowerCAmelCase							     =							True
                  _lowerCAmelCase							     =							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()
                  with torch.no_grad():
                        _lowerCAmelCase							     =							model(**self._prepare_for_class(__snake_case			,			__snake_case  )  )

                  self.assertEqual(out_len + 1			,			len(__snake_case  )  )

                  _lowerCAmelCase							     =							outputs.attentions

                  self.assertEqual(len(__snake_case  )			,			__snake_case  )
                  # verify the first attentions (first block, first layer)
                  _lowerCAmelCase							     =							(self.model_tester.image_size // 4) ** 2
                  _lowerCAmelCase							     =							(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
                  self.assertListEqual(
                      list(self_attentions[0].shape[-3:]  )			,			[self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]			,			)




      def 				lowercase__    (			self			: int  )				->      List[str]:
            def check_hidden_states_output(__snake_case			: str			,			__snake_case			: Tuple			,			__snake_case			: Optional[int]  ):
                  _lowerCAmelCase							     =							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.eval()

                  with torch.no_grad():
                        _lowerCAmelCase							     =							model(**self._prepare_for_class(__snake_case			,			__snake_case  )  )

                  _lowerCAmelCase							     =							outputs.hidden_states

                  _lowerCAmelCase							     =							self.model_tester.num_encoder_blocks
                  self.assertEqual(len(__snake_case  )			,			__snake_case  )

                  # verify the first hidden states (first block)
                  self.assertListEqual(
                      list(hidden_states[0].shape[-3:]  )			,			[
                          self.model_tester.hidden_sizes[0],
                          self.model_tester.image_size // 4,
                          self.model_tester.image_size // 4,
                      ]			,			)

            _lowerCAmelCase     ,							_lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                  _lowerCAmelCase							     =							True
                  check_hidden_states_output(__snake_case			,			__snake_case			,			__snake_case  )

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  _lowerCAmelCase							     =							True

                  check_hidden_states_output(__snake_case			,			__snake_case			,			__snake_case  )




      def 				lowercase__    (			self			: Optional[Any]  )				->      Any:
            if not self.model_tester.is_training:
                  return

            _lowerCAmelCase     ,							_lowerCAmelCase							     =							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase							     =							True

            for model_class in self.all_model_classes:
                  if model_class in get_values(__snake_case  ):
                        continue

                  _lowerCAmelCase							     =							model_class(__snake_case  )
                  model.to(__snake_case  )
                  model.train()
                  _lowerCAmelCase							     =							self._prepare_for_class(__snake_case			,			__snake_case			,			return_labels=__snake_case  )
                  _lowerCAmelCase							     =							model(**__snake_case  ).loss
                  loss.backward()




      @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""  )
      def 				lowercase__    (			self			: Tuple  )				->      Dict:
            pass




      @slow
      def 				lowercase__    (			self			: str  )				->      Optional[int]:
            for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  _lowerCAmelCase							     =							SegformerModel.from_pretrained(__snake_case  )
                  self.assertIsNotNone(__snake_case  )




def    UpperCamelCase__      (		):





      """simple docstring"""


      _lowerCAmelCase							     =							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
      return image





@require_torch
class  UpperCAmelCase     (			unittest.TestCase   ):




      @slow
      def 				lowercase__    (			self			: Union[str, Any]  )				->      Any:
            # only resize + normalize
            _lowerCAmelCase							     =							SegformerImageProcessor(
                image_scale=(5_12, 5_12)			,			keep_ratio=__snake_case			,			align=__snake_case			,			do_random_crop=__snake_case  )
            _lowerCAmelCase							     =							SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""  ).to(
                __snake_case  )

            _lowerCAmelCase							     =							prepare_img()
            _lowerCAmelCase							     =							image_processor(images=__snake_case			,			return_tensors="""pt"""  )
            _lowerCAmelCase							     =							encoded_inputs.pixel_values.to(__snake_case  )

            with torch.no_grad():
                  _lowerCAmelCase							     =							model(__snake_case  )

            _lowerCAmelCase							     =							torch.Size((1, model.config.num_labels, 1_28, 1_28)  )
            self.assertEqual(outputs.logits.shape			,			__snake_case  )

            _lowerCAmelCase							     =							torch.tensor(
                [
                    [[-4.63_10, -5.52_32, -6.23_56], [-5.19_21, -6.14_44, -6.59_96], [-5.44_24, -6.27_90, -6.75_74]],
                    [[-12.13_91, -13.31_22, -13.95_54], [-12.87_32, -13.93_52, -14.35_63], [-12.94_38, -13.82_26, -14.25_13]],
                    [[-12.51_34, -13.46_86, -14.49_15], [-12.86_69, -14.43_43, -14.77_58], [-13.25_23, -14.58_19, -15.06_94]],
                ]  ).to(__snake_case  )
            self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,			__snake_case			,			atol=1E-4  )  )




      @slow
      def 				lowercase__    (			self			: Optional[Any]  )				->      Any:
            # only resize + normalize
            _lowerCAmelCase							     =							SegformerImageProcessor(
                image_scale=(5_12, 5_12)			,			keep_ratio=__snake_case			,			align=__snake_case			,			do_random_crop=__snake_case  )
            _lowerCAmelCase							     =							SegformerForSemanticSegmentation.from_pretrained(
                """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""  ).to(__snake_case  )

            _lowerCAmelCase							     =							prepare_img()
            _lowerCAmelCase							     =							image_processor(images=__snake_case			,			return_tensors="""pt"""  )
            _lowerCAmelCase							     =							encoded_inputs.pixel_values.to(__snake_case  )

            with torch.no_grad():
                  _lowerCAmelCase							     =							model(__snake_case  )

            _lowerCAmelCase							     =							torch.Size((1, model.config.num_labels, 1_28, 1_28)  )
            self.assertEqual(outputs.logits.shape			,			__snake_case  )

            _lowerCAmelCase							     =							torch.tensor(
                [
                    [[-13.57_48, -13.91_11, -12.65_00], [-14.35_00, -15.36_83, -14.23_28], [-14.75_32, -16.04_24, -15.60_87]],
                    [[-17.16_51, -15.87_25, -12.96_53], [-17.25_80, -17.37_18, -14.82_23], [-16.60_58, -16.87_83, -16.74_52]],
                    [[-3.64_56, -3.02_09, -1.42_03], [-3.07_97, -3.19_59, -2.00_00], [-1.87_57, -1.92_17, -1.69_97]],
                ]  ).to(__snake_case  )
            self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]			,			__snake_case			,			atol=1E-1  )  )




      @slow
      def 				lowercase__    (			self			: Any  )				->      str:
            # only resize + normalize
            _lowerCAmelCase							     =							SegformerImageProcessor(
                image_scale=(5_12, 5_12)			,			keep_ratio=__snake_case			,			align=__snake_case			,			do_random_crop=__snake_case  )
            _lowerCAmelCase							     =							SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""  ).to(
                __snake_case  )

            _lowerCAmelCase							     =							prepare_img()
            _lowerCAmelCase							     =							image_processor(images=__snake_case			,			return_tensors="""pt"""  )
            _lowerCAmelCase							     =							encoded_inputs.pixel_values.to(__snake_case  )

            with torch.no_grad():
                  _lowerCAmelCase							     =							model(__snake_case  )

            _lowerCAmelCase							     =							outputs.logits.detach().cpu()

            _lowerCAmelCase							     =							image_processor.post_process_semantic_segmentation(outputs=__snake_case			,			target_sizes=[(5_00, 3_00)]  )
            _lowerCAmelCase							     =							torch.Size((5_00, 3_00)  )
            self.assertEqual(segmentation[0].shape			,			__snake_case  )

            _lowerCAmelCase							     =							image_processor.post_process_semantic_segmentation(outputs=__snake_case  )
            _lowerCAmelCase							     =							torch.Size((1_28, 1_28)  )
            self.assertEqual(segmentation[0].shape			,			__snake_case  )












import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyImgaImgPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_image, 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      ):
						_a							:    Dict					=    KandinskyImgaImgPipeline
						_a							:    List[Any]					=    ["""prompt""", """image_embeds""", """negative_image_embeds""", """image"""]
						_a							:    str					=    [
						    """prompt""",
						    """negative_prompt""",
						    """image_embeds""",
						    """negative_image_embeds""",
						    """image""",
						]
						_a							:    List[Any]					=    [
						    """generator""",
						    """height""",
						    """width""",
						    """strength""",
						    """guidance_scale""",
						    """negative_prompt""",
						    """num_inference_steps""",
						    """return_dict""",
						    """guidance_scale""",
						    """num_images_per_prompt""",
						    """output_type""",
						    """return_dict""",
						]
						_a							:    int					=    False






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return 3_2






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return 3_2






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return self.time_input_dim






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return self.time_input_dim * 4






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return 1_0_0






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base"			)
											return tokenizer






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											torch.manual_seed(0			)
											__lowerCAmelCase				   =       MCLIPConfig(
											    numDims=self.cross_attention_dim		,			transformerDimensions=self.text_embedder_hidden_size		,			hidden_size=self.text_embedder_hidden_size		,			intermediate_size=3_7		,			num_attention_heads=4		,			num_hidden_layers=5		,			vocab_size=1_0_0_5		,			)

											__lowerCAmelCase				   =       MultilingualCLIP(_A			)
											__lowerCAmelCase				   =       text_encoder.eval()

											return text_encoder






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											torch.manual_seed(0			)

											__lowerCAmelCase				   =       {
											    "in_channels": 4,
											    # Out channels is double in channels because predicts mean and variance
											    "out_channels": 8,
											    "addition_embed_type": "text_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": "text_image_proj",
											    "cross_attention_dim": self.cross_attention_dim,
											    "attention_head_dim": 4,
											    "resnet_time_scale_shift": "scale_shift",
											    "class_embed_type": None,
											}

											__lowerCAmelCase				   =       UNetaDConditionModel(**_A			)
											return model






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return {
											    "block_out_channels": [3_2, 6_4],
											    "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": 1_2,
											    "out_channels": 3,
											    "up_block_types": [
											        "AttnUpDecoderBlock2D",
											        "UpDecoderBlock2D",
											    ],
											    "vq_embed_dim": 4,
											}






						@property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											torch.manual_seed(0			)
											__lowerCAmelCase				   =       VQModel(**self.dummy_movq_kwargs			)
											return model






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.dummy_text_encoder
											__lowerCAmelCase				   =       self.dummy_tokenizer
											__lowerCAmelCase				   =       self.dummy_unet
											__lowerCAmelCase				   =       self.dummy_movq

											__lowerCAmelCase				   =       {
											    "num_train_timesteps": 1_0_0_0,
											    "beta_schedule": "linear",
											    "beta_start": 0.0_00_85,
											    "beta_end": 0.0_12,
											    "clip_sample": False,
											    "set_alpha_to_one": False,
											    "steps_offset": 0,
											    "prediction_type": "epsilon",
											    "thresholding": False,
											}

											__lowerCAmelCase				   =       DDIMScheduler(**_A			)

											__lowerCAmelCase				   =       {
											    "text_encoder": text_encoder,
											    "tokenizer": tokenizer,
											    "unet": unet,
											    "scheduler": scheduler,
											    "movq": movq,
											}

											return components






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A=0			):




											"""simple docstring"""



											__lowerCAmelCase				   =       floats_tensor((1, self.cross_attention_dim)		,			rng=random.Random(_A			)			).to(_A			)
											__lowerCAmelCase				   =       floats_tensor((1, self.cross_attention_dim)		,			rng=random.Random(seed + 1			)			).to(_A			)
											# create init_image
											__lowerCAmelCase				   =       floats_tensor((1, 3, 6_4, 6_4)		,			rng=random.Random(_A			)			).to(_A			)
											__lowerCAmelCase				   =       image.cpu().permute(0		,			2		,			3		,			1			)[0]
											__lowerCAmelCase				   =       Image.fromarray(np.uinta(_A			)			).convert("RGB"			).resize((2_5_6, 2_5_6)			)

											if str(_A			).startswith("mps"			):
																__lowerCAmelCase				   =       torch.manual_seed(_A			)
											else:
																__lowerCAmelCase				   =       torch.Generator(device=_A			).manual_seed(_A			)
											__lowerCAmelCase				   =       {
											    "prompt": "horse",
											    "image": init_image,
											    "image_embeds": image_embeds,
											    "negative_image_embeds": negative_image_embeds,
											    "generator": generator,
											    "height": 6_4,
											    "width": 6_4,
											    "num_inference_steps": 1_0,
											    "guidance_scale": 7.0,
											    "strength": 0.2,
											    "output_type": "np",
											}
											return inputs






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       "cpu"

											__lowerCAmelCase				   =       self.get_dummy_components()

											__lowerCAmelCase				   =       self.pipeline_class(**_A			)
											__lowerCAmelCase				   =       pipe.to(_A			)

											pipe.set_progress_bar_config(disable=_A			)

											__lowerCAmelCase				   =       pipe(**self.get_dummy_inputs(_A			)			)
											__lowerCAmelCase				   =       output.images

											__lowerCAmelCase				   =       pipe(
											    **self.get_dummy_inputs(_A			)		,			return_dict=_A		,			)[0]

											__lowerCAmelCase				   =       image[0, -3:, -3:, -1]
											__lowerCAmelCase				   =       image_from_tuple[0, -3:, -3:, -1]

											assert image.shape == (1, 6_4, 6_4, 3)

											__lowerCAmelCase				   =       np.array(
											    [0.61_47_49_43, 0.6_07_35_39, 0.43_30_85_44, 0.5_92_82_69, 0.47_49_35_95, 0.46_75_59_73, 0.4_61_38_38, 0.45_36_87_97, 0.50_11_92_33]			)
											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 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       load_numpy(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
											    "/kandinsky/kandinsky_img2img_frog.npy"			)

											__lowerCAmelCase				   =       load_image(
											    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main" "/kandinsky/cat.png"			)
											__lowerCAmelCase				   =       "A red cartoon frog, 4k"

											__lowerCAmelCase				   =       KandinskyPriorPipeline.from_pretrained(
											    "kandinsky-community/kandinsky-2-1-prior"		,			torch_dtype=torch.floataa			)
											pipe_prior.to(_A			)

											__lowerCAmelCase				   =       KandinskyImgaImgPipeline.from_pretrained(
											    "kandinsky-community/kandinsky-2-1"		,			torch_dtype=torch.floataa			)
											__lowerCAmelCase				   =       pipeline.to(_A			)

											pipeline.set_progress_bar_config(disable=_A			)

											__lowerCAmelCase				   =       torch.Generator(device="cpu"			).manual_seed(0			)
											__lowerCAmelCase   ,							__lowerCAmelCase				   =       pipe_prior(
											    _A		,			generator=_A		,			num_inference_steps=5		,			negative_prompt=""		,			).to_tuple()

											__lowerCAmelCase				   =       pipeline(
											    _A		,			image=_A		,			image_embeds=_A		,			negative_image_embeds=_A		,			generator=_A		,			num_inference_steps=1_0_0		,			height=7_6_8		,			width=7_6_8		,			strength=0.2		,			output_type="np"		,			)

											__lowerCAmelCase				   =       output.images[0]

											assert image.shape == (7_6_8, 7_6_8, 3)

											assert_mean_pixel_difference(_A		,			_A			)




