ホーム 暮らし Updated v1.21 that supports Z-Image-Turbo-Fun-Controlnet-Union-2.1

Updated v1.21 that supports Z-Image-Turbo-Fun-Controlnet-Union-2.1

篠原 沙世 篠原 沙世 • 更新: • 公開: • 読了約 14 分
目次

Release Notes v1.21

n7c4f7b9a7ce0

Z-Image ControlNet Union 2.1 Support

Problem Overview

Issue: The Z-Image-Turbo-Fun-Controlnet-Union-2.1 model was not loading correctly in the custom node ComfyUI-NunchakuFluxLoraStacker's ModelPatchLoaderCustom.

Root Cause: The previous code only checked for control_layers.14 to detect version 2.0, which could not handle version 2.1's different layer count.

Modified File

File Path: nodes/misc_v2.py

Modified Section: Lines 264-287 (Z-Image ControlNet processing within ModelPatchLoaderCustom.load_model_patch method)

Code Before Fix

elif 'control_all_x_embedder.2-1.weight' in sd: # alipai z image fun controlnet
    sd = z_image_convert(sd)
    model = comfy.ldm.lumina.controlnet.ZImage_Control(device=model_device, dtype=dtype, operations=comfy.ops.manual_cast)

    # Support latest Z-Image Control model: filter out size mismatches and missing keys
    # ... filtering process ...

Problems:

  • No config was set, creating ZImage_Control with default parameters

  • n_control_layers was unspecified, so it couldn't handle version 2.1's layer count

  • The standard version (comfy_extras/nodes_model_patch.py) only supports 2.0 (fixed check for control_layers.14)

Code After Fix

elif 'control_all_x_embedder.2-1.weight' in sd: # alipai z image fun controlnet
    sd = z_image_convert(sd)
    config = 
    # Check for 2.0 or 2.1 by counting control_layers
    n_control_layers = 0
    for k in sd.keys():
        if k.startswith('control_layers.') and '.adaLN_modulation.0.weight' in k:
            layer_idx = int(k.split('.')[1])
            n_control_layers = max(n_control_layers, layer_idx + 1)

    # Fallback to 2.0 detection if dynamic count fails
    if n_control_layers == 0 and 'control_layers.14.adaLN_modulation.0.weight' in sd:
        n_control_layers = 15

    if n_control_layers > 0:
        config['n_control_layers'] = n_control_layers
        config['additional_in_dim'] = 17
        config['refiner_control'] = True
        ref_weight = sd.get("control_noise_refiner.0.after_proj.weight", None)
        if ref_weight is not None:
            if torch.count_nonzero(ref_weight) == 0:
                config['broken'] = True

    model = comfy.ldm.lumina.controlnet.ZImage_Control(device=model_device, dtype=dtype, operations=comfy.ops.manual_cast, config)

    # Support latest Z-Image Control model: filter out size mismatches and missing keys
    # ... filtering process ...

Code Explanation (Line by Line)

Lines 267-272: Dynamic Layer Count Detection
# Check for 2.0 or 2.1 by counting control_layers
n_control_layers = 0
for k in sd.keys():
    if k.startswith('control_layers.') and '.adaLN_modulation.0.weight' in k:
        layer_idx = int(k.split('.')[1])
        n_control_layers = max(n_control_layers, layer_idx + 1)

Explanation:

  • Iterates through all keys in the state dictionary

  • Finds keys matching the pattern control_layers.X.adaLN_modulation.0.weight

  • Extracts the layer index X and sets n_control_layers to the maximum index + 1

  • Version 2.0 has 15 layers (indices 0-14), version 2.1 may have a different count

  • This dynamic detection works for both versions

Lines 274-276: Version 2.0 Fallback Detection
# Fallback to 2.0 detection if dynamic count fails
if n_control_layers == 0 and 'control_layers.14.adaLN_modulation.0.weight' in sd:
    n_control_layers = 15

Explanation:

  • If dynamic counting fails (returns 0), checks for the presence of control_layers.14

  • If found, assumes version 2.0 and sets n_control_layers = 15

  • Ensures backward compatibility

Lines 278-285: Config Parameter Setup
if n_control_layers > 0:
    config['n_control_layers'] = n_control_layers
    config['additional_in_dim'] = 17
    config['refiner_control'] = True
    ref_weight = sd.get("control_noise_refiner.0.after_proj.weight", None)
    if ref_weight is not None:
        if torch.count_nonzero(ref_weight) == 0:
            config['broken'] = True

Explanation:

  • Sets n_control_layers based on detected count (supports both 2.0 and 2.1)

  • additional_in_dim=17: Additional channels for inpaint mask (16 base channels + 1 mask channel)

  • refiner_control=True: Enables refiner support

  • broken flag: If refiner weight is all zeros, sets broken=True to handle edge cases

Line 287: Model Creation
model = comfy.ldm.lumina.controlnet.ZImage_Control(device=model_device, dtype=dtype, operations=comfy.ops.manual_cast, config)

Explanation:

  • Creates the model with the calculated config parameters

  • Initializes with the correct layer count for version 2.0 or 2.1

Fix Effects

Before Fix:

  • Only supported version 2.0 (fixed 15 layers)

  • Version 2.1 models failed to load correctly

After Fix:

  • ✅ Version 2.1 Support: Dynamically detects layer count, correctly loads version 2.1 models

  • ✅ Version 2.0 Compatibility: Existing version 2.0 models continue to work

  • ✅ Better than Standard: Standard version only supports 2.0, custom version supports both 2.0 and 2.1

Summary

  • Problem: Could not handle version 2.1's layer count

  • Solution: Dynamically count control_layers and set config accordingly

  • Result: Now supports both version 2.0 and 2.1 models

The Z-Image-Turbo-Fun-Controlnet-Union-2.1 model can now be loaded correctly.

同じカテゴリの記事

篠原 沙世

アニメとゲームの新作情報を担当。制作会社・配信元の公式告知と登壇情報を照合して記事化します。

おすすめの関連記事