





import gc
import random
import unittest

import numpy as np
import torch

from diffusers import (
    DDIMScheduler,
    KandinskyVaaControlnetPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
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							(					__UpperCamelCase,  unittest.TestCase			):
 UpperCAmelCase__													=		KandinskyVaaControlnetPipeline
 UpperCAmelCase__													=		["""image_embeds""", """negative_image_embeds""", """hint"""]
 UpperCAmelCase__													=		["""image_embeds""", """negative_image_embeds""", """hint"""]
 UpperCAmelCase__													=		[
     """generator""",
     """height""",
     """width""",
     """latents""",
     """guidance_scale""",
     """num_inference_steps""",
     """return_dict""",
     """guidance_scale""",
     """num_images_per_prompt""",
     """output_type""",
     """return_dict""",
 ]
 UpperCAmelCase__													=		False
 @property
 def      A_	(      self  :      Union[str, Any]	)			->	List[Any]:
     return 32
 @property
 def      A_	(      self  :      Optional[int]	)			->	str:
     return 32
 @property
 def      A_	(      self  :      str	)			->	Tuple:
     return self.time_input_dim
 @property
 def      A_	(      self  :      int	)			->	Any:
     return self.time_input_dim * 4
 @property
 def      A_	(      self  :      List[str]	)			->	int:
     return 100
 @property
 def      A_	(      self  :      int	)			->	Union[str, Any]:
     torch.manual_seed(0	)

     lowerCamelCase__    :      List[str]					 =		{
         'in_channels': 8,
         # Out channels is double in channels because predicts mean and variance
         'out_channels': 8,
         'addition_embed_type': 'image_hint',
         '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,
     }

     lowerCamelCase__    :      Dict					 =		UNetaDConditionModel(**UpperCAmelCase	)
     return model
 @property
 def      A_	(      self  :      Any	)			->	Optional[int]:
     return {
         "block_out_channels": [32, 32, 64, 64],
         "down_block_types": [
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "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", "UpDecoderBlock2D", "UpDecoderBlock2D"],
         "vq_embed_dim": 4,
     }
 @property
 def      A_	(      self  :      Union[str, Any]	)			->	List[str]:
     torch.manual_seed(0	)
     lowerCamelCase__    :      Optional[Any]					 =		VQModel(**self.dummy_movq_kwargs	)
     return model
 def      A_	(      self  :      Tuple	)			->	Dict:
     lowerCamelCase__    :      Optional[Any]					 =		self.dummy_unet
     lowerCamelCase__    :      str					 =		self.dummy_movq

     lowerCamelCase__    :      Optional[Any]					 =		DDIMScheduler(
         num_train_timesteps=1000			,     beta_schedule='linear'			,     beta_start=0.0_0_0_8_5			,     beta_end=0.0_1_2			,     clip_sample=UpperCAmelCase			,     set_alpha_to_one=UpperCAmelCase			,     steps_offset=1			,     prediction_type='epsilon'			,     thresholding=UpperCAmelCase			,     )

     lowerCamelCase__    :      Optional[Any]					 =		{
         'unet': unet,
         'scheduler': scheduler,
         'movq': movq,
     }
     return components
 def      A_	(      self  :      str			,     UpperCAmelCase  :      Tuple			,     UpperCAmelCase  :      str=0	)			->	List[Any]:
     lowerCamelCase__    :      Any					 =		floats_tensor((1, self.text_embedder_hidden_size)			,     rng=random.Random(UpperCAmelCase	)	).to(UpperCAmelCase	)
     lowerCamelCase__    :      Dict					 =		floats_tensor((1, self.text_embedder_hidden_size)			,     rng=random.Random(seed + 1	)	).to(
         UpperCAmelCase	)

     # create hint
     lowerCamelCase__    :      Any					 =		floats_tensor((1, 3, 64, 64)			,     rng=random.Random(UpperCAmelCase	)	).to(UpperCAmelCase	)

     if str(UpperCAmelCase	).startswith('mps'	):
         lowerCamelCase__    :      Dict					 =		torch.manual_seed(UpperCAmelCase	)
     else:
         lowerCamelCase__    :      List[str]					 =		torch.Generator(device=UpperCAmelCase	).manual_seed(UpperCAmelCase	)
     lowerCamelCase__    :      Optional[Any]					 =		{
         'image_embeds': image_embeds,
         'negative_image_embeds': negative_image_embeds,
         'hint': hint,
         'generator': generator,
         'height': 64,
         'width': 64,
         'guidance_scale': 4.0,
         'num_inference_steps': 2,
         'output_type': 'np',
     }
     return inputs







 def      A_	(      self  :      List[Any]	)			->	Any:
     lowerCamelCase__    :      int					 =		'cpu'

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

     lowerCamelCase__    :      List[str]					 =		self.pipeline_class(**UpperCAmelCase	)
     lowerCamelCase__    :      Union[str, Any]					 =		pipe.to(UpperCAmelCase	)

     pipe.set_progress_bar_config(disable=UpperCAmelCase	)

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

     lowerCamelCase__    :      Union[str, Any]					 =		pipe(
         **self.get_dummy_inputs(UpperCAmelCase	)			,     return_dict=UpperCAmelCase			,     )[0]

     lowerCamelCase__    :      List[Any]					 =		image[0, -3:, -3:, -1]
     lowerCamelCase__    :      Dict					 =		image_from_tuple[0, -3:, -3:, -1]

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

     lowerCamelCase__    :      Dict					 =		np.array(
         [0.6_9_5_9_8_2_6, 0.8_6_8_2_7_9, 0.7_5_5_8_0_9_2, 0.6_8_7_6_9_4_6_7, 0.8_5_8_0_5_8_0_4, 0.6_5_9_7_7_4_9_6, 0.4_4_8_8_5_3_0_2, 0.5_9_5_9_1_1_1, 0.4_2_5_1_5_9_5]	)

     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			):
 def      A_	(      self  :      Optional[int]	)			->	Optional[int]:
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()







 def      A_	(      self  :      Optional[int]	)			->	Optional[Any]:
     lowerCamelCase__    :      int					 =		load_numpy(
         'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
         '/kandinskyv22/kandinskyv22_controlnet_robotcat_fp16.npy'	)

     lowerCamelCase__    :      Tuple					 =		load_image(
         'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
         '/kandinskyv22/hint_image_cat.png'	)
     lowerCamelCase__    :      str					 =		torch.from_numpy(np.array(UpperCAmelCase	)	).float() / 2_5_5.0
     lowerCamelCase__    :      Optional[Any]					 =		hint.permute(2			,     0			,     1	).unsqueeze(0	)

     lowerCamelCase__    :      Optional[Any]					 =		KandinskyVaaPriorPipeline.from_pretrained(
         'kandinsky-community/kandinsky-2-2-prior'			,     torch_dtype=torch.floataa	)
     pipe_prior.to(UpperCAmelCase	)

     lowerCamelCase__    :      Union[str, Any]					 =		KandinskyVaaControlnetPipeline.from_pretrained(
         'kandinsky-community/kandinsky-2-2-controlnet-depth'			,     torch_dtype=torch.floataa	)
     lowerCamelCase__    :      str					 =		pipeline.to(UpperCAmelCase	)
     pipeline.set_progress_bar_config(disable=UpperCAmelCase	)

     lowerCamelCase__    :      Union[str, Any]					 =		'A robot, 4k photo'

     lowerCamelCase__    :      int					 =		torch.Generator(device='cuda'	).manual_seed(0	)
     lowerCamelCase__ ,							lowerCamelCase__    :      Tuple					 =		pipe_prior(
         UpperCAmelCase			,     generator=UpperCAmelCase			,     num_inference_steps=5			,     negative_prompt=''			,     ).to_tuple()

     lowerCamelCase__    :      Union[str, Any]					 =		torch.Generator(device='cuda'	).manual_seed(0	)
     lowerCamelCase__    :      List[Any]					 =		pipeline(
         image_embeds=UpperCAmelCase			,     negative_image_embeds=UpperCAmelCase			,     hint=UpperCAmelCase			,     generator=UpperCAmelCase			,     num_inference_steps=100			,     output_type='np'			,     )

     lowerCamelCase__    :      List[str]					 =		output.images[0]

     assert image.shape == (512, 512, 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 numpy as np
import pytest

from datasets.arrow_dataset import Dataset
from datasets.search import ElasticSearchIndex, FaissIndex, MissingIndex

from .utils import require_elasticsearch, require_faiss


_UpperCAmelCase				:       str	   =				pytest.mark.integration

@require_faiss
class    lowerCAmelCase							(					__UpperCamelCase			):
 def      A_	(      self  :      List[Any]	)			->	Union[str, Any]:
     lowerCamelCase__    :      List[Any]					 =		Dataset.from_dict({'filename': ['my_name-train' + '_' + str(UpperCAmelCase	) for x in np.arange(30	).tolist()]}	)
     return dset
 def      A_	(      self  :      Optional[Any]	)			->	Optional[int]:
     import faiss

     lowerCamelCase__    :      Dataset					 =		self._create_dummy_dataset()
     lowerCamelCase__    :      List[Any]					 =		dset.map(
         lambda UpperCAmelCase			,     UpperCAmelCase	: {"vecs": i * np.ones(5			,     dtype=np.floataa	)}			,     with_indices=UpperCAmelCase			,     keep_in_memory=UpperCAmelCase	)
     lowerCamelCase__    :      Tuple					 =		dset.add_faiss_index('vecs'			,     batch_size=100			,     metric_type=faiss.METRIC_INNER_PRODUCT	)
     lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		dset.get_nearest_examples('vecs'			,     np.ones(5			,     dtype=np.floataa	)	)
     self.assertEqual(examples['filename'][0]			,     'my_name-train_29'	)
     dset.drop_index('vecs'	)
 def      A_	(      self  :      Union[str, Any]	)			->	int:
     import faiss

     lowerCamelCase__    :      Dataset					 =		self._create_dummy_dataset()
     dset.add_faiss_index_from_external_arrays(
         external_arrays=np.ones((30, 5)	) * np.arange(30	).reshape(-1			,     1	)			,     index_name='vecs'			,     batch_size=100			,     metric_type=faiss.METRIC_INNER_PRODUCT			,     )
     lowerCamelCase__ ,							lowerCamelCase__    :      Optional[Any]					 =		dset.get_nearest_examples('vecs'			,     np.ones(5			,     dtype=np.floataa	)	)
     self.assertEqual(examples['filename'][0]			,     'my_name-train_29'	)
 def      A_	(      self  :      List[str]	)			->	Tuple:
     import faiss

     lowerCamelCase__    :      Dataset					 =		self._create_dummy_dataset()
     dset.add_faiss_index_from_external_arrays(
         external_arrays=np.ones((30, 5)	) * np.arange(30	).reshape(-1			,     1	)			,     index_name='vecs'			,     metric_type=faiss.METRIC_INNER_PRODUCT			,     )

     # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
     # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
     # see https://bugs.python.org/issue14243 and
     # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
     with tempfile.NamedTemporaryFile(delete=UpperCAmelCase	) as tmp_file:
         dset.save_faiss_index('vecs'			,     tmp_file.name	)
         dset.load_faiss_index('vecs2'			,     tmp_file.name	)
     os.unlink(tmp_file.name	)

     lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		dset.get_nearest_examples('vecs2'			,     np.ones(5			,     dtype=np.floataa	)	)
     self.assertEqual(examples['filename'][0]			,     'my_name-train_29'	)
 def      A_	(      self  :      Any	)			->	Optional[Any]:
     lowerCamelCase__    :      Dataset					 =		self._create_dummy_dataset()
     dset.add_faiss_index_from_external_arrays(
         external_arrays=np.ones((30, 5)	) * np.arange(30	).reshape(-1			,     1	)			,     index_name='vecs'	)
     dset.drop_index('vecs'	)
     self.assertRaises(UpperCAmelCase			,     partial(dset.get_nearest_examples			,     'vecs2'			,     np.ones(5			,     dtype=np.floataa	)	)	)







 def      A_	(      self  :      Dict	)			->	Dict:
     from elasticsearch import Elasticsearch

     lowerCamelCase__    :      Dataset					 =		self._create_dummy_dataset()
     with patch('elasticsearch.Elasticsearch.search'	) as mocked_search, patch(
         'elasticsearch.client.IndicesClient.create'	) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk'	) as mocked_bulk:
         lowerCamelCase__    :      List[Any]					 =		{'acknowledged': True}
         mocked_bulk.return_value([(True, None)] * 30	)
         lowerCamelCase__    :      int					 =		{'hits': {'hits': [{'_score': 1, '_id': 29}]}}
         lowerCamelCase__    :      List[str]					 =		Elasticsearch()

         dset.add_elasticsearch_index('filename'			,     es_client=UpperCAmelCase	)
         lowerCamelCase__ ,							lowerCamelCase__    :      Dict					 =		dset.get_nearest_examples('filename'			,     'my_name-train_29'	)
         self.assertEqual(examples['filename'][0]			,     'my_name-train_29'	)


@require_faiss
class    lowerCAmelCase							(					__UpperCamelCase			):
 def      A_	(      self  :      Any	)			->	Dict:
     import faiss

     lowerCamelCase__    :      Tuple					 =		FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT	)

     # add vectors
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsNotNone(index.faiss_index	)
     self.assertEqual(index.faiss_index.ntotal			,     5	)
     index.add_vectors(np.zeros((5, 5)			,     dtype=np.floataa	)	)
     self.assertEqual(index.faiss_index.ntotal			,     10	)

     # single query
     lowerCamelCase__    :      int					 =		np.zeros(5			,     dtype=np.floataa	)
     lowerCamelCase__    :      Any					 =		1
     lowerCamelCase__ ,							lowerCamelCase__    :      Union[str, Any]					 =		index.search(UpperCAmelCase	)
     self.assertRaises(UpperCAmelCase			,     index.search			,     query.reshape(-1			,     1	)	)
     self.assertGreater(scores[0]			,     0	)
     self.assertEqual(indices[0]			,     1	)

     # batched queries
     lowerCamelCase__    :      str					 =		np.eye(5			,     dtype=np.floataa	)[::-1]
     lowerCamelCase__ ,							lowerCamelCase__    :      Dict					 =		index.search_batch(UpperCAmelCase	)
     self.assertRaises(UpperCAmelCase			,     index.search_batch			,     queries[0]	)
     lowerCamelCase__    :      str					 =		[scores[0] for scores in total_scores]
     lowerCamelCase__    :      List[str]					 =		[indices[0] for indices in total_indices]
     self.assertGreater(np.min(UpperCAmelCase	)			,     0	)
     self.assertListEqual([4, 3, 2, 1, 0]			,     UpperCAmelCase	)
 def      A_	(      self  :      List[Any]	)			->	List[Any]:
     import faiss

     lowerCamelCase__    :      Any					 =		FaissIndex(string_factory='Flat'	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsInstance(index.faiss_index			,     faiss.IndexFlat	)
     lowerCamelCase__    :      Tuple					 =		FaissIndex(string_factory='LSH'	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsInstance(index.faiss_index			,     faiss.IndexLSH	)
     with self.assertRaises(UpperCAmelCase	):
         lowerCamelCase__    :      List[str]					 =		FaissIndex(string_factory='Flat'			,     custom_index=faiss.IndexFlat(5	)	)
 def      A_	(      self  :      List[str]	)			->	Optional[int]:
     import faiss

     lowerCamelCase__    :      Optional[Any]					 =		faiss.IndexFlat(5	)
     lowerCamelCase__    :      int					 =		FaissIndex(custom_index=UpperCAmelCase	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)
     self.assertIsInstance(index.faiss_index			,     faiss.IndexFlat	)







 def      A_	(      self  :      Any	)			->	Optional[int]:
     import faiss

     lowerCamelCase__    :      Any					 =		FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT	)
     index.add_vectors(np.eye(5			,     dtype=np.floataa	)	)

     # Setting delete=False and unlinking manually is not pretty... but it is required on Windows to
     # ensure somewhat stable behaviour. If we don't, we get PermissionErrors. This is an age-old issue.
     # see https://bugs.python.org/issue14243 and
     # https://stackoverflow.com/questions/23212435/permission-denied-to-write-to-my-temporary-file/23212515
     with tempfile.NamedTemporaryFile(delete=UpperCAmelCase	) as tmp_file:
         index.save(tmp_file.name	)
         lowerCamelCase__    :      List[Any]					 =		FaissIndex.load(tmp_file.name	)
     os.unlink(tmp_file.name	)

     lowerCamelCase__    :      List[str]					 =		np.zeros(5			,     dtype=np.floataa	)
     lowerCamelCase__    :      Tuple					 =		1
     lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		index.search(UpperCAmelCase	)
     self.assertGreater(scores[0]			,     0	)
     self.assertEqual(indices[0]			,     1	)


@require_faiss
def        SCREAMING_SNAKE_CASE      (       _UpperCAmelCase )			->     Any:
    import faiss

    lowerCamelCase__    :      Optional[Any]					 =		FaissIndex(metric_type=faiss.METRIC_INNER_PRODUCT )
    index.add_vectors(np.eye(5						,	dtype=np.floataa ) )

    lowerCamelCase__    :      Optional[int]					 =		'index.faiss'
    lowerCamelCase__    :      Optional[Any]					 =		F"""mock://{index_name}"""
    index.save(_UpperCAmelCase						,	storage_options=mockfs.storage_options )
    lowerCamelCase__    :      Tuple					 =		FaissIndex.load(_UpperCAmelCase						,	storage_options=mockfs.storage_options )

    lowerCamelCase__    :      Optional[int]					 =		np.zeros(5						,	dtype=np.floataa )
    lowerCamelCase__    :      Dict					 =		1
    lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		index.search(_UpperCAmelCase )
    assert scores[0] > 0
    assert indices[0] == 1


@require_elasticsearch
class    lowerCAmelCase							(					__UpperCamelCase			):
 def      A_	(      self  :      Dict	)			->	List[Any]:
     from elasticsearch import Elasticsearch

     with patch('elasticsearch.Elasticsearch.search'	) as mocked_search, patch(
         'elasticsearch.client.IndicesClient.create'	) as mocked_index_create, patch('elasticsearch.helpers.streaming_bulk'	) as mocked_bulk:
         lowerCamelCase__    :      Any					 =		Elasticsearch()
         lowerCamelCase__    :      Tuple					 =		{'acknowledged': True}
         lowerCamelCase__    :      Tuple					 =		ElasticSearchIndex(es_client=UpperCAmelCase	)
         mocked_bulk.return_value([(True, None)] * 3	)
         index.add_documents(['foo', 'bar', 'foobar']	)

         # single query
         lowerCamelCase__    :      Optional[int]					 =		'foo'
         lowerCamelCase__    :      str					 =		{'hits': {'hits': [{'_score': 1, '_id': 0}]}}
         lowerCamelCase__ ,							lowerCamelCase__    :      Optional[Any]					 =		index.search(UpperCAmelCase	)
         self.assertEqual(scores[0]			,     1	)
         self.assertEqual(indices[0]			,     0	)

         # single query with timeout
         lowerCamelCase__    :      Any					 =		'foo'
         lowerCamelCase__    :      List[str]					 =		{'hits': {'hits': [{'_score': 1, '_id': 0}]}}
         lowerCamelCase__ ,							lowerCamelCase__    :      Tuple					 =		index.search(UpperCAmelCase			,     request_timeout=30	)
         self.assertEqual(scores[0]			,     1	)
         self.assertEqual(indices[0]			,     0	)

         # batched queries
         lowerCamelCase__    :      List[str]					 =		['foo', 'bar', 'foobar']
         lowerCamelCase__    :      str					 =		{'hits': {'hits': [{'_score': 1, '_id': 1}]}}
         lowerCamelCase__ ,							lowerCamelCase__    :      str					 =		index.search_batch(UpperCAmelCase	)
         lowerCamelCase__    :      List[str]					 =		[scores[0] for scores in total_scores]
         lowerCamelCase__    :      List[Any]					 =		[indices[0] for indices in total_indices]
         self.assertGreater(np.min(UpperCAmelCase	)			,     0	)
         self.assertListEqual([1, 1, 1]			,     UpperCAmelCase	)

         # batched queries with timeout
         lowerCamelCase__    :      str					 =		['foo', 'bar', 'foobar']
         lowerCamelCase__    :      List[Any]					 =		{'hits': {'hits': [{'_score': 1, '_id': 1}]}}
         lowerCamelCase__ ,							lowerCamelCase__    :      Optional[Any]					 =		index.search_batch(UpperCAmelCase			,     request_timeout=30	)
         lowerCamelCase__    :      Optional[Any]					 =		[scores[0] for scores in total_scores]
         lowerCamelCase__    :      Dict					 =		[indices[0] for indices in total_indices]
         self.assertGreater(np.min(UpperCAmelCase	)			,     0	)
         self.assertListEqual([1, 1, 1]			,     UpperCAmelCase	)





