




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 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         lowercase    :int      	=						VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion"				)
         # remove text_unet
         pipe.remove_unused_weights()
         pipe.to(_lowerCAmelCase				)
         pipe.set_progress_bar_config(disable=_lowerCAmelCase				)

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

         with tempfile.TemporaryDirectory() as tmpdirname:
           pipe.save_pretrained(_lowerCAmelCase				)
           lowercase    :Tuple      	=						VersatileDiffusionTextToImagePipeline.from_pretrained(_lowerCAmelCase				)
         pipe.to(_lowerCAmelCase				)
         pipe.set_progress_bar_config(disable=_lowerCAmelCase				)

         lowercase    :Optional[int]      	=						generator.manual_seed(0				)
         lowercase    :List[Any]      	=						pipe(
             prompt=_lowerCAmelCase       ,					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 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[Any]      	=						VersatileDiffusionTextToImagePipeline.from_pretrained(
             "shi-labs/versatile-diffusion"       ,					torch_dtype=torch.floataa				)
         pipe.to(_lowerCAmelCase				)
         pipe.set_progress_bar_config(disable=_lowerCAmelCase				)

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

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

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









import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment




@require_huggingface_suite
class    __lowerCAmelCase       (							unittest.TestCase):






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[str]      	=						inspect.getfile(accelerate.test_utils				)
         lowercase    :List[Any]      	=						os.path.sep.join(
             mod_file.split(os.path.sep				)[:-1] + ["scripts", "external_deps", "test_metrics.py"]				)

         from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

         lowercase    :str      	=						test_metrics






       @require_cpu
       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         debug_launcher(self.test_metrics.main       ,					num_processes=1				)






       @require_cpu
       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         debug_launcher(self.test_metrics.main				)






       @require_single_gpu
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         self.test_metrics.main()






       @require_multi_gpu
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         print(F"Found {torch.cuda.device_count()} devices."				)
         lowercase    :str      	=						["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
         with patch_environment(omp_num_threads=1				):
           execute_subprocess_async(_lowerCAmelCase       ,					env=os.environ.copy()				)



