




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


       def    lowerCAmelCase   ( self	)   ->	Dict:



            '''simple docstring'''




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


       def    lowerCAmelCase   ( self	)   ->	str:



            '''simple docstring'''




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

            sd_pipe.set_scheduler('''sample_euler'''	)

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

            __snake_case            =	output.images

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

            assert image.shape == (1, 512, 512, 3)
            __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    lowerCAmelCase   ( self	)   ->	str:



            '''simple docstring'''




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

            sd_pipe.set_scheduler('''sample_euler'''	)

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

            __snake_case            =	output.images

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

            assert image.shape == (1, 512, 512, 3)
            __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    lowerCAmelCase   ( self	)   ->	str:



            '''simple docstring'''




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

            sd_pipe.set_scheduler('''sample_dpmpp_2m'''	)

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

            __snake_case            =	output.images

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

            assert image.shape == (1, 512, 512, 3)
            __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'''



def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     __snake_case            =	abs(_lowerCamelCase						)
     __snake_case            =	0
     while n > 0:
          res += n % 10
          n //= 10
     return res







def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     __snake_case            =	abs(_lowerCamelCase						)
     return n if n < 10 else n % 10 + sum_of_digits(n // 10						)







def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     return sum(int(_lowerCamelCase						) for c in str(abs(_lowerCamelCase						)						)						)







def       _UpperCamelCase			()-> None:




     '''simple docstring'''





     from collections.abc import Callable
     from timeit import timeit

     def benchmark_a_function(_lowerCamelCase				:  Callable ,						_lowerCamelCase				:  int						) -> None:
          __snake_case            =	f'''{func.__name__}({value})'''
          __snake_case            =	timeit(f'''__main__.{call}''' ,						setup='''import __main__'''						)
          print(f'''{call:56} = {func(_lowerCamelCase						)} -- {timing:.4f} seconds'''						)

     for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76):
          for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact):
               benchmark_a_function(_lowerCamelCase ,						_lowerCamelCase						)
          print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()






