-
Notifications
You must be signed in to change notification settings - Fork 461
/
Statement.java
291 lines (260 loc) · 9.93 KB
/
Statement.java
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
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* 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.parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.Interval;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class Statement implements ParseInfo
{
protected static final Log LOG = LogFactory.getLog(Statement.class.getName());
public static final String OUTPUTSTATEMENT = "WRITE";
// parameter names for seq()
public static final String SEQ_FROM = "from";
public static final String SEQ_TO = "to";
public static final String SEQ_INCR = "incr";
public static final String SOURCE = "source";
public static final String SETWD = "setwd";
public static final String MATRIX_DATA_TYPE = "matrix";
public static final String FRAME_DATA_TYPE = "frame";
public static final String SCALAR_DATA_TYPE = "scalar";
public static final String DOUBLE_VALUE_TYPE = "double";
public static final String BOOLEAN_VALUE_TYPE = "boolean";
public static final String INT_VALUE_TYPE = "int";
public static final String STRING_VALUE_TYPE = "string";
// String constants related to Grouped Aggregate parameters
public static final String GAGG_TARGET = "target";
public static final String GAGG_GROUPS = "groups";
public static final String GAGG_WEIGHTS = "weights";
public static final String GAGG_FN = "fn";
public static final String GAGG_FN_SUM = "sum";
public static final String GAGG_FN_COUNT = "count";
public static final String GAGG_FN_MEAN = "mean";
public static final String GAGG_FN_VARIANCE = "variance";
public static final String GAGG_FN_CM = "centralmoment";
public static final String GAGG_FN_CM_ORDER = "order";
public static final String GAGG_NUM_GROUPS = "ngroups";
// String constants related to parameter server builtin function
public static final String PS_MODEL = "model";
public static final String PS_FEATURES = "features";
public static final String PS_LABELS = "labels";
public static final String PS_VAL_FEATURES = "val_features";
public static final String PS_VAL_LABELS = "val_labels";
public static final String PS_UPDATE_FUN = "upd";
public static final String PS_AGGREGATION_FUN = "agg";
public static final String PS_MODE = "mode";
public static final String PS_GRADIENTS = "gradients";
public static final String PS_SEED = "seed";
public enum PSModeType {
FEDERATED, LOCAL, REMOTE_SPARK
}
public static final String PS_UPDATE_TYPE = "utype";
public enum PSUpdateType {
BSP, ASP, SSP;
public boolean isBSP() {
return this == BSP;
}
public boolean isASP() {
return this == ASP;
}
}
public static final String PS_FREQUENCY = "freq";
public enum PSFrequency {
BATCH, EPOCH
}
public static final String PS_FED_WEIGHING = "weighing";
public static final String PS_FED_RUNTIME_BALANCING = "runtime_balancing";
public enum PSRuntimeBalancing {
NONE, RUN_MIN, CYCLE_AVG, CYCLE_MAX, SCALE_BATCH
}
public static final String PS_EPOCHS = "epochs";
public static final String PS_BATCH_SIZE = "batchsize";
public static final String PS_PARALLELISM = "k";
public static final String PS_SCHEME = "scheme";
public enum PSScheme {
DISJOINT_CONTIGUOUS, DISJOINT_ROUND_ROBIN, DISJOINT_RANDOM, OVERLAP_RESHUFFLE
}
public enum FederatedPSScheme {
KEEP_DATA_ON_WORKER, SHUFFLE, REPLICATE_TO_MAX, SUBSAMPLE_TO_MIN, BALANCE_TO_AVG
}
public static final String PS_HYPER_PARAMS = "hyperparams";
public static final String PS_CHECKPOINTING = "checkpointing";
public enum PSCheckpointing {
NONE, EPOCH, EPOCH10
}
// String constants related to federated parameter server functionality
// prefixed with code: "1701-NCC-" to not overwrite anything
public static final String PS_FED_BATCH_SIZE = "1701-NCC-batch_size";
public static final String PS_FED_DATA_SIZE = "1701-NCC-data_size";
public static final String PS_FED_POSS_BATCHES_LOCAL = "1701-NCC-poss_batches_local";
public static final String PS_FED_NAMESPACE = "1701-NCC-namespace";
public static final String PS_FED_GRADIENTS_FNAME = "1701-NCC-gradients_fname";
public static final String PS_FED_AGGREGATION_FNAME = "1701-NCC-aggregation_fname";
public static final String PS_FED_BATCHCOUNTER_VARID = "1701-NCC-batchcounter_varid";
public static final String PS_FED_MODEL_VARID = "1701-NCC-model_varid";
public abstract boolean controlStatement();
public abstract VariableSet variablesRead();
public abstract VariableSet variablesUpdated();
public abstract void initializeforwardLV(VariableSet activeIn);
public abstract VariableSet initializebackwardLV(VariableSet lo);
public abstract Statement rewriteStatement(String prefix);
// Used only insider python parser to allow for ignoring newline logic
private boolean isEmptyNewLineStatement = false;
public boolean isEmptyNewLineStatement() {
return isEmptyNewLineStatement;
}
public void setEmptyNewLineStatement(boolean isEmptyNewLineStatement) {
this.isEmptyNewLineStatement = isEmptyNewLineStatement;
}
///////////////////////////////////////////////////////////////////////////
// store exception info + position information for statements
///////////////////////////////////////////////////////////////////////////
private String _filename;
private int _beginLine, _beginColumn;
private int _endLine, _endColumn;
private String _text;
@Override
public void setFilename(String passed) { _filename = passed; }
@Override
public void setBeginLine(int passed) { _beginLine = passed; }
@Override
public void setBeginColumn(int passed) { _beginColumn = passed;}
@Override
public void setEndLine(int passed) { _endLine = passed; }
@Override
public void setEndColumn(int passed) { _endColumn = passed; }
/**
* Set ParserRuleContext values (begin line, begin column, end line, end
* column, and text).
*
* @param ctx
* the antlr ParserRuleContext
*/
public void setCtxValues(ParserRuleContext ctx) {
setBeginLine(ctx.start.getLine());
setBeginColumn(ctx.start.getCharPositionInLine());
setEndLine(ctx.stop.getLine());
setEndColumn(ctx.stop.getCharPositionInLine());
// preserve whitespace if possible
if ((ctx.start != null) && (ctx.stop != null) && (ctx.start.getStartIndex() != -1)
&& (ctx.stop.getStopIndex() != -1) && (ctx.start.getStartIndex() <= ctx.stop.getStopIndex())
&& (ctx.start.getInputStream() != null)) {
String text = ctx.start.getInputStream()
.getText(Interval.of(ctx.start.getStartIndex(), ctx.stop.getStopIndex()));
if (text != null) {
text = text.trim();
}
setText(text);
} else {
String text = ctx.getText();
if (text != null) {
text = text.trim();
}
setText(text);
}
}
/**
* Set ParserRuleContext values (begin line, begin column, end line, end
* column, and text) and file name.
*
* @param ctx
* the antlr ParserRuleContext
* @param filename
* the filename (if it exists)
*/
public void setCtxValuesAndFilename(ParserRuleContext ctx, String filename) {
setCtxValues(ctx);
setFilename(filename);
}
@Override
public int getBeginLine() { return _beginLine; }
@Override
public int getBeginColumn() { return _beginColumn; }
@Override
public int getEndLine() { return _endLine; }
@Override
public int getEndColumn() { return _endColumn; }
@Override
public String getFilename() { return _filename; }
public String printErrorLocation() {
String file = _filename;
if (file == null) {
file = "";
} else {
file = file + " ";
}
if (getText() != null) {
return "ERROR: " + file + "[line " + _beginLine + ":" + _beginColumn + "] -> " + getText() + " -- ";
} else {
return "ERROR: " + file + "[line " + _beginLine + ":" + _beginColumn + "] -- ";
}
}
public String printWarningLocation() {
String file = _filename;
if (file == null) {
file = "";
} else {
file = file + " ";
}
if (getText() != null) {
return "WARNING: " + file + "[line " + _beginLine + ":" + _beginColumn + "] -> " + getText() + " -- ";
} else {
return "WARNING: " + file + "[line " + _beginLine + ":" + _beginColumn + "] -- ";
}
}
public void raiseValidateError(String msg, boolean conditional) {
raiseValidateError(msg, conditional, null);
}
public void raiseValidateError(String msg, boolean conditional, String errorCode) {
if (conditional) {// warning if conditional
String fullMsg = this.printWarningLocation() + msg;
LOG.warn(fullMsg);
} else {// error and exception if unconditional
String fullMsg = this.printErrorLocation() + msg;
if (errorCode != null)
throw new LanguageException(fullMsg, errorCode);
else
throw new LanguageException(fullMsg);
}
}
@Override
public String getText() {
return _text;
}
@Override
public void setText(String text) {
this._text = text;
}
/**
* Set parse information.
*
* @param parseInfo
* parse information, such as beginning line position, beginning
* column position, ending line position, ending column position,
* text, and filename
*/
public void setParseInfo(ParseInfo parseInfo) {
_beginLine = parseInfo.getBeginLine();
_beginColumn = parseInfo.getBeginColumn();
_endLine = parseInfo.getEndLine();
_endColumn = parseInfo.getEndColumn();
_text = parseInfo.getText();
_filename = parseInfo.getFilename();
}
}