aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/component/Model.java
blob: 76d93c38aee6b2bfa27e090021f038a3d1b1a402 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.model.container.component;

import com.yahoo.config.ModelReference;
import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.config.model.builder.xml.XmlHelper;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.path.Path;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import com.yahoo.vespa.model.container.xml.ModelIdResolver;
import org.w3c.dom.Element;

import java.net.URI;
import java.util.Objects;
import java.util.Optional;

/**
 * Represents a model, e.g ONNX model for an embedder.
 *
 * @author bjorncs
 */
class Model {
    private final String paramName;
    private final String modelId;
    private final URI url;
    private final ApplicationFile file;
    private final ModelReference ref;

    private Model(DeployState ds, String paramName, String modelId, URI url, Path file) {
        this.paramName = Objects.requireNonNull(paramName);
        if (modelId == null && url == null && file == null)
            throw new IllegalArgumentException("At least one of 'model-id', 'url' or 'path' must be specified");
        this.modelId = modelId;
        this.url = url;
        this.file = file != null ? ds.getApplicationPackage().getFile(file) : null;
        this.ref = ModelIdResolver.resolveToModelReference(
                paramName, Optional.ofNullable(modelId), Optional.ofNullable(url).map(URI::toString),
                Optional.ofNullable(file).map(Path::toString), ds);
    }

    static Model fromParams(DeployState ds, String paramName, String modelId, URI url, Path file) {
        return new Model(ds, paramName, modelId, url, file);
    }

    static Optional<Model> fromXml(DeployState ds, Element parent, String name) {
        return XmlHelper.getOptionalChild(parent, name).map(e -> fromXml(ds, e));
    }

    static Model fromXml(DeployState ds, Element model) {
        var modelId = XmlHelper.getOptionalAttribute(model, "model-id").orElse(null);
        var url = XmlHelper.getOptionalAttribute(model, "url").map(URI::create).orElse(null);
        var path = XmlHelper.getOptionalAttribute(model, "path").map(Path::fromString).orElse(null);
        return new Model(ds, model.getTagName(), modelId, url, path);
    }

    void registerOnnxModelCost(ApplicationContainerCluster c) {
        var resolvedUrl = resolvedUrl().orElse(null);
        if (file != null) c.onnxModelCost().registerModel(file);
        else if (resolvedUrl != null) c.onnxModelCost().registerModel(resolvedUrl);
    }

    String name() { return paramName; }
    Optional<String> modelId() { return Optional.ofNullable(modelId); }
    Optional<URI> url() { return Optional.ofNullable(url); }
    Optional<URI> resolvedUrl() { return ref.url().map(u -> URI.create(u.value())); }
    Optional<ApplicationFile> file() { return Optional.ofNullable(file); }
    ModelReference modelReference() { return ref; }
}