



'''simple docstring'''





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        lowerCAmelCase_(				A__						, unittest.TestCase     ):
  '''simple docstring'''




  __lowercase							:      Optional[int]				=      KandinskyImgaImgPipeline
  __lowercase							:      List[Any]				=      ['''prompt''', '''image_embeds''', '''negative_image_embeds''', '''image''']
  __lowercase							:      Union[str, Any]				=      [
      '''prompt''',
      '''negative_prompt''',
      '''image_embeds''',
      '''negative_image_embeds''',
      '''image''',
  ]
  __lowercase							:      Optional[Any]				=      [
      '''generator''',
      '''height''',
      '''width''',
      '''strength''',
      '''guidance_scale''',
      '''negative_prompt''',
      '''num_inference_steps''',
      '''return_dict''',
      '''guidance_scale''',
      '''num_images_per_prompt''',
      '''output_type''',
      '''return_dict''',
  ]
  __lowercase							:      Union[str, Any]				=      False




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      return 32




  @property
  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      return 32




  @property
  def 							UpperCAmelCase_	(    self      )    ->		int:
      return self.time_input_dim




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      return self.time_input_dim * 4




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      return 100




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      lowerCAmelCase__ :  Optional[Any]							      =					XLMRobertaTokenizerFast.from_pretrained("""YiYiXu/tiny-random-mclip-base"""      )
      return tokenizer




  @property
  def 							UpperCAmelCase_	(    self      )    ->		int:
      torch.manual_seed(0      )
      lowerCAmelCase__ :  Union[str, Any]							      =					MCLIPConfig(
          numDims=self.cross_attention_dim  ,transformerDimensions=self.text_embedder_hidden_size  ,hidden_size=self.text_embedder_hidden_size  ,intermediate_size=37  ,num_attention_heads=4  ,num_hidden_layers=5  ,vocab_size=1005  ,)

      lowerCAmelCase__ :  str							      =					MultilingualCLIP(__UpperCAmelCase      )
      lowerCAmelCase__ :  List[str]							      =					text_encoder.eval()

      return text_encoder




  @property
  def 							UpperCAmelCase_	(    self      )    ->		int:
      torch.manual_seed(0      )

      lowerCAmelCase__ :  Optional[int]							      =					{
          "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__ :  str							      =					UNetaDConditionModel(**__UpperCAmelCase      )
      return model




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Dict:
      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      )    ->		Union[str, Any]:
      torch.manual_seed(0      )
      lowerCAmelCase__ :  Tuple							      =					VQModel(**self.dummy_movq_kwargs      )
      return model




  def 							UpperCAmelCase_	(    self      )    ->		str:
      lowerCAmelCase__ :  int							      =					self.dummy_text_encoder
      lowerCAmelCase__ :  List[str]							      =					self.dummy_tokenizer
      lowerCAmelCase__ :  List[str]							      =					self.dummy_unet
      lowerCAmelCase__ :  int							      =					self.dummy_movq

      lowerCAmelCase__ :  Optional[int]							      =					{
          "num_train_timesteps": 1000,
          "beta_schedule": "linear",
          "beta_start": 0.0_0_0_8_5,
          "beta_end": 0.0_1_2,
          "clip_sample": False,
          "set_alpha_to_one": False,
          "steps_offset": 0,
          "prediction_type": "epsilon",
          "thresholding": False,
      }

      lowerCAmelCase__ :  int							      =					DDIMScheduler(**__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					{
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "unet": unet,
          "scheduler": scheduler,
          "movq": movq,
      }

      return components




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase  ,__UpperCAmelCase=0      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  Tuple							      =					floats_tensor((1, self.cross_attention_dim)  ,rng=random.Random(__UpperCAmelCase      )      ).to(__UpperCAmelCase      )
      lowerCAmelCase__ :  Optional[Any]							      =					floats_tensor((1, self.cross_attention_dim)  ,rng=random.Random(seed + 1      )      ).to(__UpperCAmelCase      )
      # create init_image
      lowerCAmelCase__ :  Tuple							      =					floats_tensor((1, 3, 64, 64)  ,rng=random.Random(__UpperCAmelCase      )      ).to(__UpperCAmelCase      )
      lowerCAmelCase__ :  Optional[int]							      =					image.cpu().permute(0  ,2  ,3  ,1      )[0]
      lowerCAmelCase__ :  Dict							      =					Image.fromarray(np.uinta(__UpperCAmelCase      )      ).convert("""RGB"""      ).resize((256, 256)      )

      if str(__UpperCAmelCase      ).startswith("""mps"""      ):
          lowerCAmelCase__ :  int							      =					torch.manual_seed(__UpperCAmelCase      )
      else:
          lowerCAmelCase__ :  str							      =					torch.Generator(device=__UpperCAmelCase      ).manual_seed(__UpperCAmelCase      )
      lowerCAmelCase__ :  int							      =					{
          "prompt": "horse",
          "image": init_image,
          "image_embeds": image_embeds,
          "negative_image_embeds": negative_image_embeds,
          "generator": generator,
          "height": 64,
          "width": 64,
          "num_inference_steps": 10,
          "guidance_scale": 7.0,
          "strength": 0.2,
          "output_type": "np",
      }
      return inputs






  def 							UpperCAmelCase_	(    self      )    ->		int:
      lowerCAmelCase__ :  List[str]							      =					"cpu"

      lowerCAmelCase__ :  Dict							      =					self.get_dummy_components()

      lowerCAmelCase__ :  int							      =					self.pipeline_class(**__UpperCAmelCase      )
      lowerCAmelCase__ :  Any							      =					pipe.to(__UpperCAmelCase      )

      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Optional[int]							      =					pipe(**self.get_dummy_inputs(__UpperCAmelCase      )      )
      lowerCAmelCase__ :  Optional[int]							      =					output.images

      lowerCAmelCase__ :  int							      =					pipe(
          **self.get_dummy_inputs(__UpperCAmelCase      )  ,return_dict=__UpperCAmelCase  ,)[0]

      lowerCAmelCase__ :  List[Any]							      =					image[0, -3:, -3:, -1]
      lowerCAmelCase__ :  Optional[Any]							      =					image_from_tuple[0, -3:, -3:, -1]

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

      lowerCAmelCase__ :  List[str]							      =					np.array(
          [0.6_1_4_7_4_9_4_3, 0.6_0_7_3_5_3_9, 0.4_3_3_0_8_5_4_4, 0.5_9_2_8_2_6_9, 0.4_7_4_9_3_5_9_5, 0.4_6_7_5_5_9_7_3, 0.4_6_1_3_8_3_8, 0.4_5_3_6_8_7_9_7, 0.5_0_1_1_9_2_3_3]      )
      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        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  def 							UpperCAmelCase_	(    self      )    ->		Any:
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()






  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      lowerCAmelCase__ :  List[str]							      =					load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
          """/kandinsky/kandinsky_img2img_frog.npy"""      )

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

      lowerCAmelCase__ :  Any							      =					KandinskyPriorPipeline.from_pretrained(
          """kandinsky-community/kandinsky-2-1-prior"""  ,torch_dtype=torch.floataa      )
      pipe_prior.to(__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					KandinskyImgaImgPipeline.from_pretrained(
          """kandinsky-community/kandinsky-2-1"""  ,torch_dtype=torch.floataa      )
      lowerCAmelCase__ :  Dict							      =					pipeline.to(__UpperCAmelCase      )

      pipeline.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Any							      =					torch.Generator(device="""cpu"""      ).manual_seed(0      )
      lowerCAmelCase__ :  List[Any]							      =					pipe_prior(
          __UpperCAmelCase  ,generator=__UpperCAmelCase  ,num_inference_steps=5  ,negative_prompt=""""""  ,).to_tuple()

      lowerCAmelCase__ :  Optional[Any]							      =					pipeline(
          __UpperCAmelCase  ,image=__UpperCAmelCase  ,image_embeds=__UpperCAmelCase  ,negative_image_embeds=__UpperCAmelCase  ,generator=__UpperCAmelCase  ,num_inference_steps=100  ,height=768  ,width=768  ,strength=0.2  ,output_type="""np"""  ,)

      lowerCAmelCase__ :  Tuple							      =					output.images[0]

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

      assert_mean_pixel_difference(__UpperCAmelCase  ,__UpperCAmelCase      )










import os
import tempfile
from functools import partial
from unittest import TestCase
from unittest.mock import patch

import datasets
import datasets.config

from .utils import require_beam





class 			__a  (    datasets.BeamBasedBuilder       ):







 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      return datasets.DatasetInfo(
          features=datasets.Features({"content": datasets.Value("string"						)}						)							,   supervised_keys=SCREAMING_SNAKE_CASE							,   )







 def 					__lowercase					(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Any						):


      '''simple docstring'''






      return [datasets.SplitGenerator(name=datasets.Split.TRAIN							,   gen_kwargs={"examples": get_test_dummy_examples()}						)]







 def 					__lowercase					(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Tuple							,   SCREAMING_SNAKE_CASE					:   int						):


      '''simple docstring'''






      import apache_beam as beam

      return pipeline | "Load Examples" >> beam.Create(SCREAMING_SNAKE_CASE						)







class 			__a  (    datasets.BeamBasedBuilder       ):







 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      return datasets.DatasetInfo(
          features=datasets.Features({"a": datasets.Sequence({"b": datasets.Value("string"						)}						)}						)							,   supervised_keys=SCREAMING_SNAKE_CASE							,   )







 def 					__lowercase					(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Tuple							,   SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      return [
          datasets.SplitGenerator(name=datasets.Split.TRAIN							,   gen_kwargs={"examples": get_test_nested_examples()}						)
      ]







 def 					__lowercase					(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Tuple							,   SCREAMING_SNAKE_CASE					:   Any						):


      '''simple docstring'''






      import apache_beam as beam

      return pipeline | "Load Examples" >> beam.Create(SCREAMING_SNAKE_CASE						)







def 		SCREAMING_SNAKE_CASE							(					) ->	List[str]:
     return [(i, {"content": content}) for i, content in enumerate(["foo", "bar", "foobar"]			)]







def 		SCREAMING_SNAKE_CASE							(					) ->	Optional[Any]:
     return [(i, {"a": {"b": [content]}}) for i, content in enumerate(["foo", "bar", "foobar"]			)]





class 			__a  (    A__       ):







 @require_beam
 def 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__			:   int       				=  len(get_test_dummy_examples()						)
      with tempfile.TemporaryDirectory() as tmp_cache_dir:
           UpperCamelCase__			:   List[Any]       				=  DummyBeamDataset(cache_dir=SCREAMING_SNAKE_CASE							,   beam_runner="DirectRunner"						)
           builder.download_and_prepare()
           self.assertTrue(
               os.path.exists(
                   os.path.join(SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   F'{builder.name}-train.arrow'						)						)						)
           self.assertDictEqual(builder.info.features							,   datasets.Features({"content": datasets.Value("string"						)}						)						)
           UpperCamelCase__			:   Optional[Any]       				=  builder.as_dataset()
           self.assertEqual(dset["train"].num_rows							,   SCREAMING_SNAKE_CASE						)
           self.assertEqual(dset["train"].info.splits["train"].num_examples							,   SCREAMING_SNAKE_CASE						)
           self.assertDictEqual(dset["train"][0]							,   get_test_dummy_examples()[0][1]						)
           self.assertDictEqual(
               dset["train"][expected_num_examples - 1]							,   get_test_dummy_examples()[expected_num_examples - 1][1]						)
           self.assertTrue(
               os.path.exists(os.path.join(SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   "dataset_info.json"						)						)						)
           del dset







 @require_beam
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      import apache_beam as beam

      UpperCamelCase__			:   Any       				=  beam.io.parquetio.WriteToParquet

      UpperCamelCase__			:   Tuple       				=  len(get_test_dummy_examples()						)
      with tempfile.TemporaryDirectory() as tmp_cache_dir:
           UpperCamelCase__			:   str       				=  DummyBeamDataset(cache_dir=SCREAMING_SNAKE_CASE							,   beam_runner="DirectRunner"						)
           with patch("apache_beam.io.parquetio.WriteToParquet"						) as write_parquet_mock:
                UpperCamelCase__			:   Optional[Any]       				=  partial(SCREAMING_SNAKE_CASE							,   num_shards=2						)
                builder.download_and_prepare()
           self.assertTrue(
               os.path.exists(
                   os.path.join(
                       SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   F'{builder.name}-train-00000-of-00002.arrow'						)						)						)
           self.assertTrue(
               os.path.exists(
                   os.path.join(
                       SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   F'{builder.name}-train-00000-of-00002.arrow'						)						)						)
           self.assertDictEqual(builder.info.features							,   datasets.Features({"content": datasets.Value("string"						)}						)						)
           UpperCamelCase__			:   List[Any]       				=  builder.as_dataset()
           self.assertEqual(dset["train"].num_rows							,   SCREAMING_SNAKE_CASE						)
           self.assertEqual(dset["train"].info.splits["train"].num_examples							,   SCREAMING_SNAKE_CASE						)
           # Order is not preserved when sharding, so we just check that all the elements are there
           self.assertListEqual(sorted(dset["train"]["content"]						)							,   sorted(["foo", "bar", "foobar"]						)						)
           self.assertTrue(
               os.path.exists(os.path.join(SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   "dataset_info.json"						)						)						)
           del dset







 @require_beam
 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






      with tempfile.TemporaryDirectory() as tmp_cache_dir:
           UpperCamelCase__			:   int       				=  DummyBeamDataset(cache_dir=SCREAMING_SNAKE_CASE						)
           self.assertRaises(datasets.builder.MissingBeamOptions							,   builder.download_and_prepare						)







 @require_beam
 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   List[Any]       				=  len(get_test_nested_examples()						)
      with tempfile.TemporaryDirectory() as tmp_cache_dir:
           UpperCamelCase__			:   Any       				=  NestedBeamDataset(cache_dir=SCREAMING_SNAKE_CASE							,   beam_runner="DirectRunner"						)
           builder.download_and_prepare()
           self.assertTrue(
               os.path.exists(
                   os.path.join(SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   F'{builder.name}-train.arrow'						)						)						)
           self.assertDictEqual(
               builder.info.features							,   datasets.Features({"a": datasets.Sequence({"b": datasets.Value("string"						)}						)}						)						)
           UpperCamelCase__			:   List[Any]       				=  builder.as_dataset()
           self.assertEqual(dset["train"].num_rows							,   SCREAMING_SNAKE_CASE						)
           self.assertEqual(dset["train"].info.splits["train"].num_examples							,   SCREAMING_SNAKE_CASE						)
           self.assertDictEqual(dset["train"][0]							,   get_test_nested_examples()[0][1]						)
           self.assertDictEqual(
               dset["train"][expected_num_examples - 1]							,   get_test_nested_examples()[expected_num_examples - 1][1]						)
           self.assertTrue(
               os.path.exists(os.path.join(SCREAMING_SNAKE_CASE							,   builder.name							,   "default"							,   "0.0.0"							,   "dataset_info.json"						)						)						)
           del dset