Repository navigation
Conversation
Adds `BatchTokenIterator` for running B prompts through one model in lockstep — emits a `[Int]` of length B per step, amortising model compute across the batch via [B, L] input tensors. ## Scope (v1) - Same-length prompts only (pad in Swift if needed). - Pure-attention models (Gemma4, GPT-OSS, Nemotron H). GatedDelta / Mamba hybrids (Qwen3.5) are follow-up — their sequential SSM state needs per-sequence isolation that's beyond the scope of a single PR. - No LogitProcessor penalty chain — repetition / frequency / presence penalties would need per-sequence state. Greedy / top-p / top-k / categorical samplers work row-wise on [B, V] and are supported. - No speculative / n-gram draft on the batched path. ## Implementation Key simplifications: - **KV caches are already [B, H, L, D]-shaped** in `KVCacheSimple` and `RotatingKVCache`. No cache changes needed — the first `update()` call sizes the cache from the input's batch dim. - **Samplers already work on [B, V]** — `argMax(axis: -1)` and `categorical` both reduce along the last axis. - The only real additions are (1) stacking `[LMInput]` into `[B, L]` in the initializer and (2) the per-step `[B] → [Int]` GPU→CPU drain. Step structure mirrors `TokenIterator`: previous batch of sampled tokens drained via `asArray(Int.self)` while the next forward pass runs via `asyncEval`. ## Tests `Tests/MLXLMTests/BatchTokenIteratorTests.swift`: 1. **B=4 forward pass: identical rows → identical logits** — direct sanity check that the underlying model is batch-safe. Pins row-wise logit equality at 1e-4. 2. **B=1 degeneracy** — iterator emits the correct count and per-step shape. (Bit-exact token equality vs `TokenIterator` is unreliable with tiny random-weight test models because argmax flips on sub-1e-4 kernel-path differences; real-model parity is the acceptance test for production.) 3. **B=4 step shape** — every step emits exactly B tokens. ## Follow-up - Per-sequence penalty processor chains (repetition / frequency / presence). - Variable-length prompt support (per-sequence cache offsets). - Hybrid / Mamba model support with per-sequence SSM state. - Real-model benchmarks: `benchmark.sh --batch 2,4` to validate amortisation on Gemma4 E2B and GPT-OSS-20B. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Regression Test Results (M5 Max 128GB)Cherry-picked onto alpha (with #56 already applied). All 5 models tested with Note: Had to rename my existing Results @ 1k context
Verdict: No regressions. All within noise. |
Avoids collision with Eric's BatchTokenIterator in PR ekryski#57. Different designs — ours orchestrates independent TokenIterator sessions, his does true batched [B,L] forward pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: Tom Turney <tturney@psyguard.ai>
Avoids collision with Eric's BatchTokenIterator in PR ekryski#57. Different designs — ours orchestrates independent TokenIterator sessions, his does true batched [B,L] forward pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: Tom Turney <tturney@psyguard.ai>
Avoids collision with Eric's BatchTokenIterator in PR #57. Different designs — ours orchestrates independent TokenIterator sessions, his does true batched [B,L] forward pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-Authored-By: Tom Turney <tturney@psyguard.ai>
|
Closing as already-applied. The content of this PR landed on GitHub still shows this PR as mergeable because the SHA Complementary work that landed in #79 on top of this foundation:
|
Summary
Adds `BatchTokenIterator` for running B prompts through one model in lockstep. Emits `[Int]` of length B per step, amortising model compute across the batch via `[B, L]` input tensors.
Usage
```swift
let inputs: [LMInput] = prompts.map { LMInput(tokens: tokenise($0)) }
var iter = try BatchTokenIterator(
inputs: inputs, model: model, parameters: parameters)
for batchTokens in iter {
// batchTokens is [Int] of length B — batchTokens[i] is the token
// just emitted for sequence i.
}
```
Scope (v1)
Implementation
The cache and sampler machinery were already batch-shape-correct. Main additions:
Matches `TokenIterator`'s `asyncEval` pattern so the next forward pass kicks off while the caller drains this step.
Test plan
New `BatchTokenIteratorTests`:
Bit-exact token equality vs `TokenIterator` is deliberately not tested — argmax at tiny random-weight scale flips on sub-1e-4 kernel-path differences. Real-model `--ppl` / correctness validation is the right acceptance gate for production parity.
Existing safety suite (`KVCacheTests`, `SpeculativeDecodingTests`, `Gemma3nOffsetTests`, `MaskedScatterTests`) continues to pass.
Follow-up