Repository navigation
Fix batched causal mask (b>1) across Qwen3/GLM4/SmolLM3/Z-Image serving models - #38
Merged
Merged
Conversation
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.
Fixes the batched causal-mask under-sizing bug across the fork's serving-optimized models, by cherry-picking upstream huggingface#3780 (@aashish-thapa, addresses huggingface#3582). Commits and authorship preserved.
The bug
8 models build the causal mask as
Tensor::from_slice(&mask, (b, 1, tgt, tgt + offset), …)while the slice only holdstgt * (tgt + offset)elements — one batch element's worth. Forb > 1this under-sizes the storage:from_sliceagainst a concrete shape skips the length check, so the GPU standard-attention path (l > 1prefill) gets garbage/NaN, and CPU-flash builds fail outright. Affected:qwen3,quantized_qwen3,qwen3_moe,quantized_qwen3_moe,glm4_new,quantized_glm4,smol/smollm3,z_image/text_encoder— i.e. exactly the batched-serving path.The fix
Build the mask batch-independently as
(1, 1, tgt, tgt + offset)and letbroadcast_addfan it across the batch — correct for anyb, and cheaper than materializingbcopies. Includes a CPU regression test (qwen3::tests::causal_mask_is_batch_independent) asserting the shape, the causal-triangle values, and that broadcasting ontob > 1scores gives every row the same mask.Why huggingface#3780 (arbitration among the cluster)
Several upstream PRs target this "Qwen3 mask for batch>1" family (huggingface#3646/huggingface#3624/huggingface#3610/huggingface#3586/huggingface#3611/huggingface#3609/huggingface#3484/huggingface#3743). huggingface#3780 is the right pick for this fork:
causal_maskhelper across the same 8 files — and cherry-picks cleanly, because the fork inherited that helper from upstream.CI gate (candle's own, all green)
cargo fmt --all -- --checkcargo check --workspacecargo clippy --workspace --tests --examples --benches -- -D warningscargo test -p candle-transformers --lib causal_mask— regression + sibling mask tests pass