Skip to content
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

make ModelName immutable and fix model weight #427

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/v1alpha2/inferencemodel_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type InferenceModelSpec struct {
//
// +kubebuilder:validation:MaxLength=256
// +kubebuilder:validation:Required
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="modelName is immutable"
ModelName string `json:"modelName"`

// Criticality defines how important it is to serve the model compared to other models referencing the same pool.
Expand Down Expand Up @@ -175,7 +176,7 @@ type TargetModel struct {
// Conversely weights are optional, so long as ALL targetModels do not specify a weight.
//
// +optional
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Minimum=1
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It set to 0, it will panic too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we allow to set 0 weight which users can do so to disabled the model instead of removing the model?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar question came up in Gateway API and we ultimately decided that we'd rather have people remove the backend altogether to avoid any confusion. In practice this likely just means commenting out some YAML. I'd prefer to follow the same pattern here.

// +kubebuilder:validation:Maximum=1000000
Weight *int32 `json:"weight,omitempty"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ spec:
an error will be returned specifying that no valid target model is found.
maxLength: 256
type: string
x-kubernetes-validations:
- message: modelName is immutable
rule: self == oldSelf
poolRef:
description: PoolRef is a reference to the inference pool, the pool
must exist in the same namespace.
Expand Down Expand Up @@ -143,7 +146,7 @@ spec:
Conversely weights are optional, so long as ALL targetModels do not specify a weight.
format: int32
maximum: 1000000
minimum: 0
minimum: 1
type: integer
required:
- name
Expand Down
11 changes: 9 additions & 2 deletions pkg/epp/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,25 @@ func stripLabelKeyAliasFromLabelMap(labels map[v1alpha2.LabelKey]v1alpha2.LabelV
}

func RandomWeightedDraw(logger logr.Logger, model *v1alpha2.InferenceModel, seed int64) string {
var weights int32

source := rand.NewSource(rand.Int63())
if seed > 0 {
source = rand.NewSource(seed)
}
r := rand.New(source)

// all the weight values are nil, then we should return random model name
if model.Spec.TargetModels[0].Weight == nil {
index := r.Int31n(int32(len(model.Spec.TargetModels)))
return model.Spec.TargetModels[index].Name
}

var weights int32
for _, model := range model.Spec.TargetModels {
weights += *model.Weight
}
logger.V(logutil.TRACE).Info("Weights for model computed", "model", model.Name, "weights", weights)
randomVal := r.Int31n(weights)
// TODO: optimize this without using loop
for _, model := range model.Spec.TargetModels {
if randomVal < *model.Weight {
return model.Name
Expand Down
21 changes: 20 additions & 1 deletion pkg/epp/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,33 @@ func TestRandomWeightedDraw(t *testing.T) {
},
want: "v1.1",
},
{
name: "weighted distribution with weight unset",
model: &v1alpha2.InferenceModel{
Spec: v1alpha2.InferenceModelSpec{
TargetModels: []v1alpha2.TargetModel{
{
Name: "canary",
},
{
Name: "v1.1",
},
{
Name: "v1",
},
},
},
},
want: "canary",
},
}
var seedVal int64 = 420
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for range 10000 {
model := RandomWeightedDraw(logger, test.model, seedVal)
if model != test.want {
t.Errorf("Model returned!: %v", model)
t.Errorf("Model returned: %v != %v", model, test.want)
break
}
}
Expand Down