aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/main/java/ai/vespa/rankingexpression/importer/operations/Shape.java
blob: c1cffd4243e8cbe6e049c892bed1f019d92b5545 (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
// Copyright Yahoo. 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.OrderedTensorType;
import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.tensor.IndexedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.functions.TensorFunction;

import java.util.List;

public class Shape extends IntermediateOperation {

    public Shape(String modelName, String nodeName, List<IntermediateOperation> inputs) {
        super(modelName, nodeName, inputs);
        createConstantValue();
    }

    @Override
    protected OrderedTensorType lazyGetType() {
        if ( ! allInputTypesPresent(1)) return null;

        OrderedTensorType inputType = inputs.get(0).type().get();
        return new OrderedTensorType.Builder(resultValueType())
                .add(TensorType.Dimension.indexed(vespaName(), inputType.dimensions().size()))
                .build();
    }

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

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

    @Override
    public Shape withInputs(List<IntermediateOperation> inputs) {
        return new Shape(modelName(), name(), inputs);
    }

    private void createConstantValue() {
        if (!allInputTypesPresent(1)) {
            return;
        }
        OrderedTensorType inputType = inputs.get(0).type().get();
        IndexedTensor.BoundBuilder builder = (IndexedTensor.BoundBuilder) Tensor.Builder.of(type().get().type());
        List<TensorType.Dimension> inputDimensions = inputType.dimensions();
        for (int i = 0; i < inputDimensions.size(); i++) {
            builder.cellByDirectIndex(i, inputDimensions.get(i).size().orElse(-1L));
        }
        this.setConstantValue(new TensorValue(builder.build()));
    }

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

}