Skip to content

feat(scheduler): park a chunked prefill under memory pressure instead of aborting it - #4479

Closed
e-Evolution wants to merge 15 commits into
jundot:mainfrom
e-Evolution:stack/pb-prefill-park
Closed

e-Evolution wants to merge 15 commits into
jundot:mainfrom
e-Evolution:stack/pb-prefill-park

Conversation

@e-Evolution

@e-Evolution e-Evolution commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Slice 2 of 12 of the pause-and-resume cycle proposed in #4440 (the improvement is explained in the write-up's section The strategy: pause, don't abort). Depends on #4478 (P-a). GitHub shows every earlier unmerged slice in this diff; please review only this slice's 8 commits below. I keep it as a draft and rebase it on main after the previous slice merges.

Under hard, non-emergency memory pressure the enforcer calls abort_all_requests, which throws away every request's prefilled KV. This slice parks one prefilling request instead.

  • Enforcer. At hard, non-emergency pressure it asks each scheduler to park a prefill victim and defers the abort while one is parkable. The previous abort runs when nothing is left to park, the deferral budget is spent, or the pressure is an emergency. Every poll reports its level to the schedulers for the resume hysteresis.
  • Scheduler. At step start it parks one victim per step: the most recent chunked-prefill arrival, never the oldest while another is parkable. The request keeps its stream, prompt and arrival time, drops its live cache through the deferred teardown, and re-queues in arrival order ahead of newer requests. It resumes after park_resume_ok_polls consecutive ok polls with a fresh prefix lookup that restores its committed blocks; those blocks are pinned (block refs plus SSD index hashes) until resume, finish or abort, and stay reclaimable from RAM through the dirty-eviction write-back.
  • abort_all_requests spares parked requests only through an explicit spare_parked=True from the non-emergency keep-loaded path, so idle-LRU eviction and unloads still drain them.
  • Media prompts prefilled in the external loop now commit progress too (under the same extra keys as their final store), and every skipped commit logs its reason once per request.
  • Shutdown waits on prefill commits stay inside the engine-close watchdog.

Default: Off (scheduler.memory_pressure_park: false; env OMLX_MEMORY_PRESSURE_PARK). Also max_parks_per_request (3) and park_resume_ok_polls (3). With park off, the enforcer and scheduler behave as on main.

Stack and merge order

Each slice applies on the one before and keeps main's behaviour with its switches at their defaults. Merge in this order:

# PR Slice
1 #4478 P-a: Prefill progress commit
2 this PR P-b: Prefill park instead of abort (this PR)
3 #4480 S1: Single victim and exact-chain cache primitives
4 #4481 S2: Decode pause and resume
5 #4482 S3: Memory-aware resume priority
6 #4483 S5: Cached-media text suffix through the chunked path
7 #4484 G: memory_scheduling setting
8 #4485 S4: Never-fits gate, fits-later wait, capacity API
9 #4486 S6: Resume under the hard watermark
10 #4487 S7: Enforcer: external-loop prefills, no live victim, emergency pause-first
11 #4488 S8: Admission progress clock and solo admission
12 #4489 S9: Hot cache in the never-fits line, stall clock across park holds

Commits in this slice (15 files changed, 2531 insertions(+), 35 deletions(-)):

e37bdfc8 feat(enforcer): request a prefill park before aborting at hard pressure
21dc1902 feat(scheduler): park a chunked prefill under memory pressure instead of aborting it
3c459e2d fix(engine): spare parked requests only when the caller keeps the model loaded
4fed9d39 fix(scheduler): bound the park hold gate and let a parked head pass when idle
5ed4e225 fix(scheduler): resume parked requests one per window and keep parked blocks reclaimable from RAM
96c8c5cb refactor(scheduler): name the resume-poll saturation sentinel and test lost hot-cache write-back on resume
289a9d46 fix(scheduler): commit prefill progress for media prompts and log every skip reason
7be02915 fix(scheduler): keep shutdown waits on prefill commits inside the teardown watchdog

Validation

  • Focused: python -m pytest tests/test_scheduler_park.py tests/test_scheduler_prefill_commit_vlm.py tests/test_engine_core.py tests/test_process_memory_enforcer.py tests/test_scheduler_prefill_memory_guard.py tests/test_prefix_cache.py tests/test_paged_ssd_cache.py
  • CI's command (pytest tests/ -m "not slow and not integration" -n 3 --dist loadgroup, Python 3.11, a venv with main's pins) at this slice's tip on main f3ad34d: 17,252 passed, 3 failed, 718 skipped; main f3ad34d: 17,119 passed, 3 failed, 718 skipped. The 3 failures are tests/test_gemma4_rendering.py on both (a local asset model without a chat template).
  • Ruff: no new finding against main; Black is clean on the changed lines. Every commit imports omlx.server and omlx.scheduler and collects its tests.

The real-model numbers below are in the write-up's Final results section.

Real-model runs: Apple M5 Max (128 GB), macOS 27.0.1, Python 3.11, Qwen3.8-27B-oQ4e-mtp (4-bit, Lightning MTP, TurboQuant 4-bit KV), one server at a time, one run per scenario. They used my fork rebased on main c0b1056 (this whole cycle plus my fork's other features), against main c0b1056 from the same venv; the stack on current main was not run on the real model.

  • Forced pressure (three greedy prompts of about 130k / 115k / 100k tokens started 0.5 s apart, low custom ceiling, hard_threshold 0.8): an earlier run parked a request at 129,024 committed tokens and resumed it 0.5 s later.
  • With the full cycle, text decode at 44 GB and image prefill at 44 GB each completed 3/3 with 0 aborts and 2 prefill parks resumed; a row parked during a packed prefill (perf(prefill): batch concurrent prompt prefills into one forward #4361) resumed, 3/3, 0 aborts.

What remains untested: Park alone (without the later slices) was not run on the real model; in the full-cycle runs, the decoding rows that this slice cannot pause were handled by later slices. One Mac and one model family.

Checklist

  • I read CONTRIBUTING.md.
  • I understand every change in this PR and can explain it in review.
  • For UI or UX changes, I attached before and after screenshots.

Contribution from the lead maintainer of ADempiere (https://github.com/adempiere/adempiere/tree/hotfix/3.9.4.002). Author: victor.perez@e-evolution.com.

…during prefill

Blocks were stored only when a request finished, so a request aborted
mid-prefill lost all of its prefill work and a re-issue of the same prompt
prefilled from token zero.

With prefill_progress_commit_blocks = N (0 = off, the default), chunked and
external prefill now store every N completed blocks under the request's own
block table, which stays alive so later commits and the final store extend
it. Each commit window is detached from the live cache; TurboQuant layers are
converted with the same rules as the final conversion (the codec is
per-vector and fixed-seed, so windowed blocks are byte-identical to
whole-prefix ones), and a restored TurboQuant prefix is sliced from its packed
state. Commits are skipped under soft or hard memory pressure, while a
previous commit is in flight, and for prompts that are not a plain token
prefix. Aborts and prefill failures wait for an in-flight commit and leave the
committed blocks in the prefix cache.

A request requeued after a prefill memory failure still prefills cold, as
_requeue_or_fail_prefill documents, when the setting is on.

BlockAwarePrefixCache.store_cache gains cache_covers_suffix_only so a window
longer than the stored prefix is not mistaken for a full-sequence cache.
Add scheduler.prefill_progress_commit_blocks to the scheduler settings (and
OMLX_PREFILL_PROGRESS_COMMIT_BLOCKS), validate that it is not negative, and
pass it to SchedulerConfig.
… MLX thread

Teardown of a request with an in-flight incremental prefill commit no longer
waits up to 60s on the inference thread. The destructive steps (block release,
snapshot retirement and cleanup) are queued and run on the scheduler thread
once the commit future has finished, whether it succeeded or not. Commits get
their own single-thread executor so they never queue behind final stores, and
the final store of a request is chained after its own commit on the store
worker. Shutdown waits a bounded time and abandons stuck commits.
…commit

A stuck commit blocked the final-store worker forever, holding store-gate
slots and stalling admission. The wait is now bounded; on timeout the
final store is skipped, logged once, the gate slot is released and the
table and snapshot teardown stays deferred behind the commit.
…dule

Scheduler.shutdown was no longer the first shutdown definition in
scheduler.py, so the registry-cleanup guard test inspected the
executor's shutdown instead and failed (a regression from the deferred
commit teardown earlier in this series). Behavior is unchanged:
Scheduler.shutdown still releases registry rows by model.
Add scheduler.memory_pressure_park, max_parks_per_request and
park_resume_ok_polls settings (env overrides, default off). At hard,
non-emergency pressure the enforcer asks each scheduler to park a prefill
victim and defers the abort while one is parkable; the legacy abort runs
when nothing is left to park, the deferral budget is spent, or pressure
is an emergency. Every poll reports its level to the schedulers for the
resume hysteresis.
… of aborting it

With memory_pressure_park on, the scheduler consumes the enforcer's park
flag at step start and parks one victim per step: the most recent
chunked-prefill arrival, never the oldest while another is parkable. The
request keeps its stream, prompt and arrival time, drops its live cache
through the deferred teardown, and re-queues in arrival order ahead of
newer requests. It resumes after park_resume_ok_polls consecutive ok
polls with a fresh prefix lookup that restores the committed blocks,
bypasses the cold-retry guard and the admission-stall timeout, and its
committed blocks are pinned (block refs plus SSD index hashes) until the
resume, finish or abort. Bulk pressure aborts spare parked requests.
…el loaded

abort_all_requests spared parked requests whenever no reason was given, so
idle-LRU eviction and the emergency busy-victim path left them orphaned or
blocked the pending unload from draining. Make it an explicit spare_parked
keyword (default False) and pass True only from the non-emergency hard
keep-loaded path.
… blocks reclaimable from RAM

- clear parked on abort and guard the deferred pin by request identity
- consume the ok streak on resume; max-hold and stale-monitor escapes resume one per pass
- stop pinning parked blocks in the hot cache; SSD pin plus dirty-eviction write-back keeps them restorable
- count the settle window in enforcer polls instead of queries
- replace the victim sentinel with an explicit flag
…ry skip reason

Conversations that carry an image resend it every turn, so the VLM engine
prefills them through the external loop with fused embeddings and
request-wide media cache keys. That loop never called the incremental commit
and the commit guard excluded any prompt with media keys, so long prompts
never became durable and were re-prefilled in full after an abort.

Media prompts now commit under the same extra-key arguments their final
store uses (block keys depend only on where a block ends), except for models
that need an atomic multimodal prefix. Every early return of the commit path
logs its reason once per request at INFO, each commit logs its token range,
and a park request that finds no victim logs why per request.
…rdown watchdog

The engine-close watchdog exits the process after 60s without SSD write
progress. A final store waited up to 120s for a stuck commit and shutdown
could spend 40s on commit waits before reporting any progress.

The final-store wait now polls and stops as soon as shutdown cancels the
commits, the shutdown waits total at most 20s, and a commit that is still
writing is reported to the watchdog as persistence progress.
@jundot

jundot commented Oct 14, 2026

Copy link
Copy Markdown
Owner

I'm closing this PR as part of the pause-and-resume stack replaced by #4509. I've posted the replacement's scope and remaining proposals in the discussion update.

@jundot jundot closed this Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants