
import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
				import sqlitea

				import sqlalchemy


__lowerCamelCase								=					datasets.utils.logging.get_logger(__name__)
@dataclass
class    _UpperCamelCase(   datasets.BuilderConfig				):
						__A:						Union[str, "sqlalchemy.sql.Selectable"]				   =      None
						__A:						Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]				   =      None
						__A:						Optional[Union[str, List[str]]]				   =      None
						__A:						bool				   =      True
						__A:						Optional[Union[List, Tuple, Dict]]				   =      None
						__A:						Optional[Union[List, Dict]]				   =      None
						__A:						Optional[List[str]]				   =      None
						__A:						Optional[int]				   =      1_00_00
						__A:						Optional[datasets.Features]				   =      None

						def 						a__ (					self		:			Optional[int]			):
													if self.sql is None:
																				raise ValueError("sql must be specified"			)
													if self.con is None:
																				raise ValueError("con must be specified"			)

						def 						a__ (					self		:			int	,						_lowerCamelCase		:			dict	,						_lowerCamelCase		:			Optional[datasets.Features] = None	,						):
													_UpperCAmelCase  :			Dict	       =		config_kwargs.copy()
													# We need to stringify the Selectable object to make its hash deterministic

													# The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
													_UpperCAmelCase  :			List[str]	       =		config_kwargs["sql"]
													if not isinstance(_lowerCamelCase	,						_lowerCamelCase			):
																				if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																											import sqlalchemy

																											if isinstance(_lowerCamelCase	,						sqlalchemy.sql.Selectable			):
																																		_UpperCAmelCase  :			int	       =		sqlalchemy.create_engine(config_kwargs["con"].split("://"			)[0] + "://"			)
																																		_UpperCAmelCase  :			Any	       =		str(sql.compile(dialect=engine.dialect			)			)
																																		_UpperCAmelCase  :			List[str]	       =		sql_str
																											else:
																																		raise TypeError(
																																		    f"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(_lowerCamelCase			)}: {sql}"""			)
																				else:
																											raise TypeError(
																											    f"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(_lowerCamelCase			)}: {sql}"""			)
													_UpperCAmelCase  :			Any	       =		config_kwargs["con"]
													if not isinstance(_lowerCamelCase	,						_lowerCamelCase			):
																				_UpperCAmelCase  :			int	       =		id(_lowerCamelCase			)
																				logger.info(
																				    f"""SQL connection 'con' of type {type(_lowerCamelCase			)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."""			)

													return super().create_config_id(_lowerCamelCase	,						custom_features=_lowerCamelCase			)






						@property
						def 						a__ (					self		:			Optional[Any]			):
													_UpperCAmelCase  :			Optional[Any]	       =		{
													    "index_col": self.index_col,
													    "columns": self.columns,
													    "params": self.params,
													    "coerce_float": self.coerce_float,
													    "parse_dates": self.parse_dates,
													}
													return pd_read_sql_kwargs







class    _UpperCamelCase(   datasets.ArrowBasedBuilder				):
						__A:						int				   =      SqlConfig

						def 						a__ (					self		:			Optional[int]			):
													return datasets.DatasetInfo(features=self.config.features			)

						def 						a__ (					self		:			Tuple	,						_lowerCamelCase		:			Dict			):
													return [datasets.SplitGenerator(name=datasets.Split.TRAIN	,						gen_kwargs={}			)]

						def 						a__ (					self		:			Tuple	,						_lowerCamelCase		:			pa.Table			):
													if self.config.features is not None:
																				_UpperCAmelCase  :			Any	       =		self.config.features.arrow_schema
																				if all(not require_storage_cast(_lowerCamelCase			) for feature in self.config.features.values()			):
																											# cheaper cast
																											_UpperCAmelCase  :			Optional[int]	       =		pa.Table.from_arrays([pa_table[field.name] for field in schema]	,						schema=_lowerCamelCase			)
																				else:
																											# more expensive cast; allows str <-> int/float or str to Audio for example
																											_UpperCAmelCase  :			Optional[Any]	       =		table_cast(_lowerCamelCase	,						_lowerCamelCase			)
													return pa_table






						def 						a__ (					self		:			Optional[Any]			):
													_UpperCAmelCase  :			List[str]	       =		self.config.chunksize
													_UpperCAmelCase  :			Tuple	       =		pd.read_sql(
													    self.config.sql	,						self.config.con	,						chunksize=_lowerCamelCase	,						**self.config.pd_read_sql_kwargs			)
													_UpperCAmelCase  :			int	       =		[sql_reader] if chunksize is None else sql_reader
													for chunk_idx, df in enumerate(_lowerCamelCase			):
																				_UpperCAmelCase  :			List[str]	       =		pa.Table.from_pandas(_lowerCamelCase			)
																				yield chunk_idx, self._cast_table(_lowerCamelCase			)






import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
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    _UpperCamelCase(   SCREAMING_SNAKE_CASE		,						unittest.TestCase				):
						__A:						Dict				   =      KandinskyPipeline
						__A:						List[Any]				   =      [
						    """prompt""",
						    """image_embeds""",
						    """negative_image_embeds""",
						]
						__A:						Dict				   =      ["""prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds"""]
						__A:						List[str]				   =      [
						    """generator""",
						    """height""",
						    """width""",
						    """latents""",
						    """guidance_scale""",
						    """negative_prompt""",
						    """num_inference_steps""",
						    """return_dict""",
						    """guidance_scale""",
						    """num_images_per_prompt""",
						    """output_type""",
						    """return_dict""",
						]
						__A:						List[str]				   =      False

						@property
						def 						a__ (					self		:			List[Any]			):
													return 32

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

						@property
						def 						a__ (					self		:			List[Any]			):
													return self.time_input_dim

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

						@property
						def 						a__ (					self		:			Dict			):
													return 1_00

						@property
						def 						a__ (					self		:			Tuple			):
													_UpperCAmelCase  :			Optional[int]	       =		XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base"			)
													return tokenizer

						@property
						def 						a__ (					self		:			Tuple			):
													torch.manual_seed(0			)
													_UpperCAmelCase  :			int	       =		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=10_05	,						)

													_UpperCAmelCase  :			Optional[Any]	       =		MultilingualCLIP(_lowerCamelCase			)
													_UpperCAmelCase  :			str	       =		text_encoder.eval()

													return text_encoder

						@property
						def 						a__ (					self		:			Any			):
													torch.manual_seed(0			)

													_UpperCAmelCase  :			Any	       =		{
													    "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,
													}

													_UpperCAmelCase  :			Optional[Any]	       =		UNetaDConditionModel(**_lowerCamelCase			)
													return model

						@property
						def 						a__ (					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 						a__ (					self		:			Tuple			):
													torch.manual_seed(0			)
													_UpperCAmelCase  :			Dict	       =		VQModel(**self.dummy_movq_kwargs			)
													return model

						def 						a__ (					self		:			List[Any]			):
													_UpperCAmelCase  :			Union[str, Any]	       =		self.dummy_text_encoder
													_UpperCAmelCase  :			Any	       =		self.dummy_tokenizer
													_UpperCAmelCase  :			List[Any]	       =		self.dummy_unet
													_UpperCAmelCase  :			Union[str, Any]	       =		self.dummy_movq

													_UpperCAmelCase  :			str	       =		DDIMScheduler(
													    num_train_timesteps=10_00	,						beta_schedule="linear"	,						beta_start=0.0_00_85	,						beta_end=0.0_12	,						clip_sample=_lowerCamelCase	,						set_alpha_to_one=_lowerCamelCase	,						steps_offset=1	,						prediction_type="epsilon"	,						thresholding=_lowerCamelCase	,						)

													_UpperCAmelCase  :			Optional[Any]	       =		{
													    "text_encoder": text_encoder,
													    "tokenizer": tokenizer,
													    "unet": unet,
													    "scheduler": scheduler,
													    "movq": movq,
													}
													return components

						def 						a__ (					self		:			int	,						_lowerCamelCase		:			Any	,						_lowerCamelCase		:			List[str]=0			):
													_UpperCAmelCase  :			Tuple	       =		floats_tensor((1, self.cross_attention_dim)	,						rng=random.Random(_lowerCamelCase			)			).to(_lowerCamelCase			)
													_UpperCAmelCase  :			Tuple	       =		floats_tensor((1, self.cross_attention_dim)	,						rng=random.Random(seed + 1			)			).to(_lowerCamelCase			)
													if str(_lowerCamelCase			).startswith("mps"			):
																				_UpperCAmelCase  :			Union[str, Any]	       =		torch.manual_seed(_lowerCamelCase			)
													else:
																				_UpperCAmelCase  :			Optional[Any]	       =		torch.Generator(device=_lowerCamelCase			).manual_seed(_lowerCamelCase			)
													_UpperCAmelCase  :			int	       =		{
													    "prompt": "horse",
													    "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 						a__ (					self		:			List[Any]			):
													_UpperCAmelCase  :			Optional[Any]	       =		"cpu"

													_UpperCAmelCase  :			Dict	       =		self.get_dummy_components()

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

													pipe.set_progress_bar_config(disable=_lowerCamelCase			)

													_UpperCAmelCase  :			Dict	       =		pipe(**self.get_dummy_inputs(_lowerCamelCase			)			)
													_UpperCAmelCase  :			int	       =		output.images

													_UpperCAmelCase  :			Tuple	       =		pipe(
													    **self.get_dummy_inputs(_lowerCamelCase			)	,						return_dict=_lowerCamelCase	,						)[0]

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

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

													_UpperCAmelCase  :			Dict	       =		np.array(
													    [0.32_86_63, 1.0, 0.23_21_68_73, 1.0, 0.92_71_75_64, 0.4_63_90_46, 0.96_89_47_77, 0.31_71_33_78, 0.6_29_39_53]			)

													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    _UpperCamelCase(   unittest.TestCase				):

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






						def 						a__ (					self		:			str			):
													_UpperCAmelCase  :			List[str]	       =		load_numpy(
													    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
													    "/kandinsky/kandinsky_text2img_cat_fp16.npy"			)

													_UpperCAmelCase  :			Dict	       =		KandinskyPriorPipeline.from_pretrained(
													    "kandinsky-community/kandinsky-2-1-prior"	,						torch_dtype=torch.floataa			)
													pipe_prior.to(_lowerCamelCase			)

													_UpperCAmelCase  :			str	       =		KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1"	,						torch_dtype=torch.floataa			)
													_UpperCAmelCase  :			Union[str, Any]	       =		pipeline.to(_lowerCamelCase			)
													pipeline.set_progress_bar_config(disable=_lowerCamelCase			)

													_UpperCAmelCase  :			Tuple	       =		"red cat, 4k photo"

													_UpperCAmelCase  :			List[Any]	       =		torch.Generator(device="cuda"			).manual_seed(0			)
													_UpperCAmelCase       ,_UpperCAmelCase  :			int	       =		pipe_prior(
													    _lowerCamelCase	,						generator=_lowerCamelCase	,						num_inference_steps=5	,						negative_prompt=""	,						).to_tuple()

													_UpperCAmelCase  :			Tuple	       =		torch.Generator(device="cuda"			).manual_seed(0			)
													_UpperCAmelCase  :			Dict	       =		pipeline(
													    _lowerCamelCase	,						image_embeds=_lowerCamelCase	,						negative_image_embeds=_lowerCamelCase	,						generator=_lowerCamelCase	,						num_inference_steps=1_00	,						output_type="np"	,						)

													_UpperCAmelCase  :			Tuple	       =		output.images[0]

													assert image.shape == (5_12, 5_12, 3)

													assert_mean_pixel_difference(_lowerCamelCase	,						_lowerCamelCase			)




