|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package approximateprefix |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/uuid" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 26 | + |
| 27 | + fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer" |
| 28 | + "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/plugin" |
| 29 | + fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling" |
| 30 | + attrmodels "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/models" |
| 31 | + attrprefix "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/datalayer/attribute/prefix" |
| 32 | +) |
| 33 | + |
| 34 | +// modelLenEndpoint builds an endpoint whose /v1/models attribute reports the |
| 35 | +// given max_model_len values, one per model entry. With no values, the attribute |
| 36 | +// is left unset. |
| 37 | +func modelLenEndpoint(maxModelLens ...int) fwksched.Endpoint { |
| 38 | + attrs := fwkdl.NewAttributes() |
| 39 | + if maxModelLens != nil { |
| 40 | + models := make(attrmodels.ModelDataCollection, len(maxModelLens)) |
| 41 | + for i, ml := range maxModelLens { |
| 42 | + models[i] = attrmodels.ModelData{ID: "m", MaxModelLen: ml} |
| 43 | + } |
| 44 | + attrs.Put(attrmodels.ModelsAttributeKey.String(), models) |
| 45 | + } |
| 46 | + return fwksched.NewEndpoint(&fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}, fwkdl.NewMetrics(), attrs) |
| 47 | +} |
| 48 | + |
| 49 | +// TestProduceAutoTunesFromModelLen verifies the prefix cap is derived from the |
| 50 | +// endpoint's max_model_len attribute when auto-tuning, and falls back otherwise. |
| 51 | +func TestProduceAutoTunesFromModelLen(t *testing.T) { |
| 52 | + disableMinBlockSizeClamp(t) |
| 53 | + |
| 54 | + // A 20-block prompt at block size 1. |
| 55 | + tokens := make([]uint32, 20) |
| 56 | + for i := range tokens { |
| 57 | + tokens[i] = uint32(i + 1) |
| 58 | + } |
| 59 | + |
| 60 | + produceBlocks := func(t *testing.T, p *dataProducer, ep fwksched.Endpoint) int { |
| 61 | + t.Helper() |
| 62 | + req := &fwksched.InferenceRequest{RequestID: uuid.NewString(), TargetModel: "m", Body: tokenizedBody(tokens)} |
| 63 | + assert.NoError(t, p.Produce(context.Background(), req, []fwksched.Endpoint{ep})) |
| 64 | + state, err := plugin.ReadPluginStateKey[*SchedulingContextState]( |
| 65 | + p.PluginState(), req.RequestID, plugin.StateKey(ApproxPrefixCachePluginType)) |
| 66 | + assert.NoError(t, err) |
| 67 | + return len(state.PerPromptHashes[0]) |
| 68 | + } |
| 69 | + |
| 70 | + autoTuneConfig := config{ |
| 71 | + AutoTune: true, |
| 72 | + BlockSizeTokens: 1, |
| 73 | + MaxPrefixBlocksToMatch: defaultMaxPrefixBlocks, |
| 74 | + MaxPrefixTokensToMatch: defaultMaxPrefixTokens, // left at default -> auto-tune on |
| 75 | + LRUCapacityPerServer: defaultLRUCapacityPerServer, |
| 76 | + } |
| 77 | + |
| 78 | + t.Run("caps at max_model_len when known", func(t *testing.T) { |
| 79 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 80 | + assert.NoError(t, err) |
| 81 | + assert.True(t, p.config.autoTuneMaxPrefixTokens) |
| 82 | + // max_model_len = 10 -> cap = 10/blockSize(1) = 10 blocks. |
| 83 | + assert.Equal(t, 10, produceBlocks(t, p, modelLenEndpoint(10))) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("uses largest across models", func(t *testing.T) { |
| 87 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 88 | + assert.NoError(t, err) |
| 89 | + // Largest wins regardless of position (first entry here). |
| 90 | + assert.Equal(t, 15, produceBlocks(t, p, modelLenEndpoint(15, 8, 12))) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("falls back on negative max_model_len", func(t *testing.T) { |
| 94 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 95 | + assert.NoError(t, err) |
| 96 | + assert.Equal(t, 20, produceBlocks(t, p, modelLenEndpoint(-5))) |
| 97 | + }) |
| 98 | + |
| 99 | + t.Run("falls back on empty models collection", func(t *testing.T) { |
| 100 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 101 | + assert.NoError(t, err) |
| 102 | + // Attribute present but no model entries. |
| 103 | + attrs := fwkdl.NewAttributes() |
| 104 | + attrs.Put(attrmodels.ModelsAttributeKey.String(), attrmodels.ModelDataCollection{}) |
| 105 | + ep := fwksched.NewEndpoint(&fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}, fwkdl.NewMetrics(), attrs) |
| 106 | + assert.Equal(t, 20, produceBlocks(t, p, ep)) |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("falls back to default when attribute absent", func(t *testing.T) { |
| 110 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 111 | + assert.NoError(t, err) |
| 112 | + // No models attribute -> default cap (131072) far exceeds 20 blocks. |
| 113 | + assert.Equal(t, 20, produceBlocks(t, p, modelLenEndpoint())) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("falls back when max_model_len is zero", func(t *testing.T) { |
| 117 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 118 | + assert.NoError(t, err) |
| 119 | + // Attribute present but server did not report the field. |
| 120 | + assert.Equal(t, 20, produceBlocks(t, p, modelLenEndpoint(0))) |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("explicit cap is not auto-tuned", func(t *testing.T) { |
| 124 | + explicit := autoTuneConfig |
| 125 | + explicit.MaxPrefixTokensToMatch = 5 // operator-pinned, non-default |
| 126 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, explicit, testHandle()) |
| 127 | + assert.NoError(t, err) |
| 128 | + assert.False(t, p.config.autoTuneMaxPrefixTokens) |
| 129 | + // Even with a known model length, the explicit cap (5) wins. |
| 130 | + assert.Equal(t, 5, produceBlocks(t, p, modelLenEndpoint(10))) |
| 131 | + }) |
| 132 | + |
| 133 | + t.Run("empty pods uses default cap", func(t *testing.T) { |
| 134 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, autoTuneConfig, testHandle()) |
| 135 | + assert.NoError(t, err) |
| 136 | + |
| 137 | + req := &fwksched.InferenceRequest{RequestID: uuid.NewString(), TargetModel: "m", Body: tokenizedBody(tokens)} |
| 138 | + assert.NoError(t, p.Produce(context.Background(), req, []fwksched.Endpoint{})) |
| 139 | + state, err := plugin.ReadPluginStateKey[*SchedulingContextState]( |
| 140 | + p.PluginState(), req.RequestID, plugin.StateKey(ApproxPrefixCachePluginType)) |
| 141 | + assert.NoError(t, err) |
| 142 | + assert.Equal(t, 20, len(state.PerPromptHashes[0])) |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +// TestConsumesDeclaresModelsDependency verifies the producer declares the |
| 147 | +// /v1/models attribute as an optional dependency so the cap can be auto-tuned |
| 148 | +// when present and fall back when absent. |
| 149 | +func TestConsumesDeclaresModelsDependency(t *testing.T) { |
| 150 | + p, err := newDataProducer(context.Background(), ApproxPrefixCachePluginType, defaultConfig, testHandle()) |
| 151 | + assert.NoError(t, err) |
| 152 | + _, ok := p.Consumes().Optional[attrmodels.ModelsAttributeKey] |
| 153 | + assert.True(t, ok, "models attribute must be an optional dependency") |
| 154 | +} |
| 155 | + |
| 156 | +// TestMaxModelLenHelper covers the attribute reader's non-collection and |
| 157 | +// missing-attribute paths. |
| 158 | +func TestMaxModelLenHelper(t *testing.T) { |
| 159 | + // Missing attribute -> 0. |
| 160 | + assert.Equal(t, 0, maxModelLen(modelLenEndpoint())) |
| 161 | + |
| 162 | + // Wrong type stored under the key -> 0. |
| 163 | + attrs := fwkdl.NewAttributes() |
| 164 | + attrs.Put(attrmodels.ModelsAttributeKey.String(), attrprefix.NewPrefixCacheMatchInfo(0, 0, 0)) |
| 165 | + ep := fwksched.NewEndpoint(&fwkdl.EndpointMetadata{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}}, fwkdl.NewMetrics(), attrs) |
| 166 | + assert.Equal(t, 0, maxModelLen(ep)) |
| 167 | +} |
0 commit comments