




import gc
import tempfile
import unittest

import numpy as np
import torch

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


__a		     =   False







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



	"""simple docstring"""






	def    __a     (					self    ) ->      Optional[int]:





								"""simple docstring"""


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

	def    __a     (					self    ) ->      int:





								"""simple docstring"""


								lowercase__  :	Optional[Any]        =							VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"    )
								# remove text_unet
								pipe.remove_unused_weights()
								pipe.to(__lowerCAmelCase    )
								pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

								lowercase__  :	List[Any]        =							load_image(
								    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"    )

								lowercase__  :	Optional[int]        =							torch.manual_seed(0    )
								lowercase__  :	int        =							pipe(
								    prompt="first prompt"	,				image=__lowerCAmelCase	,				text_to_image_strength=0.75	,				generator=__lowerCAmelCase	,				guidance_scale=7.5	,				num_inference_steps=2	,				output_type="numpy"	,				).images

								with tempfile.TemporaryDirectory() as tmpdirname:
															pipe.save_pretrained(__lowerCAmelCase    )
															lowercase__  :	Optional[Any]        =							VersatileDiffusionDualGuidedPipeline.from_pretrained(__lowerCAmelCase    )

								pipe.to(__lowerCAmelCase    )
								pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

								lowercase__  :	int        =							generator.manual_seed(0    )
								lowercase__  :	int        =							pipe(
								    prompt="first prompt"	,				image=__lowerCAmelCase	,				text_to_image_strength=0.75	,				generator=__lowerCAmelCase	,				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    __a     (					self    ) ->      List[str]:





								"""simple docstring"""


								lowercase__  :	Optional[Any]        =							VersatileDiffusionDualGuidedPipeline.from_pretrained("shi-labs/versatile-diffusion"    )
								pipe.remove_unused_weights()
								pipe.to(__lowerCAmelCase    )
								pipe.set_progress_bar_config(disable=__lowerCAmelCase    )

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

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

								assert image.shape == (1, 512, 512, 3)
								lowercase__  :	Dict        =							np.array([0.07_87, 0.08_49, 0.08_26, 0.08_12, 0.08_07, 0.07_95, 0.08_18, 0.07_98, 0.07_79]    )

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





def  snake_case_					(		SCREAMING_SNAKE_CASE_    )      ->			list:
							lowercase__  :	Optional[Any]        =							len(SCREAMING_SNAKE_CASE_    )

							# Set the gap value to be decreased by a factor of 1.3
							# after each iteration
							lowercase__  :	Tuple        =							1.3

							# Continue sorting until the gap is 1
							while gap > 1:
														# Decrease the gap value
														lowercase__  :	Tuple        =							int(gap / shrink    )

														# Sort the elements using insertion sort
														for i in range(SCREAMING_SNAKE_CASE_    ,len(SCREAMING_SNAKE_CASE_    )    ):
																					lowercase__  :	int        =							collection[i]
																					lowercase__  :	Union[str, Any]        =							i
																					while j >= gap and collection[j - gap] > temp:
																												lowercase__  :	str        =							collection[j - gap]
																												j -= gap
																					lowercase__  :	List[Any]        =							temp

							return collection


if __name__ == "__main__":
						import doctest

						doctest.testmod()