-
Notifications
You must be signed in to change notification settings - Fork 0
/
CIFAR100_latency.py
409 lines (263 loc) · 13.2 KB
/
CIFAR100_latency.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from mpi4py import MPI
import numpy as np
import random
from array import array
import math
import time
import sys
import pickle as pickle
#import resnet
from torchvision import transforms
import torch
from torchvision.datasets import FashionMNIST
from torchvision import models
#import tensorflow.keras as K
import resnet
#Berrut Encoder
def encoder(X,N):
[K,H,W]=np.shape(X)
alpha=np.zeros(K)
for j in range(K):
alpha[j]=np.cos(((2*j+1)*np.pi)/(2*K))
all_z=np.zeros(N)
for i in range(N):
all_z[i]=np.cos((i*np.pi)/(N-1))
coded_X=np.zeros([N,H,W])
for n in range(N):
z=all_z[n]
den=0
for j in range(K):
den = den+(np.power(-1, j)) / (z - alpha[j])
for i in range(K):
coded_X[n,]=coded_X[n,]+(((np.power(-1, i)) / (z - alpha[i]))/den)*X[i,]
return coded_X
#Berrut Decoder
def decoder(Y,K,N,returned_points_indices):
F=len(returned_points_indices)
alpha=np.zeros(K)
for j in range(K):
alpha[j]=np.cos(((2*j+1)*np.pi)/(2*K))
z_bar=np.zeros(N)
for i in range(N):
z_bar[i]=np.cos((i*np.pi)/(N-1))
probs=np.zeros([K,num_of_classes])
for digit in range(num_of_classes):
for i in range(K):
z=alpha[i]
den = 0
for j in range(F):
den = den + ((np.power(-1,j))/(z - z_bar[returned_points_indices[j]]))
for l in range(F):
probs[i,digit] = probs[i,digit] + ((((np.power(-1, l)) / (z - z_bar[returned_points_indices[l]]))/den)*Y[returned_points_indices[l],digit])
return probs
def model_out(data):
outputs=np.zeros(num_of_classes)
data=data.reshape([3,W,W])
torch_sample = torch.from_numpy(data).float()
torch_sample = torch_sample.permute( 2, 0, 1)
torch_sample = torch_sample.unsqueeze(0)
outputs= model(torch_sample).detach().numpy()[0]
# _, predicts = torch.max(pred, 1)
# predictions[i]=predicts.numpy()[0]
return outputs
if __name__ == "__main__":
iterations=1
#mode="ParM"
mode="Approxifer"
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
master_node_id=0
client_node_id=1
first_worker_node_id=2
S=1 ## num of stragglers
N=size-first_worker_node_id
K=N-S
parity_model_id = first_worker_node_id + N - 1
background_teraffic_exponent=4
min_packet_size=10**background_teraffic_exponent
max_packet_size=2*10**background_teraffic_exponent
# min_packet_size=1
# max_packet_size=2
## Data set sizes
## MNIST
# H=28
# W=28
# num_of_classes=10
H=3*32
W=32
num_of_classes=100
batch_size=K
sample_batch = np.zeros([batch_size,H,W])
predictions=np.zeros([K,1])
# model=resnet.ResNet18()
# PATH='../base_model_trained_files/fashion-mnist/resnet18/model.t7'
# model.load_state_dict(torch.load(PATH))
#model=models.resnet18(pretrained=True)
model = resnet.ResNet18()
#MNIST
#PATH = '../base_model_trained_files/fashion-mnist/resnet18/model.t7'
#CIFAR-100
PATH= '../base_model_trained_files/cifar100/resnet152/model.t7'
model.load_state_dict(torch.load(PATH))
model.eval()
transform = transforms.Compose([transforms.ToTensor()
])
# if rank==random_packet_sender_id:
# dummy_packet = np.random.random(np.random.randint(min_packet_size, max_packet_size))
# req=comm.Isend(dummy_packet, dest=random_packet_receiver_id)
# req.Wait()
#
# if rank==random_packet_receiver_id:
# dummy_packet = np.random.random(np.random.randint(min_packet_size, max_packet_size))
# req=comm.Irecv(dummy_packet, source=random_packet_receiver_id)
# req.Wait()
wait_times=np.zeros([iterations,1], dtype=float)
### master nodes
for itr in range(iterations):
if rank==master_node_id:
# print("rank is ", rank)
# print("Hello from the master node")
##### Generating background terafic
[random_packet_sender_id, random_packet_receiver_id] = np.random.permutation(np.arange(first_worker_node_id, N + first_worker_node_id))[0:2]
#print(random_packet_sender_id, random_packet_receiver_id)
for i in range(first_worker_node_id, N+first_worker_node_id):
comm.send([random_packet_sender_id, random_packet_receiver_id], dest=i, tag=2*itr) # msg 2
# print("Master sent the ids")
comm.send("Start", dest=client_node_id, tag=(K+1)*itr+K)#msg 1
# print("Master loaded datasets")
data_batch=np.zeros([K,H,W])
labels=np.zeros([K,1])
##########################################################ParM################################################################
if mode=="ParM":
data_batch = comm.recv(source=client_node_id, tag=K * itr ) # msg 3
for i in range(K):
comm.Isend(data_batch[i,], dest=first_worker_node_id+i, tag=2*itr+1) #msg 4
parity=data_batch.sum(axis=0)
comm.Isend(parity, dest=first_worker_node_id + K, tag=2*itr+1) #msg 4
# print("Master sent out data")
receive_objects_array = []
received_data_from_workers = np.zeros([N, num_of_classes,1])
for i in range(first_worker_node_id, N + first_worker_node_id):
receive_objects_array.append(comm.irecv(received_data_from_workers[i - first_worker_node_id,], source=i, tag=itr)) #msg 5
# print("Master is waiting for the results . . .")
#is_received_from_worker = np.array(np.zeros([N, 1]), dtype=bool)
not_received_yet_ids=[i for i in range(first_worker_node_id,first_worker_node_id+N)]
parity_is_back=False
straggler_id=parity_model_id
while len(not_received_yet_ids)>S:
for worker_id in not_received_yet_ids:
if receive_objects_array[worker_id-first_worker_node_id].Test():
if worker_id==parity_model_id:
not_received_yet_ids.remove(worker_id)
parity_is_back=True
else:
labels[worker_id - first_worker_node_id]=np.argmax(received_data_from_workers[worker_id - first_worker_node_id,])
#comm.Isend(label, dest=client_node_id, tag=(K+1)*itr+worker_id-first_worker_node_id)# msg 6
not_received_yet_ids.remove(worker_id)
if parity_is_back:
straggler_id=not_received_yet_ids[0]
returned_pos_except_parity=[i for i in range(N)]
returned_pos_except_parity.remove(straggler_id-first_worker_node_id)
returned_pos_except_parity.remove(parity_model_id-first_worker_node_id)
received_data_from_workers[straggler_id-first_worker_node_id,]=received_data_from_workers[parity_model_id-first_worker_node_id,]-received_data_from_workers[returned_pos_except_parity,].sum(axis=0)
labels[straggler_id - first_worker_node_id] = np.argmax(received_data_from_workers[straggler_id - first_worker_node_id,])
#comm.Isend(label, dest=client_node_id, tag=(K+1)*itr+straggler_id - first_worker_node_id)# msg 6
receive_objects_array[straggler_id - first_worker_node_id].Cancel()
comm.send(labels, dest=client_node_id, tag=(K + 1) * itr)# msg 6
# print("Job is done")
#####################################################Approxifer########################################################################
elif mode=="Approxifer":
data_batch=comm.recv(source=client_node_id, tag=K*itr)# msg 3
## encoding using Berrut
coded_batch=encoder(data_batch,N)
for i in range(N):
comm.Isend(coded_batch[i,], dest=first_worker_node_id+i, tag=2*itr+1) #msg 4
# print("Master sent out data")
receive_objects_array = []
received_data_from_workers = np.zeros([N, num_of_classes,1])
for i in range(first_worker_node_id, N + first_worker_node_id):
receive_objects_array.append(comm.irecv(received_data_from_workers[i - first_worker_node_id,], source=i, tag=itr)) #msg 5
# print("Master is waiting for the results . . .")
#is_received_from_worker = np.array(np.zeros([N, 1]), dtype=bool)
not_received_yet_ids=[i for i in range(first_worker_node_id,first_worker_node_id+N)]
enough_for_decoding=False
while len(not_received_yet_ids)>S:
for worker_id in not_received_yet_ids:
if receive_objects_array[worker_id-first_worker_node_id].Test():
not_received_yet_ids.remove(worker_id)
straggler_id = not_received_yet_ids[0]
received_results_ids=np.concatenate((np.arange(0,straggler_id-first_worker_node_id),np.arange(straggler_id-first_worker_node_id+1,N)))
decoded_result=decoder(received_data_from_workers,K,N,received_results_ids)
labels=np.argmax(decoded_result,axis=1)
receive_objects_array[straggler_id - first_worker_node_id].Cancel()
comm.send(labels, dest=client_node_id,tag=(K + 1) * itr ) # msg 6
####### client
elif rank==client_node_id:
message=comm.recv(source=master_node_id,tag=(K+1)*itr+K)## msg 1
send_times = np.zeros(1)
recv_times = np.zeros(1)
# print("Hi from the client node")
#### sending queries
comm.send(sample_batch,dest=master_node_id, tag=K*itr)# msg 3
send_times=time.time()
# print("queries are sent")
#receive_objects_array = []
#for i in range(K):
receive_times = 0
predictions=comm.recv(source=master_node_id,tag=(K+1)*itr) #msg 6
receive_times = time.time()
# receive_times=np.zeros(K)
# ids_whose_labels_not_received_yet = [i for i in range(K)]
# while len(ids_whose_labels_not_received_yet)>0:
# for id in ids_whose_labels_not_received_yet:
# if receive_objects_array[i].Test():
# ids_whose_labels_not_received_yet.remove(id)
# receive_times[id]=time.time()
# print("labels are received")
#print(receive_times-send_times)
#wait_times.append(receive_times-send_times)
wait_times[itr]= receive_times-send_times
if itr==iterations-1:
mean=wait_times.mean()
median=np.percentile(wait_times,50)
percentile99=np.percentile(wait_times,99)
percentile995=np.percentile(wait_times,99.5)
percentile999=np.percentile(wait_times,99.9)
print(mode)
print("mean is ", mean)
print("median is", median)
print("99 percentile is ", percentile99)
print("99.5 percentile is", percentile995)
print("99.9 percentile is ", percentile999)
stats=[mean, median, percentile995, percentile995, percentile999]
#stats=str(mean), ' ',str(median),' ',str(percentile99), ' ',str(percentile995), ' ',str(percentile999)
np.savetxt('K='+str(K)+'N='+str(N)+'iterations='+str(iterations)+'mode'+str(mode)+'teraficexponent'+str(background_teraffic_exponent)+"BATCH_stats.txt", stats)
np.savetxt('K=' + str(K) + 'N=' + str(N) + 'iterations=' + str(iterations) +'mode'+str(mode)+ 'teraficexponent'+str(background_teraffic_exponent)+"BATCH_wait_times.txt", wait_times)
# preq=comm.Send_init(sample_batch, dest=master_node_id)
# send_times=np.zeros(K)
# preq.Start()
#print((preq.__getattribute__))
# for i in range(K):
# preq.Start()
# send_times[i]=time.time()
##### worker nodes
elif rank<size:
# print("rank is ", rank)
##emulating the background teraffic
ids=np.zeros(2,dtype=int)
ids=comm.recv(source=0, tag=2*itr) #msg 2
random_packet_sender_id=ids[0]
random_packet_receiver_id=ids[1]
if rank==random_packet_sender_id:
dummy_packet = np.random.random(np.random.randint(min_packet_size, max_packet_size))
comm.send(dummy_packet, dest=random_packet_receiver_id, tag=itr)#msg 7
if rank==random_packet_receiver_id:
comm.recv(source=random_packet_sender_id, tag=itr)# msg 7
sample=np.zeros([H,W])
req=comm.irecv(sample, source=0, tag=2*itr+1)# msg 4
req.Wait()
# print("Process", rank, "received the data")
soft_labels=model_out(sample)
comm.Isend(soft_labels, dest=0, tag=itr) # msg 5
# print("Process", rank, "sent the result")