aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/main/java/ai/vespa/rankingexpression/importer/operations/Transpose.java
blob: 9eecf6c40f404f9f794415b8a929d92e642f5860 (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 Vespa.ai. 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.tensor.TensorType;
import com.yahoo.tensor.functions.TensorFunction;

import java.util.List;

public class Transpose extends IntermediateOperation {

    private final AttributeMap attributes;

    public Transpose(String modelName, String nodeName, List<IntermediateOperation> inputs, AttributeMap attributes) {
        super(modelName, nodeName, inputs);
        this.attributes = attributes;
    }

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

        OrderedTensorType inputType = inputs.get(0).type().get();

        OrderedTensorType.Builder typeBuilder = new OrderedTensorType.Builder(resultValueType());
        for (int i = 0; i < inputType.rank(); ++i) {
            int inputIndex = inputType.rank() - 1 - i;
            if (attributes.getList("perm").isPresent()) {
                inputIndex = (int) attributes.getList("perm").get().get(i).asDouble();
            }
            TensorType.Dimension inputDimension = inputType.dimensions().get(inputIndex);
            typeBuilder.add(TensorType.Dimension.indexed(inputDimension.name(), inputDimension.size().get()));
        }
        OrderedTensorType result = typeBuilder.build();
        return typeBuilder.build();
    }

    @Override
    protected TensorFunction<Reference> lazyGetFunction() {
        if (!allInputFunctionsPresent(1))
            return null;
        return inputs.get(0).function().orElse(null);
    }

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

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

}