





import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionModelEditingPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




@skip_mps
class    _a  (  A__		,	A__		,	A__		,	unittest.TestCase):

      """simple docstring"""


      UpperCamelCase__       						=   StableDiffusionModelEditingPipeline
      UpperCamelCase__       						=   TEXT_TO_IMAGE_PARAMS
      UpperCamelCase__       						=   TEXT_TO_IMAGE_BATCH_PARAMS
      UpperCamelCase__       						=   TEXT_TO_IMAGE_IMAGE_PARAMS
      UpperCamelCase__       						=   TEXT_TO_IMAGE_IMAGE_PARAMS






      def 	UpperCAmelCase_   (						self:	Union[str, Any]			):
          '''simple docstring'''


          torch.manual_seed(0			)
          UpperCamelCase__:					Dict												=      UNetaDConditionModel(
              block_out_channels=(32, 64)					,  layers_per_block=2					,  sample_size=32					,  in_channels=4					,  out_channels=4					,  down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")					,  up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")					,  cross_attention_dim=32					,  )
          UpperCamelCase__:					List[str]												=      DDIMScheduler()
          torch.manual_seed(0			)
          UpperCamelCase__:					Optional[Any]												=      AutoencoderKL(
              block_out_channels=[32, 64]					,  in_channels=3					,  out_channels=3					,  down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]					,  up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]					,  latent_channels=4					,  )
          torch.manual_seed(0			)
          UpperCamelCase__:					Union[str, Any]												=      CLIPTextConfig(
              bos_token_id=0					,  eos_token_id=2					,  hidden_size=32					,  intermediate_size=37					,  layer_norm_eps=1e-05					,  num_attention_heads=4					,  num_hidden_layers=5					,  pad_token_id=1					,  vocab_size=1000					,  )
          UpperCamelCase__:					List[str]												=      CLIPTextModel(__snake_case			)
          UpperCamelCase__:					List[str]												=      CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"			)

          UpperCamelCase__:					List[Any]												=      {
              '''unet''': unet,
              '''scheduler''': scheduler,
              '''vae''': vae,
              '''text_encoder''': text_encoder,
              '''tokenizer''': tokenizer,
              '''safety_checker''': None,
              '''feature_extractor''': None,
          }
          return components






      def 	UpperCAmelCase_   (						self:	Optional[Any]					,  __lowerCamelCase:	Any					,  __lowerCamelCase:	Tuple=0			):
          '''simple docstring'''


          UpperCamelCase__:					int												=      torch.manual_seed(__snake_case			)
          UpperCamelCase__:					Dict												=      {
              '''prompt''': '''A field of roses''',
              '''generator''': generator,
              # Setting height and width to None to prevent OOMs on CPU.
              '''height''': None,
              '''width''': None,
              '''num_inference_steps''': 2,
              '''guidance_scale''': 6.0,
              '''output_type''': '''numpy''',
          }
          return inputs






      def 	UpperCAmelCase_   (						self:	List[Any]			):
          '''simple docstring'''


          UpperCamelCase__:					List[Any]												=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
          UpperCamelCase__:					int												=      self.get_dummy_components()
          UpperCamelCase__:					Optional[Any]												=      StableDiffusionModelEditingPipeline(**__snake_case			)
          UpperCamelCase__:					Any												=      sd_pipe.to(__snake_case			)
          sd_pipe.set_progress_bar_config(disable=__snake_case			)

          UpperCamelCase__:					int												=      self.get_dummy_inputs(__snake_case			)
          UpperCamelCase__:					Optional[Any]												=      sd_pipe(**__snake_case			).images
          UpperCamelCase__:					Tuple												=      image[0, -3:, -3:, -1]
          assert image.shape == (1, 64, 64, 3)

          UpperCamelCase__:					Optional[int]												=      np.array([0.4_755, 0.5_132, 0.4_976, 0.3_904, 0.3_554, 0.4_765, 0.5_139, 0.5_158, 0.4_889]			)

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






      def 	UpperCAmelCase_   (						self:	List[Any]			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[int]												=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
          UpperCamelCase__:					Optional[Any]												=      self.get_dummy_components()
          UpperCamelCase__:					str												=      StableDiffusionModelEditingPipeline(**__snake_case			)
          UpperCamelCase__:					List[str]												=      sd_pipe.to(__snake_case			)
          sd_pipe.set_progress_bar_config(disable=__snake_case			)

          UpperCamelCase__:					int												=      self.get_dummy_inputs(__snake_case			)
          UpperCamelCase__:					Union[str, Any]												=      '''french fries'''
          UpperCamelCase__:					Optional[Any]												=      sd_pipe(**__snake_case					,  negative_prompt=__snake_case			)
          UpperCamelCase__:					Tuple												=      output.images
          UpperCamelCase__:					Dict												=      image[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)

          UpperCamelCase__:					Optional[Any]												=      np.array([0.4_992, 0.5_101, 0.5_004, 0.3_949, 0.3_604, 0.4_735, 0.5_216, 0.5_204, 0.4_913]			)

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






      def 	UpperCAmelCase_   (						self:	List[Any]			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
          UpperCamelCase__:					Any												=      self.get_dummy_components()
          UpperCamelCase__:					Any												=      EulerAncestralDiscreteScheduler(
              beta_start=0.00_085					,  beta_end=0.012					,  beta_schedule="scaled_linear"			)
          UpperCamelCase__:					Any												=      StableDiffusionModelEditingPipeline(**__snake_case			)
          UpperCamelCase__:					Union[str, Any]												=      sd_pipe.to(__snake_case			)
          sd_pipe.set_progress_bar_config(disable=__snake_case			)

          UpperCamelCase__:					int												=      self.get_dummy_inputs(__snake_case			)
          UpperCamelCase__:					List[Any]												=      sd_pipe(**__snake_case			).images
          UpperCamelCase__:					int												=      image[0, -3:, -3:, -1]

          assert image.shape == (1, 64, 64, 3)

          UpperCamelCase__:					List[str]												=      np.array([0.4_747, 0.5_372, 0.4_779, 0.4_982, 0.5_543, 0.4_816, 0.5_238, 0.4_904, 0.5_027]			)

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






      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[int]												=      '''cpu'''  # ensure determinism for the device-dependent torch.Generator
          UpperCamelCase__:					Tuple												=      self.get_dummy_components()
          UpperCamelCase__:					int												=      PNDMScheduler()
          UpperCamelCase__:					Any												=      StableDiffusionModelEditingPipeline(**__snake_case			)
          UpperCamelCase__:					Union[str, Any]												=      sd_pipe.to(__snake_case			)
          sd_pipe.set_progress_bar_config(disable=__snake_case			)

          UpperCamelCase__:					Union[str, Any]												=      self.get_dummy_inputs(__snake_case			)
          # the pipeline does not expect pndm so test if it raises error.
          with self.assertRaises(__snake_case			):
              UpperCamelCase__:					List[Any]												=      sd_pipe(**__snake_case			).images






      def 	UpperCAmelCase_   (						self:	int			):
          '''simple docstring'''


          super().test_inference_batch_single_identical(expected_max_diff=5e-3			)






      def 	UpperCAmelCase_   (						self:	List[Any]			):
          '''simple docstring'''


          super().test_attention_slicing_forward_pass(expected_max_diff=5e-3			)




@slow
@require_torch_gpu
class    _a  (  unittest.TestCase):

      """simple docstring"""


      def 	UpperCAmelCase_   (						self:	Optional[int]			):
          '''simple docstring'''


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






      def 	UpperCAmelCase_   (						self:	int					,  __lowerCamelCase:	Optional[int]=0			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      torch.manual_seed(__snake_case			)
          UpperCamelCase__:					int												=      {
              '''prompt''': '''A field of roses''',
              '''generator''': generator,
              '''num_inference_steps''': 3,
              '''guidance_scale''': 7.5,
              '''output_type''': '''numpy''',
          }
          return inputs






      def 	UpperCAmelCase_   (						self:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[Any]												=      '''CompVis/stable-diffusion-v1-4'''
          UpperCamelCase__:					Dict												=      StableDiffusionModelEditingPipeline.from_pretrained(__snake_case					,  safety_checker=__snake_case			)
          pipe.to(__snake_case			)
          pipe.set_progress_bar_config(disable=__snake_case			)
          pipe.enable_attention_slicing()

          UpperCamelCase__:					Dict												=      self.get_inputs()
          UpperCamelCase__:					Optional[Any]												=      pipe(**__snake_case			).images
          UpperCamelCase__:					str												=      image[0, -3:, -3:, -1].flatten()

          assert image.shape == (1, 512, 512, 3)

          UpperCamelCase__:					int												=      np.array(
              [0.6_749_496, 0.6_386_453, 0.51_443_267, 0.66_094_905, 0.61_921_215, 0.5_491_332, 0.5_744_417, 0.58_075_106, 0.5_174_658]			)

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

          # make sure image changes after editing
          pipe.edit_model("A pack of roses"					,  "A pack of blue roses"			)

          UpperCamelCase__:					Dict												=      pipe(**__snake_case			).images
          UpperCamelCase__:					Union[str, Any]												=      image[0, -3:, -3:, -1].flatten()

          assert image.shape == (1, 512, 512, 3)

          assert np.abs(expected_slice - image_slice			).max() > 1e-1






      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          torch.cuda.empty_cache()
          torch.cuda.reset_max_memory_allocated()
          torch.cuda.reset_peak_memory_stats()

          UpperCamelCase__:					Any												=      '''CompVis/stable-diffusion-v1-4'''
          UpperCamelCase__:					int												=      DDIMScheduler.from_pretrained(__snake_case					,  subfolder="scheduler"			)
          UpperCamelCase__:					int												=      StableDiffusionModelEditingPipeline.from_pretrained(
              __snake_case					,  scheduler=__snake_case					,  safety_checker=__snake_case			)
          UpperCamelCase__:					Tuple												=      pipe.to(__snake_case			)
          pipe.set_progress_bar_config(disable=__snake_case			)
          pipe.enable_attention_slicing(1			)
          pipe.enable_sequential_cpu_offload()

          UpperCamelCase__:					List[str]												=      self.get_inputs()
          UpperCamelCase__:					int												=      pipe(**__snake_case			)

          UpperCamelCase__:					Union[str, Any]												=      torch.cuda.max_memory_allocated()
          # make sure that less than 4.4 GB is allocated
          assert mem_bytes < 4.4 * 10**9








from manim import *




class    _a  (  UpperCamelCase__):

      """simple docstring"""


      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[Any]												=      Rectangle(height=0.5					,  width=0.5			)
          UpperCamelCase__:					Optional[int]												=      Rectangle(height=0.46					,  width=0.46			).set_stroke(width=0			)
          UpperCamelCase__:					List[Any]												=      Rectangle(height=0.25					,  width=0.25			)

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

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

          UpperCamelCase__:					str												=      [mem.copy() for i in range(6			)]
          UpperCamelCase__:					Tuple												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)

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

          UpperCamelCase__:					int												=      []
          UpperCamelCase__:					Optional[Any]												=      []

          for i, rect in enumerate(__lowerCamelCase			):
              rect.set_stroke(__lowerCamelCase			)

              UpperCamelCase__:					Tuple												=      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(model_cpu_arr[0]					,  direction=__lowerCamelCase					,  buff=0.0			)
              else:
                  cpu_target.next_to(model_cpu_arr[i - 1]					,  direction=__lowerCamelCase					,  buff=0.0			)
              self.add(__lowerCamelCase			)
              model_cpu_arr.append(__lowerCamelCase			)

          self.add(*__lowerCamelCase					,  *__lowerCamelCase			)

          UpperCamelCase__:					int												=      [meta_mem.copy() for i in range(6			)]
          UpperCamelCase__:					Dict												=      [meta_mem.copy() for i in range(6			)]
          UpperCamelCase__:					Union[str, Any]												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					Tuple												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					int												=      VGroup(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					List[Any]												=      Text("Disk"					,  font_size=24			)
          UpperCamelCase__:					Union[str, Any]												=      Group(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0.5					,  aligned_edge=__lowerCamelCase			)
          disk.move_to([-4.0, -1.25, 0]			)
          self.add(__lowerCamelCase					,  __lowerCamelCase			)

          UpperCamelCase__:					Optional[int]												=      []

          for i in range(6			):
              UpperCamelCase__:					List[str]												=      fill.copy().set_fill(__lowerCamelCase					,  opacity=0.8			)
              target.move_to(disk_left_col_base[i]			).scale(0.5			)
              cpu_disk_arr.append(__lowerCamelCase			)

          self.add(*__lowerCamelCase			)

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

          UpperCamelCase__:					str												=      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			)

          UpperCamelCase__:					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			)

          UpperCamelCase__:					Dict												=      MarkupText(F"The offloaded weights are all sent to the CPU."					,  font_size=24			)
          step_a.move_to([2, 2, 0]			)

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

          for i in range(6			):
              UpperCamelCase__:					List[Any]												=      cpu_disk_arr[i]
              UpperCamelCase__:					Optional[int]												=      rect.copy().set_fill(__lowerCamelCase					,  opacity=0.8			).scale(2.0			)
              cpa.generate_target()
              cpa.target.move_to(model_base[i]			)

              if i == 0:
                  rect.set_fill(__lowerCamelCase					,  opacity=0.8			)
                  rect.generate_target()
                  rect.target.move_to(cpu_left_col_base[0]			).scale(2.0			)

                  self.remove(
                      *__lowerCamelCase					,  *__lowerCamelCase					,  )

              else:
                  rect.generate_target()
                  rect.target.move_to(cpu_left_col_base[i]			).scale(2.0			)
              self.play(MoveToTarget(__lowerCamelCase			)					,  MoveToTarget(__lowerCamelCase			)					,  model_base[i].animate.set_stroke(__lowerCamelCase			)			)
          self.play(FadeOut(__lowerCamelCase			)			)

          UpperCamelCase__:					Dict												=      MarkupText(
              F"Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed."					,  font_size=24					,  )
          step_a.move_to([2, 2, 0]			)

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

          UpperCamelCase__:					List[str]												=      []
          UpperCamelCase__:					Optional[Any]												=      []
          for i in range(6			):
              UpperCamelCase__:					List[str]												=      Arrow(start=__lowerCamelCase					,  end=__lowerCamelCase					,  color=__lowerCamelCase					,  buff=0.5			)
              a.next_to(model_base[i].get_left()					,  __lowerCamelCase					,  buff=0.2			)
              arrows.append(__lowerCamelCase			)
              animations.append(Write(__lowerCamelCase			)			)
          self.play(*__lowerCamelCase			)
          self.wait()

