Repository navigation
Description
Problem / Background
Temp-0 output of Qwen3 changes from run to run on GB10 (NVIDIA GB10, sm_121). Greedy decoding of a primary model family should be byte-identical across processes, and the project's verification practice depends on byte-identical before/after greedy comparisons, which this makes unusable for Qwen3. It was first noticed while verifying PR #2117 (#2090): the unchanged binary's Qwen3-1.7B output diverged between three runs at characters 1300 to 1351, so a before/after parity comparison was impossible for that model (TECHNICAL_REPORTS/2117-penalty-history-token-order-20261006.en.md, section 5).
Both earlier GB10 greedy-nondeterminism reports were real defects: #910 (Gemma 4 temp-0 nondeterminism, a cp.async race in qmm_sm80 on sm_121) and #817/#824 (DeepSeek-V2-Lite). For the latter, PR #829 overlaid patches/mlx/backend/cuda/rms_norm.cu to revert an assumed upstream small-axis RMSNorm regression, and #831 then found the overlay itself was the defect (its two-stage reduction read past its shared scratch) and deleted it, so the current build uses the pinned upstream kernel (docs/upstream/mlx-cuda-rmsnorm-small-axis-regression.md, src/loading/mod.rs:512-530).
Measurements
All on 2026-10-06, GB10, main at d253154, release build target/release/mlxcel, quiet host (load 1.0), each run a separate process under gpu-lock. Command: mlxcel generate -m models/mlx/<model> -p "<story prompt> /no_think" -n 800 --temp 0, prompt from docs/benchmark_results/data/prompt-lookup-governor-gb10-2026-10-02/harness/prompts/story.txt.
| Model | Runs | Default | MLX_USE_CUDA_GRAPHS=0 |
|---|---|---|---|
| Qwen3-1.7B-4bit | 4 | 4 distinct outputs, first divergence vs run 0 at chars 1286, 1149, 1149 | 4 distinct, divergence at char 1075 |
| Qwen3-0.6B-4bit | 3 | 2 distinct, divergence at char 1308 | same |
| Llama-3.2-1B-Instruct-4bit | 3 | byte-identical | byte-identical |
| Gemma-3-1B-it-4bit | 3 | byte-identical | byte-identical |
CUDA graphs are not the cause.
The existing gate tests/cuda_qmm_determinism.rs defaults to models/llama-3.2-1b-instruct-4bit (line 47), which is deterministic here, so it cannot catch this. It also repeats within one process (10 iterations, line 73), while the failure above is observed across processes. #1558 separately reports that the test skips instead of failing when its checkpoint is missing (lines 68-71).
Leads (hypotheses, not conclusions)
- Qwen3 applies per-head q_norm/k_norm RMSNorm over head_dim 128 (
src/models/qwen3.rs, graph path at decode by default; the fused QKV+norm+RoPE path at lines 280-300 is opt-in viaMLXCEL_FUSED_QK_NORM, off by default persrc/lib/mlxcel-core/src/layers.rs:676). Gemma 3 also normalizes q/k, but over head_dim 256. This is the same small-axis CUDA RMSNorm dispatch band fix(cuda): restore DeepSeek-V2 output by reverting the regressed RMSNorm kernel #829/fix(cuda): make incremental decode graph-capture-safe for DeepSeek-V2 and Gemma 4 to re-enable CUDA graphs #831 dealt with. - Quantized matmul kernels at Qwen3's shapes (the bug: Gemma 4 26B A4B temp-0 decode is non-deterministic run-to-run and corrupts near-tied tokens independently of the fused MoE kernel #910 class).
- Any reduction using atomics or split-K, or a per-process algorithm or dispatch choice.
Localize before changing kernels: (1) whether the difference reproduces in-process (the test's loop, pointed at a Qwen3 checkpoint with MLXCEL_TEST_QMM_MODEL_DIR) or only across processes; (2) whether prefill logits already differ or only decode steps (hash per step, as the test does); (3) bisect by op, for example by routing the per-head norm through a reference path.
Scope
- In scope: GB10 (sm_121) CUDA, Qwen3 dense 4-bit checkpoints, the determinism gate.
- Out of scope: Metal, ROCm, sm_70 (owned by fix(cuda/quant): quantized prefill is not bitwise reproducible on sm_70, and its determinism test never runs #1558).
Acceptance Criteria
- A written root cause: the op and kernel that produce run-to-run differences for Qwen3 on GB10 and the mechanism (race, atomics, uninitialized memory, nondeterministic algorithm choice), or a justified, evidenced finding that it is inherent and bounded.
- If a defect is found and fixed: Qwen3-0.6B and Qwen3-1.7B 4-bit greedy output byte-identical across at least 5 separate runs of the command above, Llama and Gemma still identical, and no decode throughput regression beyond noise (measured once at the end on a quiet host).
-
tests/cuda_qmm_determinism.rs(or a sibling test) covers a Qwen3 checkpoint so the gate would have caught this, including a cross-process check if the failure does not reproduce in-process; coordinate with fix(cuda/quant): quantized prefill is not bitwise reproducible on sm_70, and its determinism test never runs #1558, which makes the test fail instead of skip when its checkpoint is missing. - The fix is integrated in the real decode path (
mlxcel generate,run, chat, and the server), not a standalone kernel.
Verification
for m in qwen3-1.7b-4bit qwen3-0.6b-4bit llama-3.2-1b-instruct-4bit gemma-3-1b-it-4bit; do
for graphs in default off; do
for i in 1 2 3 4 5; do
env $([ "$graphs" = off ] && echo MLX_USE_CUDA_GRAPHS=0) \
gpu-lock run --tag qwen3-determinism -- target/release/mlxcel generate \
-m "models/mlx/$m" -p "$(cat docs/benchmark_results/data/prompt-lookup-governor-gb10-2026-10-02/harness/prompts/story.txt) /no_think" \
-n 800 --temp 0 2>/dev/null \
| sed '1,/^Generating\.\.\.$/d; s/\[Generated [0-9]* tokens in .*$//' > "/tmp/det-$m-$graphs-$i.txt"
done
echo "$m graphs=$graphs: $(sha256sum /tmp/det-$m-$graphs-*.txt | cut -d' ' -f1 | sort -u | wc -l) distinct output(s)"
done
doneA pass is 1 distinct output(s) for every model and both graph settings. Also run cargo test --release --features cuda --test cuda_qmm_determinism with the new Qwen3 coverage.
Related: #2090, PR #2117, #910, #817, #824, PR #829, #831, #1558.
Activity
Jaeyeong-CHOI commentedon Oct 6, 2026
Root cause identified: order-dependent softmax denominator reduction in cuDNN
I investigated this on GB10 and traced the captured Qwen3 decode nondeterminism to unordered cross-warp FP32 accumulation of the softmax denominator inside cuDNN.
Four warps accumulate partial sums through a shared-memory CAS loop. This prevents lost updates, but does not fix the addition order. With the captured inputs, different orders change the denominator by one FP32 ULP. The subsequent approximate reciprocal and normalization put the output exactly at the BF16 midpoint or immediately below it, explaining the two originally observed outputs.
Reproduction
sm_121), CUDA 13.0, mlxcel based253154c.[1,16,1,128], K/V backing[1,8,512,128], live KV length 259.Causal verification
This goes beyond observing that disabling cuDNN helps:
0123versus0132, each stable over 1,000 samples. The GPU reciprocal/multiply results were checked, then converted using exact BF16 round-to-nearest-even.The in-kernel intervention specifically covers the 9.24.1 FP32-output diagnostic variant; it was not repeated on 9.13. The two in-kernel fixed orders both round to original BF16 A; the separate captured-operand replay demonstrates the A/B split. These are distinct controls, not a claim that the ascending/descending in-kernel pair directly produced BF16 A/B.
Workaround and remaining scope
A separate candidate patch routes BF16, single-query, D128 attention through the existing MLX fallback while retaining cuDNN for multi-query prefill. Both Qwen3 models were stable in five fresh CLI processes per graph setting; re-enabling cuDNN in the same candidate binary restored variability.
This is a workaround, not a cuDNN implementation fix. The diagnostic kernel modification is not intended for deployment.
I am not claiming all acceptance criteria are complete: additional Llama variability exists in the unmodified baseline, its internal cause remains unresolved, and candidate server/chat behavior and production throughput still need validation.
Supporting material
These links are pinned to an immutable evidence commit in my fork. No cuDNN binaries or modified cubins are distributed. The original archive documentation retains its packaging-time status; the overview clarifies the subsequent publication and investigation timeline. No fix PR or production deployment is included in this report.
inureyes commentedon Oct 6, 2026
Root cause found, and the fix is scoped to an opt-in switch by maintainer decision.
Mechanism. From 256 KV positions a one-row decode SDPA goes to cuDNN (
use_cudnn_for_decoding: k and v unsliced to the cache buffer, true lengths through a padding mask). On GB10 with cuDNN 9.27 the heuristics' first engine for the head_dim 128 graph iseng8_k40=0_k41=2_k24=2_k38=1_k27=0; knob 38 isCUDNN_KNOB_TYPE_STREAM_K, which combines partial key reductions in completion order. In isolation (q[1,16,1,128], k/v 300 rows of a 512-row buffer) it gave 2 distinct outputs in 4000 identical calls, differing by at most 7.6e-6 (ULP level, so summation order, not wrong data). The stream-K-off candidate..._k38=0_...gave 1. cuDNN does not tag the engine NONDETERMINISTIC, sodeselect_numeric_noteskeeps it. In the model, logits were bit-identical through prefill and the first 224 decode steps and diverged at steps 225 to 247 (prompt 34 tokens, cache just past 256), in-process as well as across processes. Not the KV in-place write (#1959) and not CUDA graphs: both toggles left it unchanged.Why opt-in. Stream-K is also what makes long-context decode fast. Decode throughput relative to today's default on GB10:
sdpa_vectorThe default stays fast (and nondeterministic).
MLXCEL_SDPA_DETERMINISTIC=1routes one-row decode tosdpa_vectorand bars stream-K engines on the remaining cuDNN graphs. With it, 5 separate processes of the issue's command gave one output each for Qwen3-0.6B, 1.7B and 4B, Llama-3.2-1B and Gemma-3-1B, with CUDA graphs on and off, and a 7366-token prompt was identical over 3 runs. The second acceptance criterion therefore holds under the switch, not by default; parity and determinism work should set it, and the CUDA determinism tests set it themselves.fix(cuda): fall back when no stream-K-off SDPA plan builds
inureyes commentedon Oct 6, 2026
@Jaeyeong-CHOI thank you for the analysis above. I missed it before posting the previous comment, and it goes further than mine. The two fit together: the engine the heuristics pick for this graph is the stream-K one, and your in-kernel intervention locates the order dependence in its unordered cross-warp reduction of the softmax denominator. The stream-K-off candidate I compared also differs in tile and kernel-config knobs, so knob 38 is the correlate I measured, not an isolated cause. PR #2162 now says so and cites your analysis in the code comment,
docs/environment-variables.mdand the upstream draft.PR #2162 takes the same direction as your workaround (one-row decode off cuDNN, prefill kept on cuDNN), as an opt-in
MLXCEL_SDPA_DETERMINISTIC=1rather than the default, because cuDNN's engine is also the fast long-context decode here (default-path numbers in the comment above). With the switch, decode runssdpa_vector, including its two-pass kernel past 1024 keys. On the default path, 1500 of 2048 keys differed in 446 of 2000 calls.On Llama: 5 separate processes of an 800-token generation were byte-identical here on both paths, so I could not reproduce the variability you saw; if you have the command and prompt, it would be worth its own issue. Separately,
mlxcel-servergave the same output for a request across restarts in both modes, but a repeat served from the prompt-prefix cache differs from the first, uncached run. That is a different path and is not covered by this PR.fix(cuda): add MLXCEL_SDPA_DETERMINISTIC for reproducible decode (lab…
fix(vision): turn pictures upright by their EXIF orientation (#2993)