





'''simple docstring'''

import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







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





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""    )
      __SCREAMING_SNAKE_CASE      							=       sd_pipe.to(lowerCamelCase    )
      sd_pipe.set_progress_bar_config(disable=lowerCamelCase    )

      sd_pipe.set_scheduler("""sample_euler"""    )

      __SCREAMING_SNAKE_CASE      							=       """A painting of a squirrel eating a burger"""
      __SCREAMING_SNAKE_CASE      							=       torch.manual_seed(0    )
      __SCREAMING_SNAKE_CASE      							=       sd_pipe([prompt]				,generator=lowerCamelCase				,guidance_scale=9.0				,num_inference_steps=20				,output_type="""np"""    )

      __SCREAMING_SNAKE_CASE      							=       output.images

      __SCREAMING_SNAKE_CASE      							=       image[0, -3:, -3:, -1]

      assert image.shape == (1, 512, 512, 3)
      __SCREAMING_SNAKE_CASE      							=       np.array([0.0_447, 0.0_492, 0.0_468, 0.0_408, 0.0_383, 0.0_408, 0.0_354, 0.0_380, 0.0_339]    )

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





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
      __SCREAMING_SNAKE_CASE      							=       sd_pipe.to(lowerCamelCase    )
      sd_pipe.set_progress_bar_config(disable=lowerCamelCase    )

      sd_pipe.set_scheduler("""sample_euler"""    )

      __SCREAMING_SNAKE_CASE      							=       """A painting of a squirrel eating a burger"""
      __SCREAMING_SNAKE_CASE      							=       torch.manual_seed(0    )
      __SCREAMING_SNAKE_CASE      							=       sd_pipe([prompt]				,generator=lowerCamelCase				,guidance_scale=9.0				,num_inference_steps=20				,output_type="""np"""    )

      __SCREAMING_SNAKE_CASE      							=       output.images

      __SCREAMING_SNAKE_CASE      							=       image[0, -3:, -3:, -1]

      assert image.shape == (1, 512, 512, 3)
      __SCREAMING_SNAKE_CASE      							=       np.array([0.1_237, 0.1_320, 0.1_438, 0.1_359, 0.1_390, 0.1_132, 0.1_277, 0.1_175, 0.1_112]    )

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





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
      __SCREAMING_SNAKE_CASE      							=       sd_pipe.to(lowerCamelCase    )
      sd_pipe.set_progress_bar_config(disable=lowerCamelCase    )

      sd_pipe.set_scheduler("""sample_dpmpp_2m"""    )

      __SCREAMING_SNAKE_CASE      							=       """A painting of a squirrel eating a burger"""
      __SCREAMING_SNAKE_CASE      							=       torch.manual_seed(0    )
      __SCREAMING_SNAKE_CASE      							=       sd_pipe(
          [prompt]				,generator=lowerCamelCase				,guidance_scale=7.5				,num_inference_steps=15				,output_type="""np"""				,use_karras_sigmas=lowerCamelCase				,)

      __SCREAMING_SNAKE_CASE      							=       output.images

      __SCREAMING_SNAKE_CASE      							=       image[0, -3:, -3:, -1]

      assert image.shape == (1, 512, 512, 3)
      __SCREAMING_SNAKE_CASE      							=       np.array(
          [0.11_381_689, 0.12_112_921, 0.1_389_457, 0.12_549_606, 0.1_244_964, 0.10_831_517, 0.11_562_866, 0.10_867_816, 0.10_499_048]    )

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












'''simple docstring'''

import timeit

import numpy as np

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features.features import _ArrayXD

def 						__magic_name__       (							__UpperCAmelCase      )						->					Tuple:

   '''simple docstring'''







   def wrapper(*__UpperCAmelCase							,	**__UpperCAmelCase      ):
      __SCREAMING_SNAKE_CASE      							=       timeit.default_timer()
      __SCREAMING_SNAKE_CASE      							=       func(*__UpperCAmelCase							,	**__UpperCAmelCase      )
      __SCREAMING_SNAKE_CASE      							=       timeit.default_timer() - starttime
      return delta

   __SCREAMING_SNAKE_CASE      							=       func.__name__

   return wrapper

def 						__magic_name__       (							__UpperCAmelCase							,	__UpperCAmelCase=100							,	__UpperCAmelCase=None      )						->					Optional[Any]:

   '''simple docstring'''







   __SCREAMING_SNAKE_CASE      							=       []
   __SCREAMING_SNAKE_CASE      							=       seq_shapes or {}
   for i in range(__UpperCAmelCase      ):
      __SCREAMING_SNAKE_CASE      							=       {}
      for col_id, (k, v) in enumerate(features.items()      ):
         if isinstance(__UpperCAmelCase							,	_ArrayXD      ):
            __SCREAMING_SNAKE_CASE      							=       np.random.rand(*v.shape      ).astype(v.dtype      )
         elif isinstance(__UpperCAmelCase							,	datasets.Value      ):
            if v.dtype == "string":
               __SCREAMING_SNAKE_CASE      							=       """The small grey turtle was surprisingly fast when challenged."""
            else:
               __SCREAMING_SNAKE_CASE      							=       np.random.randint(10							,	size=1      ).astype(v.dtype      ).item()
         elif isinstance(__UpperCAmelCase							,	datasets.Sequence      ):
            while isinstance(__UpperCAmelCase							,	datasets.Sequence      ):
               __SCREAMING_SNAKE_CASE      							=       v.feature
            __SCREAMING_SNAKE_CASE      							=       seq_shapes[k]
            __SCREAMING_SNAKE_CASE      							=       np.random.rand(*__UpperCAmelCase      ).astype(v.dtype      )
         __SCREAMING_SNAKE_CASE      							=       data

      dummy_data.append((i, example)      )

   return dummy_data







def 						__magic_name__       (							__UpperCAmelCase							,	__UpperCAmelCase							,	__UpperCAmelCase=100							,	__UpperCAmelCase=None      )						->					str:

   '''simple docstring'''







   __SCREAMING_SNAKE_CASE      							=       generate_examples(__UpperCAmelCase							,	num_examples=__UpperCAmelCase							,	seq_shapes=__UpperCAmelCase      )

   with ArrowWriter(features=__UpperCAmelCase							,	path=__UpperCAmelCase      ) as writer:
      for key, record in dummy_data:
         __SCREAMING_SNAKE_CASE      							=       features.encode_example(__UpperCAmelCase      )
         writer.write(__UpperCAmelCase      )

      __SCREAMING_SNAKE_CASE							,     __SCREAMING_SNAKE_CASE      							=       writer.finalize()

   if not num_final_examples == num_examples:
      raise ValueError(
          f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}."""      )

   __SCREAMING_SNAKE_CASE      							=       datasets.Dataset.from_file(filename=__UpperCAmelCase							,	info=datasets.DatasetInfo(features=__UpperCAmelCase      )      )

   return dataset





