-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathneuron_error_models.py
311 lines (245 loc) · 9.57 KB
/
neuron_error_models.py
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
"""pytorchfi.error_models provides different error models out-of-the-box for use."""
import logging
import torch
from pytorchfi import core
from pytorchfi.util import *
# Helper Functions
def random_batch_element(pfi):
return random.randint(0, pfi.get_total_batches() - 1)
def random_neuron_location(pfi, layer=-1):
if layer == -1:
layer = random.randint(0, pfi.get_total_layers() - 1)
dim = pfi.get_layer_dim(layer)
shape = pfi.get_layer_shape(layer)
dim1_shape = shape[1]
dim1_rand = random.randint(0, dim1_shape - 1)
if dim > 2:
dim2_shape = shape[2]
dim2_rand = random.randint(0, dim2_shape - 1)
else:
dim2_rand = None
if dim > 3:
dim3_shape = shape[3]
dim3_rand = random.randint(0, dim3_shape - 1)
else:
dim3_rand = None
return (layer, dim1_rand, dim2_rand, dim3_rand)
# Neuron Perturbation Models
# single random neuron error in single batch element
def random_neuron_inj(pfi, min_val=-1, max_val=1):
b = random_batch_element(pfi)
(layer, C, H, W) = random_neuron_location(pfi)
err_val = random_value(min_val=min_val, max_val=max_val)
return pfi.declare_neuron_fi(
batch=[b], layer_num=[layer], dim1=[C], dim2=[H], dim3=[W], value=[err_val]
)
# single random neuron error in each batch element.
def random_neuron_inj_batched(pfi, min_val=-1, max_val=1, rand_loc=True, rand_val=True):
batch, layer_num, c_rand, h_rand, w_rand, value = ([] for i in range(6))
if not rand_loc:
(layer, C, H, W) = random_neuron_location(pfi)
if not rand_val:
err_val = random_value(min_val=min_val, max_val=max_val)
for i in range(pfi.get_total_batches()):
if rand_loc:
(layer, C, H, W) = random_neuron_location(pfi)
if rand_val:
err_val = random_value(min_val=min_val, max_val=max_val)
batch.append(i)
layer_num.append(layer)
c_rand.append(C)
h_rand.append(H)
w_rand.append(W)
value.append(err_val)
return pfi.declare_neuron_fi(
batch=batch,
layer_num=layer_num,
dim1=c_rand,
dim2=h_rand,
dim3=w_rand,
value=value,
)
# one random neuron error per layer in single batch element
def random_inj_per_layer(pfi, min_val=-1, max_val=1):
batch, layer_num, c_rand, h_rand, w_rand, value = ([] for i in range(6))
b = random_batch_element(pfi)
for i in range(pfi.get_total_layers()):
(layer, C, H, W) = random_neuron_location(pfi, layer=i)
batch.append(b)
layer_num.append(layer)
c_rand.append(C)
h_rand.append(H)
w_rand.append(W)
value.append(random_value(min_val=min_val, max_val=max_val))
return pfi.declare_neuron_fi(
batch=batch,
layer_num=layer_num,
dim1=c_rand,
dim2=h_rand,
dim3=w_rand,
value=value,
)
# one random neuron error per layer in each batch element
def random_inj_per_layer_batched(
pfi, min_val=-1, max_val=1, rand_loc=True, rand_val=True
):
batch, layer_num, c_rand, h_rand, w_rand, value = ([] for i in range(6))
for i in range(pfi.get_total_layers()):
if not rand_loc:
(layer, C, H, W) = random_neuron_location(pfi, layer=i)
if not rand_val:
err_val = random_value(min_val=min_val, max_val=max_val)
for b in range(pfi.get_total_batches()):
if rand_loc:
(layer, C, H, W) = random_neuron_location(pfi, layer=i)
if rand_val:
err_val = random_value(min_val=min_val, max_val=max_val)
batch.append(b)
layer_num.append(layer)
c_rand.append(C)
h_rand.append(H)
w_rand.append(W)
value.append(err_val)
return pfi.declare_neuron_fi(
batch=batch,
layer_num=layer_num,
dim1=c_rand,
dim2=h_rand,
dim3=w_rand,
value=value,
)
class single_bit_flip_func(core.fault_injection):
def __init__(self, model, batch_size, input_shape=None, **kwargs):
if input_shape is None:
input_shape = [3, 224, 224]
super().__init__(model, batch_size, input_shape=input_shape, **kwargs)
logging.basicConfig(format="%(asctime)-15s %(clientip)s %(user)-8s %(message)s")
self.bits = kwargs.get("bits", 8)
self.LayerRanges = []
def set_conv_max(self, data):
self.LayerRanges = data
def reset_conv_max(self, data):
self.LayerRanges = []
def get_conv_max(self, layer):
return self.LayerRanges[layer]
@staticmethod
def _twos_comp(val, bits):
if (val & (1 << (bits - 1))) != 0:
val = val - (1 << bits)
return val
def _twos_comp_shifted(self, val, nbits):
return (1 << nbits) + val if val < 0 else self._twos_comp(val, nbits)
def _flip_bit_signed(self, orig_value, max_value, bit_pos):
# quantum value
save_type = orig_value.dtype
total_bits = self.bits
logging.info("Original Value: %d", orig_value)
quantum = int((orig_value / max_value) * ((2.0 ** (total_bits - 1))))
twos_comple = self._twos_comp_shifted(quantum, total_bits) # signed
logging.info("Quantum: %d", quantum)
logging.info("Twos Couple: %d", twos_comple)
# binary representation
bits = bin(twos_comple)[2:]
logging.info("Bits: %s", bits)
# sign extend 0's
temp = "0" * (total_bits - len(bits))
bits = temp + bits
if len(bits) != total_bits:
raise AssertionError
logging.info("sign extend bits %s", bits)
# flip a bit
# use MSB -> LSB indexing
if bit_pos >= total_bits:
raise AssertionError
bits_new = list(bits)
bit_loc = total_bits - bit_pos - 1
if bits_new[bit_loc] == "0":
bits_new[bit_loc] = "1"
else:
bits_new[bit_loc] = "0"
bits_str_new = "".join(bits_new)
logging.info("New bits: %s", bits_str_new)
# GPU contention causes a weird bug...
if not bits_str_new.isdigit():
logging.info("Error: Not all the bits are digits (0/1)")
# convert to quantum
if not bits_str_new.isdigit():
raise AssertionError
new_quantum = int(bits_str_new, 2)
out = self._twos_comp(new_quantum, total_bits)
logging.info("Out: %s", out)
# get FP equivalent from quantum
new_value = out * ((2.0 ** (-1 * (total_bits - 1))) * max_value)
logging.info("New Value: %d", new_value)
return torch.tensor(new_value, dtype=save_type)
def single_bit_flip_signed_across_batch(self, module, input_val, output):
corrupt_conv_set = self.get_corrupt_layer()
range_max = self.get_conv_max(self.get_current_layer())
logging.info("Current layer: %s", self.get_current_layer())
logging.info("Range_max: %s", range_max)
if type(corrupt_conv_set) is list:
inj_list = list(
filter(
lambda x: corrupt_conv_set[x] == self.get_current_layer(),
range(len(corrupt_conv_set)),
)
)
for i in inj_list:
self.assert_inj_bounds(index=i)
prev_value = output[self.corrupt_batch[i]][self.corrupt_dim1[i]][
self.corrupt_dim2[i]
][self.corrupt_dim3[i]]
rand_bit = random.randint(0, self.bits - 1)
logging.info("Random Bit: %d", rand_bit)
new_value = self._flip_bit_signed(prev_value, range_max, rand_bit)
output[self.corrupt_batch[i]][self.corrupt_dim1[i]][
self.corrupt_dim2[i]
][self.corrupt_dim3[i]] = new_value
else:
if self.get_current_layer() == corrupt_conv_set:
prev_value = output[self.corrupt_batch][self.corrupt_dim1][
self.corrupt_dim2
][self.corrupt_dim3]
rand_bit = random.randint(0, self.bits - 1)
logging.info("Random Bit: %d", rand_bit)
new_value = self._flip_bit_signed(prev_value, range_max, rand_bit)
output[self.corrupt_batch][self.corrupt_dim1][self.corrupt_dim2][
self.corrupt_dim3
] = new_value
self.updateLayer()
if self.get_current_layer() >= self.get_total_layers():
self.reset_current_layer()
def random_neuron_single_bit_inj_batched(pfi, layer_ranges, rand_loc=True):
pfi.set_conv_max(layer_ranges)
batch, layer_num, c_rand, h_rand, w_rand = ([] for i in range(5))
if not rand_loc:
(layer, C, H, W) = random_neuron_location(pfi)
for i in range(pfi.get_total_batches()):
if rand_loc:
(layer, C, H, W) = random_neuron_location(pfi)
batch.append(i)
layer_num.append(layer)
c_rand.append(C)
h_rand.append(H)
w_rand.append(W)
return pfi.declare_neuron_fi(
batch=batch,
layer_num=layer_num,
dim1=c_rand,
dim2=h_rand,
dim3=w_rand,
function=pfi.single_bit_flip_signed_across_batch,
)
def random_neuron_single_bit_inj(pfi, layer_ranges):
# TODO Support multiple error models via list
pfi.set_conv_max(layer_ranges)
batch = random_batch_element(pfi)
(layer, C, H, W) = random_neuron_location(pfi)
return pfi.declare_neuron_fi(
batch=[batch],
layer_num=[layer],
dim1=[C],
dim2=[H],
dim3=[W],
function=pfi.single_bit_flip_signed_across_batch,
)