





from manim import *







class 				__lowerCAmelCase       (    UpperCamelCase__):







      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__	:					List[Any]										=Rectangle(height=0.5		,      width=0.5							)
        a__	:					List[Any]										=Rectangle(height=0.46		,      width=0.46							).set_stroke(width=0							)

        a__	:					int										=[mem.copy() for i in range(6							)]
        a__	:					Optional[int]										=[mem.copy() for i in range(6							)]
        a__	:					Any										=VGroup(*lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0							)
        a__	:					str										=VGroup(*lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0							)
        a__	:					str										=VGroup(lowerCAmelCase__		,      lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0							)
        a__	:					str										=Text("CPU"		,      font_size=2_4							)
        a__	:					Union[str, Any]										=Group(lowerCAmelCase__		,      lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0.5		,      aligned_edge=lowerCAmelCase__							)
        cpu.move_to([-2.5, -0.5, 0]							)
        self.add(lowerCAmelCase__							)

        a__	:					Optional[Any]										=[mem.copy() for i in range(4							)]
        a__	:					Any										=VGroup(*lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0							)
        a__	:					Union[str, Any]										=Text("GPU"		,      font_size=2_4							)
        a__	:					Optional[int]										=Group(lowerCAmelCase__		,      lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0.5		,      aligned_edge=lowerCAmelCase__							)
        gpu.move_to([-1, -1, 0]							)
        self.add(lowerCAmelCase__							)

        a__	:					Union[str, Any]										=[mem.copy() for i in range(6							)]
        a__	:					Tuple										=VGroup(*lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0							)

        a__	:					List[str]										=Text("Model"		,      font_size=2_4							)
        a__	:					Any										=Group(lowerCAmelCase__		,      lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0.5		,      aligned_edge=lowerCAmelCase__							)
        model.move_to([3, -1.0, 0]							)
        self.add(lowerCAmelCase__							)

        a__	:					Optional[Any]										=[]
        for i, rect in enumerate(lowerCAmelCase__							):
          rect.set_stroke(lowerCAmelCase__							)
          # target = fill.copy().set_fill(YELLOW, opacity=0.7)
          # target.move_to(rect)
          # self.add(target)

          a__	:					Optional[Any]										=Rectangle(height=0.46 / 4		,      width=0.46 / 3							).set_stroke(width=0.0							).set_fill(lowerCAmelCase__		,      opacity=0.7							)

          if i == 0:
            cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)		,      buff=0.02		,      direction=lowerCAmelCase__							)
            cpu_target.set_x(cpu_target.get_x() + 0.1							)
          elif i == 3:
            cpu_target.next_to(cpu_targs[0]		,      direction=lowerCAmelCase__		,      buff=0.0							)
          else:
            cpu_target.next_to(cpu_targs[i - 1]		,      direction=lowerCAmelCase__		,      buff=0.0							)
          self.add(lowerCAmelCase__							)
          cpu_targs.append(lowerCAmelCase__							)

        a__	:					List[Any]										=[mem.copy() for i in range(6							)]
        a__	:					int										=VGroup(*lowerCAmelCase__							).arrange(lowerCAmelCase__		,      buff=0							)

        a__	:					Tuple										=Text("Loaded Checkpoint"		,      font_size=2_4							)
        a__	:					int										=Group(lowerCAmelCase__		,      lowerCAmelCase__							).arrange(lowerCAmelCase__		,      aligned_edge=lowerCAmelCase__		,      buff=0.4							)
        checkpoint.move_to([3, 0.5, 0]							)

        a__	:					Any										=Square(side_length=2.2							)
        key.move_to([-5, 2, 0]							)

        a__	:					Union[str, Any]										=MarkupText(
            F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''		,      font_size=1_8		,      )

        key_text.move_to([-5, 2.4, 0]							)

        self.add(lowerCAmelCase__		,      lowerCAmelCase__							)

        a__	:					Optional[int]										=MarkupText(
            F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''		,      font_size=1_8		,      )

        blue_text.next_to(lowerCAmelCase__		,      DOWN * 2.4		,      aligned_edge=key_text.get_left()							)

        a__	:					Any										=MarkupText(
            F'''Next, a <i><span fgcolor="{BLUE}">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor="{BLUE}">single shard</span>.'''		,      font_size=2_4		,      )
        step_a.move_to([2, 2, 0]							)
        self.play(Write(lowerCAmelCase__							)		,      Write(lowerCAmelCase__							)							)

        self.play(Write(lowerCAmelCase__		,      run_time=1							)		,      Create(lowerCAmelCase__		,      run_time=1							)							)

        a__	:					Optional[Any]										=[]
        a__	:					Dict										=[]
        for i, rect in enumerate(lowerCAmelCase__							):
          a__	:					Union[str, Any]										=fill.copy().set_fill(lowerCAmelCase__		,      opacity=0.7							)
          target.move_to(lowerCAmelCase__							)
          first_animations.append(GrowFromCenter(lowerCAmelCase__		,      run_time=1							)							)

          a__	:					Union[str, Any]										=target.copy()
          cpu_target.generate_target()
          if i < 5:
            cpu_target.target.move_to(cpu_left_col_base[i + 1]							)
          else:
            cpu_target.target.move_to(cpu_right_col_base[i - 5]							)
          second_animations.append(MoveToTarget(lowerCAmelCase__		,      run_time=1.5							)							)

        self.play(*lowerCAmelCase__							)
        self.play(*lowerCAmelCase__							)
        self.wait()













import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


UpperCAmelCase	:	int		     =       False







class 				__lowerCAmelCase       (    unittest.TestCase):
      pass







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







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




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







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__	:					str										=VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"		,      torch_dtype=torch.floataa							)
        pipe.to(lowerCAmelCase__							)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					int										=load_image(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"							)

        a__	:					Optional[Any]										=torch.manual_seed(0							)
        a__	:					Optional[Any]										=pipe.dual_guided(
            prompt="first prompt"		,      image=lowerCAmelCase__		,      text_to_image_strength=0.75		,      generator=lowerCAmelCase__		,      guidance_scale=7.5		,      num_inference_steps=2		,      output_type="numpy"		,      ).images

        with tempfile.TemporaryDirectory() as tmpdirname:
          pipe.save_pretrained(lowerCAmelCase__							)
          a__	:					str										=VersatileDiffusionPipeline.from_pretrained(lowerCAmelCase__		,      torch_dtype=torch.floataa							)
        pipe.to(lowerCAmelCase__							)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					Optional[Any]										=generator.manual_seed(0							)
        a__	:					Tuple										=pipe.dual_guided(
            prompt="first prompt"		,      image=lowerCAmelCase__		,      text_to_image_strength=0.75		,      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    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__	:					str										=VersatileDiffusionPipeline.from_pretrained("shi-labs/versatile-diffusion"		,      torch_dtype=torch.floataa							)
        pipe.to(lowerCAmelCase__							)
        pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					Optional[Any]										="cyberpunk 2077"
        a__	:					int										=load_image(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"							)
        a__	:					Union[str, Any]										=torch.manual_seed(0							)
        a__	:					Tuple										=pipe.dual_guided(
            prompt=lowerCAmelCase__		,      image=lowerCAmelCase__		,      text_to_image_strength=0.75		,      generator=lowerCAmelCase__		,      guidance_scale=7.5		,      num_inference_steps=5_0		,      output_type="numpy"		,      ).images

        a__	:					int										=image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        a__	:					Any										=np.array([0.14_48, 0.16_19, 0.17_41, 0.10_86, 0.11_47, 0.11_28, 0.11_99, 0.11_65, 0.10_01]							)

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

        a__	:					str										="A painting of a squirrel eating a burger "
        a__	:					Optional[int]										=torch.manual_seed(0							)
        a__	:					str										=pipe.text_to_image(
            prompt=lowerCAmelCase__		,      generator=lowerCAmelCase__		,      guidance_scale=7.5		,      num_inference_steps=5_0		,      output_type="numpy"							).images

        a__	:					Any										=image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

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

        a__	:					Optional[Any]										=pipe.image_variation(lowerCAmelCase__		,      generator=lowerCAmelCase__		,      output_type="numpy"							).images

        a__	:					Union[str, Any]										=image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        a__	:					Any										=np.array([0.30_76, 0.31_23, 0.32_84, 0.37_82, 0.37_70, 0.38_94, 0.42_97, 0.43_31, 0.44_56]							)

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






