Skip to content

feat(scheduler): sequence-aware pad trim for model-owned families - #2140

Merged
inureyes merged 3 commits into
mainfrom
feature/issue-1755-model-owned-pad-trim
Oct 6, 2026
Merged

inureyes merged 3 commits into
mainfrom
feature/issue-1755-model-owned-pad-trim

Conversation

@inureyes

@inureyes inureyes commented Oct 6, 2026

Copy link
Copy Markdown
Member

Summary

The scheduler trimmed padded-prefill positions only from the CachePool entry's KVCaches. A family whose own layout is model-owned keeps its K/V inside the model, so that trim reached nothing and offset ran ahead of the real token count; PR #1752 opted Gemma 3, AFMoE and Llama 4 out of padded prefill for that reason. This adds LanguageModel::trim_sequence_state(seq_id, excess) and routes all four scheduler pad trims (batched, full, chunked first chunk, continuation) through one helper that trims the pool caches and, for a model-owned layout, calls the hook. A failed rewind aborts the request instead of decoding from a desynchronized offset.

  • Gemma 3 implements the hook (server) and trim_internal_caches (CLI) and takes padded prefill again. RotatingKVCache::rewind_padded_prefill also cuts the physical ring back: a plain trim leaves the pad keys in the slots the next decode step keeps once a padded chunk outgrows the window.
  • deepseek_v4 (pooled compression caches), bailing_moe_linear (GLA state), qwen3_next (gated-delta state) and PipelineServerModel (state behind the pipeline runtime) now answer supports_padded_prefill() -> false with the reason. AFMoE and Llama 4 stay opted out; their comments say what lifting it takes.
  • The trait doc states the model-owned exception. The hook default delegates to trim_internal_caches for non-batching models and fails closed for batching ones.
  • Other behavior changes: new MLXCEL_FORCE_PADDED_PREFILL forces tile alignment on any hardware (CLI and server); the server now honors MLXCEL_NO_PADDED_PREFILL; LoadedModel and the VLM wrapper delegate both trim hooks (LoadedModel previously dropped trim_internal_caches for every family); the continuation-chunk mask offset falls back to the prefill cursor when the pool entry is empty.

CUDA reachability (negative result)

Model-owned families never reach padded prefill on CUDA today. The only model-owned family with supports_batched_prefill() == true is qwen3_5, which declines padding, so the longest-row padded batched site is unreachable for them; the single-sequence sites were hardware-gated with no force override. The forced-alignment env is what makes the path testable here.

Test plan (GB10, CUDA, release)

  • New cache::prefill_rewind tests (5) and scheduler_model_owned_pad_trim_tests (4: full prefill inside and past the window on dense and paged backends, chunked prefill, snapshot reuse after a padded turn). With the Gemma 3 rewind disabled the scheduler tests fail: offset 32 vs 13, 96 vs 37.
  • server::batch::scheduler 178 passed, block_reclaim 23, lookahead 11, speculative 175, generate::tests 36, models::gemma3, models::afmoe 28, muse_glimmer 109. Clippy (both crates, -D warnings) and fmt clean.
  • mlxcel-server with gemma-3-4b-it-4bit, --prefill-chunk-size 500, greedy, prompts of 176, 730 and 1852 tokens (none a multiple of 32, the last past the 1024 window). Forced padding vs unpadded, --parallel 1 sequential and --parallel 4 concurrent: the short prompt is identical everywhere; the others diverge only where the top-2 logprobs are equal at the reported resolution (0.25), and unpadded --parallel 1 vs --parallel 4 diverge at the same long-prompt position with no padding involved. With the rewind disabled, outputs diverge at token 1 (top-2 gap 1.0 and 1.5) and the 730-token prompt degenerates into repeated texId.
  • mlxcel generate with gemma-3-4b-it-4bit, forced padding vs unpadded: identical 40-token output.

Not verified on this host

  • The M5 manual check from the issue and Metal numerics of the padded path (no Neural Accelerator here).
  • cudaStreamEndCapture aborts hit about one in four repeated runs of several suites, including the comment-only gemma4 suite; every suite passed on rerun.

Follow-up: decode lookahead stays off for model-owned families (#2139). The hook cannot serve that teardown as is, because the rewind requires a chronological buffer and a decode write into a wrapped ring is not one.

Closes #1755

The scheduler trimmed padded-prefill positions only from the CachePool entry's KVCaches. A family whose own layout is model-owned keeps its K/V in the model, so the trim reached nothing and offset ran ahead of the real token count by the pad width. That is why PR #1752 opted Gemma 3, AFMoE and Llama 4 out of padded prefill.

LanguageModel::trim_sequence_state(seq_id, excess) is the new hook. Every scheduler prefill site (batched, full, chunked first and continuation) now goes through one helper that trims the pool caches and, for a model-owned layout, calls the hook; a failed rewind aborts the request. The default delegates to trim_internal_caches for non-batching models and fails closed for batching ones. The continuation-chunk mask offset falls back to the prefill cursor when the pool entry is empty.

Gemma 3 implements the hook and trim_internal_caches (CLI) and takes padded prefill again. RotatingKVCache::rewind_padded_prefill also cuts the physical ring buffer back, because a plain trim leaves pad keys in the slots the next decode step keeps once the padded chunk outgrows the window. LoadedModel and the VLM wrapper now delegate both trim hooks.

deepseek_v4 (pooled compression caches), bailing_moe_linear (GLA state), qwen3_next (gated-delta state) and the pipeline server model opt out explicitly with the reason. MLXCEL_FORCE_PADDED_PREFILL forces tile alignment on any hardware, and the server now honors MLXCEL_NO_PADDED_PREFILL too.

Validated on GB10 (CUDA): new rewind and scheduler tests pass and fail with the Gemma 3 rewind disabled (offset 32 vs 13); gemma-3-4b-it-4bit through mlxcel-server with forced padding matches unpadded runs up to exact logprob ties, while the disabled rewind diverges at token 1.

Closes #1755
Turn 1 runs a padded prefill on tiny Gemma 3 and must donate a snapshot; turn 2 must adopt it. This is the combination PR #1752 gave up. Refs #1755
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions priority:medium Medium priority area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core mlxcel-core: MLX FFI, primitives, KV cache, layers area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:medium Medium priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(scheduler): sequence-aware cache trim hook so model-owned families can use padded prefill

1 participant