aboutsummaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/main/java/ai/vespa/models/evaluation/OnnxModel.java
blob: e606c946ca10c5e7d35be7d9e80b674d6edac51c (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
// Copyright Vespa.ai. 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.OnnxEvaluator;
import ai.vespa.modelintegration.evaluator.OnnxEvaluatorOptions;
import ai.vespa.modelintegration.evaluator.OnnxRuntime;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A named ONNX model that should be evaluated with OnnxEvaluator.
 *
 * @author lesters
 */
class OnnxModel implements AutoCloseable {

    static class InputSpec {
        String onnxName;
        String source;
        TensorType wantedType;
        InputSpec(String name, String source, TensorType tType) {
            this.onnxName = name;
            this.source = source;
            this.wantedType = tType;
        }
        InputSpec(String name, String source) { this(name, source, null); }
    }

    static class OutputSpec {
        String onnxName;
        String outputAs;
        TensorType expectedType;
        OutputSpec(String name, String as, TensorType tType) {
            this.onnxName = name;
            this.outputAs = as;
            this.expectedType = tType;
        }
        OutputSpec(String name, String as) { this(name, as, null); }
    }

    final List<InputSpec> inputSpecs = new ArrayList<>();
    final List<OutputSpec> outputSpecs = new ArrayList<>();

    void addInputMapping(String onnxName, String source) {
        if (evaluator != null)
            throw new IllegalStateException("input mapping must be added before load()");
        inputSpecs.add(new InputSpec(onnxName, source));
    }
    void addOutputMapping(String onnxName, String outputAs) {
        if (evaluator != null)
            throw new IllegalStateException("output mapping must be added before load()");
        outputSpecs.add(new OutputSpec(onnxName, outputAs));
    }

    private final String name;
    private final File modelFile;
    private final OnnxEvaluatorOptions options;
    private final OnnxRuntime onnx;

    private OnnxEvaluator evaluator;
    private final Map<String, ExpressionNode> exprPerOutput = new HashMap<>();

    OnnxModel(String name, File modelFile, OnnxEvaluatorOptions options, OnnxRuntime onnx) {
        this.name = name;
        this.modelFile = modelFile;
        this.options = options;
        this.onnx = onnx;
    }

    public String name() {
        return name;
    }

    public void load() {
        if (evaluator == null) {
            evaluator = onnx.evaluatorOf(modelFile.getPath(), options);
            fillInputTypes(evaluator().getInputs());
            fillOutputTypes(evaluator().getOutputs());
            fillOutputExpressions();
        }
    }

    void fillInputTypes(Map<String, OnnxEvaluator.IdAndType> wantedTypes) {
        if (inputSpecs.isEmpty()) {
            for (var entry : wantedTypes.entrySet()) {
                String name = entry.getKey();
                String source = entry.getValue().id();
                TensorType tType = entry.getValue().type();
                var spec = new InputSpec(name, source, tType);
                inputSpecs.add(spec);
            }
        } else {
            if (wantedTypes.size() != inputSpecs.size()) {
                throw new IllegalArgumentException("Onnx model " + name() +
                                                   ": Mismatch between " + inputSpecs.size() +
                                                   " configured inputs and " +
                                                   wantedTypes.size() + " actual model inputs");
            }
            for (var spec : inputSpecs) {
                var entry = wantedTypes.get(spec.onnxName);
                if (entry == null) {
                    throw new IllegalArgumentException("Onnx model " + name() +
                                                       ": No type in actual model for configured input "
                                                       + spec.onnxName);
                }
                spec.wantedType = entry.type();
            }
        }
    }

    void fillOutputTypes(Map<String, OnnxEvaluator.IdAndType> outputTypes) {
        if (outputSpecs.isEmpty()) {
            for (var entry : outputTypes.entrySet()) {
                String name = entry.getKey();
                String as = entry.getValue().id();
                TensorType tType = entry.getValue().type();
                var spec = new OutputSpec(name, as, tType);
                outputSpecs.add(spec);
            }
        } else {
            if (outputTypes.size() != outputSpecs.size()) {
                throw new IllegalArgumentException("Onnx model " + name() +
                                                   ": Mismatch between " + outputSpecs.size() +
                                                   " configured outputs and " +
                                                   outputTypes.size() + " actual model outputs");
            }
            for (var spec : outputSpecs) {
                var entry = outputTypes.get(spec.onnxName);
                if (entry == null) {
                    throw new IllegalArgumentException("Onnx model " + name() +
                                                       ": No type in actual model for configured output "
                                                       + spec.onnxName);
                }
                spec.expectedType = entry.type();
            }
        }
    }

    public Map<String, TensorType> inputs() {
        var map = new HashMap<String, TensorType>();
        for (var spec : inputSpecs) {
            map.put(spec.source, spec.wantedType);
        }
        return map;
    }

    public Map<String, TensorType> outputs() {
        var map = new HashMap<String, TensorType>();
        for (var spec : outputSpecs) {
            map.put(spec.outputAs, spec.expectedType);
        }
        return map;
    }

    void fillOutputExpressions() {
        for (var spec : outputSpecs) {
            var node = new OnnxExpressionNode(this, spec.onnxName, spec.expectedType, spec.outputAs);
            exprPerOutput.put(spec.outputAs, node);
        }
    }

    ExpressionNode getExpressionForOutput(String outputName) {
        if (outputName == null && exprPerOutput.size() == 1) {
            return exprPerOutput.values().iterator().next();
        }
        return exprPerOutput.get(outputName);
    }

    public Tensor evaluate(Map<String, Tensor> inputs, String output) {
        var mapped = new HashMap<String, Tensor>();
        for (var spec : inputSpecs) {
            Tensor val = inputs.get(spec.source);
            if (val == null) {
                throw new IllegalArgumentException("evaluate ONNX model " + name() + ": missing input from source " + spec.source);
            }
            mapped.put(spec.onnxName, val);
        }
        String onnxName = null;
        for (var spec : outputSpecs) {
            if (spec.outputAs.equals(output)) {
                onnxName = spec.onnxName;
            }
        }
        if (onnxName == null) {
            throw new IllegalArgumentException("evaluate ONNX model " + name() + ": no output available as: " + output);
        }
        return unmappedEvaluate(mapped, onnxName);
    }

    Tensor unmappedEvaluate(Map<String, Tensor> inputs, String onnxOutputName) {
        return evaluator().evaluate(inputs, onnxOutputName);
    }

    private OnnxEvaluator evaluator() {
        if (evaluator == null) {
            throw new IllegalStateException("ONNX model has not been loaded.");
        }
        return evaluator;
    }

    @Override public void close() {
        if (evaluator != null) {
            evaluator.close();
            evaluator = null;
        }
    }
}