



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




					'''simple docstring'''



					pass






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




					'''simple docstring'''



					def 						__UpperCAmelCase     (  self				:	Optional[Any]					)  ->       List[str]:



						'''simple docstring'''





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




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       int       =		VersatileDiffusionTextToImagePipeline.from_pretrained('shi-labs/versatile-diffusion'					)
						# remove text_unet
						pipe.remove_unused_weights()
						pipe.to(_UpperCamelCase					)
						pipe.set_progress_bar_config(disable=_UpperCamelCase					)

						_lowercase					:       Optional[Any]       =		"""A painting of a squirrel eating a burger """
						_lowercase					:       List[str]       =		torch.manual_seed(0					)
						_lowercase					:       Tuple       =		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					)
							_lowercase					:       Optional[int]       =		VersatileDiffusionTextToImagePipeline.from_pretrained(_UpperCamelCase					)
						pipe.to(_UpperCamelCase					)
						pipe.set_progress_bar_config(disable=_UpperCamelCase					)

						_lowercase					:       List[Any]       =		generator.manual_seed(0					)
						_lowercase					:       str       =		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				:	str					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Dict       =		VersatileDiffusionTextToImagePipeline.from_pretrained(
						    'shi-labs/versatile-diffusion'     ,  torch_dtype=torch.floataa					)
						pipe.to(_UpperCamelCase					)
						pipe.set_progress_bar_config(disable=_UpperCamelCase					)

						_lowercase					:       List[str]       =		"""A painting of a squirrel eating a burger """
						_lowercase					:       Union[str, Any]       =		torch.manual_seed(0					)
						_lowercase					:       Union[str, Any]       =		pipe(
						    prompt=_UpperCamelCase     ,  generator=_UpperCamelCase     ,  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





'''simple docstring'''

_A				:    Dict     						='''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

_A				:    Dict     						=[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
_A				:    Dict     						={
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}
