-
Notifications
You must be signed in to change notification settings - Fork 0
/
FaultInjectionManager.py
295 lines (235 loc) · 11.3 KB
/
FaultInjectionManager.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
import os
import shutil
import time
import math
import random
from datetime import timedelta
import torch
from torch.nn import Module
from torch.utils.data import DataLoader
from tqdm import tqdm
from FaultGenerators.WeightFaultInjector import WeightFaultInjector
from FaultGenerators.utils import float32_bit_flip
class FaultInjectionManager:
def __init__(self,
network: Module,
network_name: str,
device: torch.device,
loader: DataLoader):
self.network = network
self.network_name = network_name
self.loader = loader
self.device = device
self.transient = False
self.one = True
self.inject = False
self.current = None
self.size = {}
self.spike = {}
self.faults = {}
self.mask = {}
self.num_step = 25
# The clean output of the network after the first run
self.clean_output_scores = list()
self.clean_output_indices = list()
# The weight fault injector
self.weight_fault_injector = WeightFaultInjector(self.network)
# The output dir
self.label_output_dir = f'output/{self.network_name}/pt/label/batch_size_{self.loader.batch_size}'
self.clean_output_dir = f'output/{self.network_name}/pt/clean/batch_size_{self.loader.batch_size}'
self.faulty_output_dir = f'output/{self.network_name}/pt/faulty/batch_size_{self.loader.batch_size}'
# Create the output dir
os.makedirs(self.label_output_dir, exist_ok=True)
os.makedirs(self.clean_output_dir, exist_ok=True)
os.makedirs(self.faulty_output_dir, exist_ok=True)
def run_clean(self, fault_manager):
"""
Run a clean inference of the network
:return: A string containing the formatted time elapsed from the beginning to the end of the fault injection
campaign
"""
self.inject = False
with torch.no_grad():
# Start measuring the time elapsed
start_time = time.time()
# Cycle all the batches in the data loader
pbar = tqdm(self.loader,
colour='green',
desc=f'Clean Run',
ncols=shutil.get_terminal_size().columns)
for batch_id, batch in enumerate(pbar):
#print(batch_id)
data, label = batch
#print(len(label)) total of 10000 images
data = data.to(self.device)
# Run inference on the current batch
scores, indices = self.__run_inference_on_batch(data=data)
# Save the output
torch.save(scores, f'{self.clean_output_dir}/batch_{batch_id}.pt')
torch.save(label, f'{self.label_output_dir}/batch_{batch_id}.pt')
# Append the results to a list
self.clean_output_scores.append(scores)
self.clean_output_indices.append(indices)
# Stop measuring the time
elapsed = math.ceil(time.time() - start_time)
return str(timedelta(seconds=elapsed))
def run_faulty_campaign(self,
fault_list: list,
different_scores: bool = False) -> str:
"""
Run a faulty injection campaign for the network
:param fault_list: list of fault to inject
:param different_scores: Default False. If True, compare the faulty scores with the clean scores, otherwise
compare the top-1 indices
:return: A string containing the formatted time elapsed from the beginning to the end of the fault injection
campaign
"""
total_different_predictions = 0
total_predictions = 0
self.inject = True
with torch.no_grad():
# Start measuring the time elapsed
start_time = time.time()
pbar = tqdm(self.loader,
total=len(self.loader) * len(fault_list),
colour='green',
desc=f'Fault Injection campaign',
ncols=shutil.get_terminal_size().columns * 2)
# Cycle all the batches in the data loader
for batch_id, batch in enumerate(self.loader):
data, _ = batch
data = data.to(self.device)
# Inject all the faults in a single batch
for fault_id, fault in enumerate(fault_list):
if fault.layer_name.split('.')[1] != 'potential' and fault.layer_name.split('.')[1] != 'spike':
# Inject faults in the weight
self.__inject_fault_on_weight(fault, fault_mode='stuck-at')
self.current = None
else:
self.current = [fault.layer_name.split('.')[0], fault.layer_name.split('.')[1], fault.tensor_index, fault.bit, random.randint(0,24)]
if (fault.layer_name.split('.')[1] != 'potential' and fault.layer_name.split('.')[1] != 'spike') or self.one == True:
# Run inference on the current batch
faulty_scores, faulty_indices = self.__run_inference_on_batch(data=data)
# Save the output
torch.save(faulty_scores, f'{self.faulty_output_dir}/fault_{fault_id}_batch_{batch_id}.pt')
# Measure the different predictions
if different_scores:
different_predictions = int(torch.ne(faulty_scores,
self.clean_output_scores[batch_id]).sum())
else:
different_predictions = int(torch.ne(torch.Tensor(faulty_indices),
torch.Tensor(self.clean_output_indices[batch_id])).sum())
# Measure the loss in accuracy
total_different_predictions += different_predictions
total_predictions += len(batch[0])
different_predictions_percentage = 100 * total_different_predictions / total_predictions
pbar.set_postfix({'Different': f'{different_predictions_percentage:.4f}%'})
if fault.layer_name.split('.')[1] != 'potential' and fault.layer_name.split('.')[1] != 'spike':
# Restore the golden value
self.weight_fault_injector.restore_golden()
# Update the progress bar
pbar.update(1)
# Stop measuring the time
elapsed = math.ceil(time.time() - start_time)
return str(timedelta(seconds=elapsed))
def __run_inference_on_batch(self,
data: torch.Tensor):
"""
Rim a fault injection on a single batch
:param data: The input data from the batch
:return: a tuple (scores, indices) where the scores are the vector score of each element in the batch and the
indices are the argmax of the vector score
"""
# Execute the network on the batch
network_output = self.network(data)
prediction = torch.topk(network_output, k=1)
# Get the score and the indices of the predictions
prediction_scores = network_output.cpu()
prediction_indices = [int(fault) for fault in prediction.indices]
return prediction_scores, prediction_indices
def __inject_fault_on_weight(self,
fault,
fault_mode='stuck-at'):
"""
Inject a fault in one of the weight of the network
:param fault: The fault to inject
:param fault_mode: Default 'stuck-at'. One of either 'stuck-at' or 'bit-flip'. Which kind of fault model to
employ
"""
if fault_mode == 'stuck-at':
self.weight_fault_injector.inject_stuck_at(layer_name=f'{fault.layer_name}',
tensor_index=fault.tensor_index,
bit=fault.bit,
value=fault.value)
elif fault_mode == 'bit-flip':
self.weight_fault_injector.inject_bit_flip(layer_name=f'{fault.layer_name}',
tensor_index=fault.tensor_index,
bit=fault.bit,)
else:
print('FaultInjectionManager: Invalid fault mode')
quit()
def flatten_layer_dim(self, layer):
'''
For a given layer (Multi dimensional array) compute
the dimension of the flatten array (Mono dimensional array)
'''
n = 1
for dim in self.size[layer]:
n = n*dim
return n
def compute_flatten_index(self, layer, index_arr):
'''
Given an index of the layer (Multi dimensional array) return
the corresponding index of the flatten array
'''
index = 0
for n in range(len(index_arr)):
i = n+1
tmp = index_arr[-i]
for y in range(n):
i2 = y+1
tmp = tmp *self.size[layer][-i2]
index = index + tmp
return index
def compute_mask(self):
'''
Compute the mask to perform the xor injection
'''
for layer, faults in self.faults.items():
if layer not in self.mask.keys():
self.mask[layer] = torch.zeros(self.size[layer], dtype=torch.int32)
for tensor_index, bit_index in faults:
self.mask[layer][tensor_index] += 2**bit_index
def injection_dict(self, pot, spike, layer, pot_or_spike, tensor_index, bit_index):
'''
inject faults using the dictionary
'''
if pot_or_spike == "potential":
for i in range(self.size[layer][0]):
golden_value = float(pot[(i,) + tensor_index])
faulty_value = float32_bit_flip(golden_value=golden_value, bit=bit_index)
pot[(i,) + tensor_index] = faulty_value
else: #to be modified
for i in range(self.size[layer][0]):
golden_value = float(pot[(i,) + tensor_index])
faulty_value = float32_bit_flip(golden_value=golden_value, bit=bit_index)
pot[(i,) + tensor_index] = faulty_value
def injection_xor(self, pot, layer):
'''
inject faults using the XOR mask
'''
pot_int = pot.view(torch.int)
pot_int_incjeted = torch.bitwise_xor(pot_int, self.mask[layer])
pot = pot_int_incjeted.view(torch.float)
def injection(self, spike, pot, layer, curr_step):
if self.inject:
if self.current != None:
if self.one == False:
self.injection_xor(pot, layer)
else:
if self.current[0] == layer:
if self.transient == False or (self.transient and curr_step == self.current[4]):
self.injection_dict(pot, spike, layer, self.current[1], self.current[2], self.current[3])
else:
self.size[layer] = pot.shape
self.spike[layer] = spike.shape