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.

Support MiniMax-H3 LightVAE (#17049)

Jukka Seppänen1d9cec9e8cab
diff --git a/comfy/ldm/minimax/vae.py b/comfy/ldm/minimax/vae.py
index a2ffb6893..9f17dad58 100644
--- a/comfy/ldm/minimax/vae.py
+++ b/comfy/ldm/minimax/vae.py
@@ -173,8 +173,18 @@ class ResnetBlock3D(nn.Module):
return self.conv2(h, pre_norm=self.norm2, residual=x)
+class ResnetShortcut3D(nn.Module):
+ # resnet block pruned down to its shortcut (LightVAE Lite encoder)
+ def __init__(self, in_channels, out_channels):
+ super().__init__()
+ self.nin_shortcut = CausalConv3d(in_channels, out_channels, kernel_size=1) if in_channels != out_channels else nn.Identity()
+
+ def forward(self, x):
+ return self.nin_shortcut(x)
+
+
class EncoderFCN3D(nn.Module):
- def __init__(self, ch, ch_mult, space_down, time_down, num_res_blocks, in_channels, z_channels, double_z=True):
+ def __init__(self, ch, ch_mult, space_down, time_down, num_res_blocks, in_channels, z_channels, double_z=True, kept_blocks=None):
super().__init__()
self.num_levels = len(ch_mult)
if isinstance(num_res_blocks, int):
@@ -192,8 +202,9 @@ class EncoderFCN3D(nn.Module):
down = nn.Module()
down.block = nn.ModuleList()
for i in range(self.num_res_blocks[i_level]):
+ block = ResnetBlock3D if kept_blocks is None or (i_level, i) in kept_blocks else ResnetShortcut3D
down.block.append(
- ResnetBlock3D(
+ block(
in_channels=block_in[i_level] if i == 0 else block_mid[i_level],
out_channels=block_mid[i_level],
)
@@ -425,6 +436,9 @@ class MiniMaxH3VideoVAE(nn.Module):
operations=ops,
num_layers=36,
still_frame=None,
+ encoder_kept_blocks=None,
+ encoder=True,
+ decoder=True,
):
super().__init__()
self.vae_ratio = int(math.prod(space_down))
@@ -448,26 +462,29 @@ class MiniMaxH3VideoVAE(nn.Module):
self.tile_size = tile_size
self.tile_overlap_min = tile_overlap_min
- self.encoder = EncoderFCN3D(
- ch=ch,
- ch_mult=list(ch_mult),
- space_down=list(space_down),
- time_down=list(time_down),
- num_res_blocks=num_res_blocks,
- in_channels=in_channels,
- z_channels=z_channels,
- double_z=True,
- )
- self.quant_conv = ops.Conv3d(z_channels * 2, 2 * embed_dim, 1)
- self.post_quant_conv = ops.Conv3d(embed_dim, z_channels, 1)
- self.decoder = ViT3DDecoder(
- patch_size=self.vae_ratio,
- patch_size_t=self.vae_ratio_t,
- in_channels=z_channels,
- out_channels=out_ch,
- num_layers=num_layers,
- operations=operations,
- )
+ if encoder:
+ self.encoder = EncoderFCN3D(
+ ch=ch,
+ ch_mult=list(ch_mult),
+ space_down=list(space_down),
+ time_down=list(time_down),
+ num_res_blocks=num_res_blocks,
+ in_channels=in_channels,
+ z_channels=z_channels,
+ double_z=True,
+ kept_blocks=encoder_kept_blocks,
+ )
+ self.quant_conv = ops.Conv3d(z_channels * 2, 2 * embed_dim, 1)
+ if decoder:
+ self.post_quant_conv = ops.Conv3d(embed_dim, z_channels, 1)
+ self.decoder = ViT3DDecoder(
+ patch_size=self.vae_ratio,
+ patch_size_t=self.vae_ratio_t,
+ in_channels=z_channels,
+ out_channels=out_ch,
+ num_layers=num_layers,
+ operations=operations,
+ )
self.register_buffer("latents_mean", torch.tensor(LATENTS_MEAN))
self.register_buffer("latents_std", torch.tensor(LATENTS_STD))
diff --git a/comfy/sd.py b/comfy/sd.py
index 2f106a7bd..ac62bf795 100644
--- a/comfy/sd.py
+++ b/comfy/sd.py
@@ -1026,7 +1026,7 @@ class VAE:
#Force cast it for --disable-dynamic-vram users until there is a true core fix.
if not comfy.memory_management.aimdo_enabled:
self.disable_offload = True
- elif "decoder.transformer_blocks.0.scale1" in sd and "encoder.down.5.block.0.conv1.weight" in sd: # MiniMax H3 video VAE
+ elif "decoder.transformer_blocks.0.scale1" in sd or "encoder.down.5.block.0.nin_shortcut.weight" in sd: # MiniMax H3 video VAE, or just its decoder / encoder
minimax_ops = comfy.ops.disable_weight_init
minimax_quant = comfy.utils.detect_layer_quantization(sd, "")
if minimax_quant is not None: # int8+convrot quantized decoder
@@ -1035,7 +1035,9 @@ class VAE:
still_frame = None
if metadata is not None and metadata.get("h3_t1_direct") == "true": # decoder fine-tuned for single-latent stills
still_frame = int(metadata.get("h3_t1_output_slice", 3))
- self.first_stage_model = comfy.ldm.minimax.vae.MiniMaxH3VideoVAE(operations=minimax_ops, num_layers=minimax_layers, still_frame=still_frame)
+ # LightVAE Lite encoders prune resnet blocks down to their shortcut
+ minimax_kept = {(int(k.split(".")[2]), int(k.split(".")[4])) for k in sd if k.startswith("encoder.down.") and k.endswith(".conv1.weight")}
+ self.first_stage_model = comfy.ldm.minimax.vae.MiniMaxH3VideoVAE(operations=minimax_ops, num_layers=minimax_layers, still_frame=still_frame, encoder_kept_blocks=minimax_kept, encoder="encoder.conv_in.weight" in sd, decoder=minimax_layers > 0)
self.latent_channels = 24
self.latent_dim = 3
# frames 17k+5 <-> latents 5k+2, 16x spatial