summaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/main/java/ai/vespa/models/evaluation/OnnxModel.java
blob: 19a9a1dccd5fd18008f9ebd3ebc20e3442bfac83 (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
// 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.OnnxEvaluator;
import ai.vespa.modelintegration.evaluator.OnnxEvaluatorOptions;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

import java.io.File;
import java.util.Map;

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

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

    private OnnxEvaluator evaluator;

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

    public String name() {
        return name;
    }

    public void load() {
        if (evaluator == null) {
            evaluator = new OnnxEvaluator(modelFile.getPath(), options);
        }
    }

    public Map<String, TensorType> inputs() {
        return evaluator().getInputInfo();
    }

    public Map<String, TensorType> outputs() {
        return evaluator().getOutputInfo();
    }

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

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

}