summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/application/validation/change/RestartOnDeployForOnnxModelChangesValidatorTest.java
blob: 4c0786ea87962417b16c7f9dd55324b32b14716d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change;

import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.config.model.api.ApplicationClusterEndpoint;
import com.yahoo.config.model.api.ConfigChangeAction;
import com.yahoo.config.model.api.ContainerEndpoint;
import com.yahoo.config.model.api.OnnxModelCost;
import com.yahoo.config.model.api.OnnxModelOptions;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.provision.InMemoryProvisioner;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.application.validation.ValidationTester;
import com.yahoo.vespa.model.test.utils.VespaModelCreatorWithMockPkg;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author hmusum
 */
public class RestartOnDeployForOnnxModelChangesValidatorTest {

    // Must be so large that changing model set or options requires restart (due to using more memory than available),
    // but not so large that deployment will not work at all with one model
    private static final long defaultCost = 723456789;
    private static final long defaultHash = 0;


    @Test
    void validate_no_changes() {
        VespaModel current = createModel();
        VespaModel next = createModel();
        List<ConfigChangeAction> result = validateModel(current, next);
        assertEquals(0, result.size());
    }

    @Test
    void validate_changed_estimated_cost() {
        VespaModel current = createModel(onnxModelCost(70000000, defaultHash));
        VespaModel next = createModel(onnxModelCost(723456789, defaultHash));
        List<ConfigChangeAction> result = validateModel(current, next);
        assertEquals(1, result.size());
        assertTrue(result.get(0).validationId().isEmpty());
        assertEquals("Onnx model 'https://data.vespa.oath.cloud/onnx_models/e5-base-v2/model.onnx' has changed (estimated cost), need to restart services in container cluster 'cluster1'", result.get(0).getMessage());
    }

    @Test
    void validate_changed_estimated_cost_non_hosted() {
        boolean hosted = false;
        VespaModel current = createModel(onnxModelCost(70000000, defaultHash), hosted);
        VespaModel next = createModel(onnxModelCost(723456789, defaultHash), hosted);
        List<ConfigChangeAction> result = validateModel(current, next, hosted);
        assertEquals(0, result.size());
    }

    @Test
    void validate_changed_hash() {
        VespaModel current = createModel();
        VespaModel next = createModel(onnxModelCost(defaultCost, 123));
        List<ConfigChangeAction> result = validateModel(current, next);
        assertEquals(1, result.size());
        assertStartsWith("Onnx model 'https://data.vespa.oath.cloud/onnx_models/e5-base-v2/model.onnx' has changed (model hash)", result);
    }

    @Test
    void validate_changed_option() {
        VespaModel current = createModel();
        VespaModel next = createModel(onnxModelCost(), true, "sequential");
        List<ConfigChangeAction> result = validateModel(current, next);
        assertEquals(1, result.size());
        assertStartsWith("Onnx model 'https://data.vespa.oath.cloud/onnx_models/e5-base-v2/model.onnx' has changed (model option(s))", result);
    }

    @Test
    void validate_changed_model_set() {
        VespaModel current = createModel();
        VespaModel next = createModel(onnxModelCost(), true, "parallel", "e5-small-v2");
        List<ConfigChangeAction> result = validateModel(current, next);
        assertEquals(1, result.size());
        assertStartsWith("Onnx model set has changed from [https://data.vespa.oath.cloud/onnx_models/e5-base-v2/model.onnx] " +
                                 "to [https://data.vespa.oath.cloud/onnx_models/e5-small-v2/model.onnx",
                         result);
    }

    private static List<ConfigChangeAction> validateModel(VespaModel current, VespaModel next) {
        return validateModel(current, next, true);
    }

    private static List<ConfigChangeAction> validateModel(VespaModel current, VespaModel next, boolean hosted) {
        return ValidationTester.validateChanges(new RestartOnDeployForOnnxModelChangesValidator(),
                                                next,
                                                deployStateBuilder(hosted).previousModel(current).build());
    }

    private static OnnxModelCost onnxModelCost() {
        return onnxModelCost(defaultCost, defaultHash);
    }

    private static OnnxModelCost onnxModelCost(long estimatedCost, long hash) {
        return (appPkg, applicationId, clusterId) -> new OnnxModelCost.Calculator() {

            private final Map<String, OnnxModelCost.ModelInfo> models = new HashMap<>();
            private boolean restartOnDeploy = false;

            @Override
            public long aggregatedModelCostInBytes() { return estimatedCost; }

            @Override
            public void registerModel(ApplicationFile path, OnnxModelOptions onnxModelOptions) {}

            @Override
            public void registerModel(URI uri, OnnxModelOptions onnxModelOptions) {
                models.put(uri.toString(), new OnnxModelCost.ModelInfo(uri.toString(), estimatedCost, hash, onnxModelOptions));
            }

            @Override
            public Map<String, OnnxModelCost.ModelInfo> models() { return models; }

            @Override
            public void setRestartOnDeploy() { restartOnDeploy = true; }

            @Override
            public boolean restartOnDeploy() { return restartOnDeploy; }

            @Override
            public void store() {}
        };
    }

    private static VespaModel createModel() {
        return createModel(onnxModelCost(), true);
    }

    private static VespaModel createModel(OnnxModelCost onnxModelCost) {
        return createModel(onnxModelCost, true, "parallel");
    }

    private static VespaModel createModel(OnnxModelCost onnxModelCost, boolean hosted) {
        return createModel(onnxModelCost, hosted, "parallel");
    }

    private static VespaModel createModel(OnnxModelCost onnxModelCost, boolean hosted, String executionMode) {
        return createModel(onnxModelCost, hosted, executionMode, "e5-base-v2");
    }

    private static VespaModel createModel(OnnxModelCost onnxModelCost, boolean hosted, String executionMode, String modelId) {
        DeployState.Builder builder = deployStateBuilder(hosted)
                .onnxModelCost(onnxModelCost);
        return hosted ? hostedModel(builder, executionMode, modelId) : nonHostedModel(builder, executionMode, modelId);
    }

    private static VespaModel hostedModel(DeployState.Builder builder, String executionMode, String modelId) {
        var servicesXml  = """
                          <services version='1.0'>
                            <container id='cluster1' version='1.0'>
                              <component id="hf-embedder" type="hugging-face-embedder">
                                           <transformer-model model-id="%s" url="https://my/url/%s.onnx"/>
                                           <tokenizer-model model-id="e5-base-v2-vocab" path="app/tokenizer.json"/>
                                           <onnx-execution-mode>%s</onnx-execution-mode>
                              </component>
                              <nodes count='1'>
                                <resources vcpu='1' memory='2Gb' disk='25Gb'/>
                              </nodes>
                            </container>
                          </services>
                          """.formatted(modelId, modelId, executionMode);

        var deploymentXml = """
                            <deployment version='1.0' empty-host-ttl='1d'>
                              <instance id='default'>
                                <prod>
                                  <region>us-east-1</region>
                                  <region empty-host-ttl='0m'>us-north-1</region>
                                  <region>us-west-1</region>
                                </prod>
                              </instance>
                            </deployment>
                            """;

        var applicationPackage = new MockApplicationPackage.Builder()
                .withServices(servicesXml)
                .withDeploymentSpec(deploymentXml)
                .build();

        return new VespaModelCreatorWithMockPkg(applicationPackage).create(builder);
    }

    private static VespaModel nonHostedModel(DeployState.Builder builder, String executionMode, String modelId) {
        var xml = """
                                       <services version='1.0'>
                                         <container id='cluster1' version='1.0'>
                                           <http>
                                             <server id='server1' port='8080'/>
                                           </http>
                                         <component id="hf-embedder" type="hugging-face-embedder">
                                           <transformer-model model-id="%s" url="https://my/url/%s.onnx"/>
                                           <tokenizer-model model-id="e5-base-v2-vocab" path="app/tokenizer.json"/>
                                           <onnx-execution-mode>%s</onnx-execution-mode>
                                         </component>
                                         </container>
                                       </services>
                """.formatted(modelId, modelId, executionMode);

        return new VespaModelCreatorWithMockPkg(null, xml).create(builder);
    }


    private static DeployState.Builder deployStateBuilder(boolean hosted) {
        var builder = new DeployState.Builder()
                .properties((new TestProperties()).setHostedVespa(hosted));
        if (hosted)
            builder.endpoints(Set.of(new ContainerEndpoint("cluster1", ApplicationClusterEndpoint.Scope.zone, List.of("tc.example.com"))))
                    .modelHostProvisioner(new InMemoryProvisioner(5, new NodeResources(1, 2, 25, 0.3), true));
        return builder;
    }

    private static void assertStartsWith(String expected, List<ConfigChangeAction> result) {
        assertTrue(result.get(0).getMessage().startsWith(expected));
    }

}