Skip to content

Studio MiniMax-H3: run the Qwen3-VL vision tower in float32 as the reference does - #17835

Merged
danielhanchen merged 2 commits into
mainfrom
studio-h3-vision-tower-fp32
Nov 15, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-h3-vision-tower-fp32

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Summary

MiniMax-H3 (video) and MiniMax-H3-Image condition on a Qwen3-VL text encoder whose vision tower turns reference pictures and keyframes into vision tokens. The reference implementation runs that tower in float32: it feeds float32 pixels and casts the bf16 weights up to the input per op. Studio ran it in bf16 (pixels rounded to bf16, bf16 matmuls), which put the vision tokens 4 to 7% (median) away from the reference's, with hundreds of tokens off by more than 30%.

This PR runs the vision tower the reference's way, scoped to the tower only:

  • every vision linear, convolution and layer norm computes in float32 on the bf16 weights (a temporary upcast per op, no float32 copy kept);
  • pixels stay float32 all the way into the patch embedding;
  • the position grid is interpolated in the table dtype, as the reference does;
  • the merged rows and DeepStack features leave the tower in the dtype they did before, so the language model layers are unchanged.

UNSLOTH_H3_VISION_FP32=0 restores the bf16 tower bit for bit.

Changes

  • studio/backend/core/inference/h3_vision_fp32.py (new): h3_vision_float32(text_encoder) wraps get_image_features / get_video_features of the Qwen3-VL model for one call, under a torch function mode that upcasts low-precision weights for linear / layer_norm / conv3d when the input is float32 and keeps the pixel tensor's own downcasts in float32. On CUDA the tower's float32 matmuls run without TF32, the reference's default. Transformers releases that sum the position-embedding taps in float32 get the reference's bf16 sum swapped in (checked against transformers' own value; releases with fast_pos_embed_interpolate, including Studio's pin, already do it the reference's way). Diffusers' MiniMax-H3 steps round pixels to the encoder dtype before the model call; Studio's steps offer their float32 pixels, and the wrapper uses them only when the rounded input equals that source rounded.
  • video_minimax_h3_sampling.py: the FL2VA and Ref2VA text-encoder steps run their conditioner call under h3_vision_float32 and offer their float32 pixels.
  • diffusion_minimax_h3_image.py: qwen3vl_hidden_state passes float32 pixels and runs the conditioner under h3_vision_float32.
  • tests/test_h3_vision_fp32.py (new).

Results

Vision tokens vs the reference (CPU, real weights)

The real Qwen3-VL vision tower of the MiniMax-H3 conditioner (bf16, 595M parameters), run three ways on the same float32 pixels: the reference implementation's own tower, Studio before, Studio after. Per merged vision token, relative L2 error against the reference's tokens, both sides in bf16 (what the language model receives).

Input Arm median p99 max tokens over 5% min cosine
1 keyframe 1344x768 (1,008 tokens) before 5.7e-2 4.4e-1 7.3e-1 572 0.781
after 9.7e-5 4.6e-4 7.3e-4 0 1.000
1 reference 1024x1024 (1,024 tokens) before 6.6e-2 5.7e-1 3.0 630 0.596
after 1.1e-4 4.3e-4 9.6e-4 0 1.000
3 references (2,560 tokens) before 3.9e-2 5.0e-1 3.0 1,064 0.241
after 8.6e-5 4.2e-4 9.6e-4 0 1.000

The DeepStack features follow the same pattern (median 2e-2 to 6e-2 before, 5e-5 to 1e-4 after). With the kill switch the output is bit-identical to before on all three inputs. Keeping the pixels in float32 matters: the float32 tower fed bf16-rounded pixels lands at a 0.9 to 1.7% median.

MiniMax-H3-Image, Colab G4 (RTX PRO 6000), bf16 on both sides

LPIPS to the reference render (same picture, prompt and seed). Before = the tree this change sits on (the MiniMax-H3 and MiniMax-H3-Image reference-default changes now on main, including the decoder-dtype decode fix), after = this change.

Render before after
1 reference, 1536x1024 (e1) 0.089 0.099
3 references, 1536x1024 (e3) 0.144 0.142
1 reference, template size 2496x1664 (c1) 0.022 0.010
3 references, template size 2496x1664 (c3) 0.292 0.169
text to image 2048 (t2) 0.056 0.056 (no vision, pixel-identical)

Conditioner output against the reference's own conditioning for the same render (first DiT call):

before after
1 reference: context relative L2 0.173 0.038
1 reference: vision tokens, median 4.9% 2.3%
1 reference: tokens over 30% 108 / 1,566 3 / 1,566
3 references: context relative L2 0.233 0.047
3 references: vision tokens, median 6.5% 2.3%
3 references: tokens over 30% 596 / 4,167 16 / 4,167

A/A: before/before, after/after and before/kill-switch are pixel-identical on every render, so every nonzero distance above is the change. e1 moves 0.009 further from the reference while its conditioning moves 4.5x closer; c1 uses the same conditioning and halves.

Cost (encode of a request with new pictures; repeats hit the prompt cache): conditioner 0.45 -> 0.67 s for 1 reference and 1.04 -> 1.58 s for 3, which is +3% of the render (7.5 -> 7.7 s and 14.9 -> 15.5 s). Peak allocated memory during the conditioner +0.02 / +0.05 GB; render peak unchanged (70.24 / 71.21 GB).

MiniMax-H3 video, Colab G4

main (with the default int8 conditioner) against this branch, int8 denoiser, 20 steps, 1344x768, 124 frames, 3 seeds each, one fresh process per arm on one VM (torch 2.13.0+cu130, transformers 5.5.4, diffusers 0.42.1). LPIPS to the reference render of the same seed, every 4th frame; lower is closer.

Job Seed before after
fl2va (first and last frame) 4242 0.0276 0.0245
fl2va 11 0.1436 0.1455
fl2va 777 0.1148 0.1143
ref2va_long (reference picture and video) 4242 0.323 0.365
ref2va_long 11 0.2993 0.3415
ref2va_long 777 0.4313 0.3315

Means: fl2va 0.0953 -> 0.0948, ref2va_long 0.3512 -> 0.3460; closer on 3 of 6, so on these renders the change is a wash in LPIPS. Single renders move both ways because the trajectories are chaotic; the direct measure is the conditioning itself: fl2va conditioning against the reference's own conditioning goes from relative L2 1.026 to 0.625 and cosine 0.541 to 0.814, the same on all 3 seeds (the rest of the gap is the int8 conditioner; the reference conditioning comes from its nvfp4_awq file). Likeness of ref2va_long to its reference picture (DINOv2 cosine) is closer on 2 of 3 seeds (0.6625 / 0.2304 / 0.3503 -> 0.6521 / 0.2951 / 0.4253).

Unchanged where it should be: text-only video (t2va) gives identical prompt embeddings, final latents and frames before and after, and UNSLOTH_H3_VISION_FP32=0 reproduces main exactly on fl2va, ref2va_long and t2va. Two main runs are bit-identical, so every difference above is the change.

Job conditioner s, before -> after render s, before -> after peak allocated in the conditioner, GiB
fl2va 0.52 -> 0.75 190.59 to 190.71 -> 190.74 to 190.98 49.66 -> 49.66
ref2va_long 0.70 -> 0.95 308.87 to 309.21 -> 309.41 to 309.50 50.24 -> 50.24

The tower costs about +0.25 s per request with pictures (+0.1 to +0.2% of the render) and no allocated memory.

Tests

  • tests/test_h3_vision_fp32.py, 12 tests: vision leaves compute in float32 while the decoder layers receive and return bf16; parameters keep their dtype and storage and nothing stays patched; the kill switch is bit-identical to the bf16 tower; text-only prompts are bit-identical; an offered float32 source replaces rounded pixels only when it matches; the reference position-embedding sum against a transcription of the reference; the step wiring for Ref2VA, FL2VA and MiniMax-H3-Image. Pass on transformers 5.17 / torch 2.14 and transformers 4.57.6 / torch 2.6.
  • MiniMax-H3 and MiniMax-H3-Image backend suites: 1697 passed, 160 skipped, 4 failed; the 4 (test_h3_ref_encode.py::test_chunked_reference_matches_stock_bit_for_bit[22|34|39|56]) fail identically on the merge base.
  • prepush_gate.py: PASS.

Not covered

  • The CPU parity on real weights ran on transformers 5.17 (the release whose position sum is replaced). On Studio's pin (5.5.4) the interpolation is transformers' own, which is the reference's algorithm; the GPU runs used 5.5.4.
  • The video results cover 3 seeds per job, too few to call a direction on single renders; the conditioning distance is the measure that moves consistently.
  • The replaced position-sum code does not run on Studio's pinned transformers 5.5.4 (it engages from the release that changed the sum, tested on 5.17 on CPU).
  • fp16 is checked on CPU only (vision rows 6.29e-4 -> 3.91e-4 from a float32 oracle, finite); a T4 render of this change was not run.
  • No AMD or Apple render (code read: ROCm takes the same path, MPS skips the TF32 switch); CPU-only torch 2.6 imports and engages it.

…eference implementation does

The vision tower of the MiniMax-H3 conditioner (video and MiniMax-H3-Image)
now runs every linear, convolution and layer norm in float32 on its bf16
weights, cast per op, with the pixels kept in float32 and the position grid
interpolated in the table dtype. The language model layers are unchanged and
receive the vision rows in the dtype they did before.
UNSLOTH_H3_VISION_FP32=0 restores the bf16 tower.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T14:17:51.517148Z 282e485 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 282e48500b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Nov 15, 2026
…ource lint red on main) (#17838)

## Summary

`Source lint` has been red on `main` since #17817 merged (also seen on
the staging run of #17835):

```
No new risky loader call sites: Failed
1 risky loader call site(s) not in the baseline:
  unsloth/models/_block_compile.py:1364  [dynamic-import] import_module
```

`_band_kernel_check` loads the band-attention kernel module through
`importlib.import_module(_LH_MODULE)`. The risky-loader lint flags a
dynamic import whose module name is not written at the call site. Here
the name is the constant `"unsloth.kernels.large_head_attention"`, so
nothing is actually dynamic.

## Change

`from unsloth.kernels import large_head_attention as lh` replaces the
`importlib` call. It stays in the same place, so the module is still
loaded lazily, only for a layer whose sliding-window head dim qualifies,
and still inside the `try/except` that falls back to the stock attention
path. No baseline entry is needed. `sys.modules[_LH_MODULE]` lookups
elsewhere are unchanged.

## Verification

On a clean export of the tree:

| | `scripts/lint_risky_loaders.py` |
|---|---|
| main | 1 call site not in the baseline (`_block_compile.py:1364`) |
| this branch | ok: 117 risky loader call sites, all recorded |

`tests/test_block_compile.py tests/test_large_head_attention.py
tests/security/test_lint_risky_loaders.py`: 120 passed, 68 skipped
(CUDA-only cases).
[skip ci]
@danielhanchen
danielhanchen merged commit d2fba56 into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the studio-h3-vision-tower-fp32 branch November 15, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant