-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_sampling.py
More file actions
182 lines (159 loc) · 7.75 KB
/
client_sampling.py
File metadata and controls
182 lines (159 loc) · 7.75 KB
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
'''
Sunwoo Lee, Ph.D.
<sunwool@inha.ac.kr>
2023.03.05
'''
from mpi4py import MPI
import numpy as np
import math
import time
import tensorflow as tf
class sampling:
def __init__ (self, num_clients, num_workers, num_candidates):
self.comm = MPI.COMM_WORLD
self.rank = self.comm.Get_rank()
self.size = self.comm.Get_size()
self.num_workers = num_workers
self.num_clients = num_clients
self.num_candidates = num_candidates
self.num_local_workers = int(num_workers / self.size)
self.num_local_clients = int(num_clients / self.size)
self.local_losses = np.full((self.num_clients), np.Inf)
self.fixed_losses = np.full((self.num_clients), np.Inf)
self.local_norms = np.zeros((self.num_clients))
self.avg_norms = np.zeros((self.num_clients))
self.num_updates = np.zeros((self.num_clients))
self.active_devices = np.zeros((self.num_clients))
self.rng = np.random.default_rng(int(time.time()))
np.random.seed(int(time.time()))
def random (self):
self.active_devices = np.random.choice(np.arange(self.num_clients), size = self.num_workers, replace = False)
self.active_devices = self.comm.bcast(self.active_devices, root = 0)
return self.active_devices
def power_of_choice (self, epoch_id):
# 1. Sample M clients randomly without replacements.
candidates = self.rng.choice(np.arange(self.num_clients), size = self.num_candidates, replace = False)
self.active_devices = candidates[np.argsort(self.local_losses[candidates])[-self.num_workers:]]
self.active_devices = self.comm.bcast(self.active_devices, root = 0)
return self.active_devices
def power_of_choice2 (self, epoch_id):
# 1. Sample M clients randomly without replacements.
border = (self.num_clients - self.num_workers) // 4
candidates = self.rng.choice(np.arange(self.num_clients), size = self.num_workers + border*3, replace = False)
lossprob = np.sort(candidates[np.argsort(self.local_losses[candidates])[-(self.num_workers + border*2):]])
candidates = self.rng.choice(lossprob, size = self.num_workers + border, replace = False)
normprob = candidates[np.argsort(self.local_norms[candidates])[:self.num_workers]]
self.active_devices = self.comm.bcast(normprob, root = 0)
if self.rank == 0:
f = open("l.txt", "a")
f.write("epoch %3d: " %(epoch_id))
for i in range (len(lossprob)):
f.write("%3d " %(lossprob[i]))
f.write("\n")
f.close()
f = open("n.txt", "a")
f.write("epoch %3d: " %(epoch_id))
for i in range (len(normprob)):
f.write("%3d " %(normprob[i]))
f.write("\n")
f.close()
f = open("w.txt", "w")
for i in range (self.num_clients):
f.write("client %3d: loss: %10.7f norm: %10.7f\n" %(i, self.local_losses[i], self.local_norms[i]))
f.close()
return self.active_devices
def power_of_choice3 (self, epoch_id):
border = 0
candidates = self.rng.choice(np.arange(self.num_clients), size = self.num_candidates, replace = False)
lossprob = np.sort(candidates[np.argsort(self.local_losses[candidates])[-(self.num_workers + border):]])
normprob = np.sort(candidates[np.argsort(self.local_norms[candidates])[:(self.num_workers + border)]])
common = np.sort(np.intersect1d(lossprob, normprob))
if len(common) > self.num_workers:
#common = np.sort(common[np.argsort(self.local_losses[common])[-self.num_workers:]])
common = self.rng.choice(common, size = self.num_workers, replace = False)
num_extra = self.num_workers - len(common)
if num_extra > 0:
candidates = lossprob[~np.isin(lossprob, common)]
#extra = np.sort(candidates[np.argsort(self.local_losses[candidates])[-num_extra:]])
extra = self.rng.choice(candidates, size = num_extra, replace = False)
clients = np.sort(np.concatenate((common, extra), axis=0))
else:
clients = common
self.active_devices = self.comm.bcast(clients, root = 0)
if self.rank == 0:
f = open("c.txt", "a")
f.write("epoch %3d (%3d): " %(epoch_id, len(common)))
for i in range (len(common)):
f.write("%3d(%6.4f) " %(common[i], self.local_losses[common[i]]))
f.write("\n")
f.close()
if num_extra > 0:
f = open("e.txt", "a")
f.write("epoch %3d (%3d): " %(epoch_id, len(extra)))
for i in range (len(extra)):
f.write("%3d(%6.4f) " %(extra[i], self.local_losses[extra[i]]))
f.write("\n")
f.close()
f = open("l.txt", "a")
f.write("epoch %3d: " %(epoch_id))
for i in range (len(lossprob)):
f.write("%3d " %(lossprob[i]))
f.write("\n")
f.close()
f = open("n.txt", "a")
f.write("epoch %3d: " %(epoch_id))
for i in range (len(normprob)):
f.write("%3d " %(normprob[i]))
f.write("\n")
f.close()
f = open("w.txt", "w")
for i in range (self.num_clients):
f.write("client %3d: loss: %10.7f norm: %10.7f\n" %(i, self.local_losses[i], self.local_norms[i]))
f.close()
return self.active_devices
def leadership (self, epoch_id):
r = 256
weights = np.ones((self.num_clients))
b = 4
lossprob = np.sort(np.argsort(self.local_losses)[-(self.num_workers + 2*b):])
normprob = np.sort(lossprob[np.argsort(self.local_norms[lossprob])[:(self.num_workers + b)]])
weights[lossprob] *= r
weights[normprob] *= r
weights /= sum(weights)
self.active_devices = self.rng.choice(np.arange(self.num_clients), size = self.num_workers, replace = False, p = weights)
if self.rank == 0:
f = open("l.txt", "a")
f.write("epoch %3d: " %(epoch_id))
for i in range (len(lossprob)):
f.write("%3d " %(lossprob[i]))
f.write("\n")
f.close()
f = open("n.txt", "a")
f.write("epoch %3d: " %(epoch_id))
for i in range (len(normprob)):
f.write("%3d " %(normprob[i]))
f.write("\n")
f.close()
f = open("w.txt", "w")
for i in range (self.num_clients):
f.write("client %3d: loss: %10.7f norm: %10.7f weight: %13.10f\n" %(i, self.local_losses[i], self.local_norms[i], weights[i]))
f.close()
self.active_devices = self.comm.bcast(self.active_devices, root = 0)
return self.active_devices
def update_loss (self, aggregated_losses):
for i in range (len(aggregated_losses)):
client_id = self.active_devices[i]
self.local_losses[client_id] = aggregated_losses[i]
def update_norm (self, aggregated_norms):
for i in range (len(aggregated_norms)):
client_id = self.active_devices[i]
self.local_norms[client_id] = aggregated_norms[i]
accum = self.avg_norms[client_id] * self.num_updates[client_id] + self.local_norms[client_id]
self.num_updates[client_id] += 1
self.avg_norms[client_id] = accum / self.num_updates[client_id]
def reset (self):
self.local_losses = np.full((self.num_clients), np.Inf)
self.fixed_losses = np.full((self.num_clients), np.Inf)
self.local_norms = np.zeros((self.num_clients))
self.avg_norms = np.zeros((self.num_clients))
self.num_updates = np.zeros((self.num_clients))