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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// 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.OrderedTensorType;
import ai.vespa.rankingexpression.importer.DimensionRenamer;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.functions.Reduce;
import com.yahoo.tensor.functions.TensorFunction;
import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleBinaryOperator;
public class Join extends IntermediateOperation {
private final DoubleBinaryOperator operator;
public Join(String modelName, String nodeName, List<IntermediateOperation> inputs, DoubleBinaryOperator operator) {
super(modelName, nodeName, inputs);
this.operator = operator;
}
@Override
protected OrderedTensorType lazyGetType() {
if ( ! allInputTypesPresent(2)) return null;
OrderedTensorType a = largestInput().type().get();
OrderedTensorType b = smallestInput().type().get();
OrderedTensorType.Builder builder = new OrderedTensorType.Builder(resultValueType());
int sizeDifference = a.rank() - b.rank();
for (int i = 0; i < a.rank(); ++i) {
TensorType.Dimension aDim = a.dimensions().get(i);
long size = aDim.size().orElse(-1L);
if (i - sizeDifference >= 0) {
TensorType.Dimension bDim = b.dimensions().get(i - sizeDifference);
size = Math.max(size, bDim.size().orElse(-1L));
}
if (aDim.type() == TensorType.Dimension.Type.indexedBound) {
builder.add(TensorType.Dimension.indexed(aDim.name(), size));
} else if (aDim.type() == TensorType.Dimension.Type.indexedUnbound) {
builder.add(TensorType.Dimension.indexed(aDim.name()));
} else if (aDim.type() == TensorType.Dimension.Type.mapped) {
builder.add(TensorType.Dimension.mapped(aDim.name()));
}
}
return builder.build();
}
@Override
protected TensorFunction lazyGetFunction() {
if ( ! allInputTypesPresent(2)) return null;
if ( ! allInputFunctionsPresent(2)) return null;
IntermediateOperation a = largestInput();
IntermediateOperation b = smallestInput();
List<String> aDimensionsToReduce = new ArrayList<>();
List<String> bDimensionsToReduce = new ArrayList<>();
int sizeDifference = a.type().get().rank() - b.type().get().rank();
for (int i = 0; i < b.type().get().rank(); ++i) {
TensorType.Dimension bDim = b.type().get().dimensions().get(i);
TensorType.Dimension aDim = a.type().get().dimensions().get(i + sizeDifference);
long bSize = bDim.size().orElse(-1L);
long aSize = aDim.size().orElse(-1L);
if (bSize == 1L && aSize != 1L) {
bDimensionsToReduce.add(bDim.name());
}
if (aSize == 1L && bSize != 1L) {
aDimensionsToReduce.add(bDim.name());
}
}
TensorFunction aReducedFunction = a.function().get();
if (aDimensionsToReduce.size() > 0) {
aReducedFunction = new Reduce(a.function().get(), Reduce.Aggregator.sum, aDimensionsToReduce);
}
TensorFunction bReducedFunction = b.function().get();
if (bDimensionsToReduce.size() > 0) {
bReducedFunction = new Reduce(b.function().get(), Reduce.Aggregator.sum, bDimensionsToReduce);
}
// retain order of inputs
if (a == inputs.get(1)) {
TensorFunction temp = bReducedFunction;
bReducedFunction = aReducedFunction;
aReducedFunction = temp;
}
return new com.yahoo.tensor.functions.Join(aReducedFunction, bReducedFunction, operator);
}
@Override
public void addDimensionNameConstraints(DimensionRenamer renamer) {
if ( ! allInputTypesPresent(2)) return;
OrderedTensorType a = largestInput().type().get();
OrderedTensorType b = smallestInput().type().get();
int sizeDifference = a.rank() - b.rank();
for (int i = 0; i < b.rank(); ++i) {
String bDim = b.dimensions().get(i).name();
String aDim = a.dimensions().get(i + sizeDifference).name();
renamer.addConstraint(aDim, bDim, DimensionRenamer.Constraint.equal(false), this);
}
}
private IntermediateOperation largestInput() {
OrderedTensorType a = inputs.get(0).type().get();
OrderedTensorType b = inputs.get(1).type().get();
return a.rank() >= b.rank() ? inputs.get(0) : inputs.get(1);
}
private IntermediateOperation smallestInput() {
OrderedTensorType a = inputs.get(0).type().get();
OrderedTensorType b = inputs.get(1).type().get();
return a.rank() < b.rank() ? inputs.get(0) : inputs.get(1);
}
@Override
public Join withInputs(List<IntermediateOperation> inputs) {
return new Join(modelName(), name(), inputs, operator);
}
@Override
public String operationName() { return "Join"; }
}
|