[BugFix] Treat negative expert ids as empty routing slots in GGUF MoE - #133
Closed
KaigeGao1110 wants to merge 1 commit into
Closed
KaigeGao1110 wants to merge 1 commit into
KaigeGao1110 wants to merge 1 commit into
Conversation
vLLM's top-k router writes id -1 for padding tokens (VLLM_MOE_SKIP_PADDING is on by default) and its own MoE kernels skip those slots. The GGUF kernels index expert weights directly by id, so -1 reads the bytes before the expert tensor; in the engine's profile run, where every token is padding, that is an illegal memory access at startup. Send empty slots to expert 0 with weight 0 instead, so they contribute nothing. Done out of place and without a host sync, leaving the caller's ids untouched. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Author
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.
The bug
vLLM's top-k router writes expert id
-1for padding slots (VLLM_MOE_SKIP_PADDING, on bydefault) and its own fused-MoE kernels skip those slots. The GGUF kernels index expert
weights directly by id, so
-1reads the bytes immediately before the expert tensor.This is an out-of-bounds read on the device. In vLLM's memory-profiling run every token is
padding, so it is an illegal memory access during engine startup, before the model has
served anything. We hit it on the first full load of
unsloth/Qwen3.8-Flash-Next-GGUFUD-IQ4_XS.
It reaches every path below
_fused_moe_gguf: theggml_moe_a8tile GEMM (throughmoe_align_block_size), theggml_moe_a8_vecper-row kernel, and the dequantize fallbackloop, since all three consume
topk_idsas given.The fix
Map empty slots onto expert 0 with weight 0, so they contribute nothing to the sum:
Done out of place, so the caller's tensors are untouched, and with no host sync, so it is
safe under CUDA graph capture. Three elementwise ops on a
(tokens, top_k)tensor; thecost is not measurable next to the GEMMs that follow.
Expert 0 is an arbitrary in-range choice — any valid id works, because the weight is zero.
Test
tests/test_moe_padding_ids.pybuilds a small Q8_0 expert set and runs_fused_moe_ggufwith two kinds of padding at once: a block of fully-padded tokens (every slot
-1) and onetoken with a single empty slot. It then asserts, in order:
count_nonzero == 0), not merely afinite value;
weight set to zero, so the empty slot contributed nothing rather than something small;
topk_idsandtopk_weightscome back unmodified, because vLLM records those tensors forreplay and this fix must not write through them.
Parametrised at 16 and 128 tokens so both the per-row path and the
x.shape[0] > 64tilepath are exercised.
Evidence
Run on 1x RTX PRO 6000 Blackwell WE, torch 2.9, vLLM 0.28.1rc1.dev628+g2a02f6efe, with this
branch's
fused_moe.pyand then withmain's, everything else identical:Scope
Eight lines in one function, plus the test. No kernel, no build, no ABI change. When the
router emits no negative ids the mask is all-false and the result is unchanged.
🤖 Generated with Claude Code