aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/ml/ModelEvaluationTest.java
blob: 137907cb003811277985d65256905c593cbdcca0 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.ml;

import ai.vespa.modelintegration.evaluator.OnnxRuntime;
import ai.vespa.models.evaluation.Model;
import ai.vespa.models.evaluation.ModelsEvaluator;
import ai.vespa.models.handler.ModelsEvaluationHandler;
import com.yahoo.component.ComponentId;
import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.filedistribution.fileacquirer.FileAcquirer;
import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer;
import com.yahoo.io.IOUtils;
import com.yahoo.path.Path;
import com.yahoo.tensor.TensorType;
import com.yahoo.vespa.config.search.RankProfilesConfig;
import com.yahoo.vespa.config.search.core.OnnxModelsConfig;
import com.yahoo.vespa.config.search.core.RankingConstantsConfig;
import com.yahoo.vespa.config.search.core.RankingExpressionsConfig;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

/**
 * Tests stateless model evaluation (turned on by the "model-evaluation" tag in "container")
 *
 * @author bratseth
 */
public class ModelEvaluationTest {

    /** Tests that we do not load models (which would waste memory) when not requested */
    @Test
    void testMl_serving_not_activated() {
        Path appDir = Path.fromString("src/test/cfg/application/ml_serving_not_activated");
        try {
            ImportedModelTester tester = new ImportedModelTester("ml_serving", appDir);
            VespaModel model = tester.createVespaModel();
            ApplicationContainerCluster cluster = model.getContainerClusters().get("container");
            assertNull(cluster.getComponentsMap().get(new ComponentId(ModelsEvaluator.class.getName())));

            RankProfilesConfig.Builder b = new RankProfilesConfig.Builder();
            cluster.getConfig(b);
            RankProfilesConfig config = new RankProfilesConfig(b);

            assertEquals(0, config.rankprofile().size());
        }
        finally {
            IOUtils.recursiveDeleteDir(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile());
        }
    }

    @Test
    void testMl_serving() throws IOException {
        assumeTrue(OnnxRuntime.isRuntimeAvailable());
        Path appDir = Path.fromString("src/test/cfg/application/ml_serving");
        Path storedAppDir = appDir.append("copy");
        try {
            ImportedModelTester tester = new ImportedModelTester("ml_serving", appDir);
            assertHasMlModels(tester.createVespaModel(), appDir);

            // At this point the expression is stored - copy application to another location which do not have a models dir
            storedAppDir.toFile().mkdirs();
            IOUtils.copy(appDir.append("services.xml").toString(), storedAppDir.append("services.xml").toString());
            IOUtils.copyDirectory(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile(),
                    storedAppDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile());
            ImportedModelTester storedTester = new ImportedModelTester("ml_serving", storedAppDir);
            assertHasMlModels(storedTester.createVespaModel(), appDir);
        }
        finally {
            IOUtils.recursiveDeleteDir(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile());
            IOUtils.recursiveDeleteDir(storedAppDir.toFile());
        }
    }

    @Test
    void testContainerSpecificModelSettings() {
        Path appDir = Path.fromString("src/test/cfg/application/onnx_cluster_specific");
        try {
            ImportedModelTester tester = new ImportedModelTester("mul", appDir);
            VespaModel model = tester.createVespaModel();
            OnnxModelsConfig.Model c1Model = getOnnxModelsConfig(model.getContainerClusters().get("c1"));
            OnnxModelsConfig.Model c2Model = getOnnxModelsConfig(model.getContainerClusters().get("c2"));
            assertEquals(2, c1Model.stateless_intraop_threads());
            assertEquals(4, c2Model.stateless_intraop_threads());
            assertEquals(0, c1Model.gpu_device());
            assertEquals(1, c2Model.gpu_device());
        } finally {
            IOUtils.recursiveDeleteDir(appDir.append(ApplicationPackage.MODELS_GENERATED_DIR).toFile());
        }

    }

    private OnnxModelsConfig.Model getOnnxModelsConfig(ApplicationContainerCluster cluster) {
        OnnxModelsConfig.Builder ob = new OnnxModelsConfig.Builder();
        cluster.getConfig(ob);
        return new OnnxModelsConfig(ob).model(0);
    }

    private void assertHasMlModels(VespaModel model, Path appDir) {
        ApplicationContainerCluster cluster = model.getContainerClusters().get("container");
        assertNotNull(cluster.getComponentsMap().get(new ComponentId(ModelsEvaluator.class.getName())));

        assertNotNull(cluster.getComponentsMap().get(new ComponentId(ModelsEvaluationHandler.class.getName())));
        assertTrue(cluster.getHandlers().stream()
                .anyMatch(h -> h.getComponentId().toString().equals(ModelsEvaluationHandler.class.getName())));

        RankProfilesConfig.Builder b = new RankProfilesConfig.Builder();
        cluster.getConfig(b);
        RankProfilesConfig config = new RankProfilesConfig(b);

        RankingConstantsConfig.Builder cb = new RankingConstantsConfig.Builder();
        cluster.getConfig(cb);
        RankingConstantsConfig constantsConfig = new RankingConstantsConfig(cb);

        RankingExpressionsConfig.Builder ce = new RankingExpressionsConfig.Builder();
        cluster.getConfig(ce);
        RankingExpressionsConfig expressionsConfig = ce.build();

        OnnxModelsConfig.Builder ob = new OnnxModelsConfig.Builder();
        cluster.getConfig(ob);
        OnnxModelsConfig onnxModelsConfig = new OnnxModelsConfig(ob);

        assertEquals(5, config.rankprofile().size());
        Set<String> modelNames = config.rankprofile().stream().map(v -> v.name()).collect(Collectors.toSet());
        assertTrue(modelNames.contains("xgboost_2_2"));
        assertTrue(modelNames.contains("lightgbm_regression"));
        assertTrue(modelNames.contains("add_mul"));
        assertTrue(modelNames.contains("small_constants_and_functions"));
        assertTrue(modelNames.contains("sqrt"));

        // Compare profile content in a denser format than config:
        StringBuilder sb = new StringBuilder();
        for (RankProfilesConfig.Rankprofile.Fef.Property p : findProfile("small_constants_and_functions", config).property())
            sb.append(p.name()).append(": ").append(p.value()).append("\n");
        assertEquals(profile, sb.toString());

        Map<String, File> fileMap = new HashMap<>();
        for (OnnxModelsConfig.Model onnxModel : onnxModelsConfig.model()) {
            fileMap.put(onnxModel.fileref().value(), appDir.append(onnxModel.fileref().value()).toFile());
        }
        FileAcquirer fileAcquirer = MockFileAcquirer.returnFiles(fileMap);
        ModelsEvaluator evaluator = new ModelsEvaluator(config, constantsConfig, expressionsConfig, onnxModelsConfig, fileAcquirer);

        assertEquals(5, evaluator.models().size());

        Model xgboost = evaluator.models().get("xgboost_2_2");
        assertNotNull(xgboost);
        assertNotNull(xgboost.evaluatorOf());
        assertNotNull(xgboost.evaluatorOf("xgboost_2_2"));

        Model lightgbm = evaluator.models().get("lightgbm_regression");
        assertNotNull(lightgbm);
        assertNotNull(lightgbm.evaluatorOf());
        assertNotNull(lightgbm.evaluatorOf("lightgbm_regression"));

        Model add_mul = evaluator.models().get("add_mul");
        assertNotNull(add_mul);
        assertEquals(2, add_mul.functions().size());
        assertNotNull(add_mul.evaluatorOf("output1"));
        assertNotNull(add_mul.evaluatorOf("output2"));
        assertNotNull(add_mul.evaluatorOf("default.output1"));
        assertNotNull(add_mul.evaluatorOf("default.output2"));
        assertNotNull(add_mul.evaluatorOf("default", "output1"));
        assertNotNull(add_mul.evaluatorOf("default", "output2"));
        assertNotNull(evaluator.evaluatorOf("add_mul", "output1"));
        assertNotNull(evaluator.evaluatorOf("add_mul", "output2"));
        assertNotNull(evaluator.evaluatorOf("add_mul", "default.output1"));
        assertNotNull(evaluator.evaluatorOf("add_mul", "default.output2"));
        assertNotNull(evaluator.evaluatorOf("add_mul", "default", "output1"));
        assertNotNull(evaluator.evaluatorOf("add_mul", "default", "output2"));
        assertEquals(TensorType.fromSpec("tensor<float>(d0[1])"), add_mul.functions().get(0).getArgumentType("input1"));
        assertEquals(TensorType.fromSpec("tensor<float>(d0[1])"), add_mul.functions().get(0).getArgumentType("input2"));

        Model sqrt = evaluator.models().get("sqrt");
        assertNotNull(sqrt);
        assertEquals(1, sqrt.functions().size());
        assertNotNull(sqrt.evaluatorOf());
        assertNotNull(sqrt.evaluatorOf("out_layer_1_1"));  // converted from "out/layer/1:1"
        assertNotNull(evaluator.evaluatorOf("sqrt"));
        assertNotNull(evaluator.evaluatorOf("sqrt", "out_layer_1_1"));
        assertEquals(TensorType.fromSpec("tensor<float>(d0[1])"), sqrt.functions().get(0).getArgumentType("input"));
    }

    private final String profile =
            "rankingExpression(output).rankingScript: onnx(small_constants_and_functions).output\n" +
            "rankingExpression(output).type: tensor<float>(d0[3])\n";

    private RankProfilesConfig.Rankprofile.Fef findProfile(String name, RankProfilesConfig config) {
        for (RankProfilesConfig.Rankprofile profile : config.rankprofile()) {
            if (profile.name().equals(name))
                return profile.fef();
        }
        throw new IllegalArgumentException("No profile named " + name);
    }

}