-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommunicator_nodegree.py
756 lines (582 loc) · 25.8 KB
/
communicator_nodegree.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import copy
import numpy as np
import time
import torch
from mpi4py import MPI
from compressors import get_top_k
import torch.distributed as dist
from comm_helpers import flatten_tensors, unflatten_tensors
class Communicator(object):
""" Classs designed for communicating local models at workers """
def __init__(self, rank, size):
# mpi4py function
self.comm = MPI.COMM_WORLD
#self.comm = torch.distributed
self.rank = rank
self.size = size
self.iter = 0
def communicate(self, model):
self.iter += 1
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# necessary preprocessing
self.prepare_comm_buffer()
# communication happens here
# record the communication time
comm_time = self.averaging()
# Update local models
new_model = self.reset_model(model)
return comm_time, new_model
def reset_model(self, model):
# Reset local models to be the averaged model
# (CPU Version: CUDA function not allowed in cpu)
self.updatedvec = unflatten_tensors(self.recv_buffer.cuda(), self.tensor_list)
for f, t in zip(self.updatedvec, self.tensor_list):
#t.set_(f)
t=f
# test model update or not
tmp_model1 = copy.deepcopy(model)
pointer = 0
for param in model.parameters():
param.data = self.updatedvec[pointer].view(param.size())
pointer += 1
# test model update or not
tmp_model2 = copy.deepcopy(model)
modelCheck = True
for p1, p2 in zip(tmp_model1.parameters(), tmp_model2.parameters()):
if p1.data.ne(p2.data).sum() > 0:
modelCheck = False
if modelCheck == True:
print('same, model not change')
else:
print('different, model updated')
return model
def prepare_comm_buffer(self):
raise NotImplemented
def averaging(self):
raise NotImplemented
class PScommunicator(Communicator):
def __init__(self, rank, size):
super(PScommunicator, self).__init__(rank, size)
def prepare_comm_buffer(self):
self.send_buffer = flatten_tensors(self.tensor_list)
self.recv_tmp = torch.zeros_like(self.tensor_list)
self.recv_buffer = torch.zeros_like(self.tensor_list)
def averaging(self):
self.comm.barrier()
tic = time.time()
# send parameters - Method 1: using send and receive to collect parameters
# mpi4py code
#send_Seq = self.comm.Send(self.send_buffer, dest = 0)
# Send Parameters - Method2: using All gather and Broadcast to collect parameters
self.comm.barrier()
toc = time.time()
return toc - tic
def averagingPS(self):
self.comm.barrier()
tic = time.time()
"""
# receive parameters - Method 1: using send and receive to collect parameters
for i in range(1, self.size - 1):
# receive the send_buff_list[leftNode]
# (torch.distributed code)
# self.comm.recv(self.recv_tmp, src=i)
# mpi4py code
self.comm.Recv(self.recv_tmp, source=i)
# buffer sum up
self.recv_buffer += self.recv_tmp
"""
# receive parameters - Method 2: using All gather and Broadcast to collect parameters
self.comm.allreduce(self.send_buffer, op=MPI.SUM)
self.recv_buffer = self.send_buffer
self.recv_buffer /= float(self.size)
# Braodcast signal
# mpi4py code
# self.comm.broadcast(tensor=self.recv_buffer, src=0)
# torch.distributed code
# mpi4py code
self.comm.Bsend(self.recv_buffer, src = 0)
self.comm.barrier()
toc = time.time()
return toc - tic
def communicate(self, model):
self.iter += 1
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# necessary preprocessing
self.prepare_comm_buffer()
# communication happens here
# record the communication time
comm_time = self.averaging()
# Update local models
new_model = self.reset_model(model)
return comm_time, new_model
def communicatePS(self, model):
self.iter += 1
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# necessary preprocessing
self.prepare_comm_buffer()
# communication happens here
# record the communication time
comm_time = self.averagingPS()
# reset local models
self.recv_buffer = torch.zeros_like(self.tensor_list)
return comm_time, self.updatedvec
class ringCommunicator(Communicator):
def __init__(self, rank, size):
super(ringCommunicator, self).__init__(rank, size)
def prepare_comm_buffer(self):
self.send_buffer = flatten_tensors(self.tensor_list)
self.send_buffer_list = self.send_buffer.chunk(self.size)
self.recv_tmp = torch.zeros_like(self.send_buffer_list[0])
self.recv_buffer = self.send_buffer
def averaging(self):
"""
info:
self.rank: from 0 to 7
self.size: 8
verify:
# GPU 0
# left = (0 - 1 + 8) % 8 = 7
# right = (0 + 1) % 8 = 1
# iteration i = 0:
# chunk_num = (8 - 0 + 0 - 1) % 8 = 7
# left_chunk_num = (8 - 7 + 0 - 1) % 8 = 0
# iteration i = 1:
# chunk_num = (8 - 0 + 0 + 1 - 1) % 8 = 0
# left_chunk_num = (8 - 7 + 1 - 1) % 8 = 1
verified
:return communication time
"""
self.comm.barrier()
tic = time.time()
left = ((self.rank - 1) + self.size) % self.size
right = (self.rank + 1) % self.size
# convert into list for add operation
self.send_buffer_list = list(self.send_buffer_list)
for i in range(self.size - 1):
# get the chunk number of current nodes/ left nodes for each iteration
chunk_num = (self.size - self.rank + i - 1) % self.size
left_chunk_num = (self.size - left + i - 1) % self.size
print("This is rank", self.rank, "The world size is", self.size)
print("The chunk number going to send:", chunk_num, "The chunk number going to receive:", left_chunk_num)
print("The total number of chunk is:", len(self.send_buffer_list))
# communicate using sendrecv
self.recv_tmp = self.comm.sendrecv(self.send_buffer_list[chunk_num], source=left, dest=right)
self.send_buffer_list[left_chunk_num] += self.recv_tmp
# communicate using send and recv
"""
if (self.rank % 2 == 0):
self.recv_tmp = self.comm.sendrecv(self.send_buffer_list[chunk_num], source=left, dest=right, sendtag=0, recvtag=1)
else:
self.recv_tmp = self.comm.recv(source= left, tag=0)
send_seq = self.comm.send(self.send_buffer_list[chunk_num], dest = right, tag=1)
"""
# ring allreduce sum up
self.send_buffer_list[left_chunk_num] += self.recv_tmp
# convert the list into tuple for
self.send_buffer_list = tuple(self.send_buffer_list)
# dechunk to flatten tensor
self.recv_buffer = torch.cat(self.send_buffer_list[:], 0)
self.recv_buffer /= float(self.size)
self.comm.barrier()
toc = time.time()
return toc - tic
class centralizedCommunicator(Communicator):
""" Perform AllReduce at each iteration """
def __init__(self, rank, size):
super(centralizedCommunicator, self).__init__(rank, size)
def prepare_comm_buffer(self):
# faltten tensors
self.send_buffer = flatten_tensors(self.tensor_list).cpu()
self.recv_buffer = torch.zeros_like(self.send_buffer)
def averaging(self):
self.comm.barrier()
tic = time.time()
# AllReduce
# (mpi4py function)
self.recv_buffer = self.comm.allreduce(self.send_buffer, op=MPI.SUM)
# (torch.distributed function)
#self.recv_buffer = self.comm.all_reduce(self.send_buffer, op=ReduceOp.SUM)
# (mpi4py function)
self.recv_buffer.div_(self.size)
#self.recv_buffer /= float(self.size)
self.comm.barrier()
toc = time.time()
return toc - tic
class decenCommunicator(Communicator):
""" decentralized averaging according to a topology sequence """
def __init__(self, rank, size, topology):
super(decenCommunicator, self).__init__(rank, size)
self.topology = topology
self.neighbor_weight = topology.neighbor_weight
self.iter = 0
def prepare_comm_buffer(self):
# faltten tensors
# (.cpu() is used in GPU devices)
self.send_buffer = flatten_tensors(self.tensor_list).cpu()
#self.send_buffer = flatten_tensors(self.tensor_list)
self.recv_tmp = torch.zeros_like(self.send_buffer)
self.recv_buffer = torch.zeros_like(self.send_buffer)
self.updatedvec = self.tensor_list
def averaging(self, active_flags):
self.comm.barrier()
tic = time.time()
# decentralized averaging
degree = 0 # record the degree of each node
for graph_id, flag in enumerate(active_flags):
if flag == 0:
continue
else:
if self.topology.neighbors_info[graph_id][self.rank] != -1:
degree += 1
neighbor_rank = self.topology.neighbors_info[graph_id][self.rank]
print("The rank is:", self.rank, "The neighbor rank is:", neighbor_rank)
print("Information going to send:", self.send_buffer)
# Receive neighbor's model: x_j
# (mpi4py code)
#print("start send and receive step")
self.recv_tmp = self.comm.sendrecv(self.send_buffer, source=neighbor_rank, dest=neighbor_rank)
# (torch.distributed code)
# send_seq = self.comm.isend(self.send_buffer, dst=neighbor_rank)
#print("finish send and receive step")
#recv_seq = self.comm.recv(self.recv_tmp, src=neighbor_rank)
#print("finish receive step")
#send_seq.wait()
#print("finish send wait")
#recv_seq.wait()
#print("finish receive wait")
# Aggregate neighbors' models: alpha * sum_j x_j
self.recv_buffer.add_(self.neighbor_weight, self.recv_tmp)
#self.recv_buffer += self.recv_tmp * self.neighbor_weight
#print("start average step")
# compute self weight according to degree
selfweight = 1 - degree * self.neighbor_weight
# compute weighted average: (1-d*alpha)x_i + alpha * sum_j x_j
self.recv_buffer.add_(selfweight, self.send_buffer)
self.comm.barrier()
toc = time.time()
#print("finish average step")
return toc - tic
def communicate(self, model):
# get activated topology at current iteration
active_flags = self.topology.active_flags[self.iter]
self.iter += 1
# if no subgraphs are activated,
# then directly start next iteration
if np.sum(active_flags) == 0:
return 0
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# necessary preprocess
self.prepare_comm_buffer()
# decentralized averaging according to activated topology
# record the communication time
comm_time = self.averaging(active_flags)
# update local models
new_model = self.reset_model(model)
return comm_time, new_model
class ChocoCommunicator(Communicator):
""" decentralized averaging using compressed gradients (top-k)
SOTA Paper 2019: Decentralized Stochastic Optimization and Gossip Algorithms with Compressed Communication
"""
def __init__(self, rank, size, topology, ratio, consensus_lr):
super(ChocoCommunicator, self).__init__(rank, size)
self.topology = topology
self.neighbor_weight = topology.neighbor_weight
self.iter = 0
self.initialized = False
self.consensus_lr = consensus_lr
self.ratio = ratio
def prepare_comm_buffer(self):
# flatten tensors
# If not initialized, then initialize x_hat and s
self.x = flatten_tensors(self.tensor_list).cpu()
if not self.initialized:
self.x_hat = torch.zeros_like(self.x)
self.s = torch.zeros_like(self.x)
self.initialized = True
tic = time.time()
# get compressed message
# here, we use top_k compressor on GPU
# one can define more in compressors.py
self.send_buffer = self.x - self.x_hat
values, indices = get_top_k(self.send_buffer.cuda(), self.ratio)
toc = time.time()
values, indices = values.cpu(), indices.cpu()
self.compressed = {"values": values, "indices": indices}
return toc - tic
def averaging(self, active_flags):
self.comm.barrier()
tic = time.time()
# decentralized averaging according to activated topology
degree = 0
for graph_id, flag in enumerate(active_flags):
if flag == 0:
continue
else:
if self.topology.neighbors_info[graph_id][self.rank] != -1:
degree += 1
neighbor_rank = self.topology.neighbors_info[graph_id][self.rank]
# Receive neighbor's message q_j
# (mpi4py code)
self.recv_tmp = self.comm.sendrecv(self.compressed, source=neighbor_rank, dest=neighbor_rank)
#send_seq = self.comm.isend(self.compressed, dst=neighbor_rank)
#send_seq.wait()
#recv_seq = self.comm.recv(self.recv_tmp, src=neighbor_rank)
# Update aggregated model s += sum w_ij q_j
self.s[self.recv_tmp["indices"]] += self.neighbor_weight * self.recv_tmp["values"]
# Compute self weight
selfweight = 1 - degree * self.neighbor_weight
# Update aggregated model s += w_ii q_i
self.s[self.compressed["indices"]] += selfweight * self.compressed["values"]
# Update x_hat = x_hat + q_i
self.x_hat[self.compressed["indices"]] += self.compressed["values"]
# Update local model parameters: x = x + consensus_lr*(s-x_hat)
self.x.add_(self.consensus_lr, self.s).sub_(self.consensus_lr, self.x_hat)
self.comm.barrier()
toc = time.time()
return toc - tic
def communicate(self, model):
# get activated topology at current iteration
active_flags = self.topology.active_flags[self.iter]
self.iter += 1
# if no subgraphs are activated,
# then directly start next iteration
if np.sum(active_flags) == 0:
return 0
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# necessary preprocess
# there is an additional encoding time
encode_time = self.prepare_comm_buffer()
# decentralized averaging
# record the communication time
comm_time = self.averaging(active_flags)
total_commtime = encode_time + comm_time
# update local models
new_model = self.reset_model(model)
return total_commtime, new_model
def reset_model(self, model):
# Reset local models to be the averaged model
# (CPU Version: CUDA function not allowed in cpu)
self.updatedvec = unflatten_tensors(self.x.cuda(), self.tensor_list)
for f, t in zip(self.updatedvec, self.tensor_list):
#t.set_(f)
t=f
# test model update or not
tmp_model1 = copy.deepcopy(model)
pointer = 0
for param in model.parameters():
param.data = self.updatedvec[pointer].view(param.size())
pointer += 1
# test model update or not
tmp_model2 = copy.deepcopy(model)
modelCheck = True
for p1, p2 in zip(tmp_model1.parameters(), tmp_model2.parameters()):
if p1.data.ne(p2.data).sum() > 0:
modelCheck = False
if modelCheck == True:
print('same, model not change')
else:
print('different, model updated')
return model
class LLDSGDCommunicator(Communicator):
""" decentralized averaging according to a topology sequence """
def __init__(self, rank, size, topology):
super(LLDSGDCommunicator, self).__init__(rank, size)
self.topology = topology
self.neighbor_weight = topology.neighbor_weight
self.iter = 0
def prepare_comm_buffer(self):
# faltten tensors
# (.cpu() is used in GPU devices)
self.send_buffer = flatten_tensors(self.tensor_list).cpu()
#self.send_buffer = flatten_tensors(self.tensor_list)
self.recv_tmp = torch.zeros_like(self.send_buffer)
self.recv_buffer = torch.zeros_like(self.recv_tmp)
self.updatedvec = self.tensor_list
def averaging(self, active_flags):
self.comm.barrier()
tic = time.time()
# decentralized averaging
degree = 0 # record the degree of each node
for graph_id, flag in enumerate(active_flags):
if flag == 0:
continue
else:
if self.topology.neighbors_info[graph_id][self.rank] != -1:
degree += 1
neighbor_rank = self.topology.neighbors_info[graph_id][self.rank]
print("The rank is:", self.rank, "The neighbor rank is:", neighbor_rank)
print("Information going to send:", self.send_buffer)
# Receive neighbor's model: x_j
# (mpi4py code)
#print("start send and receive step")
self.recv_tmp = self.comm.sendrecv(self.send_buffer, source=neighbor_rank, dest=neighbor_rank)
# (torch.distributed code)
# send_seq = self.comm.isend(self.send_buffer, dst=neighbor_rank)
#print("finish send and receive step")
#recv_seq = self.comm.recv(self.recv_tmp, src=neighbor_rank)
#print("finish receive step")
#send_seq.wait()
#print("finish send wait")
#recv_seq.wait()
#print("finish receive wait")
# Aggregate neighbors' models: alpha * sum_j x_j
self.recv_buffer.add_(self.neighbor_weight, self.recv_tmp)
# self.recv_buffer += self.recv_tmp * self.neighbor_weight
#print("start average step")
# compute self weight according to degree
selfweight = 1 - degree * self.neighbor_weight
# compute weighted average: (1-d*alpha)x_i + alpha * sum_j x_j
self.recv_buffer.add_(selfweight, self.send_buffer)
self.comm.barrier()
toc = time.time()
#print("finish average step")
return toc - tic
def communicate(self, model):
# get activated topology at current iteration
active_flags = self.topology.active_flags[self.iter]
self.iter += 1
# if no subgraphs are activated,
# then directly start next iteration
if np.sum(active_flags) == 0:
return 0
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# necessary preprocess
self.prepare_comm_buffer()
# decentralized averaging according to activated topology
# record the communication time
comm_time = self.averaging(active_flags)
# update local models
new_model = self.reset_model(model)
return comm_time, new_model
def LLDSGDaveraging(self, active_flags, loss):
# store the loss and degree (LLDSGD)
loss_list = [loss.tolist()]
degree_List = list()
self.comm.barrier()
tic = time.time()
# decentralized averaging
degree = 0 # record the degree of each node
for graph_id, flag in enumerate(active_flags):
if flag == 0:
continue
else:
if self.topology.neighbors_info[graph_id][self.rank] != -1:
degree += 1
# store the model and degree
self.tensor_list[-1] = torch.tensor([degree]).cuda()
degree_List.append(degree)
self.send_buffer = flatten_tensors(self.tensor_list).cpu()
models = [self.send_buffer.tolist()]
for graph_id, flag in enumerate(active_flags):
if flag == 0:
continue
else:
if self.topology.neighbors_info[graph_id][self.rank] != -1:
neighbor_rank = self.topology.neighbors_info[graph_id][self.rank]
print("The rank is:", self.rank, "The neighbor rank is:", neighbor_rank)
print("Information going to send:", self.send_buffer)
# Receive neighbor's model: x_j
# (mpi4py code)
#print("start send and receive step")
self.recv_tmp = self.comm.sendrecv(self.send_buffer, source=neighbor_rank, dest=neighbor_rank)
# store the neighbor model
models.append(self.recv_tmp.tolist())
loss_list = loss_list + [self.recv_tmp[-2].tolist()]
degree_List = degree_List + [self.recv_tmp[-1].tolist()]
# Aggregate neighbors' models: alpha * sum_j x_j
self.recv_buffer.add_(self.neighbor_weight, self.recv_tmp)
#self.recv_buffer += self.recv_tmp * self.neighbor_weight
print("start average step")
# compute self weight according to degree
selfweight = 1 - degree * self.neighbor_weight
# compute weighted average: (1-d*alpha)x_i + alpha * sum_j x_j
self.recv_buffer.add_(selfweight, self.send_buffer)
# LLDSGD pull force to local best neighbor. - 0.1 the pull force parameter
print("lenth of lost is :", len(loss_list))
if (len(loss_list) > 1):
# get min_loss and max_degree
min_loss = min(loss_list)
max_degree = max(degree_List)
# index for min_loss and max_degree
min_index = loss_list.index(min_loss)
maxdegree_index = degree_List.index(max_degree)
# get model with max degree and min loss
best_local_model = models[min_index]
max_degree_model = models[maxdegree_index]
best_local_model = torch.FloatTensor(best_local_model)
max_degree_model = torch.FloatTensor(max_degree_model)
self.recv_buffer = torch.mul(self.recv_buffer, 0.7)
self.recv_buffer += torch.mul(best_local_model, 0.3)
# self.recv_buffer += torch.mul(max_degree_model, 0.3)
self.comm.barrier()
toc = time.time()
#print("finish average step")
return toc - tic
def LLDSGDcommunicate(self, model, loss):
# get activated topology at current iteration
active_flags = self.topology.active_flags[self.iter]
self.iter += 1
# if no subgraphs are activated,
# then directly start next iteration
if np.sum(active_flags) == 0:
return 0
# stack all model parameters into one tensor list
self.tensor_list = list()
for param in model.parameters():
self.tensor_list.append(param.data)
# append loss.item() to the end of buffer
self.tensor_list.append(loss)
# place reserved for degree
self.tensor_list.append(torch.tensor([1.0]).cuda())
# necessary preprocess
self.prepare_comm_buffer()
# loss not included in model
self.updatedvec = self.tensor_list[:-2]
# decentralized averaging according to activated topology
# record the communication time
comm_time = self.LLDSGDaveraging(active_flags, loss)
# update local models
new_model = self.LLSGD_reset_model(model)
return comm_time, new_model
def LLSGD_reset_model(self, model):
# Reset local models to be the averaged model
# (CPU Version: CUDA function not allowed in cpu)
self.updatedvec = unflatten_tensors(self.recv_buffer[:-2].cuda(), self.tensor_list[:-2])
for f, t in zip(self.updatedvec, self.tensor_list[:-2]):
#t.set_(f)
t=f
# test model update or not
tmp_model1 = copy.deepcopy(model)
pointer = 0
for param in model.parameters():
param.data = self.updatedvec[pointer].view(param.size())
pointer += 1
# test model update or not
tmp_model2 = copy.deepcopy(model)
modelCheck = True
for p1, p2 in zip(tmp_model1.parameters(), tmp_model2.parameters()):
if p1.data.ne(p2.data).sum() > 0:
modelCheck = False
if modelCheck == True:
print('same, model not change')
else:
print('different, model updated')
return model