-
Notifications
You must be signed in to change notification settings - Fork 60
feat: profiling kv cache index implementations (#108) #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,178 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Copyright 2025 The llm-d Authors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You may obtain a copy of the License at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| limitations under the License. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "flag" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "math/rand/v2" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/llm-d/llm-d-kv-cache-manager/pkg/kvcache/kvblock" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "k8s.io/apimachinery/pkg/util/sets" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const modelName = "bert-base-uncased" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type IndexProfileResult struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AddTime time.Duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LookupTime time.Duration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // TODO @kartikx: Use a more realistic workload if possible. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func generateWorkloadKeys(numKeys int) []kvblock.Key { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Uses time as seed to ensure that different profiling runs get different keys. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| randGen := rand.New(rand.NewPCG(42, uint64(time.Now().UnixNano()))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you generate the same workload keys for all profiling sessions? That way, the only difference will be the indexer implementation. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keys := make([]kvblock.Key, numKeys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i := range numKeys { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keys[i] = kvblock.Key{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ModelName: modelName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ChunkHash: randGen.Uint64(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func averageIndexProfileResults(durations []IndexProfileResult) IndexProfileResult { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(durations) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return IndexProfileResult{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var total IndexProfileResult | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, d := range durations { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total.AddTime += d.AddTime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total.LookupTime += d.LookupTime | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count := time.Duration(len(durations)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return IndexProfileResult{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AddTime: total.AddTime / count, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LookupTime: total.LookupTime / count, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func profileInMemoryIndex(numTrials, numKeys int) (IndexProfileResult, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return runProfileTrials(func() *kvblock.IndexConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return kvblock.DefaultIndexConfig() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, numTrials, numKeys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func profileRedisIndex(numTrials, numKeys int) (IndexProfileResult, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return runProfileTrials(func() *kvblock.IndexConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &kvblock.IndexConfig{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RedisConfig: kvblock.DefaultRedisIndexConfig(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EnableMetrics: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, numTrials, numKeys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func profileCostIndex(numTrials, numKeys int) (IndexProfileResult, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return runProfileTrials(func() *kvblock.IndexConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &kvblock.IndexConfig{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CostAwareMemoryConfig: kvblock.DefaultCostAwareMemoryIndexConfig(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EnableMetrics: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, numTrials, numKeys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // runProfileTrials returns averaged results over multiple profiling runs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func runProfileTrials(createConfig func() *kvblock.IndexConfig, numTrials, numKeys int) (IndexProfileResult, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| profileResults := make([]IndexProfileResult, numTrials) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i := range numTrials { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx := context.Background() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| indexConfig := createConfig() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| index, err := kvblock.NewIndex(ctx, indexConfig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return IndexProfileResult{}, fmt.Errorf("failed to create index: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result, err := measureIndexRun(ctx, index, "pod1", numKeys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return IndexProfileResult{}, fmt.Errorf("failed to profile index: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+100
to
+113
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for i := range numTrials { | |
| ctx := context.Background() | |
| indexConfig := createConfig() | |
| index, err := kvblock.NewIndex(ctx, indexConfig) | |
| if err != nil { | |
| return IndexProfileResult{}, fmt.Errorf("failed to create index: %w", err) | |
| } | |
| result, err := measureIndexRun(ctx, index, "pod1", numKeys) | |
| if err != nil { | |
| return IndexProfileResult{}, fmt.Errorf("failed to profile index: %w", err) | |
| } | |
| ctx := context.Background() | |
| indexConfig := createConfig() | |
| index, err := kvblock.NewIndex(ctx, indexConfig) | |
| if err != nil { | |
| return IndexProfileResult{}, fmt.Errorf("failed to create index: %w", err) | |
| } | |
| for i := range numTrials { | |
| result, err := measureIndexRun(ctx, index, "pod1", numKeys) | |
| if err != nil { | |
| return IndexProfileResult{}, fmt.Errorf("failed to profile index: %w", err) | |
| } |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The empty podIdentifierSet means all pods will be returned during lookup (as documented in the Index interface). Consider adding a comment to clarify this is intentional, as it may be confusing why an empty set is used instead of including "pod1" to measure filtered lookup performance.
| podEntries := []kvblock.PodEntry{{PodIdentifier: podName, DeviceTier: "gpu"}} | |
| podEntries := []kvblock.PodEntry{{PodIdentifier: podName, DeviceTier: "gpu"}} | |
| // Intentionally use an empty podIdentifierSet to return all pods during lookup, | |
| // as documented in the Index interface. This measures unfiltered lookup performance. |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing colon after "Lookup" in the output format string. The output format should be consistent with lines 169 and 176 which have a colon after "Lookup".
| fmt.Printf("[Cost Aware] Add: %v Lookup %v \n", result.AddTime, result.LookupTime) | |
| fmt.Printf("[Cost Aware] Add: %v Lookup: %v \n", result.AddTime, result.LookupTime) |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing colon after "Lookup" in the output format string. The output format should be consistent with line 176 which has a colon after "Lookup".
| fmt.Printf("[Redis] Add: %v Lookup %v \n", result.AddTime, result.LookupTime) | |
| fmt.Printf("[Redis] Add: %v Lookup: %v \n", result.AddTime, result.LookupTime) |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra space before the newline. This should be consistent with the format: \n not \n.
| fmt.Printf("[Cost Aware] Add: %v Lookup %v \n", result.AddTime, result.LookupTime) | |
| } | |
| result, err = profileRedisIndex(*numTrials, *numKeys) | |
| if err != nil { | |
| fmt.Printf("Failed to profile redis index: %v\n", err) | |
| } else { | |
| fmt.Printf("[Redis] Add: %v Lookup %v \n", result.AddTime, result.LookupTime) | |
| } | |
| result, err = profileInMemoryIndex(*numTrials, *numKeys) | |
| if err != nil { | |
| fmt.Printf("Failed to profile in memory index: %v\n", err) | |
| } else { | |
| fmt.Printf("[InMemory] Add: %v Lookup %v \n", result.AddTime, result.LookupTime) | |
| fmt.Printf("[Cost Aware] Add: %v Lookup %v\n", result.AddTime, result.LookupTime) | |
| } | |
| result, err = profileRedisIndex(*numTrials, *numKeys) | |
| if err != nil { | |
| fmt.Printf("Failed to profile redis index: %v\n", err) | |
| } else { | |
| fmt.Printf("[Redis] Add: %v Lookup %v\n", result.AddTime, result.LookupTime) | |
| } | |
| result, err = profileInMemoryIndex(*numTrials, *numKeys) | |
| if err != nil { | |
| fmt.Printf("Failed to profile in memory index: %v\n", err) | |
| } else { | |
| fmt.Printf("[InMemory] Add: %v Lookup %v\n", result.AddTime, result.LookupTime) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The random number generator is seeded with a fixed seed (42) combined with the current time, which contradicts the comment on line 39. The fixed seed of 42 means that if the same time value is obtained (or if profiling is run very quickly in succession), the same random keys could be generated. Consider removing the fixed seed and using only the time-based seed:
rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano())))or simply use two different time-based values.