Fix `RuntimeError` shape mismatch when using `ref2va` with audio inputs

#29
by cmhacks - opened

Hi!

I ran into a tensor shape mismatch error when running MiniMaxH3ReferenceToVideo with a standalone audio reference input (ref_audios).

Issue

When passing audio conditioning along with reference images into ref2va, x.shape[0] becomes 3 ($M=3$). During the forward pass, line 235 in __init__.py throws a RuntimeError:

File "custom_nodes/ComfyUI-MiniMax-H3-Turbo/__init__.py", line 235, in forward
    x = x + (bv @ (av @ sv.T)).T
RuntimeError: The size of tensor a (3) must match the size of tensor b (2) at non-singleton dimension 0

Cause

The Turbo LoRA patch matrix (bv @ (av @ sv.T)).T evaluates to $M=2$, but x carries 3 batch/modulation segments when audio is present, causing element-wise addition to fail on dimension 0.

Proposed Fix

Dynamically matching delta.shape[0] to x.shape[0] before addition resolves the shape mismatch and allows generation to complete smoothly with audio enabled.

In ComfyUI-MiniMax-H3-Turbo/__init__.py around line 235:

# Replace: x = x + (bv @ (av @ sv.T)).T

delta = (bv @ (av @ sv.T)).T
if delta.shape[0] != x.shape[0]:
    if delta.shape[0] < x.shape[0]:
        repeat_count = x.shape[0] - delta.shape[0]
        delta = torch.cat([delta, delta[-1:].repeat(repeat_count, 1)], dim=0)
    else:
        delta = delta[:x.shape[0]]
x = x + delta

Tested on ROCm / PyTorch 2.x and confirmed it fixes the sampler crash. Let me know if you'd like a PR for this!

I have the same error. Tried making the changes you said and I get an import error.
Probably would be better to upload the fixed working file or let the author update it themselves.

Kimi K3 has perfectly solved
The pruned base model you used embedded the time into a curve, and the plugin needs to inject the LoRA's adaln update based on the internal timestamps of the model each time it performs a forward pass. The model itself (in comfy/ldm/minimax/model.py) reserves one timestamp line for each type of condition:

Text/Video line: t_v; Audio line: t_a
Visual conditions (keyframes/reference images/reference videos): pinned at max(t_v, 0.999)
Audio reference (ref_audio): pinned at max(t_a, 1.0) ← The plugin missed this line
The original _unique_t in the plugin only calculated the timestamp line for visual conditions. Once audio reference is added (independent reference audio or reference video with its own audio track), the model has 3 lines, while the plugin only prepares 2 lines. When adding adaln, the number of lines does not match and the plugin crashes directly - exactly as you saw in the error message.

Fixes (init.py)
Rewrote _unique_t: iteratively mirrored the calculation of unique_t in the model's internal part (float32 tensor operations followed by conversion to float to ensure consistent behavior on both sides), added the audio reference line, and supported custom values for audio_cond_noise_aug / visual_cond_noise_aug.
Added _payload_cond_kinds: precisely determines in minimax_payload whether there is a timestamp line for visual conditions / audio reference in this forward pass (mirroring the segmentation rules of PackedLayout).

屏幕截图 2026-08-09 145101

屏幕截图 2026-08-09 145142

Hi,
the patch proposed by RKFG works well for me. I can use a wav as a reference audio and have minimax h3 clone the voice for my video : ManA says in french "salut tout le monde. Match the voice in Audio 1.

Patch description : https://github.com/Larryvrh/ComfyUI-MiniMax-H3-Turbo/issues/3

Condition : I'm using the latest version of ComfyUI-MiniMax-H3-Turbo [7 august 2026], installed with git clone from github, with comfyui / int8 pruned / step600 v4.

Sign up or log in to comment