Repository navigation
fix(trace): score logit_trace rows in blocks so a 9216-wide forward fits - #3135
Merged
Merged
Conversation
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
Closed
5 tasks done
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.
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_memoryafter each readout) using temporary env-gated readouts on amainbuild:lm_headlm_headNo 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 andargpartitionover 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 0at 8,584 prompt tokens wrote the same 64 tokens withMLXCEL_PREFILL_CHUNK=0as 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(newa_one_row_prefill_past_the_chunk_builds_no_dense_chunk_mask)cargo test --profile test-fast --features metal,accelerate --example logit_trace(newblocked_scoring_matches_whole_chunk_scoring)cargo clippy -p mlxcel --lib --tests --example logit_trace --features metal,accelerate -- -D warnings,cargo fmt --checkCloses #3132