






'''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


_UpperCAmelCase			:		List[str]   				=    False
class  lowerCAmelCase_       (				unittest.TestCase					):
							pass



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




							def 						__snake_case					(   self     :   List[str]	):
											# clean up the VRAM after each test
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()




							def 						__snake_case					(   self     :   Dict	):
											lowerCAmelCase__      			=						VersatileDiffusionTextToImagePipeline.from_pretrained('''shi-labs/versatile-diffusion'''	)
											# remove text_unet
											pipe.remove_unused_weights()
											pipe.to(SCREAMING_SNAKE_CASE_	)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

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

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

											lowerCAmelCase__      			=						generator.manual_seed(0	)
											lowerCAmelCase__      			=						pipe(
											    prompt=SCREAMING_SNAKE_CASE_ ,							generator=SCREAMING_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 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						VersatileDiffusionTextToImagePipeline.from_pretrained(
											    '''shi-labs/versatile-diffusion''' ,							torch_dtype=torch.floataa	)
											pipe.to(SCREAMING_SNAKE_CASE_	)
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

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

											lowerCAmelCase__      			=						image[0, 253:256, 253:256, -1]

											assert image.shape == (1, 512, 512, 3)
											lowerCAmelCase__      			=						np.array([0.3_367, 0.3_169, 0.2_656, 0.3_870, 0.4_790, 0.3_796, 0.4_009, 0.4_878, 0.4_778]	)

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













import pytest

import datasets


# Import fixture modules as plugins
_UpperCAmelCase			:		Tuple   				=    ["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"]





def 	lowerCAmelCase_		(lowercase__						:       Optional[int]     ,					lowercase__						:       int       )		->    Any:


				'''simple docstring'''




				for item in items:
								if any(marker in item.keywords for marker in ['''integration''', '''unit''']       ):
												continue
								item.add_marker(pytest.mark.unit       )





def 	lowerCAmelCase_		(lowercase__						:       str       )		->    List[str]:


				'''simple docstring'''




				config.addinivalue_line('''markers'''     ,					'''torchaudio_latest: mark test to run with torchaudio>=0.12'''       )





@pytest.fixture(autouse=lowercase__       )
def 	lowerCAmelCase_		(lowercase__						:       int     ,					lowercase__						:       Dict       )		->    Optional[Any]:


				'''simple docstring'''




				lowerCAmelCase__      			=						tmp_path_factory.getbasetemp() / '''cache'''
				lowerCAmelCase__      			=						test_hf_cache_home / '''datasets'''
				lowerCAmelCase__      			=						test_hf_cache_home / '''metrics'''
				lowerCAmelCase__      			=						test_hf_cache_home / '''modules'''
				monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE'''     ,					str(lowercase__       )       )
				monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE'''     ,					str(lowercase__       )       )
				monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE'''     ,					str(lowercase__       )       )
				lowerCAmelCase__      			=						test_hf_datasets_cache / '''downloads'''
				monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH'''     ,					str(lowercase__       )       )
				lowerCAmelCase__      			=						test_hf_datasets_cache / '''downloads''' / '''extracted'''
				monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''     ,					str(lowercase__       )       )





@pytest.fixture(autouse=lowercase__     ,					scope='''session'''       )
def 	lowerCAmelCase_		()		->    int:


				'''simple docstring'''




				datasets.disable_progress_bar()





@pytest.fixture(autouse=lowercase__       )
def 	lowerCAmelCase_		(lowercase__						:       Dict       )		->    List[str]:


				'''simple docstring'''




				monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS'''     ,					lowercase__       )





@pytest.fixture
def 	lowerCAmelCase_		(lowercase__						:       List[str]       )		->    List[Any]:


				'''simple docstring'''




				monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING'''     ,					lowercase__       )





