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
83
84
85
86
87
|
// 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.DimensionRenamer;
import ai.vespa.rankingexpression.importer.OrderedTensorType;
import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue;
import com.yahoo.searchlib.rankingexpression.rule.ArithmeticNode;
import com.yahoo.searchlib.rankingexpression.rule.ArithmeticOperator;
import com.yahoo.searchlib.rankingexpression.rule.ConstantNode;
import com.yahoo.searchlib.rankingexpression.rule.EmbracedNode;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.functions.Generate;
import com.yahoo.tensor.functions.TensorFunction;
import java.util.List;
import static com.yahoo.searchlib.rankingexpression.rule.TensorFunctionNode.wrapScalar;
public class Range extends IntermediateOperation {
private double start;
private double limit;
private double delta;
private long elements;
public Range(String modelName, String nodeName, List<IntermediateOperation> inputs) {
super(modelName, nodeName, inputs);
}
private double getConstantInput(int index, String name) {
IntermediateOperation input = inputs.get(index);
if (input.getConstantValue().isEmpty()) {
throw new IllegalArgumentException("Range: " + name + " input must be a constant.");
}
Tensor value = input.getConstantValue().get().asTensor();
if ( ! input.getConstantValue().get().hasDouble()) {
throw new IllegalArgumentException("Range: " + name + " input must be a scalar.");
}
return value.asDouble();
}
@Override
protected OrderedTensorType lazyGetType() {
if ( ! allInputTypesPresent(3)) return null;
start = getConstantInput(0, "start"); // must be constant because we need to know type
limit = getConstantInput(1, "limit");
delta = getConstantInput(2, "delta");
elements = (long) Math.ceil((limit - start) / delta);
OrderedTensorType type = new OrderedTensorType.Builder(inputs.get(0).type().get().type().valueType())
.add(TensorType.Dimension.indexed(vespaName(), elements))
.build();
return type;
}
@Override
protected TensorFunction<Reference> lazyGetFunction() {
if ( ! allInputTypesPresent(3)) return null;
String dimensionName = type().get().dimensionNames().get(0);
ExpressionNode startExpr = new ConstantNode(new DoubleValue(start));
ExpressionNode deltaExpr = new ConstantNode(new DoubleValue(delta));
ExpressionNode dimExpr = new EmbracedNode(new ReferenceNode(dimensionName));
ExpressionNode stepExpr = new ArithmeticNode(deltaExpr, ArithmeticOperator.MULTIPLY, dimExpr);
ExpressionNode addExpr = new ArithmeticNode(startExpr, ArithmeticOperator.PLUS, stepExpr);
TensorFunction<Reference> function = Generate.bound(type.type(), wrapScalar(addExpr));
return function;
}
@Override
public void addDimensionNameConstraints(DimensionRenamer renamer) {
addConstraintsFrom(type, renamer);
}
@Override
public Range withInputs(List<IntermediateOperation> inputs) {
return new Range(modelName(), name(), inputs);
}
@Override
public String operationName() { return "Range"; }
}
|