summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/integration/ml/importer/operations/Shape.java
blob: da566909adc174ef28c9cfbe819e2f2c536fba0a (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.integration.ml.importer.operations;

import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.searchlib.rankingexpression.integration.ml.importer.OrderedTensorType;
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()
                .add(TensorType.Dimension.indexed(vespaName(), inputType.dimensions().size()))
                .build();
    }

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

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

    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()));
    }

}