fix(engine): implement late merge to deduplicate blocks for same-step prefix sharing#243
Open
fishAndShrimp wants to merge 1 commit into
Open
Conversation
Issue: When multiple identical prompts are scheduled in the same prefill step, they overwrite each other's hash mapping, leading to duplicate physical blocks and memory waste. Furthermore, due to the engine's lazy deletion mechanism, merely finding a hash in `hash_to_block_id` does not guarantee a valid cache hit. A block might have been freed (ref_count == 0) and returned to the free queue, but its hash mapping is left dangling for potential future reuse. Blindly reusing a dangling hash without checking its active status would corrupt the free block queue. Fix: Deduplicate blocks during `hash_blocks` by strictly validating cache hits against `used_block_ids`. This safely handles dangling hash mappings without mutating the free block queue. Specifically, this commit introduces robust cache hit handling: - Checks if the computed hash exists in `hash_to_block_id` AND verifies the corresponding block is actively alive (exists in `used_block_ids`). - If it is a valid hit, safely deallocates the redundant block newly assigned to the sequence. - Updates the sequence's block table to point to the existing shared block. - Increments the reference count of the shared block. - If the hash exists but the block is not in `used_block_ids` (a dangling mapping), it treats it as a miss and safely updates the mapping to the newly allocated block.
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.
🔗 Related Issue
Closes #219
🧭 Context & Problem
As discussed in #219, when multiple identical prompts are scheduled in the same prefill step, they experience a cache miss and are allocated separate physical blocks. During
postprocess(), they overwrite each other's hash mapping inhash_to_block_id, leading to redundant VRAM usage.Furthermore, due to the engine's lazy deletion mechanism, merely finding a hash in
hash_to_block_iddoes not guarantee a valid cache hit. A block might have been freed (ref_count == 0) and returned to the free queue, but its hash mapping is left dangling. Blindly reusing a dangling hash without checking its active status would corrupt the free block queue.🛠️ The Fix (Late Merge)
This PR implements the "Late Merge" approach proposed in the issue. It deduplicates blocks during
hash_blocksby strictly validating cache hits againstused_block_idsand token contents.Specifically, the logic now:
hash_to_block_idAND verifies the corresponding block is actively alive (exists inused_block_ids).existing_block.token_ids == token_ids), it deallocates the redundant block, updates the sequence's block table to the shared block, and increments theref_count.token_idsdiffer), the logic naturally falls through to the original behavior. It simply overwrites thehash_to_block_idmapping with the new block. The previous block remains perfectly safe and active in its sequence; it merely loses its global mapping (graceful degradation).🧪 Testing & Verification
I have verified this fix using an end-to-end Minimal Reproducible Example (MRE) that sends concurrent identical requests and monitors the
BlockManagerstate step-by-step.Here is a quick summary of the engine state during the Prefill Phase (Step 1) before and after the fix:
ref_count: 2)MRE Code & Execution Logs
Below are the exact script and logs used to verify the correct behavior of the Late Merge deduplication and reference counting.
💻 Click to expand: MRE Script
❌ Click to expand: Log BEFORE Fix (Bug Present)
✅ Click to expand: Log AFTER Fix (Bug Resolved)