


'''simple docstring'''

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


a_						:       Dict      			=				False





class   snake_case					(					unittest.TestCase    ):





  """simple docstring"""




  pass





@nightly
@require_torch_gpu
class   snake_case					(					unittest.TestCase    ):





  """simple docstring"""




  def 						snake_case	(					self  ):







   """simple docstring"""
   super().tearDown()
   gc.collect()
   torch.cuda.empty_cache()






  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"    ,						torch_dtype=torch.floataa  )
   pipe.to(lowercase__  )
   pipe.set_progress_bar_config(disable=lowercase__  )

   lowerCamelCase_  				=						load_image(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"  )

   lowerCamelCase_  				=						torch.manual_seed(0  )
   lowerCamelCase_  				=						pipe.dual_guided(
       prompt="first prompt"    ,						image=lowercase__    ,						text_to_image_strength=0.75    ,						generator=lowercase__    ,						guidance_scale=7.5    ,						num_inference_steps=2    ,						output_type="numpy"    ,						).images

   with tempfile.TemporaryDirectory() as tmpdirname:
    pipe.save_pretrained(lowercase__  )
    lowerCamelCase_  				=						VersatileDiffusionPipeline.from_pretrained(lowercase__    ,						torch_dtype=torch.floataa  )
   pipe.to(lowercase__  )
   pipe.set_progress_bar_config(disable=lowercase__  )

   lowerCamelCase_  				=						generator.manual_seed(0  )
   lowerCamelCase_  				=						pipe.dual_guided(
       prompt="first prompt"    ,						image=lowercase__    ,						text_to_image_strength=0.75    ,						generator=lowercase__    ,						guidance_scale=7.5    ,						num_inference_steps=2    ,						output_type="numpy"    ,						).images

   assert np.abs(image - new_image  ).sum() < 1e-5, "Models don't have the same forward pass"







  def 						snake_case	(					self  ):







   """simple docstring"""
   lowerCamelCase_  				=						VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"    ,						torch_dtype=torch.floataa  )
   pipe.to(lowercase__  )
   pipe.set_progress_bar_config(disable=lowercase__  )

   lowerCamelCase_  				=						"cyberpunk 2077"
   lowerCamelCase_  				=						load_image(
       "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"  )
   lowerCamelCase_  				=						torch.manual_seed(0  )
   lowerCamelCase_  				=						pipe.dual_guided(
       prompt=lowercase__    ,						image=lowercase__    ,						text_to_image_strength=0.75    ,						generator=lowercase__    ,						guidance_scale=7.5    ,						num_inference_steps=50    ,						output_type="numpy"    ,						).images

   lowerCamelCase_  				=						image[0, 253:256, 253:256, -1]

   assert image.shape == (1, 512, 512, 3)
   lowerCamelCase_  				=						np.array([0.1_448, 0.1_619, 0.1_741, 0.1_086, 0.1_147, 0.1_128, 0.1_199, 0.1_165, 0.1_001]  )

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

   lowerCamelCase_  				=						"A painting of a squirrel eating a burger "
   lowerCamelCase_  				=						torch.manual_seed(0  )
   lowerCamelCase_  				=						pipe.text_to_image(
       prompt=lowercase__    ,						generator=lowercase__    ,						guidance_scale=7.5    ,						num_inference_steps=50    ,						output_type="numpy"  ).images

   lowerCamelCase_  				=						image[0, 253:256, 253:256, -1]

   assert image.shape == (1, 512, 512, 3)
   lowerCamelCase_  				=						np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778]  )

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

   lowerCamelCase_  				=						pipe.image_variation(lowercase__    ,						generator=lowercase__    ,						output_type="numpy"  ).images

   lowerCamelCase_  				=						image[0, 253:256, 253:256, -1]

   assert image.shape == (1, 512, 512, 3)
   lowerCamelCase_  				=						np.array([0.3_076, 0.3_123, 0.3_284, 0.3_782, 0.3_770, 0.3_894, 0.4_297, 0.4_331, 0.4_456]  )

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







'''simple docstring'''

import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy





def __snake_case      (   UpperCAmelCase_		:		Optional[int]      ,       UpperCAmelCase_		:		Dict	):
 assert isinstance(UpperCAmelCase_      ,       UpperCAmelCase_	)
 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





@require_sqlalchemy
@pytest.mark.parametrize("keep_in_memory"      ,       [False, True]	)
def __snake_case      (   UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		List[str]      ,       UpperCAmelCase_		:		Union[str, Any]	):
 lowerCamelCase_  				=						tmp_path / "cache"
 lowerCamelCase_  				=						{"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_  				=						SqlDatasetReader(
      "dataset"      ,       "sqlite:///" + sqlite_path      ,       cache_dir=UpperCAmelCase_      ,       keep_in_memory=UpperCAmelCase_	).read()
 _check_sql_dataset(UpperCAmelCase_      ,       UpperCAmelCase_	)





@require_sqlalchemy
@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 __snake_case      (   UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		Optional[Any]      ,       UpperCAmelCase_		:		List[str]      ,       UpperCAmelCase_		:		str	):
 lowerCamelCase_  				=						tmp_path / "cache"
 lowerCamelCase_  				=						{"col_1": "string", "col_2": "int64", "col_3": "float64"}
 lowerCamelCase_  				=						features.copy() if features else default_expected_features
 lowerCamelCase_  				=						(
     Features({feature: Value(UpperCAmelCase_	) for feature, dtype in features.items()}	) if features is not None else None
 )
 lowerCamelCase_  				=						SqlDatasetReader("dataset"      ,       "sqlite:///" + sqlite_path      ,       features=UpperCAmelCase_      ,       cache_dir=UpperCAmelCase_	).read()
 _check_sql_dataset(UpperCAmelCase_      ,       UpperCAmelCase_	)





def __snake_case      (   UpperCAmelCase_		:		Union[str, Any]	):
 with contextlib.closing(sqlitea.connect(UpperCAmelCase_	)	) as con:
  lowerCamelCase_  				=						con.cursor()
  cur.execute("SELECT * FROM dataset"	)
  for row in cur:
   yield row





@require_sqlalchemy
def __snake_case      (   UpperCAmelCase_		:		Dict      ,       UpperCAmelCase_		:		Optional[Any]      ,       UpperCAmelCase_		:		List[str]	):
 lowerCamelCase_  				=						tmp_path / "cache"
 lowerCamelCase_  				=						os.path.join(UpperCAmelCase_      ,       "tmp.sql"	)
 lowerCamelCase_  				=						SqlDatasetReader("dataset"      ,       "sqlite:///" + sqlite_path      ,       cache_dir=UpperCAmelCase_	).read()
 SqlDatasetWriter(UpperCAmelCase_      ,       "dataset"      ,       "sqlite:///" + output_sqlite_path      ,       num_proc=1	).write()

 lowerCamelCase_  				=						iter_sql_file(UpperCAmelCase_	)
 lowerCamelCase_  				=						iter_sql_file(UpperCAmelCase_	)

 for rowa, rowa in zip(UpperCAmelCase_      ,       UpperCAmelCase_	):
  assert rowa == rowa





@require_sqlalchemy
def __snake_case      (   UpperCAmelCase_		:		Union[str, Any]      ,       UpperCAmelCase_		:		Union[str, Any]      ,       UpperCAmelCase_		:		Tuple	):
 lowerCamelCase_  				=						tmp_path / "cache"
 lowerCamelCase_  				=						os.path.join(UpperCAmelCase_      ,       "tmp.sql"	)
 lowerCamelCase_  				=						SqlDatasetReader("dataset"      ,       "sqlite:///" + sqlite_path      ,       cache_dir=UpperCAmelCase_	).read()
 SqlDatasetWriter(UpperCAmelCase_      ,       "dataset"      ,       "sqlite:///" + output_sqlite_path      ,       num_proc=2	).write()

 lowerCamelCase_  				=						iter_sql_file(UpperCAmelCase_	)
 lowerCamelCase_  				=						iter_sql_file(UpperCAmelCase_	)

 for rowa, rowa in zip(UpperCAmelCase_      ,       UpperCAmelCase_	):
  assert rowa == rowa







@require_sqlalchemy
def __snake_case      (   UpperCAmelCase_		:		Any      ,       UpperCAmelCase_		:		Tuple      ,       UpperCAmelCase_		:		Tuple	):
 lowerCamelCase_  				=						tmp_path / "cache"
 lowerCamelCase_  				=						os.path.join(UpperCAmelCase_      ,       "tmp.sql"	)
 lowerCamelCase_  				=						SqlDatasetReader("dataset"      ,       "sqlite:///" + sqlite_path      ,       cache_dir=UpperCAmelCase_	).read()
 with pytest.raises(UpperCAmelCase_	):
  SqlDatasetWriter(UpperCAmelCase_      ,       "dataset"      ,       "sqlite:///" + output_sqlite_path      ,       num_proc=0	).write()



