aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/main/java/ai/vespa/rankingexpression/importer/operations/Constant.java
blob: b12f83f274bfde81e13348689caea9c369216066 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.rankingexpression.importer.operations;

import ai.vespa.rankingexpression.importer.DimensionRenamer;
import ai.vespa.rankingexpression.importer.OrderedTensorType;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.functions.TensorFunction;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class Constant extends IntermediateOperation {

    private final String modelName;

    public Constant(String modelName, String nodeName, OrderedTensorType type) {
        super(modelName, nodeName, Collections.emptyList());
        this.modelName = modelName;
        this.type = type.rename(vespaName() + "_");
    }

    /** Constant names are prefixed by "modelName_" to avoid name conflicts between models */
//    @Override
//    public String vespaName() {
//        return modelName + "_" + vespaName(name);
//    }

    @Override
    protected OrderedTensorType lazyGetType() {
        return type;
    }

    @Override
    protected TensorFunction lazyGetFunction() {
        return null;  // will be added by function() since this is constant.
    }

    /**
     * Constant values are sent in via the constantValueFunction, as the
     * dimension names and thus the data layout depends on the dimension
     * renaming which happens after the conversion to intermediate graph.
     */
    @Override
    public Optional<Value> getConstantValue() {
        return Optional.ofNullable(constantValueFunction).map(func -> func.apply(type));
    }

    @Override
    public void addDimensionNameConstraints(DimensionRenamer renamer) {
        addConstraintsFrom(type, renamer);
    }

    @Override
    public boolean isConstant() {
        return true;
    }

    @Override
    public Constant withInputs(List<IntermediateOperation> inputs) {
        if ( ! inputs.isEmpty())
            throw new IllegalArgumentException("Constant cannot take inputs");
        Constant constant = new Constant(modelName(), name(), type);
        constant.setConstantValueFunction(constantValueFunction);
        return constant;
    }

    @Override
    public String operationName() { return "Constant"; }

    @Override
    public String toString() {
        return "Constant(" + type + ")";
    }

    @Override
    public String toFullString() {
        return "\t" + type + ":\tConstant(" + type + ")";
    }

}