






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


__snake_case    :    Union[str, Any]         =     False


class     __SCREAMING_SNAKE_CASE					(   unittest.TestCase):
						pass




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






						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"""
												lowerCAmelCase__    						=						VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion'   )
												# remove text_unet
												pipe.remove_unused_weights()
												pipe.to(_UpperCamelCase   )
												pipe.set_progress_bar_config(disable=_UpperCamelCase   )

												lowerCAmelCase__    						=						'A painting of a squirrel eating a burger '
												lowerCAmelCase__    						=						torch.manual_seed(0   )
												lowerCAmelCase__    						=						pipe(
												    prompt=_UpperCamelCase     ,						generator=_UpperCamelCase     ,						guidance_scale=7.5     ,						num_inference_steps=2     ,						output_type='numpy'   ).images

												with tempfile.TemporaryDirectory() as tmpdirname:
																		pipe.save_pretrained(_UpperCamelCase   )
																		lowerCAmelCase__    						=						VersatileDiffusionTextToImagePipeline.from_pretrained(_UpperCamelCase   )
												pipe.to(_UpperCamelCase   )
												pipe.set_progress_bar_config(disable=_UpperCamelCase   )

												lowerCAmelCase__    						=						generator.manual_seed(0   )
												lowerCAmelCase__    						=						pipe(
												    prompt=_UpperCamelCase     ,						generator=_UpperCamelCase     ,						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"""
												lowerCAmelCase__    						=						VersatileDiffusionTextToImagePipeline.from_pretrained(
												    'shi-labs/versatile-diffusion'     ,						torch_dtype=torch.floataa   )
												pipe.to(_UpperCamelCase   )
												pipe.set_progress_bar_config(disable=_UpperCamelCase   )

												lowerCAmelCase__    						=						'A painting of a squirrel eating a burger '
												lowerCAmelCase__    						=						torch.manual_seed(0   )
												lowerCAmelCase__    						=						pipe(
												    prompt=_UpperCamelCase     ,						generator=_UpperCamelCase     ,						guidance_scale=7.5     ,						num_inference_steps=50     ,						output_type='numpy'   ).images

												lowerCAmelCase__    						=						image[0, 2_53:2_56, 2_53:2_56, -1]

												assert image.shape == (1, 5_12, 5_12, 3)
												lowerCAmelCase__    						=						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













def 	_UpperCamelCase   (    UpperCamelCase_       :					int		)  ->   int:

						"""simple docstring"""



						if not isinstance(UpperCamelCase_    ,       UpperCamelCase_		):
												raise ValueError('multiplicative_persistence() only accepts integral values'		)
						if num < 0:
												raise ValueError('multiplicative_persistence() does not accept negative values'		)

						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						str(UpperCamelCase_		)

						while len(UpperCamelCase_		) != 1:
												lowerCAmelCase__    						=						[int(UpperCamelCase_		) for i in num_string]

												lowerCAmelCase__    						=						1
												for i in range(0    ,       len(UpperCamelCase_		)		):
																		total *= numbers[i]

												lowerCAmelCase__    						=						str(UpperCamelCase_		)

												steps += 1
						return steps







def 	_UpperCamelCase   (    UpperCamelCase_       :					int		)  ->   int:

						"""simple docstring"""



						if not isinstance(UpperCamelCase_    ,       UpperCamelCase_		):
												raise ValueError('additive_persistence() only accepts integral values'		)
						if num < 0:
												raise ValueError('additive_persistence() does not accept negative values'		)

						lowerCAmelCase__    						=						0
						lowerCAmelCase__    						=						str(UpperCamelCase_		)

						while len(UpperCamelCase_		) != 1:
												lowerCAmelCase__    						=						[int(UpperCamelCase_		) for i in num_string]

												lowerCAmelCase__    						=						0
												for i in range(0    ,       len(UpperCamelCase_		)		):
																		total += numbers[i]

												lowerCAmelCase__    						=						str(UpperCamelCase_		)

												steps += 1
						return steps


if __name__ == "__main__":
					import doctest

					doctest.testmod()





