Repository navigation
Studio MiniMax-H3: run the Qwen3-VL vision tower in float32 as the reference does - #17835
Merged
Merged
Conversation
…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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
UNSLOTH_H3_VISION_FP32=0restores the bf16 tower bit for bit.Changes
studio/backend/core/inference/h3_vision_fp32.py(new):h3_vision_float32(text_encoder)wrapsget_image_features/get_video_featuresof the Qwen3-VL model for one call, under a torch function mode that upcasts low-precision weights forlinear/layer_norm/conv3dwhen 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 withfast_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 underh3_vision_float32and offer their float32 pixels.diffusion_minimax_h3_image.py:qwen3vl_hidden_statepasses float32 pixels and runs the conditioner underh3_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).
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.Conditioner output against the reference's own conditioning for the same render (first DiT call):
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.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=0reproducesmainexactly on fl2va, ref2va_long and t2va. Twomainruns are bit-identical, so every difference above is the change.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.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