opencl: make the MoE expert scatter deterministic - #26464
Draft
wanghqc wants to merge 1 commit into
Draft
Conversation
kernel_moe_scatter assigns each token's slot within an expert via atomic_inc, so the packing order depends on which work-item wins the atomic. The ragged MoE prefill GEMM is sensitive to that order, so prompt processing is not reproducible: the same binary on the same prompt returns one of several outputs (Adreno X2-90, granite-3.0-3b-a800m: 2-3 distinct outputs over five greedy runs, diverging ~30 tokens in). Dense models and token generation are unaffected. Assign the slot instead as the token's rank in flat (n, k) order among the tokens routed to the same expert - a fixed function of the routing input. One workgroup per expert ranks its tokens with a workgroup scan. The scatter runs only on the prefill GEMM path. Cost on the X2-90: pp512 -0.6% (granite-3.0-3b-a800m), -0.2% (Qwen3-30B-A3B); token generation never calls it. test-backend-ops MUL_MAT_ID 383/383. GGML_OPENCL_MOE_STABLE_SCATTER=0 restores the atomic version.
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.
Overview
This PR is to fix a bug in MoE scatter.
Root cause
kernel_moe_scatterassigns each token its slot within its expert viaatomic_inc(&slot_counter[expert]), so the token→slot packing depends on which work-item wins the atomic and varies every run.Fix
Additional information
Determinism verification
Cost
Prefill only — the scatter runs on the GEMM path, decode never calls it: pp512 −0.63% on granite-3.0-3b (40 experts), −0.15% on Qwen3-30B-A3B (128 experts).
Scope
Testing
test-backend-ops -o MUL_MAT_ID: Adreno X2-90, 383 OK / 0 FAIL.
The Adreno 840 declines all MUL_MAT_ID cases at these shapes in test-backend-ops; its evidence is the real-model determinism runs above.
Requirements