from manim import *

class 					_lowercase    (     UpperCAmelCase__			):






    '''simple docstring'''




    def      _a    (    self ):
       lowerCAmelCase_:							List[str]       =	Rectangle(height=0.5       ,						width=0.5 )
       lowerCAmelCase_:							List[str]       =	Rectangle(height=0.4_6       ,						width=0.4_6 ).set_stroke(width=0 )

       lowerCAmelCase_:							Optional[int]       =	Rectangle(height=0.2_5       ,						width=0.2_5 )

       lowerCAmelCase_:							int       =	[mem.copy() for i in range(6 )]
       lowerCAmelCase_:							Optional[int]       =	[mem.copy() for i in range(6 )]
       lowerCAmelCase_:							Optional[Any]       =	VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							List[str]       =	VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							Optional[int]       =	VGroup(lowerCamelCase__       ,						lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							Dict       =	Text("CPU"       ,						font_size=24 )
       lowerCAmelCase_:							List[str]       =	Group(lowerCamelCase__       ,						lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0.5       ,						aligned_edge=lowerCamelCase__ )
       cpu.move_to([-2.5, -0.5, 0] )
       self.add(lowerCamelCase__ )

       lowerCAmelCase_:							Optional[Any]       =	[mem.copy() for i in range(4 )]
       lowerCAmelCase_:							Any       =	VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							List[str]       =	Text("GPU"       ,						font_size=24 )
       lowerCAmelCase_:							str       =	Group(lowerCamelCase__       ,						lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0.5       ,						aligned_edge=lowerCamelCase__ )
       gpu.move_to([-1, -1, 0] )
       self.add(lowerCamelCase__ )

       lowerCAmelCase_:							Optional[Any]       =	[mem.copy() for i in range(6 )]
       lowerCAmelCase_:							Dict       =	VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )

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

       lowerCAmelCase_:							Any       =	[]
       lowerCAmelCase_:							List[str]       =	[]

       for i, rect in enumerate(lowerCamelCase__ ):
          lowerCAmelCase_:							str       =	fill.copy().set_fill(lowerCamelCase__       ,						opacity=0.8 )
          target.move_to(lowerCamelCase__ )
          model_arr.append(lowerCamelCase__ )

          lowerCAmelCase_:							Optional[Any]       =	Rectangle(height=0.4_6       ,						width=0.4_6 ).set_stroke(width=0.0 ).set_fill(lowerCamelCase__       ,						opacity=0.8 )
          cpu_target.move_to(cpu_left_col_base[i] )
          model_cpu_arr.append(lowerCamelCase__ )

       self.add(*lowerCamelCase__       ,						*lowerCamelCase__ )

       lowerCAmelCase_:							str       =	[meta_mem.copy() for i in range(6 )]
       lowerCAmelCase_:							List[str]       =	[meta_mem.copy() for i in range(6 )]
       lowerCAmelCase_:							Tuple       =	VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							Union[str, Any]       =	VGroup(*lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							List[Any]       =	VGroup(lowerCamelCase__       ,						lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0 )
       lowerCAmelCase_:							int       =	Text("Disk"       ,						font_size=24 )
       lowerCAmelCase_:							Any       =	Group(lowerCamelCase__       ,						lowerCamelCase__ ).arrange(lowerCamelCase__       ,						buff=0.5       ,						aligned_edge=lowerCamelCase__ )
       disk.move_to([-4, -1.2_5, 0] )
       self.add(lowerCamelCase__       ,						lowerCamelCase__ )

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

       lowerCAmelCase_:							List[Any]       =	MarkupText(
           F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''       ,						font_size=18       ,						)

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

       self.add(lowerCamelCase__       ,						lowerCamelCase__ )

       lowerCAmelCase_:							List[str]       =	MarkupText(
           F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''       ,						font_size=18       ,						)

       blue_text.next_to(lowerCamelCase__       ,						DOWN * 2.4       ,						aligned_edge=key_text.get_left() )
       self.add(lowerCamelCase__ )

       lowerCAmelCase_:							Dict       =	MarkupText(
           F'''Now watch as an input is passed through the model\nand how the memory is utilized and handled.'''       ,						font_size=24       ,						)
       step_a.move_to([2, 2, 0] )

       self.play(Write(lowerCamelCase__ ) )

       lowerCAmelCase_:							List[Any]       =	Square(0.3 )
       input.set_fill(lowerCamelCase__       ,						opacity=1.0 )
       input.set_stroke(width=0.0 )
       input.next_to(model_base[0]       ,						lowerCamelCase__       ,						buff=0.5 )

       self.play(Write(lowerCamelCase__ ) )

       input.generate_target()
       input.target.next_to(model_arr[0]       ,						direction=lowerCamelCase__       ,						buff=0.0_2 )
       self.play(MoveToTarget(lowerCamelCase__ ) )

       self.play(FadeOut(lowerCamelCase__ ) )

       lowerCAmelCase_:							List[Any]       =	Arrow(start=lowerCamelCase__       ,						end=lowerCamelCase__       ,						color=lowerCamelCase__       ,						buff=0.5 )
       a.next_to(model_arr[0].get_left()       ,						lowerCamelCase__       ,						buff=0.2 )

       model_cpu_arr[0].generate_target()
       model_cpu_arr[0].target.move_to(gpu_rect[0] )

       lowerCAmelCase_:							Optional[int]       =	MarkupText(
           F'''As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.'''       ,						font_size=24       ,						)
       step_a.move_to([2, 2, 0] )

       self.play(Write(lowerCamelCase__       ,						run_time=3 ) )

       lowerCAmelCase_:							str       =	{"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.0_2}

       self.play(
           Write(lowerCamelCase__ )       ,						Circumscribe(model_arr[0]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						Circumscribe(model_cpu_arr[0]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						Circumscribe(gpu_rect[0]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						)
       self.play(MoveToTarget(model_cpu_arr[0] ) )

       lowerCAmelCase_:							int       =	a.copy()
       for i in range(6 ):
          a_c.next_to(model_arr[i].get_right() + 0.0_2       ,						lowerCamelCase__       ,						buff=0.2 )

          input.generate_target()
          input.target.move_to(model_arr[i].get_right() + 0.0_2 )

          lowerCAmelCase_:							List[Any]       =	AnimationGroup(
              FadeOut(lowerCamelCase__       ,						run_time=0.5 )       ,						MoveToTarget(lowerCamelCase__       ,						run_time=0.5 )       ,						FadeIn(lowerCamelCase__       ,						run_time=0.5 )       ,						lag_ratio=0.2 )

          self.play(lowerCamelCase__ )

          model_cpu_arr[i].generate_target()
          model_cpu_arr[i].target.move_to(cpu_left_col_base[i] )

          if i < 5:
             model_cpu_arr[i + 1].generate_target()
             model_cpu_arr[i + 1].target.move_to(gpu_rect[0] )
             if i >= 1:
                lowerCAmelCase_:							Any       =	0.7

             self.play(
                 Circumscribe(model_arr[i]       ,						**lowerCamelCase__ )       ,						Circumscribe(cpu_left_col_base[i]       ,						**lowerCamelCase__ )       ,						Circumscribe(cpu_left_col_base[i + 1]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						Circumscribe(gpu_rect[0]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						Circumscribe(model_arr[i + 1]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						)
             if i < 1:
                self.play(
                    MoveToTarget(model_cpu_arr[i] )       ,						MoveToTarget(model_cpu_arr[i + 1] )       ,						)
             else:
                self.play(
                    MoveToTarget(model_cpu_arr[i]       ,						run_time=0.7 )       ,						MoveToTarget(model_cpu_arr[i + 1]       ,						run_time=0.7 )       ,						)
          else:
             model_cpu_arr[i].generate_target()
             model_cpu_arr[i].target.move_to(cpu_left_col_base[-1] )
             input.generate_target()
             input.target.next_to(model_arr[-1].get_right()       ,						RIGHT + 0.0_2       ,						buff=0.2 )

             self.play(
                 Circumscribe(model_arr[-1]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						Circumscribe(cpu_left_col_base[-1]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						Circumscribe(gpu_rect[0]       ,						color=lowerCamelCase__       ,						**lowerCamelCase__ )       ,						)

             self.play(MoveToTarget(model_cpu_arr[i] ) )

          lowerCAmelCase_:							Tuple       =	a_c
          lowerCAmelCase_:							Optional[Any]       =	a_c.copy()

       input.generate_target()
       input.target.next_to(model_base[-1]       ,						RIGHT + 0.0_2       ,						buff=0.5 )
       self.play(
           FadeOut(lowerCamelCase__ )       ,						FadeOut(lowerCamelCase__       ,						run_time=0.5 )       ,						)

       lowerCAmelCase_:							Tuple       =	MarkupText(F'''Inference on a model too large for GPU memory\nis successfully completed.'''       ,						font_size=24 )
       step_a.move_to([2, 2, 0] )

       self.play(Write(lowerCamelCase__       ,						run_time=3 )       ,						MoveToTarget(lowerCamelCase__ ) )

       self.wait()
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


a  :      List[str]            =    False

class 					_lowercase    (     unittest.TestCase			):






    '''simple docstring'''
    pass





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






    '''simple docstring'''




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




    def      _a    (    self ):
       lowerCAmelCase_:							List[str]       =	VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion" )
       # remove text_unet
       pipe.remove_unused_weights()
       pipe.to(lowerCamelCase__ )
       pipe.set_progress_bar_config(disable=lowerCamelCase__ )

       lowerCAmelCase_:							Tuple       =	"A painting of a squirrel eating a burger "
       lowerCAmelCase_:							List[str]       =	torch.manual_seed(0 )
       lowerCAmelCase_:							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__ )
          lowerCAmelCase_:							int       =	VersatileDiffusionTextToImagePipeline.from_pretrained(lowerCamelCase__ )
       pipe.to(lowerCamelCase__ )
       pipe.set_progress_bar_config(disable=lowerCamelCase__ )

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

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

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

       assert image.shape == (1, 512, 512, 3)
       lowerCAmelCase_:							Optional[int]       =	np.array([0.3_3_6_7, 0.3_1_6_9, 0.2_6_5_6, 0.3_8_7_0, 0.4_7_9_0, 0.3_7_9_6, 0.4_0_0_9, 0.4_8_7_8, 0.4_7_7_8] )

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