Repository navigation
feat(scheduler): sequence-aware pad trim for model-owned families - #2140
Merged
Merged
Conversation
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
This was referenced Oct 6, 2026
11 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 scheduler trimmed padded-prefill positions only from the
CachePoolentry'sKVCaches. A family whose own layout is model-owned keeps its K/V inside the model, so that trim reached nothing andoffsetran ahead of the real token count; PR #1752 opted Gemma 3, AFMoE and Llama 4 out of padded prefill for that reason. This addsLanguageModel::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.trim_internal_caches(CLI) and takes padded prefill again.RotatingKVCache::rewind_padded_prefillalso cuts the physical ring back: a plaintrimleaves 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) andPipelineServerModel(state behind the pipeline runtime) now answersupports_padded_prefill() -> falsewith the reason. AFMoE and Llama 4 stay opted out; their comments say what lifting it takes.trim_internal_cachesfor non-batching models and fails closed for batching ones.MLXCEL_FORCE_PADDED_PREFILLforces tile alignment on any hardware (CLI and server); the server now honorsMLXCEL_NO_PADDED_PREFILL;LoadedModeland the VLM wrapper delegate both trim hooks (LoadedModelpreviously droppedtrim_internal_cachesfor 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() == trueis 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)
cache::prefill_rewindtests (5) andscheduler_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::scheduler178 passed,block_reclaim23,lookahead11,speculative175,generate::tests36,models::gemma3,models::afmoe28,muse_glimmer109. Clippy (both crates,-D warnings) and fmt clean.mlxcel-serverwith 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 1sequential and--parallel 4concurrent: 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 1vs--parallel 4diverge 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 repeatedtexId.mlxcel generatewith gemma-3-4b-it-4bit, forced padding vs unpadded: identical 40-token output.Not verified on this host
cudaStreamEndCaptureaborts 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