



'''simple docstring'''

import gc
import random
import unittest

import numpy as np
import torch

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

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()

class 	__magic_name__						(    _UpperCamelCase			,						unittest.TestCase   ):
							lowerCAmelCase       :    Tuple   				=					KandinskyVaaPipeline
							lowerCAmelCase       :    List[str]   				=					[
							    'image_embeds',
							    'negative_image_embeds',
							]
							lowerCAmelCase       :    Union[str, Any]   				=					['image_embeds', 'negative_image_embeds']
							lowerCAmelCase       :    Tuple   				=					[
							    'generator',
							    'height',
							    'width',
							    'latents',
							    'guidance_scale',
							    'num_inference_steps',
							    'return_dict',
							    'guidance_scale',
							    'num_images_per_prompt',
							    'output_type',
							    'return_dict',
							]
							lowerCAmelCase       :    str   				=					False







							@property
							def 							__lowercase			(							self					:       Tuple		):
									return 32







							@property
							def 							__lowercase			(							self					:       int		):
									return 32







							@property
							def 							__lowercase			(							self					:       int		):
									return self.time_input_dim







							@property
							def 							__lowercase			(							self					:       List[Any]		):
									return self.time_input_dim * 4







							@property
							def 							__lowercase			(							self					:       Union[str, Any]		):
									return 100







							@property
							def 							__lowercase			(							self					:       Dict		):
									torch.manual_seed(0		)

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

									_a     :      List[Any]        =							UNetaDConditionModel(**_UpperCAmelCase		)
									return model







							@property
							def 							__lowercase			(							self					:       Optional[Any]		):
									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 							__lowercase			(							self					:       Dict		):
									torch.manual_seed(0		)
									_a     :      int        =							VQModel(**self.dummy_movq_kwargs		)
									return model







							def 							__lowercase			(							self					:       Optional[Any]		):
									_a     :      int        =							self.dummy_unet
									_a     :      List[Any]        =							self.dummy_movq

									_a     :      Tuple        =							DDIMScheduler(
									    num_train_timesteps=1000	,beta_schedule='linear'	,beta_start=0.0_00_85	,beta_end=0.0_12	,clip_sample=_UpperCAmelCase	,set_alpha_to_one=_UpperCAmelCase	,steps_offset=1	,prediction_type='epsilon'	,thresholding=_UpperCAmelCase	,)

									_a     :      Tuple        =							{
									    'unet': unet,
									    'scheduler': scheduler,
									    'movq': movq,
									}
									return components







							def 							__lowercase			(							self					:       List[str]	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       Dict=0		):
									_a     :      Tuple        =							floats_tensor((1, self.text_embedder_hidden_size)	,rng=random.Random(_UpperCAmelCase		)		).to(_UpperCAmelCase		)
									_a     :      str        =							floats_tensor((1, self.text_embedder_hidden_size)	,rng=random.Random(seed + 1		)		).to(
									    _UpperCAmelCase		)
									if str(_UpperCAmelCase		).startswith('mps'		):
											_a     :      str        =							torch.manual_seed(_UpperCAmelCase		)
									else:
											_a     :      Tuple        =							torch.Generator(device=_UpperCAmelCase		).manual_seed(_UpperCAmelCase		)
									_a     :      Optional[int]        =							{
									    'image_embeds': image_embeds,
									    'negative_image_embeds': negative_image_embeds,
									    'generator': generator,
									    'height': 64,
									    'width': 64,
									    'guidance_scale': 4.0,
									    'num_inference_steps': 2,
									    'output_type': 'np',
									}
									return inputs







							def 							__lowercase			(							self					:       List[str]		):
									_a     :      Optional[Any]        =							'cpu'

									_a     :      str        =							self.get_dummy_components()

									_a     :      List[Any]        =							self.pipeline_class(**_UpperCAmelCase		)
									_a     :      Optional[int]        =							pipe.to(_UpperCAmelCase		)

									pipe.set_progress_bar_config(disable=_UpperCAmelCase		)

									_a     :      Union[str, Any]        =							pipe(**self.get_dummy_inputs(_UpperCAmelCase		)		)
									_a     :      int        =							output.images

									_a     :      str        =							pipe(
									    **self.get_dummy_inputs(_UpperCAmelCase		)	,return_dict=_UpperCAmelCase	,)[0]

									_a     :      str        =							image[0, -3:, -3:, -1]
									_a     :      str        =							image_from_tuple[0, -3:, -3:, -1]

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

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

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

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

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







							def 							__lowercase			(							self					:       Dict		):
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()







							def 							__lowercase			(							self					:       int		):
									_a     :      List[str]        =							load_numpy(
									    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
									    '/kandinskyv22/kandinskyv22_text2img_cat_fp16.npy'		)

									_a     :      Optional[int]        =							KandinskyVaaPriorPipeline.from_pretrained(
									    'kandinsky-community/kandinsky-2-2-prior'	,torch_dtype=torch.floataa		)
									pipe_prior.to(_UpperCAmelCase		)

									_a     :      List[str]        =							KandinskyVaaPipeline.from_pretrained(
									    'kandinsky-community/kandinsky-2-2-decoder'	,torch_dtype=torch.floataa		)
									_a     :      Dict        =							pipeline.to(_UpperCAmelCase		)
									pipeline.set_progress_bar_config(disable=_UpperCAmelCase		)

									_a     :      Tuple        =							'red cat, 4k photo'

									_a     :      List[str]        =							torch.Generator(device='cuda'		).manual_seed(0		)
									_a       ,					_a     :      Optional[Any]        =							pipe_prior(
									    _UpperCAmelCase	,generator=_UpperCAmelCase	,num_inference_steps=5	,negative_prompt=''	,).to_tuple()

									_a     :      List[str]        =							torch.Generator(device='cuda'		).manual_seed(0		)
									_a     :      str        =							pipeline(
									    image_embeds=_UpperCAmelCase	,negative_image_embeds=_UpperCAmelCase	,generator=_UpperCAmelCase	,num_inference_steps=100	,output_type='np'	,)

									_a     :      Optional[int]        =							output.images[0]

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

									assert_mean_pixel_difference(_UpperCAmelCase	,_UpperCAmelCase		)









'''simple docstring'''

import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
							import sqlitea

							import sqlalchemy

class 	__magic_name__						(    _UpperCamelCase   ):







							def __init__(							self					:       Optional[int]	,_UpperCAmelCase					:       Union[str, "sqlalchemy.sql.Selectable"]	,_UpperCAmelCase					:       Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]	,_UpperCAmelCase					:       Optional[Features] = None	,_UpperCAmelCase					:       str = None	,_UpperCAmelCase					:       bool = False	,**_UpperCAmelCase					:       Dict	,):
									super().__init__(features=_UpperCAmelCase	,cache_dir=_UpperCAmelCase	,keep_in_memory=_UpperCAmelCase	,**_UpperCAmelCase		)
									_a     :      Tuple        =							Sql(
									    cache_dir=_UpperCAmelCase	,features=_UpperCAmelCase	,sql=_UpperCAmelCase	,con=_UpperCAmelCase	,**_UpperCAmelCase	,)







							def 							__lowercase			(							self					:       Dict		):
									_a     :      Optional[Any]        =							None
									_a     :      Dict        =							None
									_a     :      Dict        =							None
									_a     :      Optional[int]        =							None

									self.builder.download_and_prepare(
									    download_config=_UpperCAmelCase	,download_mode=_UpperCAmelCase	,verification_mode=_UpperCAmelCase	,base_path=_UpperCAmelCase	,)

									# Build dataset for splits
									_a     :      List[str]        =							self.builder.as_dataset(
									    split='train'	,verification_mode=_UpperCAmelCase	,in_memory=self.keep_in_memory		)
									return dataset

class 	__magic_name__						:







							def __init__(							self					:       Optional[int]	,_UpperCAmelCase					:       Dataset	,_UpperCAmelCase					:       str	,_UpperCAmelCase					:       Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]	,_UpperCAmelCase					:       Optional[int] = None	,_UpperCAmelCase					:       Optional[int] = None	,**_UpperCAmelCase					:       Dict	,):
									if num_proc is not None and num_proc <= 0:
											raise ValueError(F"""num_proc {num_proc} must be an integer > 0."""		)

									_a     :      Dict        =							dataset
									_a     :      List[Any]        =							name
									_a     :      Tuple        =							con
									_a     :      Union[str, Any]        =							batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
									_a     :      List[Any]        =							num_proc
									_a     :      Tuple        =							to_sql_kwargs







							def 							__lowercase			(							self					:       List[Any]		):
									_a     :      Tuple        =							self.to_sql_kwargs.pop('sql'	,_UpperCAmelCase		)
									_a     :      str        =							self.to_sql_kwargs.pop('con'	,_UpperCAmelCase		)
									_a     :      Optional[Any]        =							self.to_sql_kwargs.pop('index'	,_UpperCAmelCase		)

									_a     :      Any        =							self._write(index=_UpperCAmelCase	,**self.to_sql_kwargs		)
									return written







							def 							__lowercase			(							self					:       Optional[int]	,_UpperCAmelCase					:       Dict		):
									_a       ,					_a       ,					_a     :      Any        =							args
									_a     :      Tuple        =							{**to_sql_kwargs, 'if_exists': 'append'} if offset > 0 else to_sql_kwargs
									_a     :      Dict        =							query_table(
									    table=self.dataset.data	,key=slice(_UpperCAmelCase	,offset + self.batch_size		)	,indices=self.dataset._indices	,)
									_a     :      Tuple        =							batch.to_pandas()
									_a     :      Dict        =							df.to_sql(self.name	,self.con	,index=_UpperCAmelCase	,**_UpperCAmelCase		)
									return num_rows or len(_UpperCAmelCase		)







							def 							__lowercase			(							self					:       int	,_UpperCAmelCase					:       Optional[int]	,**_UpperCAmelCase					:       List[Any]		):
									_a     :      Union[str, Any]        =							0

									if self.num_proc is None or self.num_proc == 1:
											for offset in logging.tqdm(
											    range(0	,len(self.dataset		)	,self.batch_size		)	,unit='ba'	,disable=not logging.is_progress_bar_enabled()	,desc='Creating SQL from Arrow format'	,):
													written += self._batch_sql((offset, index, to_sql_kwargs)		)
									else:
											_a       ,					_a     :      List[Any]        =							len(self.dataset		), self.batch_size
											with multiprocessing.Pool(self.num_proc		) as pool:
													for num_rows in logging.tqdm(
													    pool.imap(
													        self._batch_sql	,[(offset, index, to_sql_kwargs) for offset in range(0	,_UpperCAmelCase	,_UpperCAmelCase		)]	,)	,total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size	,unit='ba'	,disable=not logging.is_progress_bar_enabled()	,desc='Creating SQL from Arrow format'	,):
															written += num_rows

									return written




