[PR #656] [CLOSED] serve: cooperative prefill preemption at safe chunk boundaries (--prefill-preempt) #2748

Closed
opened 2026-11-04 02:02:49 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/656
Author: @j-luwierski
Created: 10/3/2026
Status: ❌ Closed

Base: main ← Head: feat/prefill-preempt


📝 Commits (10+)

  • 5327b2c serve: cooperative prefill preemption at chunk boundaries (engine)
  • 2bf3786 serve: preemption scheduling over ownership periods (+YIELD, tests)
  • 0c75053 serve: preemption parity harness green; YIELD cleared at RESUME
  • 6371de2 bench: the preemption A/B latency measurement (group N)
  • abd5745 bench/harness: the repeat scenario waits for the RESUME echo before the next YIELD
  • e012b68 harness: pin the expert-cache geometry for every engine of a comparison run
  • 3af7bc2 harness: constant verify-window shapes (--spec-min-p 0)
  • e1ea03d harness: one fresh-engine retry per scenario
  • 6fa3c23 harness: pin the adaptive tier the owner's way (--adapt-every 100000)
  • 68ed5d5 harness: a paired control for every scenario

📊 Changes

22 files changed (+3893 additions, -217 deletions)

View changed files

📝 CMakeLists.txt (+11 -0)
➕ include/strata/core/crash_report.hpp (+100 -0)
➕ include/strata/core/expert_pack.hpp (+43 -0)
➕ include/strata/core/prefill_preempt_budget.hpp (+44 -0)
📝 include/strata/prefill/prefill.hpp (+11 -0)
➕ serve/engine_gate.py (+65 -0)
📝 serve/server.py (+548 -6)
➕ serve/test_engine_gate.py (+101 -0)
➕ serve/test_preempt.py (+519 -0)
➕ serve/test_preempt_regressions.py (+170 -0)
➕ src/core/crash_report_test.cpp (+87 -0)
➕ src/core/expert_pack_test.cpp (+66 -0)
➕ src/core/prefill_preempt_budget_test.cpp (+38 -0)
📝 src/core/verify.cpp (+23 -0)
📝 src/prefill/prefill.cpp (+10 -0)
📝 src/program/generate.cpp (+788 -211)
➕ tools/kv_stream_sweep.py (+266 -0)
➕ tools/prefill_preempt_bench.py (+156 -0)
➕ tools/prefill_preempt_regression.sh (+30 -0)
➕ tools/prefill_preempt_test.py (+596 -0)

...and 2 more files

📄 Description

Problem

Serving is sequential by design: the FIFO holds a mutex for a request's entire lifecycle, so a short interactive request sent during a long prefill waits for the whole prefill plus decode.

For orchestrator + sub-agent workloads sharing one Strata server, this resulted in tens of seconds of head-of-line blocking.

Measured example: 40.5 s of queue wait behind a 50K-token prefill.


Solution

This PR adds cooperative prefill preemption at safe chunk boundaries.

A long-running request A (for example, a 200K-token prompt) can park at a completed prefill chunk boundary, allowing a short request B to run immediately. Once B finishes, A is restored and continues as if it had never been interrupted.

The execution model remains intentionally simple:

  • still one sequence at a time
  • no decode batching
  • no concurrent slots

Engine (--prefill-preempt, opt-in)

  • Prefill::should_suspend

    • Checked once per completed chunk
    • Single-GPU path only
    • Suspension happens only after the chunk completes
    • The normal run tail still executes, which:
      • commits ple_prev for the boundary
      • drains the compute stream
      • drains the copy stream
  • SuspReq

    • Stores a complete suspension snapshot:
      • the running half via the existing checkpoint machinery
      • positional state of every owned QSA layer:
        • KV blocks
        • pooled rows
        • spare key
        • idx_block_pos
      • drafter KV state
    • Admission is copy-only
    • If snapshot creation fails, request A remains live and continues reading normally
  • Engine protocol:

    • SUSPENDED <pos> <total> id=N
    • RESUME id=N
    • CANCEL id=N
    • YIELD
      • boundary offer emitted by the server
      • the engine itself cannot see the server request queue
    • Output lines may carry an id=N suffix for demultiplexing
    • Requests without an ID behave exactly as before
  • Preemption gates:

    • minimum park position
    • per-request park cap
    • snapshot RAM budget admission (T18)
    • single GPU only
    • layer split fails closed

Server (--prefill-preempt, opt-in)

The FIFO lock is split into ownership periods instead of being held for the request's entire lifecycle:

GEN -> SUSPENDED
RESUME -> next SUSPENDED / DONE

A parked request waits outside the FIFO lock and receives keep-alives while suspended.

Additional behavior:

  • if the parked request's waiter disappears, the park is withdrawn in place
  • if the engine dies while a request is parked, the request fails cleanly
  • EngineRequest supports id=N suffix demultiplexing

New observability:

/metrics

  • prefill_preemptions
  • prefill_resumes
  • max_queue_wait_ms

/status

  • parked_requests

Results

Test system:

  • GPU: RTX 4070 Ti SUPER
  • Quant: IQ3_XXS
  • VRAM: 16 GB
Metric Feature off Feature on
B queue wait (A prefill = 50K) 40.5 s 2.9 s (-93%)
A total wall time 43.8 s 45.3 s (+3.4%)

The task targets were:

  • ≥ 80% reduction in queue wait
  • < 10% penalty to the interrupted request

Both targets are met with margin.


Verification

Bit-exact parity

Tokens and the complete state fingerprint are identical between:

  1. uninterrupted request A
  2. request A after park -> interim request -> restore

Verified state includes:

  • gdn
  • ple
  • tails
  • pooled
  • kv
  • dead
  • pooled_full
  • mtp
  • ple_prev

Tested suspension boundaries:

  • 4096
  • 8192
  • 12288

An interim request was executed between park and restore with decode depths:

  • 0
  • 1
  • 32

The drafter KV state is included in the parity check.

Trace diagnostics

Added diagnostics include:

  • STATE_POINT
  • VERIFY_POINT
  • WINDOW

These provide:

  • per-phase state hashes
  • per-row verification-window fingerprints
  • position
  • offered-T count
  • accepted count

GPU scenarios

12 GPU scenarios: ALL PASS

Coverage includes:

  • boundary tests ×5
  • park-only tests ×3:
    • early
    • mid
    • late
  • KV streaming:
    • mode 1
    • residency-map reset
  • repeat-3:
    • three consecutive parks
  • cancel while parked
  • verification that decode never yields

Scheduler tests

Fake-engine scheduler suite: 9 tests

Coverage includes:

  • no contention
  • preemption when another request queues
  • FIFO ordering
  • parked-request cancellation
  • queued-request cancellation
  • engine death while parked
  • shutdown without deadlock

Existing test suites

  • ctest: 63/65
  • Python: 137 OK

The two failing ctest cases are environmental and reproduce on pristine main.


Bugs Found and Fixed During Verification

The tests caught several issues during development:

  • A stale YIELD could re-park a resumed request at its next boundary.
  • preempt_count was saved before the increment, allowing --prefill-preempt-max to be bypassed.
  • A resumed tail with length <= --short-read could switch to verifier windows differently from an uninterrupted read.
  • A lend-size layout mismatch caused an E-9 failure across buffer layouts.

All of these were fixed before the final validation runs.


Limitations / Future Work

Current limitations:

  • only one parked request at a time
  • image requests never park
  • long-queue fairness currently uses a bounded wait:
    • --prefill-preempt-max-wait-s
    • default: 30 s
  • this is not yet a full scheduler
  • snapshot RAM is not enforced through a single hard allocation budget
    • admission estimates the required memory and refuses before the large allocation
    • snapshot vectors are still separate allocations
    • allocation failure can therefore still surface as bad_alloc

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/656 **Author:** [@j-luwierski](https://github.com/j-luwierski) **Created:** 10/3/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/prefill-preempt` --- ### 📝 Commits (10+) - [`5327b2c`](https://github.com/Niko1221/Strata/commit/5327b2ce5602b902b4bfdd7167a87d27150d6bb3) serve: cooperative prefill preemption at chunk boundaries (engine) - [`2bf3786`](https://github.com/Niko1221/Strata/commit/2bf3786d571167aea65ef20f3fff26781e1d3f04) serve: preemption scheduling over ownership periods (+YIELD, tests) - [`0c75053`](https://github.com/Niko1221/Strata/commit/0c75053c95d2a26761b52bc21b258552eea982e4) serve: preemption parity harness green; YIELD cleared at RESUME - [`6371de2`](https://github.com/Niko1221/Strata/commit/6371de293e2e9ebfd14243215a0d35ee89324388) bench: the preemption A/B latency measurement (group N) - [`abd5745`](https://github.com/Niko1221/Strata/commit/abd5745ed2c6b95a795621fd861402dd371c4ed4) bench/harness: the repeat scenario waits for the RESUME echo before the next YIELD - [`e012b68`](https://github.com/Niko1221/Strata/commit/e012b68e6842481d0a6639d9cdbede4d60ed7507) harness: pin the expert-cache geometry for every engine of a comparison run - [`3af7bc2`](https://github.com/Niko1221/Strata/commit/3af7bc22d205bc52bca9b8fcc5a52bdb7d9f54c4) harness: constant verify-window shapes (--spec-min-p 0) - [`e1ea03d`](https://github.com/Niko1221/Strata/commit/e1ea03d19558b753bced635f7bb8a5f65a448f7e) harness: one fresh-engine retry per scenario - [`6fa3c23`](https://github.com/Niko1221/Strata/commit/6fa3c23ff9dc4c38bfdaf760e55bd7194a251050) harness: pin the adaptive tier the owner's way (--adapt-every 100000) - [`68ed5d5`](https://github.com/Niko1221/Strata/commit/68ed5d55b2d0622e9222f0286a10946ea7046b75) harness: a paired control for every scenario ### 📊 Changes **22 files changed** (+3893 additions, -217 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+11 -0) ➕ `include/strata/core/crash_report.hpp` (+100 -0) ➕ `include/strata/core/expert_pack.hpp` (+43 -0) ➕ `include/strata/core/prefill_preempt_budget.hpp` (+44 -0) 📝 `include/strata/prefill/prefill.hpp` (+11 -0) ➕ `serve/engine_gate.py` (+65 -0) 📝 `serve/server.py` (+548 -6) ➕ `serve/test_engine_gate.py` (+101 -0) ➕ `serve/test_preempt.py` (+519 -0) ➕ `serve/test_preempt_regressions.py` (+170 -0) ➕ `src/core/crash_report_test.cpp` (+87 -0) ➕ `src/core/expert_pack_test.cpp` (+66 -0) ➕ `src/core/prefill_preempt_budget_test.cpp` (+38 -0) 📝 `src/core/verify.cpp` (+23 -0) 📝 `src/prefill/prefill.cpp` (+10 -0) 📝 `src/program/generate.cpp` (+788 -211) ➕ `tools/kv_stream_sweep.py` (+266 -0) ➕ `tools/prefill_preempt_bench.py` (+156 -0) ➕ `tools/prefill_preempt_regression.sh` (+30 -0) ➕ `tools/prefill_preempt_test.py` (+596 -0) _...and 2 more files_ </details> ### 📄 Description ## Problem Serving is sequential by design: the FIFO holds a mutex for a request's entire lifecycle, so a short interactive request sent during a long prefill waits for the whole prefill plus decode. For orchestrator + sub-agent workloads sharing one Strata server, this resulted in tens of seconds of head-of-line blocking. **Measured example:** `40.5 s` of queue wait behind a `50K`-token prefill. --- ## Solution This PR adds **cooperative prefill preemption at safe chunk boundaries**. A long-running request **A** (for example, a `200K`-token prompt) can park at a completed prefill chunk boundary, allowing a short request **B** to run immediately. Once B finishes, A is restored and continues as if it had never been interrupted. The execution model remains intentionally simple: - still **one sequence at a time** - no decode batching - no concurrent slots ### Engine (`--prefill-preempt`, opt-in) - `Prefill::should_suspend` - Checked once per **completed chunk** - Single-GPU path only - Suspension happens only after the chunk completes - The normal `run` tail still executes, which: - commits `ple_prev` for the boundary - drains the compute stream - drains the copy stream - `SuspReq` - Stores a complete suspension snapshot: - the running half via the existing checkpoint machinery - positional state of every owned QSA layer: - KV blocks - pooled rows - spare key - `idx_block_pos` - drafter KV state - Admission is **copy-only** - If snapshot creation fails, request A remains live and continues reading normally - Engine protocol: - `SUSPENDED <pos> <total> id=N` - `RESUME id=N` - `CANCEL id=N` - `YIELD` - boundary offer emitted by the server - the engine itself cannot see the server request queue - Output lines may carry an `id=N` suffix for demultiplexing - Requests without an ID behave exactly as before - Preemption gates: - minimum park position - per-request park cap - snapshot RAM budget admission (`T18`) - single GPU only - layer split fails closed ### Server (`--prefill-preempt`, opt-in) The FIFO lock is split into **ownership periods** instead of being held for the request's entire lifecycle: ```text GEN -> SUSPENDED RESUME -> next SUSPENDED / DONE ``` A parked request waits outside the FIFO lock and receives keep-alives while suspended. Additional behavior: - if the parked request's waiter disappears, the park is withdrawn in place - if the engine dies while a request is parked, the request fails cleanly - `EngineRequest` supports `id=N` suffix demultiplexing New observability: `/metrics` - `prefill_preemptions` - `prefill_resumes` - `max_queue_wait_ms` `/status` - `parked_requests` --- ## Results Test system: - **GPU:** RTX 4070 Ti SUPER - **Quant:** IQ3_XXS - **VRAM:** 16 GB | Metric | Feature off | Feature on | |---|---:|---:| | B queue wait (`A prefill = 50K`) | `40.5 s` | `2.9 s` (**-93%**) | | A total wall time | `43.8 s` | `45.3 s` (**+3.4%**) | The task targets were: - **≥ 80% reduction** in queue wait - **< 10% penalty** to the interrupted request Both targets are met with margin. --- ## Verification ### Bit-exact parity Tokens and the complete state fingerprint are identical between: 1. uninterrupted request A 2. request A after `park -> interim request -> restore` Verified state includes: - `gdn` - `ple` - `tails` - `pooled` - `kv` - `dead` - `pooled_full` - `mtp` - `ple_prev` Tested suspension boundaries: - `4096` - `8192` - `12288` An interim request was executed between park and restore with decode depths: - `0` - `1` - `32` The drafter KV state is included in the parity check. ### Trace diagnostics Added diagnostics include: - `STATE_POINT` - `VERIFY_POINT` - `WINDOW` These provide: - per-phase state hashes - per-row verification-window fingerprints - position - offered-T count - accepted count ### GPU scenarios **12 GPU scenarios: ALL PASS** Coverage includes: - boundary tests ×5 - park-only tests ×3: - early - mid - late - KV streaming: - mode `1` - residency-map reset - repeat-3: - three consecutive parks - cancel while parked - verification that decode never yields ### Scheduler tests Fake-engine scheduler suite: **9 tests** Coverage includes: - no contention - preemption when another request queues - FIFO ordering - parked-request cancellation - queued-request cancellation - engine death while parked - shutdown without deadlock ### Existing test suites - `ctest`: **63/65** - Python: **137 OK** The two failing `ctest` cases are environmental and reproduce on pristine `main`. --- ## Bugs Found and Fixed During Verification The tests caught several issues during development: - A stale `YIELD` could re-park a resumed request at its next boundary. - `preempt_count` was saved before the increment, allowing `--prefill-preempt-max` to be bypassed. - A resumed tail with length `<= --short-read` could switch to verifier windows differently from an uninterrupted read. - A lend-size layout mismatch caused an `E-9` failure across buffer layouts. All of these were fixed before the final validation runs. --- ## Limitations / Future Work Current limitations: - only **one parked request** at a time - image requests never park - long-queue fairness currently uses a bounded wait: - `--prefill-preempt-max-wait-s` - default: `30 s` - this is not yet a full scheduler - snapshot RAM is not enforced through a single hard allocation budget - admission estimates the required memory and refuses before the large allocation - snapshot vectors are still separate allocations - allocation failure can therefore still surface as `bad_alloc` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:02:49 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2748
No description provided.