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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.models.evaluation;
import ai.vespa.modelintegration.evaluator.OnnxRuntime;
import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer;
import com.yahoo.path.Path;
import com.yahoo.searchlib.rankingexpression.ExpressionFunction;
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 java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Helper for testing model import and evaluation
*
* @author bratseth
*/
public class ModelTester {
private final Map<String, Model> models;
public ModelTester(String modelConfigDirectory) {
models = createModels(modelConfigDirectory);
}
public Map<String, Model> models() { return models; }
@SuppressWarnings("deprecation")
private static Map<String, Model> createModels(String path) {
RankProfilesConfig config = ConfigGetter.getConfig(RankProfilesConfig.class, fileConfigId(path, "rank-profiles.cfg"));
RankingConstantsConfig constantsConfig = ConfigGetter.getConfig(RankingConstantsConfig.class, fileConfigId(path, "ranking-constants.cfg"));
RankingExpressionsConfig expressionsConfig = ConfigGetter.getConfig(RankingExpressionsConfig.class, fileConfigId(path, "ranking-expressions.cfg"));
OnnxModelsConfig onnxModelsConfig = ConfigGetter.getConfig(OnnxModelsConfig.class, fileConfigId(path, "onnx-models.cfg"));
Map<String, File> fileMap = new HashMap<>();
for (var cfgEntry : onnxModelsConfig.model()) {
fileMap.put(cfgEntry.fileref().value(), new File(path + cfgEntry.fileref().value()));
}
for (var cfgEntry : constantsConfig.constant()) {
fileMap.put(cfgEntry.fileref().value(), new File(path + cfgEntry.fileref().value()));
}
for (var cfgEntry : expressionsConfig.expression()) {
fileMap.put(cfgEntry.fileref().value(), new File(path + cfgEntry.fileref().value()));
}
var fileAcquirer = MockFileAcquirer.returnFiles(fileMap);
return new RankProfilesConfigImporter(fileAcquirer, new OnnxRuntime())
.importFrom(config, constantsConfig, expressionsConfig, onnxModelsConfig);
}
private static String fileConfigId(String path, String filename) {
return "file:" + path + filename;
}
public ExpressionFunction assertFunction(String name, String expression, Model model) {
assertNotNull("Model is present in config", model);
ExpressionFunction function = model.function(name);
assertNotNull("Function '" + name + "' is in " + model, function);
assertEquals(name, function.getName());
assertEquals(expression, function.getBody().getRoot().toString());
return function;
}
public void assertBoundFunction(String name, String expression, Model model) {
ExpressionFunction function = model.referencedFunctions().get(FunctionReference.fromSerial(name).get());
assertNotNull("Function '" + name + "' is present", function);
assertEquals(expression, function.getBody().getRoot().toString());
}
}
|