This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
382 lines (331 loc) · 10.3 KB
/
main.go
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
package main
import (
"io"
"log"
"net"
"os"
"os/exec"
"strconv"
"sync"
"time"
pb "federated-learning/fl-server/genproto/fl_round"
"google.golang.org/grpc"
)
var start time.Time
// constants
const (
port = ":50051"
modelFilePath = "./data/model/model.h5"
checkpointFilePath = "./data/checkpoint/fl_checkpoint"
weightUpdatesDir = "./data/weight_updates/"
chunkSize = 64 * 1024
postCheckinReconnectionTime = 8000
postUpdateReconnectionTime = 8000
estimatedRoundTime = 8000
estimatedWaitingTime = 20000
checkinLimit = 3
updateLimit = 3
VAR_NUM_CHECKINS = 0
VAR_NUM_UPDATES_START = 1
VAR_NUM_UPDATES_FINISH = 2
)
// store the result from a client
type flRoundClientResult struct {
checkpointWeight int64
checkpointFilePath string
}
// to handle read writes
// Credit: Mark McGranaghan
// Source: https://gobyexample.com/stateful-goroutines
type readOp struct {
varType int
response chan int
}
type writeOp struct {
varType int
val int
response chan bool
}
// server struct to implement gRPC Round service interface
type server struct {
reads chan readOp
writes chan writeOp
selected chan bool
numCheckIns int
numUpdatesStart int
numUpdatesFinish int
mu sync.Mutex
checkpointUpdates map[int]flRoundClientResult
}
func init() {
start = time.Now()
}
func main() {
// listen
lis, err := net.Listen("tcp", port)
check(err, "Failed to listen on port"+port)
srv := grpc.NewServer()
// server impl instance
flServer := &server{
numCheckIns: 0,
numUpdatesStart: 0,
numUpdatesFinish: 0,
checkpointUpdates: make(map[int]flRoundClientResult),
reads: make(chan readOp),
writes: make(chan writeOp),
selected: make(chan bool)}
// register FL round server
pb.RegisterFlRoundServer(srv, flServer)
// go flServer.EventLoop()
go flServer.ConnectionHandler()
// start serving
err = srv.Serve(lis)
check(err, "Failed to serve on port "+port)
}
// Check In rpc
// Client check in with FL server
// Server responds with FL checkpoint
func (s *server) CheckIn(stream pb.FlRound_CheckInServer) error {
var (
buf []byte
n int
file *os.File
)
// receive check-in request
checkinReq, err := stream.Recv()
log.Println("CheckIn Request: Client Name: ", checkinReq.Message, "Time:", time.Since(start))
// create a write operation
write := writeOp{
varType: VAR_NUM_CHECKINS,
response: make(chan bool)}
// send to handler(ConnectionHandler) via writes channel
s.writes <- write
// wait for response
if !(<-write.response) {
log.Println("CheckIn rejected")
err := stream.Send(&pb.FlData{
IntVal: postCheckinReconnectionTime,
Type: pb.Type_FL_RECONN_TIME,
})
check(err, "Unable to send post checkin reconnection time")
return nil
}
// wait for selection
if !(<-s.selected) {
log.Println("Not selected")
err := stream.Send(&pb.FlData{
IntVal: postCheckinReconnectionTime,
Type: pb.Type_FL_RECONN_TIME,
})
check(err, "Unable to send post checkin reconnection time")
return nil
}
// open file
file, err = os.Open(checkpointFilePath)
check(err, "Unable to open checkpoint file")
defer file.Close()
// make a buffer of a defined chunk size
buf = make([]byte, chunkSize)
for {
// read the content (by using chunks)
n, err = file.Read(buf)
if err == io.EOF {
return nil
}
check(err, "Unable to read checkpoint file")
// send the FL checkpoint Data (file chunk + type: FL checkpoint)
err = stream.Send(&pb.FlData{
Message: &pb.Chunk{
Content: buf[:n],
},
Type: pb.Type_FL_CHECKPOINT,
})
check(err, "Unable to send checkpoint data")
}
}
// Update rpc
// Accumulate FL checkpoint update sent by client
// TODO: delete file when error and after round completes
func (s *server) Update(stream pb.FlRound_UpdateServer) error {
var checkpointWeight int64
log.Println("Update Request: Time:", time.Since(start))
// create a write operation
write := writeOp{
varType: VAR_NUM_UPDATES_START,
response: make(chan bool)}
// send to handler (ConnectionHandler) via writes channel
s.writes <- write
// IMPORTANT
// after sending write request, we wait for response as handler is sending true and gets blocked
// if this read is not present -> handler and all subsequent rpc calls are in deadlock
if !(<-write.response) {
log.Println("Update Request Accepted: Time:", time.Since(start))
}
// create read operation
read := readOp{
varType: VAR_NUM_UPDATES_START,
response: make(chan int)}
// send to handler (ConnectionHandler) via reads channel
// log.Println("Read Request: Time:", time.Since(start))
s.reads <- read
// log.Println("Waiting Read Response: Time:", time.Since(start))
// get index to differentiate clients
index := <-read.response
log.Println("Index : ", index)
// open the file
// log.Println(weightUpdatesDir + strconv.Itoa(index))
filePath := weightUpdatesDir + "weight_updates_" + strconv.Itoa(index)
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, os.ModeAppend)
check(err, "Unable to open new checkpoint file")
defer file.Close()
for {
// receive Fl data
flData, err := stream.Recv()
// exit after data transfer completes
if err == io.EOF {
// create a write operation
write = writeOp{
varType: VAR_NUM_UPDATES_FINISH,
response: make(chan bool)}
// send to handler (ConnectionHandler) via writes channel
s.writes <- write
// put result in map
// TODO: modify by making a go-routine to do updates
s.mu.Lock()
s.checkpointUpdates[index] = flRoundClientResult{
checkpointWeight: checkpointWeight,
checkpointFilePath: filePath,
}
log.Println("Checkpoint Update: ", s.checkpointUpdates[index])
s.mu.Unlock()
if !(<-write.response) {
log.Println("Checkpoint Update confirmed. Time:", time.Since(start))
}
return stream.SendAndClose(&pb.FlData{
IntVal: postUpdateReconnectionTime,
Type: pb.Type_FL_RECONN_TIME,
})
}
check(err, "Unable to receive update data from client")
if flData.Type == pb.Type_FL_CHECKPOINT_UPDATE {
// write data to file
_, err = file.Write(flData.Message.Content)
check(err, "Unable to write into new checkpoint file")
} else if flData.Type == pb.Type_FL_CHECKPOINT_WEIGHT {
checkpointWeight = flData.IntVal
}
}
}
// Runs federated averaging
func (s *server) FederatedAveraging() {
// determine arguments
// args := ""
// for _, v := range s.checkpointUpdates {
// args += strconv.FormatInt(v.checkpointWeight, 10) + " " + v.checkpointFilePath + " "
// }
//argsList := []string{"federated_averaging.py", "--cf", checkpointFilePath, "--mf", modelFilePath, "--u", "281", "./device1/weight_updates", "281", "./device2/weight_updates", "296", "./device3/weight_updates"}
var argsList []string
argsList = append(argsList, "federated_averaging.py", "--cf", checkpointFilePath, "--mf", modelFilePath, "--u")
for _, v := range s.checkpointUpdates {
argsList = append(argsList, strconv.FormatInt(v.checkpointWeight, 10), v.checkpointFilePath)
}
log.Println("Arguments passed to federated averaging python file: ", argsList)
// if args == "" {
// return
// }
// model path
cmd := exec.Command("python3", argsList...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
check(err, "Unable to run federated averaging")
}
// Handler for connection reads and updates
// Takes care of update and checkin limits
// Credit: Mark McGranaghan
// Source: https://gobyexample.com/stateful-goroutines
func (s *server) ConnectionHandler() {
for {
select {
// read query
case read := <-s.reads:
log.Println("Handler ==> Read Query:", read.varType, "Time:", time.Since(start))
switch read.varType {
case VAR_NUM_CHECKINS:
read.response <- s.numCheckIns
case VAR_NUM_UPDATES_START:
read.response <- s.numUpdatesStart
case VAR_NUM_UPDATES_FINISH:
read.response <- s.numUpdatesFinish
}
// write query
case write := <-s.writes:
log.Println("Handler ==> Write Query:", write.varType, "Time:", time.Since(start))
switch write.varType {
case VAR_NUM_CHECKINS:
s.numCheckIns++
log.Println("Handler ==> numCheckIns", s.numCheckIns, "Time:", time.Since(start))
// if number of checkins exceed the limit, reject this one
if s.numCheckIns > checkinLimit {
log.Println("Handler ==> Excess checkins", "Time:", time.Since(start))
write.response <- false
} else if s.numCheckIns == checkinLimit {
log.Println("Handler ==> accepted", time.Since(start))
write.response <- true
// send selection success
log.Println("Handler ==> Limit reached, selecting now", "Time:", time.Since(start))
for i := 0; i < s.numCheckIns; i++ {
s.selected <- true
}
} else {
log.Println("Handler ==> accepted", "Time:", time.Since(start))
write.response <- true
}
case VAR_NUM_UPDATES_START:
s.numUpdatesStart++
log.Println("Handler ==> numUpdates", s.numUpdatesStart, "Time:", time.Since(start))
log.Println("Handler ==> accepted update", "Time:", time.Since(start))
write.response <- true
case VAR_NUM_UPDATES_FINISH:
s.numUpdatesFinish++
log.Println("Handler ==> numUpdates: ", s.numUpdatesFinish, "Finish Time:", time.Since(start))
log.Println("Handler ==> accepted update", "Time:", time.Since(start))
write.response <- true
// if enough updates available, start FA
if s.numUpdatesFinish == updateLimit {
// begin federated averaging process
log.Println("Begin FA Process")
s.FederatedAveraging()
s.resetFLVariables()
}
}
// After wait period check if everything is fine
case <-time.After(estimatedWaitingTime * time.Second):
log.Println("Timeout")
// if checkin limit is not reached
// abandon round
if s.numCheckIns < checkinLimit {
log.Println("Round Abandoned!")
// reject all devices
for i := 0; i < s.numCheckIns; i++ {
s.selected <- false
}
// reset
s.resetFLVariables()
}
// TODO: Decide about updates not received in time
}
}
}
// Check for error, log and exit if err
func check(err error, errorMsg string) {
if err != nil {
log.Fatalf(errorMsg, " ==> ", err)
}
}
func (s *server) resetFLVariables() {
s.numCheckIns = 0
s.numUpdatesStart = 0
s.numUpdatesFinish = 0
s.checkpointUpdates = make(map[int]flRoundClientResult)
}