






"""simple docstring"""







from __future__ import annotations

from scipy.special import comb  # type: ignore


class    __UpperCAmelCase:




						"""simple docstring"""




						def __init__(       self	,  snake_case__      ):
												'''simple docstring'''
												lowercase__    :				Any=	list_of_points
												# Degree determines the flexibility of the curve.
												# Degree = 1 will produce a straight line.
												lowercase__    :				List[Any]=	len(snake_case__      ) - 1




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												assert 0 <= t <= 1, "Time t must be between 0 and 1."
												lowercase__    :				list[float]=	[]
												for i in range(len(self.list_of_points      )      ):
																		# basis function for each i
																		output_values.append(
																		    comb(self.degree	,  snake_case__      ) * ((1 - t) ** (self.degree - i)) * (t**i)      )
												# the basis must sum up to 1 for it to produce a valid Bezier curve.
												assert round(sum(snake_case__      )	,  5      ) == 1
												return output_values




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''
												assert 0 <= t <= 1, "Time t must be between 0 and 1."

												lowercase__    :				str=	self.basis_function(snake_case__      )
												lowercase__    :				Dict=	0.0
												lowercase__    :				Optional[Any]=	0.0
												for i in range(len(self.list_of_points      )      ):
																		# For all points, sum up the product of i-th basis function and i-th point.
																		x += basis_function[i] * self.list_of_points[i][0]
																		y += basis_function[i] * self.list_of_points[i][1]
												return (x, y)




						def 		UpperCAmelCase_	(       self	,  snake_case__ = 0.01      ):
												'''simple docstring'''
												from matplotlib import pyplot as plt  # type: ignore

												lowercase__    :				list[float]=	[]  # x coordinates of points to plot
												lowercase__    :				list[float]=	[]  # y coordinates of points to plot

												lowercase__    :				List[str]=	0.0
												while t <= 1:
																		lowercase__    :				Any=	self.bezier_curve_function(snake_case__      )
																		to_plot_x.append(value[0]      )
																		to_plot_y.append(value[1]      )
																		t += step_size

												lowercase__    :				Optional[int]=	[i[0] for i in self.list_of_points]
												lowercase__    :				str=	[i[1] for i in self.list_of_points]

												plt.plot(
												    snake_case__	,  snake_case__	,  color="blue"	,  label="Curve of Degree " + str(self.degree      )	,  )
												plt.scatter(snake_case__	,  snake_case__	,  color="red"	,  label="Control Points"      )
												plt.legend()
												plt.show()


if __name__ == "__main__":
			import doctest

			doctest.testmod()

			BezierCurve([(1, 2), (3, 5)]).plot_curve()  # degree 1
			BezierCurve([(0, 0), (5, 5), (5, 0)]).plot_curve()  # degree 2
			BezierCurve([(0, 0), (5, 5), (5, 0), (2.5, -2.5)]).plot_curve()  # degree 3














"""simple docstring"""







import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


a			:    Union[str, Any]				 =		False


class    __UpperCAmelCase(       unittest.TestCase							):




						"""simple docstring"""




						pass


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




						"""simple docstring"""




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()




						def 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Dict=	VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion"      )
												# remove text_unet
												pipe.remove_unused_weights()
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				Any=	"A painting of a squirrel eating a burger "
												lowercase__    :				Optional[Any]=	torch.manual_seed(0      )
												lowercase__    :				List[str]=	pipe(
												    prompt=snake_case__	,  generator=snake_case__	,  guidance_scale=7.5	,  num_inference_steps=2	,  output_type="numpy"      ).images

												with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(snake_case__      )
																		lowercase__    :				Optional[Any]=	VersatileDiffusionTextToImagePipeline.from_pretrained(snake_case__      )
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				Any=	generator.manual_seed(0      )
												lowercase__    :				Tuple=	pipe(
												    prompt=snake_case__	,  generator=snake_case__	,  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 		UpperCAmelCase_	(       self      ):
												'''simple docstring'''
												lowercase__    :				Tuple=	VersatileDiffusionTextToImagePipeline.from_pretrained(
												    "shi-labs/versatile-diffusion"	,  torch_dtype=torch.floataa      )
												pipe.to(snake_case__      )
												pipe.set_progress_bar_config(disable=snake_case__      )

												lowercase__    :				List[str]=	"A painting of a squirrel eating a burger "
												lowercase__    :				Union[str, Any]=	torch.manual_seed(0      )
												lowercase__    :				Optional[Any]=	pipe(
												    prompt=snake_case__	,  generator=snake_case__	,  guidance_scale=7.5	,  num_inference_steps=50	,  output_type="numpy"      ).images

												lowercase__    :				List[str]=	image[0, 253:256, 253:256, -1]

												assert image.shape == (1, 512, 512, 3)
												lowercase__    :				Optional[int]=	np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]      )

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






