"""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__  		=						False


class        a				(		unittest.TestCase   ):
    pass



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


    def      lowerCAmelCase_  (					self	:  Any					):
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()


    def      lowerCAmelCase_  (					self	:  Optional[Any]					):
      _UpperCAmelCase						 =      VersatileDiffusionTextToImagePipeline.from_pretrained("""shi-labs/versatile-diffusion"""					)
      # remove text_unet
      pipe.remove_unused_weights()
      pipe.to(__lowerCAmelCase					)
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

      _UpperCAmelCase						 =      """A painting of a squirrel eating a burger """
      _UpperCAmelCase						 =      torch.manual_seed(0					)
      _UpperCAmelCase						 =      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					)
        _UpperCAmelCase						 =      VersatileDiffusionTextToImagePipeline.from_pretrained(__lowerCAmelCase					)
      pipe.to(__lowerCAmelCase					)
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

      _UpperCAmelCase						 =      generator.manual_seed(0					)
      _UpperCAmelCase						 =      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      lowerCAmelCase_  (					self	:  Optional[int]					):
      _UpperCAmelCase						 =      VersatileDiffusionTextToImagePipeline.from_pretrained(
          """shi-labs/versatile-diffusion"""   ,				torch_dtype=torch.floataa					)
      pipe.to(__lowerCAmelCase					)
      pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

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

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

      assert image.shape == (1, 512, 512, 3)
      _UpperCAmelCase						 =      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

"""simple docstring"""


import argparse

from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection

from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline


if __name__ == "__main__":
    UpperCAmelCase__  		=						argparse.ArgumentParser()

    parser.add_argument("""--dump_path""", default=None, type=str, required=True, help="""Path to the output model.""")

    parser.add_argument(
        """--txt2img_unclip""",
        default="""kakaobrain/karlo-v1-alpha""",
        type=str,
        required=False,
        help="""The pretrained txt2img unclip.""",
    )

    UpperCAmelCase__  		=						parser.parse_args()

    UpperCAmelCase__  		=						UnCLIPPipeline.from_pretrained(args.txtaimg_unclip)

    UpperCAmelCase__  		=						CLIPImageProcessor()
    UpperCAmelCase__  		=						CLIPVisionModelWithProjection.from_pretrained("""openai/clip-vit-large-patch14""")

    UpperCAmelCase__  		=						UnCLIPImageVariationPipeline(
        decoder=txtaimg.decoder,
        text_encoder=txtaimg.text_encoder,
        tokenizer=txtaimg.tokenizer,
        text_proj=txtaimg.text_proj,
        feature_extractor=feature_extractor,
        image_encoder=image_encoder,
        super_res_first=txtaimg.super_res_first,
        super_res_last=txtaimg.super_res_last,
        decoder_scheduler=txtaimg.decoder_scheduler,
        super_res_scheduler=txtaimg.super_res_scheduler,
    )

    imgaimg.save_pretrained(args.dump_path)
