|
| 1 | +package vllm |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/docker/model-runner/pkg/distribution/types" |
| 7 | + "github.com/docker/model-runner/pkg/inference" |
| 8 | +) |
| 9 | + |
| 10 | +type mockModelBundle struct { |
| 11 | + safetensorsPath string |
| 12 | + runtimeConfig types.Config |
| 13 | +} |
| 14 | + |
| 15 | +func (m *mockModelBundle) GGUFPath() string { |
| 16 | + return "" |
| 17 | +} |
| 18 | + |
| 19 | +func (m *mockModelBundle) SafetensorsPath() string { |
| 20 | + return m.safetensorsPath |
| 21 | +} |
| 22 | + |
| 23 | +func (m *mockModelBundle) ChatTemplatePath() string { |
| 24 | + return "" |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockModelBundle) MMPROJPath() string { |
| 28 | + return "" |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockModelBundle) RuntimeConfig() types.Config { |
| 32 | + return m.runtimeConfig |
| 33 | +} |
| 34 | + |
| 35 | +func (m *mockModelBundle) RootDir() string { |
| 36 | + return "/path/to/bundle" |
| 37 | +} |
| 38 | + |
| 39 | +func TestGetArgs(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + config *inference.BackendConfiguration |
| 43 | + bundle *mockModelBundle |
| 44 | + expected []string |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "basic args without context size", |
| 48 | + bundle: &mockModelBundle{ |
| 49 | + safetensorsPath: "/path/to/model", |
| 50 | + }, |
| 51 | + config: nil, |
| 52 | + expected: []string{ |
| 53 | + "serve", |
| 54 | + "/path/to/model", |
| 55 | + "--uds", |
| 56 | + "/tmp/socket", |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "with backend context size", |
| 61 | + bundle: &mockModelBundle{ |
| 62 | + safetensorsPath: "/path/to/model", |
| 63 | + }, |
| 64 | + config: &inference.BackendConfiguration{ |
| 65 | + ContextSize: 8192, |
| 66 | + }, |
| 67 | + expected: []string{ |
| 68 | + "serve", |
| 69 | + "/path/to/model", |
| 70 | + "--uds", |
| 71 | + "/tmp/socket", |
| 72 | + "--max-model-len", |
| 73 | + "8192", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "with runtime flags", |
| 78 | + bundle: &mockModelBundle{ |
| 79 | + safetensorsPath: "/path/to/model", |
| 80 | + }, |
| 81 | + config: &inference.BackendConfiguration{ |
| 82 | + RuntimeFlags: []string{"--gpu-memory-utilization", "0.9"}, |
| 83 | + }, |
| 84 | + expected: []string{ |
| 85 | + "serve", |
| 86 | + "/path/to/model", |
| 87 | + "--uds", |
| 88 | + "/tmp/socket", |
| 89 | + "--gpu-memory-utilization", |
| 90 | + "0.9", |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "with model context size (takes precedence)", |
| 95 | + bundle: &mockModelBundle{ |
| 96 | + safetensorsPath: "/path/to/model", |
| 97 | + runtimeConfig: types.Config{ |
| 98 | + ContextSize: ptrUint64(16384), |
| 99 | + }, |
| 100 | + }, |
| 101 | + config: &inference.BackendConfiguration{ |
| 102 | + ContextSize: 8192, |
| 103 | + }, |
| 104 | + expected: []string{ |
| 105 | + "serve", |
| 106 | + "/path/to/model", |
| 107 | + "--uds", |
| 108 | + "/tmp/socket", |
| 109 | + "--max-model-len", |
| 110 | + "16384", |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + for _, tt := range tests { |
| 116 | + t.Run(tt.name, func(t *testing.T) { |
| 117 | + config := NewDefaultVLLMConfig() |
| 118 | + args, err := config.GetArgs(tt.bundle, "/tmp/socket", inference.BackendModeCompletion, tt.config) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("unexpected error: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + if len(args) != len(tt.expected) { |
| 124 | + t.Fatalf("expected %d args, got %d\nexpected: %v\ngot: %v", len(tt.expected), len(args), tt.expected, args) |
| 125 | + } |
| 126 | + |
| 127 | + for i, arg := range args { |
| 128 | + if arg != tt.expected[i] { |
| 129 | + t.Errorf("arg[%d]: expected %q, got %q", i, tt.expected[i], arg) |
| 130 | + } |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestGetMaxModelLen(t *testing.T) { |
| 137 | + tests := []struct { |
| 138 | + name string |
| 139 | + modelCfg types.Config |
| 140 | + backendCfg *inference.BackendConfiguration |
| 141 | + expectedValue *uint64 |
| 142 | + }{ |
| 143 | + { |
| 144 | + name: "no config", |
| 145 | + modelCfg: types.Config{}, |
| 146 | + backendCfg: nil, |
| 147 | + expectedValue: nil, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: "backend config only", |
| 151 | + modelCfg: types.Config{}, |
| 152 | + backendCfg: &inference.BackendConfiguration{ |
| 153 | + ContextSize: 4096, |
| 154 | + }, |
| 155 | + expectedValue: ptrUint64(4096), |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "model config only", |
| 159 | + modelCfg: types.Config{ |
| 160 | + ContextSize: ptrUint64(8192), |
| 161 | + }, |
| 162 | + backendCfg: nil, |
| 163 | + expectedValue: ptrUint64(8192), |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "model config takes precedence", |
| 167 | + modelCfg: types.Config{ |
| 168 | + ContextSize: ptrUint64(16384), |
| 169 | + }, |
| 170 | + backendCfg: &inference.BackendConfiguration{ |
| 171 | + ContextSize: 4096, |
| 172 | + }, |
| 173 | + expectedValue: ptrUint64(16384), |
| 174 | + }, |
| 175 | + } |
| 176 | + |
| 177 | + for _, tt := range tests { |
| 178 | + t.Run(tt.name, func(t *testing.T) { |
| 179 | + result := GetMaxModelLen(tt.modelCfg, tt.backendCfg) |
| 180 | + if (result == nil) != (tt.expectedValue == nil) { |
| 181 | + t.Errorf("expected nil=%v, got nil=%v", tt.expectedValue == nil, result == nil) |
| 182 | + } else if result != nil && *result != *tt.expectedValue { |
| 183 | + t.Errorf("expected %d, got %d", *tt.expectedValue, *result) |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func ptrUint64(v uint64) *uint64 { |
| 190 | + return &v |
| 191 | +} |
0 commit comments