forked from apache/systemds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaryOp.java
More file actions
278 lines (242 loc) · 8.15 KB
/
NaryOp.java
File metadata and controls
278 lines (242 loc) · 8.15 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sysds.hops;
import org.apache.sysds.common.Types.DataType;
import org.apache.sysds.common.Types.OpOpN;
import org.apache.sysds.common.Types.ValueType;
import org.apache.sysds.hops.rewrite.HopRewriteUtils;
import org.apache.sysds.lops.Lop;
import org.apache.sysds.common.Types.ExecType;
import org.apache.sysds.lops.Nary;
import org.apache.sysds.runtime.einsum.EinsumEquationValidator;
import org.apache.sysds.runtime.meta.DataCharacteristics;
import org.apache.sysds.runtime.meta.MatrixCharacteristics;
/**
* The NaryOp Hop allows for a variable number of operands. Functionality
* such as 'printf' (overloaded into the existing print function) is an example
* of an operation that potentially takes a variable number of operands.
*
*/
public class NaryOp extends Hop {
protected OpOpN _op = null;
protected NaryOp() {
}
/**
* NaryOp constructor.
*
* @param name
* the target name, typically set by the DMLTranslator when
* constructing Hops. (For example, 'parsertemp1'.)
* @param dataType
* the target data type (SCALAR for printf)
* @param valueType
* the target value type (STRING for printf)
* @param op
* the operation type (such as PRINTF)
* @param inputs
* a variable number of input Hops
*/
public NaryOp(String name, DataType dataType, ValueType valueType,
OpOpN op, Hop... inputs) {
super(name, dataType, valueType);
_op = op;
for (int i = 0; i < inputs.length; i++) {
getInput().add(i, inputs[i]);
inputs[i].getParent().add(this);
}
refreshSizeInformation();
}
public OpOpN getOp() {
return _op;
}
@Override
public String getOpString() {
return "m(" + _op.name().toLowerCase() + ")";
}
@Override
public boolean isGPUEnabled() {
return false;
}
/**
* Construct the corresponding Lops for this Hop
*/
@Override
public Lop constructLops() {
// reuse existing lop
if (getLops() != null)
return getLops();
try {
Lop[] inLops = new Lop[getInput().size()];
for (int i = 0; i < getInput().size(); i++)
inLops[i] = getInput().get(i).constructLops();
ExecType et = optFindExecType();
Nary multipleCPLop = new Nary(_op, getDataType(), getValueType(), inLops, et);
setOutputDimensions(multipleCPLop);
setLineNumbers(multipleCPLop);
setLops(multipleCPLop);
}
catch (Exception e) {
throw new HopsException(this.printErrorLocation() + "error constructing Lops for NaryOp -- \n ", e);
}
// add reblock/checkpoint lops if necessary
constructAndSetLopsDataFlowProperties();
return getLops();
}
@Override
public boolean allowsAllExecTypes() {
return false;
}
@Override
public void computeMemEstimate(MemoTable memo) {
//overwrites default hops behavior
super.computeMemEstimate(memo);
//specific case for function call
if( _op == OpOpN.EVAL || _op == OpOpN.LIST ) {
_memEstimate = OptimizerUtils.INT_SIZE;
_outputMemEstimate = OptimizerUtils.INT_SIZE;
_processingMemEstimate = 0;
}
}
@Override
protected double computeOutputMemEstimate(long dim1, long dim2, long nnz) {
double sparsity = OptimizerUtils.getSparsity(dim1, dim2, nnz);
return OptimizerUtils.estimateSizeExactSparsity(dim1, dim2, sparsity);
}
@Override
protected ExecType optFindExecType(boolean transitive) {
checkAndSetForcedPlatform();
ExecType REMOTE = ExecType.SPARK;
//forced / memory-based / threshold-based decision
if( _etypeForced != null ) {
_etype = _etypeForced;
}
else
{
if ( OptimizerUtils.isMemoryBasedOptLevel() )
_etype = findExecTypeByMemEstimate();
// Choose CP, if the input dimensions are below threshold or if the input is a vector
else if ( areDimsBelowThreshold() )
_etype = ExecType.CP;
else
_etype = REMOTE;
//check for valid CP dimensions and matrix size
checkAndSetInvalidCPDimsAndSize();
}
//mark for recompile (forever)
setRequiresRecompileIfNecessary();
//ensure cp exec type for single-node operations
if ( _op == OpOpN.PRINTF || _op == OpOpN.EVAL || _op == OpOpN.LIST || _op == OpOpN.EINSUM
//TODO: cbind/rbind of lists only support in CP right now
|| (_op == OpOpN.CBIND && getInput().get(0).getDataType().isList())
|| (_op == OpOpN.RBIND && getInput().get(0).getDataType().isList())
|| _op.isCellOp() && getInput().stream().allMatch(h -> h.getDataType().isScalar()))
_etype = ExecType.CP;
return _etype;
}
@Override
protected double computeIntermediateMemEstimate(long dim1, long dim2, long nnz) {
return 0;
}
@Override
@SuppressWarnings("incomplete-switch")
protected DataCharacteristics inferOutputCharacteristics(MemoTable memo) {
if( !getDataType().isScalar() && !getDataType().isFrame()) {
DataCharacteristics[] dc = memo.getAllInputStats(getInput());
switch( _op ) {
case CBIND: return new MatrixCharacteristics(
HopRewriteUtils.getMaxInputDim(dc, true),
HopRewriteUtils.getSumValidInputDims(dc, false), -1,
HopRewriteUtils.getSumValidInputNnz(dc, true));
case RBIND: return new MatrixCharacteristics(
HopRewriteUtils.getSumValidInputDims(dc, true),
HopRewriteUtils.getMaxInputDim(dc, false), -1,
HopRewriteUtils.getSumValidInputNnz(dc, true));
case MIN:
case MAX:
case PLUS:
case MULT: return new MatrixCharacteristics(
HopRewriteUtils.getMaxInputDim(this, true),
HopRewriteUtils.getMaxInputDim(this, false), -1, -1);
case LIST:
return new MatrixCharacteristics(getInput().size(), 1, -1, -1);
}
}
return null; //do nothing
}
@Override
public void refreshSizeInformation() {
switch( _op ) {
case CBIND:
if( !getInput().get(0).getDataType().isList() ) {
setDim1(HopRewriteUtils.getMaxInputDim(this, true));
setDim2(HopRewriteUtils.getSumValidInputDims(this, false));
setNnz(HopRewriteUtils.getSumValidInputNnz(this));
}
break;
case RBIND:
if( !getInput().get(0).getDataType().isList() ) {
setDim1(HopRewriteUtils.getSumValidInputDims(this, true));
setDim2(HopRewriteUtils.getMaxInputDim(this, false));
setNnz(HopRewriteUtils.getSumValidInputNnz(this));
}
break;
case MIN:
case MAX:
case PLUS:
case MULT:
setDim1(getDataType().isScalar() ? 0 : HopRewriteUtils.getMaxInputDim(this, true));
setDim2(getDataType().isScalar() ? 0 : HopRewriteUtils.getMaxInputDim(this, false));
break;
case LIST:
setDim1(getInput().size());
setDim2(1);
break;
case EINSUM:
String equationString = ((LiteralOp) _input.get(0)).getStringValue();
var dims = EinsumEquationValidator.validateEinsumEquationAndReturnDimensions(equationString, this.getInput().subList(1, this.getInput().size()));
setDim1(dims.getLeft());
setDim2(dims.getMiddle());
setDataType(dims.getRight());
break;
case PRINTF:
case EVAL:
//do nothing:
}
}
@Override
public Object clone() throws CloneNotSupportedException {
NaryOp multipleOp = new NaryOp();
// copy generic attributes
multipleOp.clone(this, false);
// copy specific attributes
multipleOp._op = _op;
return multipleOp;
}
@Override
public boolean compare(Hop that) {
if (!(that instanceof NaryOp) || _op == OpOpN.PRINTF)
return false;
NaryOp that2 = (NaryOp) that;
boolean ret = (_op == that2._op
&& getInput().size() == that2.getInput().size());
for( int i=0; i<getInput().size() && ret; i++ )
ret &= (getInput().get(i) == that2.getInput().get(i));
return ret;
}
}