class_docstr: Inspired by: https://github.com/haofanwang/ControlNet-for-Diffusers/
class_docstr_tok: ["Inspired","by",":","https",":","\/\/github.com\/haofanwang\/ControlNet-for-Diffusers\/"]
docstr: Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
  to `enable_sequential_cpu_offload`, this method moves one whole model at a time to the GPU when its `forward`
  method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
  `enable_sequential_cpu_offload`, but performance is much better due to the iterative execution of the `unet`.
docstr_tok: ["Offloads","all","models","to","CPU","using","accelerate",",","reducing","memory","usage","with","a","low","impact","on","performance",".","Compared","to","`","enable_sequential_cpu_offload","`",",","this","method","moves","one","whole","model","at","a","time","to","the","GPU","when","its","`","forward","`","method","is","called",",","and","the","model","remains","in","GPU","until","the","next","model","runs",".","Memory","savings","are","lower","than","with","`","enable_sequential_cpu_offload","`",",","but","performance","is","much","better","due","to","the","iterative","execution","of","the","`","unet","`","."]
code: def enable_model_cpu_offload(self, gpu_id=0):
      r"""
      Offloads all models to CPU using accelerate, reducing memory usage with a low impact on performance. Compared
      to `enable_sequential_cpu_offload`, this method moves one whole model at a time to the GPU when its `forward`
      method is called, and the model remains in GPU until the next model runs. Memory savings are lower than with
      `enable_sequential_cpu_offload`, but performance is much better due to the iterative execution of the `unet`.
      """
      if is_accelerate_available() and is_accelerate_version(
          ">=", "0.17.0.dev0"
      ):
          from accelerate import cpu_offload_with_hook
      else:
          raise ImportError(
              "`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."
          )
  
      device = torch.device(f"cuda:{gpu_id}")
  
      hook = None
      for cpu_offloaded_model in [
          self.text_encoder,
          self.unet,
          self.vae,
      ]:
          _, hook = cpu_offload_with_hook(
              cpu_offloaded_model, device, prev_module_hook=hook
          )
  
      if self.safety_checker is not None:
          # the safety checker can offload the vae again
          _, hook = cpu_offload_with_hook(
              self.safety_checker, device, prev_module_hook=hook
          )
  
      # control net hook has be manually offloaded as it alternates with unet
      cpu_offload_with_hook(self.controlnet, device)
  
      # We'll offload the last model manually.
      self.final_offload_hook = hook
  
imports: import inspect
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import PIL.Image
  import torch
  import torch.nn.functional
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
  from diffusers import AutoencoderKL, ControlNetModel, DiffusionPipeline, UNet2DConditionModel, logging
  from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput, StableDiffusionSafetyChecker
  from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
  from diffusers.schedulers import KarrasDiffusionSchedulers
  from diffusers.utils import PIL_INTERPOLATION, is_accelerate_available, is_accelerate_version, replace_example_docstring
  from diffusers.utils.torch_utils import randn_tensor
label_desc: Use image node_id 5 for calling the StableDiffusionControlNetInpaintPipeline obj's underlying member method code with example usage: obj.enable_model_cpu_offload(gpu_id) without return types