Repository navigation
[None][fix] prevent multimodal scheduling stalls on the KV cache V2 scheduler - #19801
Conversation
…cheduler KVCacheV2Scheduler has no separate capacity stage, so MultimodalScheduler schedules the V2 batch first and then selects MM encoder items among the contexts V2 returned. By then V2 has charged each context to its token budget and grown its KV cache, which allows two permanent stalls: - V2 returns non-last chunks first. With chunked prefill, a newer multi-chunk request therefore takes the encoder budget ahead of older requests. A withheld older context whose outputs cannot fit beside the newer request's keeps the KV cache V2 grew for it, which the newer request needs to finish its prefill. - A context that cannot start encoding while live outputs fill the output budget still takes V2's token budget every pass. A newer, partially encoded request that holds those outputs is then never offered its remaining items. On this combined path MultimodalScheduler now: - passes the requests that hold encoder outputs to V2 ahead of the oldest request the output budget blocks, so that request cannot take the token budget they need to drain. Everything else keeps FCFS order. - selects items over V2's contexts in admission order, and keeps V2's order for the LLM batch. - releases the KV cache of a withheld context that the output budget blocks. This reuses V2's failed-admission cleanup, extracted as KVCacheV2Scheduler.release_context_admission. A context that only lost the race for encoder slots or tokens keeps its KV cache, as before. Add CPU tests over a real KVCacheV2Scheduler: one per stall, one showing that a request waiting for output bytes does not hold back newer ready requests, and one showing that a context that lost the encoder slot keeps its KV cache. The shared MM request fixtures take a prompt length and embedding lengths so the tests can reuse them. Signed-off-by: yechank <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (4)
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review. WalkthroughChangesMultimodal scheduler budget control
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix Sequence Diagram(s)sequenceDiagram
participant KVCacheV2Scheduler
participant MultimodalScheduler
participant KVCacheManagerV2
KVCacheV2Scheduler->>MultimodalScheduler: Provide active contexts
MultimodalScheduler->>KVCacheManagerV2: Read resident KV allocations
MultimodalScheduler->>MultimodalScheduler: Reorder requests by output budget
MultimodalScheduler->>KVCacheV2Scheduler: Release blocked context admission
MultimodalScheduler->>KVCacheManagerV2: Record selected encoder allocations
Suggested reviewers: Merge Risk: ⚪ Minimal · up to The new tests cover the reported scheduling stalls and admission cleanup. No demonstrated issue currently blocks merging after normal checks. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Comment |
|
/bot run --disable-fail-fast |
|
PR_Github #76316 [ run ] triggered by Bot. Commit: |
|
PR_Github #76316 [ run ] completed with state
|
|
/bot run --disable-fail-fast |
|
PR_Github #76391 [ run ] triggered by Bot. Commit: |
|
PR_Github #76391 [ run ] completed with state |
|
Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements. |
Dev Engineer Review
MultimodalSchedulernow prioritizes resident-output holders when an unstarted request cannot fit within the output-byte budget. It spends encoder budgets in admission order while preserving the combined scheduler’s LLM batch order. When output capacity blocks a context, it releases that context’s KV-cache admission; contexts blocked only by encoder slots or tokens keep their admission.KVCacheV2Scheduler.release_context_admissionfrees the request’s allocations from configured KV-cache managers and rewinds its context cursor.RequestSchedulerprovides a default no-op hook for schedulers that do not allocate KV cache during scheduling. Verify that custom schedulers using this path implement the hook if they allocate resources. The current changes target the item-scheduled multimodal path with KV cache V2.QA Engineer Review
The changes add four unit tests in
tests/unittest/_torch/executor/test_multimodal_scheduler.py. They cover encoder-budget ordering, starvation of partially encoded work, progress by newer requests while output capacity is blocked, and retaining KV cache when only an encoder slot blocks a request. The helper updates inmultimodal_utils.pysupport configurable prompt and embedding lengths.No reference to
test_multimodal_schedulerappears in the inspectedtests/integration/test_lists/files. These are unit tests, and no integration-list change is included. The provided context reports that the changed test file passes, but no test execution result was supplied in this inspection. Coverage verdict: sufficient for the reported stall scenarios; it does not establish that broader test suites pass.Per-File QA Perspective
tensorrt_llm/_torch/pyexecutor/scheduler/scheduler.py: Verify holder-first ordering, admission-order encoder selection, preserved LLM batch ordering, and KV-cache release only for contexts blocked by output capacity.tensorrt_llm/_torch/pyexecutor/scheduler/scheduler_v2.py: Verify that releasing admission frees resources across target, draft, and cross-KV managers and rewinds the context cursor. Failed first-chunk admissions now use the same method.tests/unittest/_torch/executor/multimodal_utils.py: Verify thatprompt_lenandembedding_lengthsgenerate consistent token inputs and multimodal metadata. These helpers have new optional parameters and defaults.tests/unittest/_torch/executor/test_multimodal_scheduler.py: Four new tests cover the combined V2 scheduler’s ordering, output-budget waits, and encoder-slot waits. No matching integration test-list reference was found.Description
Problem. For multimodal models on KV cache manager V2,
MultimodalSchedulerwrapsKVCacheV2Scheduler. V2 has no separate capacity stage, so the wrapper takes its combined path: V2 schedules the batch, then the wrapper selects MM encoder items among V2's context requests and withholds contexts that would still lack embeddings. By then V2 has already admitted each of those contexts: it charged the token budget and a request slot, and grew the context's KV cache. This allows two permanent stalls onmain. Neither reports an error: the executor keeps scheduling empty batches, V2's deadlock check only looks at generation requests, and the hang detector stays quiet because the loop keeps iterating.max_num_tokens=60, an 80-token KV pool, 2 encoder slots and a 12 B output budget (3 embedding rows).[C, A, B], because it puts non-last chunks first. The wrapper visits C first, and C's images take both encoder slots. A and B are withheld, but they keep the 50 KV tokens V2 grew for them.max_num_tokens=50, a 100-token KV pool, 1 encoder slot and a 32 B output budget (8 rows). Running decodes hold 60 KV tokens in pass 1 only.Both examples admit every request in the first pass. That happens under attention DP, which skips the executor's MM admission gate (
_apply_mm_encoder_admission). With the gate, these exact inputs finish onmain, because the gate defers the newer requests. The stalls then need staggered arrivals: 7 of 3000 fuzz cases and 6 of 600 stress cases in check 1 below.Root cause.
_select_itemsassumes FCFS order.Fix. The change is confined to the combined branch of
MultimodalScheduler.schedule_request, plus a behavior-preserving extraction in V2:_holders_first), so it cannot take the token budget they need to drain. Everything else keeps FCFS order, and no request is removed from V2's input._select_itemsvisits V2's admitted contexts inactive_requestsorder. The LLM batch keeps V2's order.KVCacheV2Scheduler.release_context_admission. That method is V2's existing cleanup for failed first-chunk admissions, moved into a method unchanged. A context withheld only for encoder slots or tokens keeps its admission, as onmain; FCFS serves it next.RequestScheduler.release_context_admissionis a no-op by default, so the wrapper needs no type checks.Hiding the blocked request from V2 also fixes stall 2. But newer requests can then start ahead of it and deadlock with it in V2's KV cache, unless they are hidden too, which holds back text requests. Passing the holders first keeps every other request where it is.
Why it is safe.
KVCacheV2Scheduler. The separate-stage path,MultimodalEagerEncoderScheduler, the executor and the LLM args are untouched.free_resourcesreleases its unstarted prefix reservation, asrelease_unused_connector_reservationsalready does for contexts excluded from the batch.main. Both drive the realKVCacheV2Scheduler,MultimodalScheduler, request admission and admission gate over a toy KV pool.mainand with this change; with the gate they are the same 15. Without the gate, onemainmultimodal stall (seed 104) ends in a V2 KV deadlock instead, and onemainV2 KV deadlock (seed 321) now finishes. No case that finishes onmainstalls.mainand finish with this change, and 4 go the other way. Those 4 are V2 KV deadlocks thatmainavoids only by timing: in 2, two decodes outgrow a pool with no host tier and V2 cannot recompute-pause the multimodal one; the other 2 are the partially encoded holder pattern in Limitations. Any change to admission timing moves V2 KV deadlocks between cases in both directions.Behavior changes.
scheduled_mm_encoder_itemsfollow that order.main.KVCacheManagerV2, against 2-3 us formain's re-admission of a context that kept its KV cache. Contexts withheld for encoder slots or tokens are not affected.Scope.
supports_mm_encoder_item_schedulingfromQwen3VLModelBase, and default to theDEFAULTencoder scheduling policy. It also covers any item-scheduled model run withuse_kv_cache_manager_v2=True.py_executor_creator.py). V2 therefore returns multi-chunk contexts even withoutenable_chunked_prefill.Limitations (unchanged by this PR).
mainas well. A V2 follow-up could let the generation eviction path suspend such contexts.main.Relation to #17876. I found this while reviewing #17876 (multimodal encoder cache unification), but the bug exists on
main. #17876 does not touchscheduler_v2.pyand keeps the same combined branch, so the port there is a small follow-up: "holds encoder outputs" becomes "holds encoder-cache entries", and the byte check becomes a read-only capacity query on itsTensorLRUCache.Test Coverage
New tests in
tests/unittest/_torch/executor/test_multimodal_scheduler.py. They are CPU-only and drive a realKVCacheV2Schedulerwith a mock KV pool. They run in theunittest/_torch/executorentries of the GPU stages, like the rest of the module. Results are the finish pass per request id.test_v2_chunked_prefill_spends_encoder_budget_in_admission_order(stall 1):{1: 2}onmain, where B and C never finish;{1: 1, 3: 3, 2: 4}with this PR.test_v2_partially_encoded_request_is_not_starved_by_older_context(stall 2):{}onmain;{2: 2, 1: 4, 3: 5}with this PR.test_v2_output_budget_wait_does_not_hold_back_newer_requests: a text request that arrives while an MM request waits for output bytes finishes in pass 2, as onmain.test_v2_context_that_lost_the_encoder_slot_keeps_its_kv_cache: a request that waits only for the encoder slot keeps its KV cache while decodes grow, and finishes in pass 3, as onmain.maintoo. They pin the two properties that the alternatives above would break: hiding the newer requests delays the text request to pass 5, and releasing every withheld context delays the second request to pass 5.tests/unittest/_torch/executor/multimodal_utils.py:make_llm_requestandmake_mm_requesttake an optional prompt length and embedding lengths, so the new tests reuse them. Existing callers are unchanged.Commands, run in the dev container on the PostMerge build that matches
95cb50c358:pytest tests/unittest/_torch/executor/test_multimodal_scheduler.pywithscheduler.pyandscheduler_v2.pyfrommain: the two stall tests fail as above; the other 40 tests pass. With this PR all 42 pass.pytest tests/unittest/_torch/executor/test_multimodal_scheduler.py tests/unittest/_torch/executor/kv_cache/test_kv_cache_v2_scheduler.py tests/unittest/_torch/executor/engine/test_multimodal.py tests/unittest/_torch/executor/test_scheduler_serializable_output.py tests/unittest/_torch/executor/test_recompute_pause.py tests/unittest/_torch/executor/test_py_scheduler.py tests/unittest/_torch/executor/kv_cache/test_dual_pool_kv_cache.py tests/unittest/_torch/executor/test_kv_connector_v2_prefix_real_manager.py: 498 passed.tests/unittest/_torch/executor/(kv_cache/test_kv_cache_manager_v2_helix_superblock.py,kv_cache/test_kv_cache_v2_{capacity_only,extra_buffers,first_new_block_probe,multimodal_runs,vocab_size}.py,kv_cache/test_kvv2_host_tier_sizing.py,test_kv_connector_v2_prefix.py,test_token_budget_fallback.py): 196 passed. The 11 failures intest_kv_cache_v2_vocab_size.pyare pre-existing and waived under nvbugs/6818710.enable_chunked_prefill=True,max_num_tokens=256,encoder_max_batch_size=1.MultimodalScheduleroverKVCacheV2Scheduler; 22 had multi-chunk contexts, and in 5 V2's context order differed from FCFS. All requests completed, token-identical tomain.release_context_admissionon the realMambaHybridCacheManagerV2.mainalso completed; 13 of 14 outputs match, and one text answer reads "two" instead of "2", consistent with a different batch composition. With the gate on, the outputs matchmainexactly.pre-commit run --files <changed files>: all hooks pass.PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
To see a list of available CI bot commands, please comment
/bot help.🤖 Generated with Claude Code