


"""simple docstring"""







import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   unittest.TestCase    ):
       """simple docstring"""


       a  :    Any       =KandinskyVaaPriorPipeline
       a  :    List[str]       =["prompt"]
       a  :    Tuple       =["prompt", "negative_prompt"]
       a  :    List[Any]       =[
           "num_images_per_prompt",
           "generator",
           "num_inference_steps",
           "latents",
           "negative_prompt",
           "guidance_scale",
           "output_type",
           "return_dict",
       ]
       a  :    List[str]       =False

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return 32

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return 32

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return self.time_input_dim

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return self.time_input_dim * 4

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return 100

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





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

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         torch.manual_seed(0						)
         lowerCAmelCase					:					Union[str, 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=1_000		,					)
         return CLIPTextModelWithProjection(snake_case__						)

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         torch.manual_seed(0						)

         lowerCAmelCase					:					Optional[int]								=							{
             "num_attention_heads": 2,
             "attention_head_dim": 12,
             "embedding_dim": self.text_embedder_hidden_size,
             "num_layers": 1,
         }

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

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         torch.manual_seed(0						)
         lowerCAmelCase					:					int								=							CLIPVisionConfig(
             hidden_size=self.text_embedder_hidden_size		,					image_size=224		,					projection_dim=self.text_embedder_hidden_size		,					intermediate_size=37		,					num_attention_heads=4		,					num_channels=3		,					num_hidden_layers=5		,					patch_size=14		,					)

         lowerCAmelCase					:					List[str]								=							CLIPVisionModelWithProjection(snake_case__						)
         return model

       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							CLIPImageProcessor(
             crop_size=224		,					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=224		,					)

         return image_processor

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							self.dummy_prior
         lowerCAmelCase					:					Dict								=							self.dummy_image_encoder
         lowerCAmelCase					:					List[Any]								=							self.dummy_text_encoder
         lowerCAmelCase					:					Optional[Any]								=							self.dummy_tokenizer
         lowerCAmelCase					:					Union[str, Any]								=							self.dummy_image_processor

         lowerCAmelCase					:					str								=							UnCLIPScheduler(
             variance_type="fixed_small_log"		,					prediction_type="sample"		,					num_train_timesteps=1_000		,					clip_sample=snake_case__		,					clip_sample_range=10.0		,					)

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

         return components

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__=0						):






         """simple docstring"""





         if str(snake_case__						).startswith("mps"						):
           lowerCAmelCase					:					str								=							torch.manual_seed(snake_case__						)
         else:
           lowerCAmelCase					:					Any								=							torch.Generator(device=snake_case__						).manual_seed(snake_case__						)
         lowerCAmelCase					:					List[str]								=							{
             "prompt": "horse",
             "generator": generator,
             "guidance_scale": 4.0,
             "num_inference_steps": 2,
             "output_type": "np",
         }
         return inputs

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							"cpu"

         lowerCAmelCase					:					str								=							self.get_dummy_components()

         lowerCAmelCase					:					Any								=							self.pipeline_class(**snake_case__						)
         lowerCAmelCase					:					List[str]								=							pipe.to(snake_case__						)

         pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Optional[int]								=							pipe(**self.get_dummy_inputs(snake_case__						)						)
         lowerCAmelCase					:					Any								=							output.image_embeds

         lowerCAmelCase					:					Dict								=							pipe(
             **self.get_dummy_inputs(snake_case__						)		,					return_dict=snake_case__		,					)[0]

         lowerCAmelCase					:					str								=							image[0, -10:]
         lowerCAmelCase					:					Tuple								=							image_from_tuple[0, -10:]

         assert image.shape == (1, 32)

         lowerCAmelCase					:					List[Any]								=							np.array(
             [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]						)

         assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
         assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < 1e-2

       @skip_mps
       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							torch_device == "cpu"
         lowerCAmelCase					:					Any								=							True
         lowerCAmelCase					:					str								=							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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							torch_device == "cpu"
         lowerCAmelCase					:					int								=							False

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









"""simple docstring"""







import pyarrow.parquet as pq
import pytest

from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config
from datasets.features.image import Image
from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases
def  a__      (    SCREAMING_SNAKE_CASE   :   str    ,				SCREAMING_SNAKE_CASE   :   Any    ):


  '''simple docstring'''







  assert isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
  assert dataset.num_rows == 4
  assert dataset.num_columns == 3
  assert dataset.column_names == ["col_1", "col_2", "col_3"]
  for feature, expected_dtype in expected_features.items():
    assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize("keep_in_memory"    ,				[False, True]    )
def  a__      (    SCREAMING_SNAKE_CASE   :   Any    ,				SCREAMING_SNAKE_CASE   :   List[str]    ,				SCREAMING_SNAKE_CASE   :   int    ):


  '''simple docstring'''







  lowerCAmelCase					:					List[Any]								=							tmp_path / "cache"
  lowerCAmelCase					:					Optional[int]								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    lowerCAmelCase					:					str								=							ParquetDatasetReader(SCREAMING_SNAKE_CASE    ,				cache_dir=SCREAMING_SNAKE_CASE    ,				keep_in_memory=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
@pytest.mark.parametrize(
    "features"    ,				[
        None,
        {"col_1": "string", "col_2": "int64", "col_3": "float64"},
        {"col_1": "string", "col_2": "string", "col_3": "string"},
        {"col_1": "int32", "col_2": "int32", "col_3": "int32"},
        {"col_1": "float32", "col_2": "float32", "col_3": "float32"},
    ]    ,				)
def  a__      (    SCREAMING_SNAKE_CASE   :   Tuple    ,				SCREAMING_SNAKE_CASE   :   Optional[Any]    ,				SCREAMING_SNAKE_CASE   :   Tuple    ):


  '''simple docstring'''







  lowerCAmelCase					:					Optional[int]								=							tmp_path / "cache"
  lowerCAmelCase					:					Optional[Any]								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowerCAmelCase					:					Tuple								=							features.copy() if features else default_expected_features
  lowerCAmelCase					:					List[str]								=							(
      Features({feature: Value(SCREAMING_SNAKE_CASE    ) for feature, dtype in features.items()}    ) if features is not None else None
  )
  lowerCAmelCase					:					List[str]								=							ParquetDatasetReader(SCREAMING_SNAKE_CASE    ,				features=SCREAMING_SNAKE_CASE    ,				cache_dir=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
@pytest.mark.parametrize("split"    ,				[None, NamedSplit("train"    ), "train", "test"]    )
def  a__      (    SCREAMING_SNAKE_CASE   :   Union[str, Any]    ,				SCREAMING_SNAKE_CASE   :   Union[str, Any]    ,				SCREAMING_SNAKE_CASE   :   Optional[int]    ):


  '''simple docstring'''







  lowerCAmelCase					:					List[Any]								=							tmp_path / "cache"
  lowerCAmelCase					:					int								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowerCAmelCase					:					Tuple								=							ParquetDatasetReader(SCREAMING_SNAKE_CASE    ,				cache_dir=SCREAMING_SNAKE_CASE    ,				split=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
  assert dataset.split == split if split else "train"
@pytest.mark.parametrize("path_type"    ,				[str, list]    )
def  a__      (    SCREAMING_SNAKE_CASE   :   List[Any]    ,				SCREAMING_SNAKE_CASE   :   Dict    ,				SCREAMING_SNAKE_CASE   :   List[str]    ):


  '''simple docstring'''







  if issubclass(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ):
    lowerCAmelCase					:					Optional[Any]								=							parquet_path
  elif issubclass(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ):
    lowerCAmelCase					:					Dict								=							[parquet_path]
  lowerCAmelCase					:					Optional[Any]								=							tmp_path / "cache"
  lowerCAmelCase					:					Any								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowerCAmelCase					:					Optional[Any]								=							ParquetDatasetReader(SCREAMING_SNAKE_CASE    ,				cache_dir=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
def  a__      (    SCREAMING_SNAKE_CASE   :   Dict    ,				SCREAMING_SNAKE_CASE   :   List[str]    ,				SCREAMING_SNAKE_CASE   :   Tuple=("train",)    ):


  '''simple docstring'''







  assert isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
  for split in splits:
    lowerCAmelCase					:					List[Any]								=							dataset_dict[split]
    assert dataset.num_rows == 4
    assert dataset.num_columns == 3
    assert dataset.column_names == ["col_1", "col_2", "col_3"]
    for feature, expected_dtype in expected_features.items():
      assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize("keep_in_memory"    ,				[False, True]    )
def  a__      (    SCREAMING_SNAKE_CASE   :   Optional[int]    ,				SCREAMING_SNAKE_CASE   :   Tuple    ,				SCREAMING_SNAKE_CASE   :   Tuple    ):


  '''simple docstring'''







  lowerCAmelCase					:					Tuple								=							tmp_path / "cache"
  lowerCAmelCase					:					List[Any]								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    lowerCAmelCase					:					Optional[Any]								=							ParquetDatasetReader(
        {"train": parquet_path}    ,				cache_dir=SCREAMING_SNAKE_CASE    ,				keep_in_memory=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_datasetdict(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
@pytest.mark.parametrize(
    "features"    ,				[
        None,
        {"col_1": "string", "col_2": "int64", "col_3": "float64"},
        {"col_1": "string", "col_2": "string", "col_3": "string"},
        {"col_1": "int32", "col_2": "int32", "col_3": "int32"},
        {"col_1": "float32", "col_2": "float32", "col_3": "float32"},
    ]    ,				)
def  a__      (    SCREAMING_SNAKE_CASE   :   Optional[int]    ,				SCREAMING_SNAKE_CASE   :   Dict    ,				SCREAMING_SNAKE_CASE   :   Optional[int]    ):


  '''simple docstring'''







  lowerCAmelCase					:					Any								=							tmp_path / "cache"
  lowerCAmelCase					:					Union[str, Any]								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowerCAmelCase					:					List[Any]								=							features.copy() if features else default_expected_features
  lowerCAmelCase					:					List[Any]								=							(
      Features({feature: Value(SCREAMING_SNAKE_CASE    ) for feature, dtype in features.items()}    ) if features is not None else None
  )
  lowerCAmelCase					:					Optional[int]								=							ParquetDatasetReader({"train": parquet_path}    ,				features=SCREAMING_SNAKE_CASE    ,				cache_dir=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_datasetdict(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    )
@pytest.mark.parametrize("split"    ,				[None, NamedSplit("train"    ), "train", "test"]    )
def  a__      (    SCREAMING_SNAKE_CASE   :   str    ,				SCREAMING_SNAKE_CASE   :   Dict    ,				SCREAMING_SNAKE_CASE   :   List[str]    ):


  '''simple docstring'''







  if split:
    lowerCAmelCase					:					List[str]								=							{split: parquet_path}
  else:
    lowerCAmelCase					:					List[str]								=							"train"
    lowerCAmelCase					:					str								=							{"train": parquet_path, "test": parquet_path}
  lowerCAmelCase					:					Optional[int]								=							tmp_path / "cache"
  lowerCAmelCase					:					List[Any]								=							{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowerCAmelCase					:					List[str]								=							ParquetDatasetReader(SCREAMING_SNAKE_CASE    ,				cache_dir=SCREAMING_SNAKE_CASE    ).read()
  _check_parquet_datasetdict(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE    ,				splits=list(path.keys()    )    )
  assert all(dataset[split].split == split for split in path.keys()    )
def  a__      (    SCREAMING_SNAKE_CASE   :   Optional[Any]    ,				SCREAMING_SNAKE_CASE   :   Any    ):


  '''simple docstring'''







  lowerCAmelCase					:					Optional[int]								=							ParquetDatasetWriter(SCREAMING_SNAKE_CASE    ,				tmp_path / "foo.parquet"    )
  assert writer.write() > 0
  lowerCAmelCase					:					List[str]								=							pq.ParquetFile(tmp_path / "foo.parquet"    )
  lowerCAmelCase					:					Union[str, Any]								=							pf.read()
  assert dataset.data.table == output_table
def  a__      (    SCREAMING_SNAKE_CASE   :   List[Any]    ,				SCREAMING_SNAKE_CASE   :   List[str]    ):


  '''simple docstring'''







  lowerCAmelCase					:					str								=							str(shared_datadir / "test_image_rgb.jpg"    )
  lowerCAmelCase					:					List[str]								=							{"image": [image_path]}
  lowerCAmelCase					:					str								=							Features({"image": Image()}    )
  lowerCAmelCase					:					Optional[Any]								=							Dataset.from_dict(SCREAMING_SNAKE_CASE    ,				features=SCREAMING_SNAKE_CASE    )
  lowerCAmelCase					:					List[Any]								=							ParquetDatasetWriter(SCREAMING_SNAKE_CASE    ,				tmp_path / "foo.parquet"    )
  assert writer.write() > 0

  lowerCAmelCase					:					Any								=							Dataset.from_parquet(str(tmp_path / "foo.parquet"    )    )
  assert dataset.features == reloaded_dataset.features

  lowerCAmelCase					:					Optional[int]								=							ParquetDatasetReader(str(tmp_path / "foo.parquet"    )    ,				streaming=SCREAMING_SNAKE_CASE    ).read()
  assert dataset.features == reloaded_iterable_dataset.features





@pytest.mark.parametrize(
    "feature, expected"    ,				[
        (Features({"foo": Value("int32"    )}    ), None),
        (Features({"image": Image(), "foo": Value("int32"    )}    ), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS),
        (Features({"nested": Sequence(Audio()    )}    ), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS),
    ]    ,				)
def  a__      (    SCREAMING_SNAKE_CASE   :   str    ,				SCREAMING_SNAKE_CASE   :   List[str]    ):


  '''simple docstring'''







  assert get_writer_batch_size(SCREAMING_SNAKE_CASE    ) == expected





