-
Notifications
You must be signed in to change notification settings - Fork 77
add unit test for prefix cache tracking scorer #311
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
Open
zhengkezhou1
wants to merge
1
commit into
llm-d:main
Choose a base branch
from
zhengkezhou1:prefix-cache-scorer-unit-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package scorer_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/llm-d/llm-d-inference-scheduler/pkg/plugins/scorer" | ||
"github.com/stretchr/testify/require" | ||
k8stypes "k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend" | ||
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" | ||
) | ||
|
||
// mockPodScorer is a mock implementation of the scorer.PodScorer interface for testing. | ||
type mockPodScorer struct { | ||
scores map[string]int | ||
err error | ||
} | ||
|
||
func (m *mockPodScorer) GetPodScores(_ context.Context, _, _ string, _ []string) (map[string]int, error) { | ||
if m.err != nil { | ||
return nil, m.err | ||
} | ||
return m.scores, nil | ||
} | ||
|
||
func TestPrefixCacheTracking_Score(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
pods []types.Pod | ||
request *types.LLMRequest | ||
mockScores map[string]int | ||
mockError error | ||
wantScoresByAddress map[string]float64 // Use address as key instead of Pod objects | ||
}{ | ||
{ | ||
name: "test normalized scores", | ||
pods: []types.Pod{ | ||
&types.PodMetrics{ | ||
Pod: &backend.Pod{ | ||
NamespacedName: k8stypes.NamespacedName{Name: "pod-a"}, | ||
Address: "10.0.0.1:8080", | ||
}, | ||
MetricsState: &backendmetrics.MetricsState{ | ||
WaitingQueueSize: 0, | ||
}, | ||
}, | ||
&types.PodMetrics{ | ||
Pod: &backend.Pod{ | ||
NamespacedName: k8stypes.NamespacedName{Name: "pod-b"}, | ||
Address: "10.0.0.2:8080", | ||
}, | ||
MetricsState: &backendmetrics.MetricsState{ | ||
WaitingQueueSize: 1, | ||
}, | ||
}, | ||
&types.PodMetrics{ | ||
Pod: &backend.Pod{ | ||
NamespacedName: k8stypes.NamespacedName{Name: "pod-c"}, | ||
Address: "10.0.0.3:8080", | ||
}, | ||
MetricsState: &backendmetrics.MetricsState{ | ||
WaitingQueueSize: 2, | ||
}, | ||
}, | ||
}, | ||
request: &types.LLMRequest{ | ||
TargetModel: "gpt-4", | ||
Prompt: "what is meaning of life?", | ||
}, | ||
mockScores: map[string]int{ | ||
"10.0.0.1:8080": 10, | ||
"10.0.0.2:8080": 20, | ||
"10.0.0.3:8080": 30, | ||
}, | ||
wantScoresByAddress: map[string]float64{ | ||
"10.0.0.1:8080": 0.0, // (10-10)/(30-10) = 0.0 | ||
"10.0.0.2:8080": 0.5, // (20-10)/(30-10) = 0.5 | ||
"10.0.0.3:8080": 1.0, // (30-10)/(30-10) = 1.0 | ||
}, | ||
}, | ||
{ | ||
name: "test nil request", | ||
pods: []types.Pod{ | ||
&types.PodMetrics{ | ||
Pod: &backend.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod-a"}}, | ||
}, | ||
}, | ||
request: nil, | ||
wantScoresByAddress: make(map[string]float64), // empty map instead of nil | ||
}, | ||
{ | ||
name: "test pod scorer error", | ||
pods: []types.Pod{ | ||
&types.PodMetrics{ | ||
Pod: &backend.Pod{ | ||
NamespacedName: k8stypes.NamespacedName{Name: "pod-a"}, | ||
Address: "10.0.0.1:8080", | ||
}, | ||
}, | ||
}, | ||
request: &types.LLMRequest{ | ||
TargetModel: "gpt-4", | ||
Prompt: "test prompt", | ||
}, | ||
mockError: errors.New("test error"), | ||
wantScoresByAddress: make(map[string]float64), // empty map instead of nil | ||
}, | ||
} | ||
|
||
for _, tt := range testcases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockScorer := &mockPodScorer{ | ||
scores: tt.mockScores, | ||
err: tt.mockError, | ||
} | ||
prefixCacheScorer := scorer.NewWithPodScorer(mockScorer) | ||
require.NotNil(t, prefixCacheScorer) | ||
got := prefixCacheScorer.Score(context.Background(), nil, tt.request, tt.pods) | ||
// Convert the result to address-based map for easier comparison | ||
gotByAddress := make(map[string]float64) | ||
for pod, score := range got { | ||
if podMetrics, ok := pod.(*types.PodMetrics); ok && podMetrics.GetPod() != nil { | ||
gotByAddress[podMetrics.GetPod().Address] = score | ||
} | ||
} | ||
if diff := cmp.Diff(tt.wantScoresByAddress, gotByAddress); diff != "" { | ||
t.Errorf("Unexpected output (-want +got): %v", diff) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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 precise prefix-cache scorer is a wrapper of the kvcache.Indexer. The wrapping logic is mostly normalization.
What this test ends up doing is test the latter - which is great, but can be achieved by testing the
indexedScoresToNormalizedScoredPods
function directly instead of using a mock PodScorer interface.I think this test should verify correctness of the scorer as-is. This would require:
kvblock.Index
kvblock.Index
with kv-block informationThere 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.
Adding data to the
kvblock.Index
alone isn't sufficient; we also need to add data to thetokensIndexer
. However, thekv-cache-manager
currently only allows access to theKVBlockIndex
. Can we add a method to thekv-cache-manager
to expose thetokensIndexer
?Uh oh!
There was an error while loading. Please reload this page.
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.
You can add data to the tokenization cache by making the calls in a "warmup" step (call once, get no scores). Note that this behavior will change soon once tokenization is synchronous, after-which there would be no need for such a step.