

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


lowerCAmelCase     :   List[Any]						     =  False
class  _A		(  unittest.TestCase):
  pass

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

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







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

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     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					)

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

        SCREAMING_SNAKE_CASE_				:  Any           =     generator.manual_seed(0					)
        SCREAMING_SNAKE_CASE_				:  int           =     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  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     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					)

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

        SCREAMING_SNAKE_CASE_				:  Any           =     image[0, 253:256, 253:256, -1]

        assert image.shape == (1, 512, 512, 3)
        SCREAMING_SNAKE_CASE_				:  int           =     np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778]					)

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









def    A_  (							a					,   a  ):

      """simple docstring"""



      return numa ^ numa < 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()






