Repository navigation
Add an optional standalone decode benchmark - #93
ronaldmannak wants to merge 17 commits into
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 10f50ff193
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
253564b to
e0dc0cf
Compare
e36259c to
cc49616
Compare
e0dc0cf to
d4fa4f5
Compare
cc49616 to
6821d03
Compare
6821d03 to
bf09f16
Compare
4e4c889 to
4b4d11f
Compare
bf09f16 to
b166406
Compare
097fa0c to
7a4cc19
Compare
3122507 to
1793cc5
Compare
7a4cc19 to
d80c2ac
Compare
dbce3a0 to
51d71ef
Compare
d80c2ac to
4f1a7d6
Compare
Batch sizes are now a comma-separated argument, 1,4,8 by default, each timed and reported per row and in total. A mixed-lengths option gives the rows of a batch prompts that shrink evenly to a sixth of the full length, so all rows but the first are left-padded. Every batch row is compared with an untimed single-sequence run of its own prompt. This replaces the two fixed comparisons (single against batch of one, batch of one against row 0 of the batch of four) with one per row for every size. Agreement stays reported, not asserted. The alternating order now puts every batch size before or after the single run. The SINGLE_ONLY build is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With resumable prefill (PR 91), a BatchTokenIterator step can feed a prefill chunk instead of yielding tokens, and rows join one step apart. DecodeBench timed the first next() as the prefill and every later step as decode, which no longer holds. The time to first token now ends at the first .token event. The decode rate counts only the steady steps: every row decodes, none of them its first token, and no prefill chunk runs. Steps that decode while also feeding a prefill chunk are reported apart, as overlap steps with their mean time. For a model that prefills whole prompts the numbers mean what they meant before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4bf5ff07d1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| try record(try iterator.next()) | ||
| let firstToken = seconds(since: start) | ||
| let decodeStart = ContinuousClock.now |
There was a problem hiding this comment.
Establish the timing boundary from actual token events
For Llama models, which already conform to ResumablePrefillModel at this commit, the first BatchTokenIterator.next() returns only a .prefill event, not the first token of every row. resumableStep() admits at most one row per call and delivers its first token on a later call. Consequently, this boundary reports TTFT before any token is delivered and includes subsequent rows' prefill and staggered admission in the purported decode-only throughput measurement, even with the default 145-token prompts. Establish the boundary using actual token events and account for staggered row admission so the reported batch decode rate remains comparable to the single-sequence rate.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in b2269b2. The time to first token now ends at the first .token event, and the decode rate counts only steady steps: every row decodes, none of them its first token, and no prefill chunk runs. Steps that decode while also feeding a prefill chunk are reported apart as overlap steps. For a model that prefills whole prompts the numbers mean what they meant before.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b2269b2919
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } | ||
| } | ||
| return Run( | ||
| tokensPerSecond: Double(steadySteps) / steadySeconds, |
There was a problem hiding this comment.
Handle runs with no steady-state decode samples
For a resumable-prefill model such as Llama, a short run can finish its earliest rows before all requested rows begin decoding. For example, requesting 4 decode tokens with the default batch sizes leaves batch(8) with no steps satisfying the steady-state predicate, so both counters remain zero and this division reports NaN throughput. Long prompts requiring multiple prefill chunks can cause the same problem even with larger token limits. Detect the absence of steady-state samples and report the measurement as unavailable with an actionable explanation, or extend the run until a measurable steady-state interval exists.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in the next commit on this branch: with no steady decode step, DecodeBench now stops with an explanation (ask for more decode tokens than the rows and their prefill chunks take to join) instead of reporting NaN. Checked with 4 decode tokens at batch size 8 on Llama 3.2 3B.
With resumable prefill the rows join one step apart, so a short run (for example 4 decode tokens at batch size 8) can end its first rows before the last one decodes. No step then decodes every row without a prefill chunk, and the steady decode rate divided zero by zero. DecodeBench now stops with an explanation instead of reporting NaN. Found by Codex. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c02bbbc68a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| var state = seed &* 6_364_136_223_846_793_005 &+ 1_442_695_040_888_963_407 | ||
| return (0 ..< length).map { _ in | ||
| state = state &* 6_364_136_223_846_793_005 &+ 1_442_695_040_888_963_407 | ||
| return Int32(1000 + (state >> 33) % 20000) |
There was a problem hiding this comment.
Bound synthetic token IDs by the loaded model's vocabulary
For a local model with a vocabulary smaller than 21,000 entries, this generator can produce token IDs outside its embedding table; with fewer than 1,001 entries, every generated token is invalid. Small-vocabulary configurations are supported—for example, Tests/MLXLMTests/Qwen3PrefillLogitsTests.swift uses a Qwen3 configuration with vocab_size: 256. Such models receive out-of-range embedding indices during the initial warm-up rather than a valid benchmark workload. Derive the sampling range from the loaded model's vocabulary size while retaining the deterministic seed.
Useful? React with 👍 / 👎.
| let stepStart = ContinuousClock.now | ||
| guard let events = try iterator.next() else { break } | ||
| let stepSeconds = seconds(since: stepStart) |
There was a problem hiding this comment.
Include between-step GPU execution in steady decode timing
For fast models or large batches, summing only the durations of iterator.next() overstates decode throughput. BatchTokenIterator.step() enqueues the successor with asyncEval before returning the current tokens, so GPU execution continues during the untimed event-processing loop, including the per-token ids.firstIndex searches and token appends. The next timed call therefore waits for only the remaining work, while steadySeconds omits execution that already happened between calls. This also biases comparisons against runSingle, which uses a continuous elapsed interval. Measure the steady-state interval continuously across consecutive qualifying steps, including the gaps between calls, rather than summing isolated call durations.
Useful? React with 👍 / 👎.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
So developers can tune the prefill chunk size on their own hardware: - An eighth argument sets PrefillParameters.stepSize for both the single-sequence and the batch paths. - Prompt lengths `arrival[:tokens]`: each batch size is a number of active rows decoding prompts of the prompt length; after 16 steps in which every row decoded, a prompt of `tokens` (default 4096) arrives. Per width it reports the newcomer's time to first token, the active rows' p50/p95/max gaps between tokens while the newcomer is admitted, and the tokens each active row emitted while it waited. Rounds replay it, alternating the order of the widths. - A README describes the options and notes that the balance depends on the GPU and the model. On an Apple M5 Max with Llama 3.2 3B 4-bit (1 and 3 active rows, 145-token prompts, a 4096-token arrival), step 512 gives a p95 gap of 78.5-79.3 ms and a 621-649 ms newcomer TTFT; step 256 gives 56.5-57.3 ms and 856-887 ms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Stacked on #91. The functional batching stack can be merged independently of this tooling.
What changed
Add only Benchmarks/DecodeBench/Package.swift and Sources/decodebench/main.swift as a separate Swift package. Time TokenIterator and BatchTokenIterator decode at every batch size asked for (
1,4,8by default), report time to first token, alternate run order, and check repeatability. Support greedy, top-p and categorical sampling, MLX_SWIFT_LM_PATH for another checkout, and SINGLE_ONLY for main.With
mixedprompt lengths the rows of a batch get prompts that shrink evenly from the full length to a sixth of it, so every row but the first is left-padded and the per-row positions and padding mask from #87 and #88 are on the timed path. Every batch row is compared with an untimed single-sequence run of its own prompt; the agreement is reported per row, not asserted. This replaces the earlier two fixed comparisons (single against batch(1), batch(1) against row 0 of batch(4)).The root package and library targets are unchanged.
Restack (2026-11-06): both commits (
d4fa4f5,b28ce06, kept on the fork asbackup/2026-11-06/codex/standalone-decode-benchmarksand…-b28ce06) are rebased unchanged onto #91's new head (bf09f16).Measurements
These were taken on
b28ce06, before the restack, and were not re-run. The restack changes #91's admission checks, which run once per joining row, not per decode step.Llama 3.2 3B Instruct 4-bit, release build, 145 prompt tokens, 128 decode tokens, 5 rounds, greedy, medians:
Equal lengths: every row of batch(1), batch(4) and batch(8) agrees with its single run on all 129 tokens. Mixed lengths: batch(4) rows agree on
[129, 129, 47, 7]tokens, batch(8) rows on[129, 129, 129, 129, 129, 129, 129, 1]. Every path repeats its tokens in every round.The early mixed-length divergences are near ties, not padding errors. A separate teacher-forced check (each prompt prefilled alone, caches merged as the iterator merges them, every row fed its single run's greedy tokens) gives the same logit difference against the single run for padded and unpadded rows: median 0.014–0.020 for padding 0 to 121, against 0.016–0.018 for an unpadded batch. The batch(8) row that parts ways after its first token does so where its single run's top two logits are 0.008 apart, and the same prompt flips at the same step in an unpadded batch of eight 24-token prompts.
Evidence
At this head (4e4c889), the package builds in release against this checkout and prints its usage with no arguments:
Before the restack, on
b28ce06:MLX_SWIFT_LM_PATHpointing at forkmain(it builds and runs there).formatleaves the file unchanged andlint --strictpasses.GitHub Actions jobs in this origin fork are skipped by the workflow's repository-owner condition; the results above are local.
Restack (2026-11-06c)
1793cc5), which includes Merge recurrent and composite cache rows without losing state #90's recurrent offset fix. The benchmark files are unchanged.7a4cc19) builds in release and was used for the before/after decode run reported in Add continuous batched token generation with checked admission #91:097fa0c.Rebased onto main
4ab2679(2026-11-07)Rebased from
c8dabc6onto main4ab2679, which adds seven commits (22bdf2c..4ab2679): surrogate-pair escapes, weight-loading progress, LFM2.5, chat-session steering, VLM image prompts out of float32, LFM3, and Qwen 3.5 VLM YaRN. Git applied every commit of the stack without conflicts.git range-diffshows this PR's commits unchanged.Release build of the benchmark at this head (
d80c2ac):At this head (
d80c2ac):The whole
MLXLMTestsbundle at this head:The failures are main's.
EmbeddingGemmaMaskTestsG7 fails as before.LFM2DSparkMetalTests.rotatingHybridCacheMatchesGreedyAcrossWindow(prompt length 3, with and without MoE) is new on main: it fails the same way on main4ab2679alone, in three of three runs, and at every stack head through #91 (-only-testing:MLXLMTests/LFM2DSparkMetalTests). The two MiniCPM5 CDATA issues arewithKnownIssue. Main's whole bundle on this machine: 1449 Swift Testing tests and 974 XCTest cases, with these same failures.Batch row operations move to
BatchableKVCache(2026-11-07b)Rebased onto #91's
51d71ef(theBatchableKVCachechange in #88-#91; see #88). This PR's two commits are unchanged (git range-diff); the benchmark uses no row operation.Release build of the benchmark at this head:
At this head (
4f1a7d6, wasd80c2ac):The whole
MLXLMTestsbundle at this head:The failures are main's, as at the previous head:
EmbeddingGemmaMaskTestsG7 andLFM2DSparkMetalTests.rotatingHybridCacheMatchesGreedyAcrossWindowfail on main4ab2679alone; the two MiniCPM5 CDATA issues arewithKnownIssue.Merge validation and padding-only extraction (2026-11-07c)
Rebased onto #91's
95844b3(the round 8 changes in #88-#90). This PR's other commits are unchanged (git range-diff); the only new commits in the stack are #88's1c399b0, #89's081b109, #90'sa35cbe2and #94's13957b4. This PR'sdecodebenchmeasured the round 8 change, before and after; see #88 for the numbers.At this head (
3a18939, was4f1a7d6):The whole
MLXLMTestsbundle at this head:The failures are main's, as at the previous heads:
EmbeddingGemmaMaskTestsG7 andLFM2DSparkMetalTests.rotatingHybridCacheMatchesGreedyAcrossWindowfail on main4ab2679alone; the two MiniCPM5 CDATA issues arewithKnownIssue.Review fixes merged forward (2026-11-07d)
Merged #91 into this branch (head
5482f7c, was3a18939; no rebase). It brings in #90'se222aa2(the typed recurrent merge refuses subclasses and mixed classes, with three new tests). Nothing else in this PR changes.At
5482f7c:BatchTokenIterator events merged in (2026-11-07e)
Merged #91 (head
99a47ff, was5482f7c; no rebase). DecodeBench now reads the iterator'sBatchEvent.tokenevents instead ofResponsevalues. Its timing is unchanged: the time-to-first-token and decode-split fixes wait for stage 2 (resumable prefill). DecodeBench builds against this head (swift build -c release:Build complete!).At
99a47ff:That is 49 tests here; 45 at
5482f7c. The 4 additions are #91's new iterator tests.Follow-up
5c3e71e, for the Codex finding on that port: DecodeBench rethrows a.failedevent's error, so a row that cannot join ends the run instead of leaving a partial batch that still reports a rate. This is how it behaved before, when admission threw. It builds against5c3e71e(Build complete!). DecodeBench is not part of the test scheme.Then merged #91's follow-up
ca604a2(headefb92249): 50 tests, all passing.GenerateParametersinitializer merged in (2026-11-07g)Merged #91's
acebd0b(e048b1a) and1e95cae(5821f85), one push each, each given a thumbs up by Codex. No changes in this PR. Head5821f85: 54 tests, all passing.DecodeBench timing for resumable prefill (2026-11-07h)
Pushed one commit at a time; Codex gave each a thumbs up before the next went out.
4bf5ff0). Codex then noted that DecodeBench's timing no longer held: with Llama prefilling resumably, the first step is a prefill, not every row's first token.b2269b2: the time to first token now ends at the first.tokenevent.c02bbbc, for a Codex finding onb2269b2: a run with no steady step, such as 4 decode tokens at batch size 8, now stops with an explanation instead of reporting NaN. Checked on Llama 3.2 3B.Head
c02bbbc: 57 tests, all passing. DecodeBench builds (swift build -c release).DecodeBench (#93 at
c02bbbc, release) on the local Llama-3.2-3B-Instruct-4bit,128 5 4096 greedy 4 mixed(prompt lengths 4096/2958/1820/682). Before is #98 atdc023a7, without this commit; after is #98 atf309640. Runs alternated before, after, after, before.Two-chunk prefill merged in (2026-11-07j)
Merged #91 at
6dd7f3c(d43364d); Codex gave #91 a thumbs up. 15 more model families opt into resumable prefill, the family test seeds its weights, and two prefill chunks stay in flight (6dd7f3c): a batch of one now reaches its first token as fast as a single run. #91's paired TTFT benchmark ran this PR's DecodeBench. The merge was clean.BatchTokenIteratorTests+QwenVLBatchedPositionTests+BatchedRecurrentParityTestsatd43364d:Merged #91 again (
96d0458). #91's fixab5075efollowed: the prefill wait now uses chunk markers instead of the cache's own arrays, which an in-place cache write had rebound to the chunk just submitted. At 128-token chunks a batch of one's first token came 125 ms sooner. The merge was clean. Focused suites at96d0458:Prefill step and arrival mode (2026-11-08)
8a05215lets developers tune the prefill chunk size on their own hardware:PrefillParameters.stepSizefor both the single-sequence and the batch paths.arrival[:tokens].tokensarrives (default 4096).Benchmarks/DecodeBench/README.mdcovers both options and notes that the balance depends on the GPU and the model.Arrival runs
ab5075e.decodebench <model> 128 3 145 greedy 1,3 arrival:4096 <step>.-DSINGLE_ONLYstill builds.Merged #91 once more (
7c434f8) for31b3e7d, a documentation note on choosingPrefillParameters.stepSizethat points to the arrival mode above. The merge was clean. Focused suites at7c434f8:#91's marker and layer-count fixes merged in (2026-11-08)
Merged #91 (
a0b4216). It brings #91 at3ac324a:6c28e8c: each prefill chunk now has one marker that owns its memory. Before, its markers were views of the cache buffers, which forced every in-place chunk to copy its buffer.3ac324a: a first row whose supplied cache has fewer layers than the model now fails withKVCacheErrorinstead of trapping in the model.The merge was clean. Focused suites at
a0b4216:Merged #91 again (
922a95c) forc9145a2. The prefill chunk marker is now a single element: one element of the last cache array instead of every array's. That removes the ~10 ms that6c28e8cadded to a batch of four's first-row TTFT on 2048-token prompts, and the marker still owns its memory. The merge was clean. Focused suites at922a95c:Not done on purpose
batchGenerate, and the token and text streams from Add fixed-batch token and decoded generation streams #97) are not timed. At Add continuous batched token generation with checked admission #91,batchGenerateis the same loop as the batch(N) path; the streams from Add fixed-batch token and decoded generation streams #97 are the layer with new per-row work (decoder, reasoning and tool parsers, stop policy), but this PR's base cannot see them.Test changes
922a95c): no test changes.a0b4216): no test changes.7c434f8): no test changes.8a05215): no test changes; DecodeBench is not part of the test targets.96d0458): no test changes.d43364d): no test changes.AI assistance: the benchmark was developed with Claude Code. Codex extracted it into this optional PR and restacked and validated it at the maintainer's request for this origin fork. Claude Code added the batch sizes and mixed lengths, and restacked it onto the rewritten #91.
🤖 Generated with Claude Code