Skip to content

fix(trace): score logit_trace rows in blocks so a 9216-wide forward fits - #3135

Merged
inureyes merged 2 commits into
mainfrom
fix/issue-3132-llama4-long-prefill-oom
Nov 16, 2026
Merged

inureyes merged 2 commits into
mainfrom
fix/issue-3132-llama4-long-prefill-oom

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

The 9216-wide Llama 4 Scout trace did not run out of memory in the model forward. It ran out in examples/logit_trace, which scored every row of the chunk over the 202,048-entry vocabulary at once. The harness now scores 1024 rows at a time. No model code changed.

Instrumentation (M5 Max, 128 GB)

I read MLX active and peak memory per layer stage (reset_peak_memory after each readout) using temporary env-gated readouts on a main build:

Stage Width Peak GB before Peak GB after
Weights loaded 9216 60.74
Largest per-layer stage (layer 47 MoE FFN) 9216 64.03
Forward as one lazy graph through lm_head 9216 67.38
Forward as one lazy graph through lm_head 16384 72.60
Trace scoring 4608 85.79
Trace scoring 9216 Metal OOM 71.18
Trace scoring 16384 75.48

No layer stage peaks more than 0.9 GB above the memory live before it. The chunked layers take no dense mask, and neither the MoE intermediates nor the cache state grows with width. The allocation responsible was emit_rows_to: its f32 cast, log-softmax and argpartition over all rows added 22.3 GB at 4608 rows and about twice that at 9216. With blocks it adds 4.8 GB at any width.

Output

Every scoring step is per row. The two-4608 trace is byte-identical before and after this change. Comparing one 9216-wide forward against two 4608-wide forwards with scripts/compare_logit_traces.py --decided 2: 0 of 3651 decided positions disagree (0.000%). Overall top-1 disagreement is 0.358% (33 positions, all with a reference gap under 0.44, 32 of them at rank 2). Perplexity is 11.7995 against 11.7967.

mlxcel generate --temp 0 at 8,584 prompt tokens wrote the same 64 tokens with MLXCEL_PREFILL_CHUNK=0 as with the default chunking. At 15,563 tokens a single-pass prefill also completed, and its summary is coherent. Both runs decode past 8192.

Results are in docs/benchmark_results/llama4-long-prefill-memory-m5max-2026-11-16.md, with the CSVs beside it.

Test plan

  • cargo test --profile test-fast --features metal,accelerate --lib models::llama4 (new a_one_row_prefill_past_the_chunk_builds_no_dense_chunk_mask)
  • cargo test --profile test-fast --features metal,accelerate --example logit_trace (new blocked_scoring_matches_whole_chunk_scoring)
  • cargo clippy -p mlxcel --lib --tests --example logit_trace --features metal,accelerate -- -D warnings, cargo fmt --check

Closes #3132

A single 9216-wide Llama 4 Scout forward through examples/logit_trace aborted with a Metal command-buffer out-of-memory error. Per-layer peak-memory readouts show the forward is not the cause: run as one lazy graph it peaks at 67.4 GB against 60.7 GB of weights, 3.3 GB of it the returned logits. The excess was emit_rows_to casting, log-softmaxing and partitioning every row over the 202,048-entry vocabulary at once, 22.3 GB above the forward at 4608 rows and twice that at 9216.

The harness now scores 1024 rows at a time. Every step is per row, so traces are byte-identical; a single 9216-wide forward scores at 71.2 GB and a 16384-wide one at 75.5 GB. A new harness test checks blocked against whole-chunk scoring, and a Llama 4 test pins that a one-row prefill past the attention chunk builds no dense chunk mask.

Single 9216-wide forward against two 4608-wide forwards: 0.000% decided-position disagreement (0 of 3651), 0.358% overall top-1, perplexity 11.7995 vs 11.7967. Results in docs/benchmark_results/llama4-long-prefill-memory-m5max-2026-11-16.md.

Closes #3132
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:models Model architectures, weights, loading, metadata status:review Under review labels Nov 16, 2026
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 16, 2026
@inureyes
inureyes marked this pull request as ready for review November 16, 2026 12:49
@inureyes
inureyes merged commit 7721da9 into main Nov 16, 2026
32 checks passed
@inureyes
inureyes deleted the fix/issue-3132-llama4-long-prefill-oom branch November 16, 2026 12:49
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(llama4): a single 9216-token Llama 4 Scout prefill forward runs out of Metal memory

1 participant