Repository navigation
[Bug] Index LoRA adapter blocks per adapter in KV event sync - #3097
Conversation
Blocks vLLM stored for an adapter went into the base model's context, so base requests matched them and adapter requests never matched engine state. Key them by the adapter's lora_name, and clear them on BlockRemoved and pod removal. Signed-off-by: MinJun Choi <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates the prefix cache router to index and route LoRA adapter blocks separately from their base models. It introduces helper functions to derive LoRA IDs from adapter names, updates block storage and removal logic to handle adapter-specific contexts, and ensures pods are removed from all model contexts. Feedback suggests using defensive type assertions when iterating over the sync.Map in RemovePrefix to prevent potential runtime panics and ensure type safety.
varungup90
left a comment
There was a problem hiding this comment.
Review: index LoRA adapter blocks per adapter in KV event sync
Verdict: approve with nits (recommendation only; I have not submitted a formal approval).
What I checked
- The bug is real. On
main,BlockStoredevents are keyed by the pod's model name andLoraIDfrom the subscription, andevent.LoraNameis ignored, while the router looks adapter requests up under the adapter's own name with-1. The two never meet, and base requests can match blocks that belong to an adapter. - Store side:
ctx.LoraID = LoraIDFromName(event.LoraName)is a 63-bit xxhash, so it can't be-1and collisions are negligible. Lookup side:indexContextmaps the adapter to its base model throughModelBaseModeland uses the same ID, inScoreAll,RouteandPostRouteUpdate. BlockRemovedhas no adapter name, so the new code finds contexts through the existingblockIndexand removes the hash from every context of that model that holds it. Because removal stays scoped bySourcePod, another pod's entry is not touched. I probed the worst case, where two pods store the same engine hash under the base model and under two different adapters: removing it from one pod left the other pod's entries in the other contexts intact.RemovePrefixnow clears the pod from every context of the model. All three callers (pod deletion,AllBlocksClearedand the sleep backstop) discard the pod's entire cache, so I agree with the change. A sibling model name is not affected.- The new
blockIndexscan releases its read lock before callingremoveBlocksFromContext, which takes the write lock inupdateBlockIndex, so there is no lock-order problem.
What I ran
gofmt -landgo veton the two touched packages: clean.go test -race ./pkg/utils/syncprefixcacheindexer/...,go test ./pkg/plugins/gateway/...andgo test ./pkg/cache/...: pass.- Mutation checks (production code altered, PR tests kept): ignoring
LoraNameon store, limitingRemovePrefixto one context, limitingBlockRemovedto the event's own context, and makingindexContextreturn(model, -1)each make one of the two new tests fail. So the tests do constrain the behaviour. - A throw-away concurrent store/match/remove/
RemovePrefixprobe under-race -count=3: clean (deleted afterwards). - CI on this head is green: unit, race, integration, lint, verify, coverage, smoke, installation E2E, Volcano gang E2E and DCO.
Not run
go test -tags zmq ./pkg/kvevent/...:libzmqisn't available here, so that package doesn't build locally. The PR doesn't touch it; the CI results above are all I have for it.golangci-lintlocally.
Nits
- Test gap on
RouteandPostRouteUpdate(inline comment). Reverting either call site tomodelName, -1leaves the wholeprefixcachepackage green. OnlyScoreAllis covered. RemovePrefixignoresloraID(inline comment). Document it on the interface method (or rename the parameter), since the signature suggests otherwise.- Base-model name must agree on both sides (inline comment). Stored blocks use the pod label; lookups use
spec.baseModel. If they ever differ, adapters silently keep scoring 0, as before this PR. - Stale doc comment:
pkg/kvevent/interfaces.go:86-88still saysLoraNameis "Reserved for per-adapter index isolation". The PR updatedevents.gobut not this one. - FYI, not a request: every adapter now has its own context, so many adapters per model count against
AIBRIX_SYNC_MAX_CONTEXTS(default 1000) and become eviction candidates earlier than before.
Generated by Claude Code
| // the adapter name, which is also the model name the request uses. | ||
| func (k *kvSyncPrefixCacheRouter) indexContext(model string) (string, int64) { | ||
| if k.cache != nil { | ||
| if base, ok := k.cache.ModelBaseModel(model); ok { |
There was a problem hiding this comment.
One thing to be aware of: the two sides of the match are derived from different sources. Stored blocks are keyed by the pod's metadata model name (constants.ModelNameFromMetadata, the model.aibrix.ai/name label), while this lookup uses ModelAdapter.spec.baseModel (falling back to the host pod's label only when spec.baseModel is empty, per setModelBaseModelLocked). The samples keep the two equal, but nothing enforces it. If an adapter's spec.baseModel differs from the pods' label, adapter requests would silently score 0 on every pod, which is where they were before this PR (so not a regression).
It would help to say in the comment that the two must agree, and possibly to log once at V(4) when ModelBaseModel resolves but no context exists for it. This is a note, not something I expect in this PR.
Generated by Claude Code
There was a problem hiding this comment.
Thanks for spelling this out. I added a line to the comment saying the two need to match, and left the log out of this PR as you suggested.
| return fmt.Errorf("sync indexer not available for KV sync routing") | ||
| } | ||
| _, prefixHashes := k.syncIndexer.MatchPrefix(modelName, int64(-1), tokens, readyPodsMap) | ||
| indexModel, loraID := k.indexContext(modelName) |
There was a problem hiding this comment.
Nit (test gap): TestKVSyncScoreAllMatchesAdapterBlocks covers ScoreAll, but not the other two call sites of indexContext: this one in PostRouteUpdate and the one in Route. I reverted each of them to modelName, -1 locally and the whole prefixcache package still passed, so a future change could undo either without a test failing. Route is the production path for adapter requests, and PostRouteUpdate is where the speculative AddPrefix goes: before this PR that call created contexts named after the adapter.
A test that routes an adapter request through Route and then checks that the prefix was added under the base model's adapter context (not under a context named after the adapter) would cover both.
Generated by Claude Code
There was a problem hiding this comment.
You're right, only ScoreAll was covered. I added a test for each of Route and PostRouteUpdate, and they also check that nothing gets indexed under the adapter's own name.
| // RemovePrefix removes a specific pod from all prefix entries of the model. | ||
| // Callers drop a pod whose whole cache is gone, so the pod is removed from the | ||
| // model's adapter contexts too, whatever loraID is passed. | ||
| func (s *SyncPrefixHashTable) RemovePrefix(modelName string, loraID int64, podName string) error { |
There was a problem hiding this comment.
Nit: loraID is now ignored, but the signature and the SyncIndexer interface in pkg/kvevent/interfaces.go (which has no doc for RemovePrefix) still suggest it scopes the call. I read all three callers (manager.go pod deletion, PurgePodPrefixCache for AllBlocksCleared, and CheckSleepStateBackstop) and agree each one drops the pod's whole cache, so the behaviour looks right. But the next caller that wants to clear one adapter would be surprised. Please put the "removes the pod from every context of the model" contract on the interface method too, or rename the parameter to _.
I checked the sibling cases with a throw-away probe (deleted): RemovePrefix(model+"-2", ...) leaves this model's contexts untouched, and ProcessBlockRemoved stays scoped by SourcePod even when two pods use the same engine hash under different contexts. The bot's suggestion to guard the type assertions here isn't needed: the neighbouring code does the same unchecked value.(*ContextData) and the map only ever holds those types.
Generated by Claude Code
There was a problem hiding this comment.
That makes sense. I added the same note to the interface method so it's clear from there too.
Add tests that fail if either call site goes back to the adapter name with -1, document that RemovePrefix clears every context of the model, and note that an adapter's baseModel has to match the pods' model label. Signed-off-by: MinJun Choi <[email protected]>
|
@varungup90 Thanks a lot for such a careful review. The mutation checks and the race probe were really helpful. I pushed b1be173 for the nits:
I hadn't thought about the context limit, so thanks for bringing it up. I've left it alone in this PR, but I'll keep it in mind. |
Pull Request Description
With KV event sync on, blocks that vLLM stores for a LoRA adapter were indexed under the base model, because the key came from the pod and the event's
lora_namewas ignored. The router, for its part, looked up adapter requests by the adapter name withloraID = -1. As a result, base requests matched adapter blocks that vLLM can't reuse for them (its block hashes include the adapter), and adapter requests never matched anything the engine reported.This keeps each adapter's blocks in their own context:
BlockStoredevents that carry alora_nameare indexed under the base model with a LoRA ID derived from that name (LoraIDFromName).ModelBaseModel, both when routing and in the post-route update. This replaces theloraID := -1 // TODO.BlockRemovedevents don't carry the adapter name, so the blocks are removed from whichever contexts of the model indexed them, using the existing block index.RemovePrefixnow drops the pod from the model's adapter contexts as well. It's called on pod deletion,AllBlocksClearedand sleep, and in each case the pod's whole cache is gone.Nothing changes for base-model traffic or for the approximate prefix-cache path.
Notes for reviewers
MatchPrefix/AddPrefixsignatures stay the same across indexers. Happy to change it to a string key if you'd prefer that.RemovePrefixno longer limits itself to theloraIDit's given. Every caller is clearing a pod whose cache is entirely gone, and the adapter contexts need clearing too.Testing
TestAdapterBlocksAreIndexedPerAdapterchecks that adapter blocks match only the adapter, and that aBlockRemovedwithout an adapter name andRemovePrefixboth clear them.TestKVSyncScoreAllMatchesAdapterBlocksroutes an adapter request and checks that it scores the pod holding the adapter's blocks.go testpasses forpkg/utils/syncprefixcacheindexer(with-race),pkg/plugins/gateway/...,pkg/cache/...andpkg/kvevent(with-tags zmq), and golangci-lint reports no issues.Related Issues
Related: #2285 (the LoRA part left after #2384 and #2587)