Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

Add a node to set the VAE compute dtype. (#17450)

comfyanonymousfbca05a62e23
diff --git a/comfy/sd.py b/comfy/sd.py
index d0ccf6fdb..5069b463d 100644
--- a/comfy/sd.py
+++ b/comfy/sd.py
@@ -494,7 +494,9 @@ class CLIP:
return self.patcher.is_dynamic()
class VAE:
- def __init__(self, sd=None, device=None, config=None, dtype=None, metadata=None):
+ def __init__(self, sd=None, device=None, config=None, dtype=None, metadata=None, no_init=False):
+ if no_init:
+ return
fast_disk = comfy.storage.state_dict_fast_disk(sd)
is_seedvr2_vae = "decoder.up_blocks.2.upsamplers.0.upscale_conv.weight" in sd
if not is_seedvr2_vae and 'decoder.up_blocks.0.resnets.0.norm1.weight' in sd.keys(): #diffusers format
@@ -1166,6 +1168,44 @@ class VAE:
logging.info("VAE load device: {}, offload device: {}, dtype: {}".format(self.device, offload_device, self.vae_dtype))
self.model_size()
+ def clone(self):
+ n = VAE(no_init=True)
+ n.patcher = self.patcher.clone()
+ n.first_stage_model = self.first_stage_model
+ n.memory_used_encode = self.memory_used_encode
+ n.memory_used_decode = self.memory_used_decode
+ n.downscale_ratio = self.downscale_ratio
+ n.upscale_ratio = self.upscale_ratio
+ n.latent_channels = self.latent_channels
+ n.latent_dim = self.latent_dim
+ n.output_channels = self.output_channels
+ n.pad_channel_value = self.pad_channel_value
+ n.process_input = self.process_input
+ n.process_output = self.process_output
+ n.working_dtypes = self.working_dtypes.copy()
+ n.disable_offload = self.disable_offload
+ n.not_video = self.not_video
+ n.size = self.size
+ n.downscale_index_formula = self.downscale_index_formula
+ n.upscale_index_formula = self.upscale_index_formula
+ n.extra_1d_channel = self.extra_1d_channel
+ n.crop_input = self.crop_input
+ n.handles_tiling = self.handles_tiling
+ n.format_encoded = self.format_encoded
+ n.audio_sample_rate = self.audio_sample_rate
+ n.device = self.device
+ n.vae_dtype = self.vae_dtype
+ n.output_device = self.output_device
+ if hasattr(self, "conv_out_channels"):
+ n.conv_out_channels = self.conv_out_channels
+ if hasattr(self, "audio_sample_rate_output"):
+ n.audio_sample_rate_output = self.audio_sample_rate_output
+ if hasattr(self, "autoencoder"):
+ n.autoencoder = self.autoencoder
+ if "_tile_bounded_shape" in self.__dict__:
+ n._tile_bounded_shape = self._tile_bounded_shape
+ return n
+
def model_size(self):
if self.size is not None:
return self.size
diff --git a/comfy_extras/nodes_model_advanced.py b/comfy_extras/nodes_model_advanced.py
index c1917d3d9..d61101992 100644
--- a/comfy_extras/nodes_model_advanced.py
+++ b/comfy_extras/nodes_model_advanced.py
@@ -371,6 +371,33 @@ class ModelComputeDtype:
return (m, )
+class VAEComputeDtype(io.ComfyNode):
+ @classmethod
+ def define_schema(cls):
+ return io.Schema(
+ node_id="VAEComputeDtype",
+ display_name="VAE Compute Dtype",
+ search_aliases=["vae precision", "vae fp32", "vae fp16", "vae bf16"],
+ category="advanced/debug",
+ description="Sets the compute dtype for VAE encoding and decoding without affecting other branches.",
+ inputs=[
+ io.Vae.Input("vae"),
+ io.Combo.Input("dtype", options=["default", "fp32", "fp16", "bf16"], default="default", advanced=True,
+ tooltip="Default keeps the input VAE's compute dtype."),
+ ],
+ outputs=[io.Vae.Output()],
+ )
+
+ @classmethod
+ def execute(cls, vae, dtype):
+ vae = vae.clone()
+ compute_dtype = node_helpers.string_to_torch_dtype(dtype)
+ if compute_dtype is not None:
+ vae.vae_dtype = compute_dtype
+ vae.patcher.set_model_compute_dtype(compute_dtype)
+ return io.NodeOutput(vae)
+
+
class ModelAttentionBackend(io.ComfyNode):
@classmethod
def define_schema(cls):
@@ -424,5 +451,6 @@ NODE_CLASS_MAPPINGS = {
"ModelNoiseScale": ModelNoiseScale,
"RescaleCFG": RescaleCFG,
"ModelComputeDtype": ModelComputeDtype,
+ "VAEComputeDtype": VAEComputeDtype,
"ModelAttentionBackend": ModelAttentionBackend,
}
diff --git a/comfy_extras/nodes_multigpu.py b/comfy_extras/nodes_multigpu.py
index d2f6fe67a..5ed3a09a5 100644
--- a/comfy_extras/nodes_multigpu.py
+++ b/comfy_extras/nodes_multigpu.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import copy
import logging
from inspect import cleandoc
from typing import TYPE_CHECKING
@@ -319,10 +318,7 @@ class SelectVAEDeviceNode(io.ComfyNode):
@classmethod
def execute(cls, vae: VAE, device: str = "default") -> io.NodeOutput:
- # VAE has no .clone(); shallow-copy the wrapper and clone the patcher
- # so we can retarget load/offload device without affecting the input VAE.
- vae = copy.copy(vae)
- vae.patcher = vae.patcher.clone()
+ vae = vae.clone()
resolved = comfy.model_management.resolve_gpu_device_option(device)
if resolved is None and device not in (None, "default"):
logging.info(f"Select VAE Device: requested device '{device}' not available, passing through unchanged.")
@@ -330,8 +326,6 @@ class SelectVAEDeviceNode(io.ComfyNode):
if resolved is not None and resolved.type == "cpu":
logging.info("Select VAE Device: CPU is not a supported choice, passing through unchanged.")
return io.NodeOutput(vae)
- if not hasattr(vae, "_select_base_device"):
- vae._select_base_device = vae.device
try:
vae.patcher = _apply_patcher_device(
vae.patcher, resolved,
@@ -343,7 +337,7 @@ class SelectVAEDeviceNode(io.ComfyNode):
# Keep VAE wrapper in sync with whatever model the patcher now owns;
# deepclone_multigpu may have produced a fresh first_stage_model.
vae.first_stage_model = vae.patcher.model
- vae.device = vae._select_base_device if resolved is None else resolved
+ vae.device = vae.patcher.load_device
return io.NodeOutput(vae)