/
FailureTestingListener.java
316 lines (259 loc) · 9.95 KB
/
FailureTestingListener.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* ******************************************************************************
* *
* *
* * This program and the accompanying materials are made available under the
* * terms of the Apache License, Version 2.0 which is available at
* * https://www.apache.org/licenses/LICENSE-2.0.
* *
* * See the NOTICE file distributed with this work for additional
* * information regarding copyright ownership.
* * 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.
* *
* * SPDX-License-Identifier: Apache-2.0
* *****************************************************************************
*/
package org.deeplearning4j.optimize.listeners;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.deeplearning4j.nn.api.Model;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.optimize.api.TrainingListener;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.*;
@Slf4j
public class FailureTestingListener implements TrainingListener, Serializable {
public enum FailureMode {OOM, SYSTEM_EXIT_1, ILLEGAL_STATE, INFINITE_SLEEP}
public enum CallType {ANY, EPOCH_START, EPOCH_END, FORWARD_PASS, GRADIENT_CALC, BACKWARD_PASS, ITER_DONE}
private final FailureTrigger trigger;
private final FailureMode failureMode;
public FailureTestingListener(@NonNull FailureMode mode, @NonNull FailureTrigger trigger){
this.trigger = trigger;
this.failureMode = mode;
}
@Override
public void iterationDone(Model model, int iteration, int epoch) {
call(CallType.ITER_DONE, model);
}
@Override
public void onEpochStart(Model model) {
call(CallType.EPOCH_START, model);
}
@Override
public void onEpochEnd(Model model) {
call(CallType.EPOCH_END, model);
}
@Override
public void onForwardPass(Model model, List<INDArray> activations) {
call(CallType.FORWARD_PASS, model);
}
@Override
public void onForwardPass(Model model, Map<String, INDArray> activations) {
call(CallType.FORWARD_PASS, model);
}
@Override
public void onGradientCalculation(Model model) {
call(CallType.GRADIENT_CALC, model);
}
@Override
public void onBackwardPass(Model model) {
call(CallType.BACKWARD_PASS, model);
}
protected void call(CallType callType, Model model){
if(!trigger.initialized()){
trigger.initialize();
}
int iter;
int epoch;
if(model instanceof MultiLayerNetwork){
iter = ((MultiLayerNetwork) model).getIterationCount();
epoch = ((MultiLayerNetwork) model).getEpochCount();
} else {
iter = ((ComputationGraph) model).getIterationCount();
epoch = ((ComputationGraph) model).getEpochCount();
}
boolean triggered = trigger.triggerFailure(callType, iter, epoch, model);
if(triggered){
log.error("*** FailureTestingListener was triggered on iteration {}, epoch {} - Failure mode is set to {} ***",
iter, epoch, failureMode);
switch (failureMode){
case OOM:
List<INDArray> list = new ArrayList<>();
while(true){
INDArray arr = Nd4j.createUninitialized(1_000_000_000);
list.add(arr);
}
//break;
case SYSTEM_EXIT_1:
log.error("Exiting due to FailureTestingListener triggering - calling System.exit(1)");
System.exit(1);
break;
case ILLEGAL_STATE:
log.error("Throwing new IllegalStateException due to FailureTestingListener triggering");
throw new IllegalStateException("FailureTestListener was triggered with failure mode " + failureMode
+ " - iteration " + iter + ", epoch " + epoch);
case INFINITE_SLEEP:
while(true){
try {
Thread.sleep(10000);
} catch (InterruptedException e){
//Ignore
}
}
default:
throw new RuntimeException("Unknown enum value: " + failureMode);
}
}
}
@Data
public static abstract class FailureTrigger implements Serializable {
private boolean initialized = false;
/**
* If true: trigger the failure. If false: don't trigger failure
* @param callType Type of call
* @param iteration Iteration number
* @param epoch Epoch number
* @param model Model
* @return
*/
public abstract boolean triggerFailure(CallType callType, int iteration, int epoch, Model model);
public boolean initialized(){
return initialized;
}
public void initialize(){
this.initialized = true;
}
}
@AllArgsConstructor
public static class And extends FailureTrigger{
protected List<FailureTrigger> triggers;
public And(FailureTrigger... triggers){
this.triggers = Arrays.asList(triggers);
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
boolean b = true;
for(FailureTrigger ft : triggers)
b &= ft.triggerFailure(callType, iteration, epoch, model);
return b;
}
@Override
public void initialize(){
super.initialize();
for(FailureTrigger ft : triggers)
ft.initialize();
}
}
public static class Or extends And {
public Or(FailureTrigger... triggers) {
super(triggers);
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
boolean b = false;
for(FailureTrigger ft : triggers)
b |= ft.triggerFailure(callType, iteration, epoch, model);
return b;
}
}
@Data
public static class RandomProb extends FailureTrigger {
private final CallType callType;
private final double probability;
private Random rng;
public RandomProb(CallType callType, double probability){
this.callType = callType;
this.probability = probability;
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
return (this.callType == CallType.ANY || callType == this.callType) && rng.nextDouble() < probability;
}
@Override
public void initialize(){
super.initialize();
this.rng = new Random();
}
}
@Data
public static class TimeSinceInitializedTrigger extends FailureTrigger {
private final long msSinceInit;
private long initTime;
public TimeSinceInitializedTrigger(long msSinceInit){
this.msSinceInit = msSinceInit;
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
return (System.currentTimeMillis() - initTime) > msSinceInit;
}
@Override
public void initialize(){
super.initialize();
this.initTime = System.currentTimeMillis();
}
}
@Data
public static class UserNameTrigger extends FailureTrigger {
private final String userName;
private boolean shouldFail = false;
public UserNameTrigger(@NonNull String userName) {
this.userName = userName;
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
return shouldFail;
}
@Override
public void initialize(){
super.initialize();
shouldFail = this.userName.equalsIgnoreCase(System.getProperty("user.name"));
}
}
//System.out.println("Hostname: " + InetAddress.getLocalHost().getHostName());
@Data
public static class HostNameTrigger extends FailureTrigger{
private final String hostName;
private boolean shouldFail = false;
public HostNameTrigger(@NonNull String hostName) {
this.hostName = hostName;
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
return shouldFail;
}
@Override
public void initialize(){
super.initialize();
try {
String hostname = InetAddress.getLocalHost().getHostName();
log.info("FailureTestingListere hostname: {}", hostname);
shouldFail = this.hostName.equalsIgnoreCase(hostname);
} catch (Exception e){
throw new RuntimeException(e);
}
}
}
@Data
public static class IterationEpochTrigger extends FailureTrigger {
private final boolean isEpoch;
private final int count;
public IterationEpochTrigger(boolean isEpoch, int count){
this.isEpoch = isEpoch;
this.count = count;
}
@Override
public boolean triggerFailure(CallType callType, int iteration, int epoch, Model model) {
return (isEpoch && epoch == count) || (!isEpoch && iteration == count);
}
}
}