Skip to content

perf(server): keep token-biased requests on the lookahead pipeline - #1951

Merged
inureyes merged 1 commit into
mainfrom
update/issue-1950-lookahead-token-bias
Sep 23, 2026
Merged

inureyes merged 1 commit into
mainfrom
update/issue-1950-lookahead-token-bias

Conversation

@inureyes

Copy link
Copy Markdown
Member

Why

The lookahead decode gate reuses the batched-fused predicate, which rejects any row with a non-empty token_bias. ignore_eos is a -inf bias on the EOS ids and logit_bias is a bias by definition, so both request shapes decoded on the synchronous path. The rejection was an input shape, not sampler state: the fused [B, vocab] -> [B] dispatch carries shared scalar params and has no per-row bias input. Token bias does not read generated history, so it folds into the logits ahead of the dispatch, exactly where the per-row sampler applies it.

What changed

  • config_supports_fused_batch / row_supports_fused_batch keep their strict meaning for callers that apply no bias (uniform_fused_batch_params, and through it batched_sample), with new ..._except_bias variants for callers that do.
  • apply_token_bias_rows applies one TokenBiasMap per row in a single pass over the union of biased ids (never vocab-sized). Rows that do not bias an id get an exact 0.0 delta and come out bitwise unchanged. A row count that disagrees with the logits panics rather than dropping a bias, which would let an ignore_eos request sample its EOS token.
  • The scheduler applies it at both dispatch points in the per-row chain position: inside batched_fused_sample_with_bias for the synchronous fused branch, and before apply_row_filters when priming a lookahead step.
  • A biased row still takes the per-row sampler while the opt-in MLXCEL_LANG_BIAS_COUNTERS pre-bias readback is enabled; LANG_BIAS_APPLIED_TOTAL is incremented per biased row on the fused path.

Measured

c4ai-command-r7b-12-2024-4bit, M1 Ultra, 512-token prompt, 128 tokens, greedy, two binaries, indexers suspended. Every row reports mlxcel_batch_decode_lookahead_steps_total so it states which path served it.

Request main this branch
plain 105.0 tok/s, 125 steps 105.3 tok/s, 125 steps
ignore_eos 98.6 tok/s, 0 steps 104.7 tok/s, 125 steps
logit_bias 98.3 tok/s, 0 steps 104.7 tok/s, 125 steps

Biased requests gain 6.2% and 6.5% and match unbiased throughput; unbiased requests are unchanged. On a build carrying #1947's decode-only command-buffer budget, which raises only the pipelined path, the same gap measured 13 to 16%.

Output equivalence: banning id 17939, the first token the unbiased run emits, changes the text on both binaries and to the same SHA-1 (ef3caa8646c3), while only this branch keeps the pipeline. Full record: docs/benchmark_results/lookahead-token-bias-m1ultra-2026-09-23.md.

Validation

cargo test --workspace --profile test-fast --features metal,accelerate: 11417 passed, 0 failed. Clippy and fmt clean. Not measured: multi-sequence batches mixing biased and unbiased rows (unit-tested only), CUDA and ROCm, stochastic sampling.

Closes #1950

The lookahead decode gate reused `row_supports_fused_batch`, which rejects any row with a non-empty `token_bias`. `ignore_eos` is a `-inf` bias on the EOS ids and `logit_bias` is a bias by definition, so both shapes fell back to synchronous decode: 98.6 and 98.3 against 105.0 tok/s unbiased on command-r7b at a 512-token prompt (M1 Ultra, indexers suspended). With PR #1947's decode-only command-buffer budget, which raises only the pipelined path, the same gap measured 13 to 16%.

The rejection was an input shape, not sampler state: the fused `[B, vocab] -> [B]` dispatch has no per-row bias input. Token bias does not read generated history, so it folds into the logits before the dispatch, where the per-row sampler already applies it.

`config_supports_fused_batch` and `row_supports_fused_batch` keep their strict meaning for callers that apply no bias (`uniform_fused_batch_params`, and through it `batched_sample`); new `..._except_bias` variants admit a biased row for callers that do. `apply_token_bias_rows` applies one map per row in one pass over the union of biased ids, never vocab-sized, with an exact `0.0` delta leaving unbiased rows bitwise unchanged, and panics on a row-count mismatch rather than dropping a bias. It runs at both dispatch points in the per-row chain position. A biased row still takes the per-row sampler while the opt-in `MLXCEL_LANG_BIAS_COUNTERS` readback is enabled.

Validated on a real checkpoint: biased requests advance `mlxcel_batch_decode_lookahead_steps_total` and match unbiased throughput, unbiased requests are unchanged, and banning an emitted token yields text identical to the synchronous path. Numbers in `docs/benchmark_results/lookahead-token-bias-m1ultra-2026-09-23.md`. Gate: 11417 passed, 0 failed, clippy and fmt clean.

Closes #1950
@inureyes inureyes added type:performance Performance improvements status:review Under review area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Sep 23, 2026
@inureyes
inureyes merged commit db8a2ce into main Sep 23, 2026
20 checks passed
@inureyes inureyes added status:done Completed and removed status:review Under review labels Sep 23, 2026
@inureyes
inureyes deleted the update/issue-1950-lookahead-token-bias branch October 6, 2026 12:12
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(server): Keep token-biased requests on the lookahead decode pipeline

1 participant