[None][perf] Memoize multimodal run metadata and drop identity block-offset gather#16232
Open
eopXD wants to merge 1 commit into
Open
[None][perf] Memoize multimodal run metadata and drop identity block-offset gather#16232eopXD wants to merge 1 commit into
eopXD wants to merge 1 commit into
Conversation
…offset gather Two host-side overhead reductions in the KV cache managers. Both are performance-only with no external behavior change. - kv_cache_manager_v2: memoize _resolve_multimodal_run_metadata on the request. Block reuse resolves it on the first context chunk, on every commit during chunked prefill, and on prefetch, but the result is a pure function of the request's immutable multimodal run layout, so the CPU tensors it builds were rebuilt identically on every call. Cache the result on the request; its lifetime bounds the cache, so no teardown is needed. This resolves the existing TODO(perf). - resource_manager: drop the identity gather in BlockManager.copy_block_offsets. base_block_offsets was a torch.arange, so base_block_offsets[block_ids] returns block_ids unchanged; convert the block ids directly and remove the now-unused arange buffer. Signed-off-by: Yueh-Ting Chen <yuehtingc@nvidia.com>
Collaborator
Author
|
/bot run |
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PyExecutor now caches multimodal run metadata per request and copies block IDs directly into destination tensors without a precomputed CPU offset tensor. ChangesMultimodal metadata caching
Block offset copying
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Collaborator
|
PR_Github #58648 [ run ] triggered by Bot. Commit: |
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.
Description
Two host-side overhead reductions in the KV cache managers. Both are performance-only with no external behavior change.
Memoize per-request multimodal run metadata (
kv_cache_manager_v2.py)._resolve_multimodal_run_metadatais resolved on the first context chunk, again on every commit during chunked prefill, and on prefetch. Its result is a pure function of the request's immutable multimodal run layout (multimodal_item_run_cu_offsets/multimodal_run_positions/multimodal_run_lengths), so the CPU tensors it builds (arange/repeat_interleave/cumsum/cat/ int64 casts) were being rebuilt identically on every call. The result is now cached on the request, whose lifetime bounds the cache, so no separate teardown is needed. This resolves the existingTODO(perf). The non-Nonepath is exercised by the VLMs that populate run metadata (Qwen2VL, Qwen3VL, LLaVA-Next, Kimi-K2.5, Nemotron-Nano).Remove an identity gather (
resource_manager.py,BlockManager.copy_block_offsets).base_block_offsetswas atorch.arange, sobase_block_offsets[block_ids]returnsblock_idsunchanged. The block ids are now converted directly and the unusedbase_block_offsetsbuffer is removed (also dropping its per-manager allocation). ThisBlockManagerbacks the RocketKV KT cache.Test Coverage
No new tests are added: both changes are performance-only with no external behavior change. Existing coverage exercises the touched paths:
tests/unittest/_torch/executor/test_kv_cache_v2_multimodal_runs.py— calls_augment_tokens_for_block_reusemultiple times on the same request, exercising the memoized_resolve_multimodal_run_metadataon both the cache-miss (build) and cache-hit (return) paths, as well as the contiguousNonepath.tests/unittest/_torch/executor/test_kv_cache_manager_v2.py— KV cache manager v2 behavior.tests/unittest/_torch/attention/sparse/rocketkv/test_rocketkv.py— RocketKV path that callsBlockManager.copy_block_offsets.PR Checklist
No API changes; no new dependencies; no CODEOWNERS or documentation changes required.
Please check this after reviewing the above items as appropriate for this PR.
Summary by CodeRabbit