






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						)			->	Union[str, Any]:




									"""simple docstring"""





									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()
		def      UpperCAmelCase__    (      self						)			->	Optional[int]:




									"""simple docstring"""





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

									sd_pipe.set_scheduler('sample_euler'						)

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

									_UpperCAmelCase					=      output.images

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

									assert image.shape == (1, 512, 512, 3)
									_UpperCAmelCase					=      np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]						)

									assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2
		def      UpperCAmelCase__    (      self						)			->	Optional[int]:




									"""simple docstring"""





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

									sd_pipe.set_scheduler('sample_euler'						)

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

									_UpperCAmelCase					=      output.images

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

									assert image.shape == (1, 512, 512, 3)
									_UpperCAmelCase					=      np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]						)

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






		def      UpperCAmelCase__    (      self						)			->	int:




									"""simple docstring"""





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

									sd_pipe.set_scheduler('sample_dpmpp_2m'						)

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

									_UpperCAmelCase					=      output.images

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

									assert image.shape == (1, 512, 512, 3)
									_UpperCAmelCase					=      np.array(
									    [0.11381689, 0.12112921, 0.1389457, 0.12549606, 0.1244964, 0.10831517, 0.11562866, 0.10867816, 0.10499048]						)

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




from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt

def 	lowerCAmelCase__			(      a__:		Sequence[float]      ,  a__:		int      ,  a__:		int	)  ->		tuple[int | None, int | None, float]:


							'''simple docstring'''





							if not arr:
														return None, None, 0
							if low == high:
														return low, high, arr[low]

							_UpperCAmelCase					=      (low + high) // 2
							_UpperCAmelCase		,	_UpperCAmelCase		,	_UpperCAmelCase					=      max_subarray(a__      ,  a__      ,  a__	)
							_UpperCAmelCase		,	_UpperCAmelCase		,	_UpperCAmelCase					=      max_subarray(a__      ,  mid + 1      ,  a__	)
							_UpperCAmelCase		,	_UpperCAmelCase		,	_UpperCAmelCase					=      max_cross_sum(a__      ,  a__      ,  a__      ,  a__	)
							if left_sum >= right_sum and left_sum >= cross_sum:
														return left_low, left_high, left_sum
							elif right_sum >= left_sum and right_sum >= cross_sum:
														return right_low, right_high, right_sum
							return cross_left, cross_right, cross_sum

def 	lowerCAmelCase__			(      a__:		Sequence[float]      ,  a__:		int      ,  a__:		int      ,  a__:		int	)  ->		tuple[int, int, float]:


							'''simple docstring'''





							_UpperCAmelCase		,	_UpperCAmelCase					=      float('-inf'	), -1
							_UpperCAmelCase		,	_UpperCAmelCase					=      float('-inf'	), -1

							_UpperCAmelCase					=      0
							for i in range(a__      ,  low - 1      ,  -1	):
														summ += arr[i]
														if summ > left_sum:
																					_UpperCAmelCase					=      summ
																					_UpperCAmelCase					=      i

							_UpperCAmelCase					=      0
							for i in range(mid + 1      ,  high + 1	):
														summ += arr[i]
														if summ > right_sum:
																					_UpperCAmelCase					=      summ
																					_UpperCAmelCase					=      i

							return max_left, max_right, (left_sum + right_sum)

def 	lowerCAmelCase__			(      a__:		int	)  ->		float:


							'''simple docstring'''





							_UpperCAmelCase					=      [randint(1      ,  a__	) for _ in range(a__	)]
							_UpperCAmelCase					=      time.time()
							max_subarray(a__      ,  0      ,  input_size - 1	)
							_UpperCAmelCase					=      time.time()
							return end - start

def 	lowerCAmelCase__			(      )  ->		None:


							'''simple docstring'''





							_UpperCAmelCase					=      [1_0, 1_0_0, 1_0_0_0, 1_0_0_0_0, 5_0_0_0_0, 1_0_0_0_0_0, 2_0_0_0_0_0, 3_0_0_0_0_0, 4_0_0_0_0_0, 5_0_0_0_0_0]
							_UpperCAmelCase					=      [time_max_subarray(a__	) for input_size in input_sizes]
							print('No of Inputs\t\tTime Taken'	)
							for input_size, runtime in zip(a__      ,  a__	):
														print(a__      ,  '\t\t'      ,  a__	)
							plt.plot(a__      ,  a__	)
							plt.xlabel('Number of Inputs'	)
							plt.ylabel('Time taken in seconds'	)
							plt.show()


if __name__ == "__main__":
		from doctest import testmod

		testmod()
