-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
643 lines (514 loc) · 23.5 KB
/
main.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
import importlib.util
from syftbox.lib import Client
from syftbox.lib import SyftPermission
from pathlib import Path
import json
import shutil
from torch import nn
import torch
from torch.utils.data import DataLoader, TensorDataset
from utils import create_participant_json_file, update_json, ParticipantStateCols
# TODO: add a syftignore to ignore mnist test dataset from syncing
# Exception name to indicate the state cannot advance
# as there are some pre-requisites that are not met
class StateNotReady(Exception):
pass
# TODO: Currently setting the permissions with public write
# change the permission model later to be more secure
# NOTE: we mainly want the aggregator to have write access to
# fl_aggregator/running/fl_project_name/fl_clients/*
def add_public_write_permission(client: Client, path: Path) -> None:
"""
Adds public write permission to the given path
"""
permission = SyftPermission.mine_with_public_write(client.email)
permission.ensure(path)
def get_all_directories(path: Path) -> list:
"""
Returns the list of directories present in the given path
"""
return [x for x in path.iterdir() if x.is_dir()]
def get_app_private_data(client: Client, app_name: str) -> Path:
"""
Returns the private data directory of the app
"""
return client.workspace.data_dir / "private" / app_name
def get_client_proj_state(project_folder: Path) -> dict:
"""
Returns the path to the state.json file for the project
"""
project_state = {}
project_state_file = project_folder / "state/state.json/"
if project_state_file.is_file():
project_state = json.load(project_state_file.open())
return project_state
def init_fl_aggregator_app(client: Client) -> None:
"""
Creates the `fl_aggregator` app in the `api_data` folder
with the following structure:
```
api_data
└── fl_aggregator
└── launch
└── running
└── done
```
"""
fl_aggregator = client.api_data("fl_aggregator")
for folder in ["launch", "running", "done"]:
fl_aggregator_folder = fl_aggregator / folder
fl_aggregator_folder.mkdir(parents=True, exist_ok=True)
# Create the private data directory for the app
# This is where the private test data will be stored
app_pvt_dir = get_app_private_data(client, "fl_aggregator")
app_pvt_dir.mkdir(parents=True, exist_ok=True)
def initialize_fl_project(client: Client, fl_config_json_path: Path) -> None:
"""
Initializes the FL project by reading the `fl_config.json` file
If the project with same name already exists in the `running` folder
then it skips creating the project
If the project does not exist, it creates a new project with the
project name and creates the folders for the clients and the aggregator
api_data
└── fl_aggregator
└── launch
└── running
└── <fl_project_name>
├── fl_clients
│ ├── ..
├── agg_weights
├── fl_config.json
├── global_model_weights.pt
├── model_arch.py
└── state.json
└── done
"""
with open(fl_config_json_path, "r") as f:
fl_config: dict = json.load(f)
proj_name = str(fl_config["project_name"])
participants = fl_config["participants"]
fl_aggregator = client.api_data("fl_aggregator")
running_folder = fl_aggregator / "running"
proj_folder = running_folder / proj_name
if proj_folder.is_dir():
print(f"FL project {proj_name} already exists")
return
else:
print(f"Creating new FL project {proj_name}")
proj_folder.mkdir(parents=True, exist_ok=True)
fl_clients_folder = proj_folder / "fl_clients"
agg_weights_folder = proj_folder / "agg_weights"
fl_clients_folder.mkdir(parents=True, exist_ok=True)
agg_weights_folder.mkdir(parents=True, exist_ok=True)
# create the folders for the participants
for participant in participants:
participant_folder = fl_clients_folder / participant
participant_folder.mkdir(parents=True, exist_ok=True)
# TODO: create a custom syft permission for the clients in the `fl_clients` folder
add_public_write_permission(client, participant_folder)
# Move the config file to the project's running folder
shutil.move(fl_config_json_path, proj_folder)
# move the model architecture to the project's running folder
model_arch_src = fl_aggregator / "launch" / fl_config["model_arch"]
shutil.move(model_arch_src, proj_folder)
# copy the global model weights to the project's agg_weights folder as `agg_model_round_0.pt`
# and move the global model weights to the project's running folder
model_weights_src = fl_aggregator / "launch" / fl_config["model_weight"]
shutil.copy(model_weights_src, agg_weights_folder / "agg_model_round_0.pt")
shutil.move(model_weights_src, proj_folder)
# Copy the metrics dashboard files to the project's public folder
metrics_folder = Path(client.my_datasite) / "public" / "fl" / proj_name
metrics_folder.mkdir(parents=True, exist_ok=True)
shutil.copy("./dashboard/index.html", metrics_folder)
shutil.copy("./dashboard/syftbox-sdk.js", metrics_folder)
shutil.copy("./dashboard/index.js", metrics_folder)
# Create a new participants.json file in the metrics folder
participant_metrics_file = metrics_folder / "participants.json"
create_participant_json_file(
participants, fl_config["rounds"], output_path=participant_metrics_file
)
# Copy the accuracy_metrics.json file to the project's metrics folder
shutil.copy("./dashboard/accuracy_metrics.json", metrics_folder)
# TODO: create a state.json file to keep track of the project state
# if needed while running the FL rounds
def launch_fl_project(client: Client) -> None:
"""
- Checks if `fl_config.json` file is present in the `launch` folder
- Check if the project exists in the `running` folder with the same `project_name`.
If not, create a new Project
a. creates a directory with the project name in running folder
b. inside the project it creates the folders of clients with a custom syft permissions
c. copies over the fl_config.json and model_arch.py and global_model_weights.pt
Example:
- Manually Copy the `fl_config.json`, `model_arch.py`, `global_model_weights.pt`
and `mnist_test_dataset.pt` to the `launch` folder
api_data
└── fl_aggregator
└── launch
├── fl_config.json (dragged and dropped by the user)
├── model_arch.py (dragged and dropped by the FL user)
├── global_model_weights.pt (dragged and dropped by the FL user)
├── mnist_test_dataset.pt
"""
launch_folder = client.api_data("fl_aggregator") / "launch"
fl_config_json_path = launch_folder / "fl_config.json"
if not fl_config_json_path.is_file():
print(f"`fl_config.json` not found in the {launch_folder} folder. Skipping...")
return
initialize_fl_project(client, fl_config_json_path)
def get_network_participants(client: Client):
exclude_dir = ["apps", ".syft"]
entries = client.datasites.iterdir()
users = []
for entry in entries:
if entry.is_dir() and entry not in exclude_dir:
users.append(entry.name)
return users
def get_participants_metric_file(client: Client, proj_folder: Path):
"""
Returns the path to the participant metrics file
"""
return client.my_datasite / "public" / "fl" / proj_folder.name / "participants.json"
def create_fl_client_request(client: Client, proj_folder: Path):
"""
Create the request folder for the fl clients
"""
fl_clients = get_all_directories(proj_folder / "fl_clients")
network_participants = get_network_participants(client)
for fl_client in fl_clients:
if fl_client.name not in network_participants:
print(f"Client {fl_client.name} is not part of the network")
continue
fl_client_app_path = (
client.datasites / fl_client.name / "api_data" / "fl_client"
)
fl_client_request_folder = fl_client_app_path / "request" / proj_folder.name
if not fl_client_request_folder.is_dir():
# Create a request folder for the client
fl_client_request_folder.mkdir(parents=True, exist_ok=True)
# Copy the fl_config.json, model_arch.py to the request folder
shutil.copy(proj_folder / "fl_config.json", fl_client_request_folder)
shutil.copy(proj_folder / "model_arch.py", fl_client_request_folder)
print(
f"Sending request to {fl_client.name} for the project {proj_folder.name}"
)
def check_fl_client_pvt_data_added(
fl_proj_folder: Path,
fl_client_name: str,
):
"""Check if the private data is added to the client"""
proj_state = get_client_proj_state(fl_proj_folder)
participant_added_data = proj_state.get("dataset_added")
# Skip if the state file is not present
if participant_added_data is None:
print(f"Private data not added to the client {fl_client_name}")
return
participants_metrics_file = get_participants_metric_file(client, fl_proj_folder)
update_json(
participants_metrics_file,
fl_client_name,
ParticipantStateCols.ADDED_PRIVATE_DATA,
participant_added_data,
)
def check_fl_client_model_training_progress(client: Client, proj_folder: Path):
"""Check if model training progress for the client"""
fl_clients = get_all_directories(proj_folder / "fl_clients")
for fl_client in fl_clients:
fl_client_running_folder = client.api_data("fl_client/running", fl_client.name)
fl_proj_folder = fl_client_running_folder / proj_folder.name
proj_state = get_client_proj_state(fl_proj_folder)
model_train_progress = proj_state.get("model_train_progress")
# Skip if the state file is not present
if model_train_progress is None:
return
participants_metrics_file = get_participants_metric_file(client, proj_folder)
update_json(
participants_metrics_file,
fl_client.name,
ParticipantStateCols.MODEL_TRAINING_PROGRESS,
model_train_progress,
)
def check_fl_client_installed(client: Client, proj_folder: Path):
"""
Checks if the client has installed the `fl_client` app
"""
fl_clients = get_all_directories(proj_folder / "fl_clients")
for fl_client in fl_clients:
fl_client_app_path = (
client.datasites / fl_client.name / "api_data" / "fl_client"
)
fl_client_request_folder = fl_client_app_path / "request"
fl_client_request_syftperm = fl_client_request_folder / "_.syftperm"
installed_fl_client_app = True
if not fl_client_request_syftperm.is_file():
print(f"FL client {fl_client.name} has not installed the app yet")
installed_fl_client_app = False
participants_metrics_file = get_participants_metric_file(client, proj_folder)
# As they have installed, update the participants.json file with state
update_json(
participants_metrics_file,
fl_client.name,
ParticipantStateCols.FL_CLIENT_INSTALLED,
installed_fl_client_app,
)
def check_proj_requests(client: Client, proj_folder: Path):
"""
Step 1: Checks if the project requests are sent to the clients
Step 2: Checks if all the clients have approved the project
Note: The clients approve the project when they move from the `request` folder to the `running` folder
"""
fl_clients = get_all_directories(proj_folder / "fl_clients")
project_unapproved_clients = []
for fl_client in fl_clients:
fl_client_app_path = (
client.datasites / fl_client.name / "api_data" / "fl_client"
)
fl_client_request_folder = fl_client_app_path / "request" / proj_folder.name
fl_client_running_folder = fl_client_app_path / "running" / proj_folder.name
# If the project is not present in the running folder and the request folder
# create a request folder for the client
if (
not fl_client_running_folder.is_dir()
and not fl_client_request_folder.is_dir()
):
print(
f"Request sent to {fl_client.name} for the project {proj_folder.name}"
)
if not fl_client_running_folder.is_dir():
project_unapproved_clients.append(fl_client.name)
else:
# If the project is present in the running folder, update the participants.json file with state
participants_metrics_file = get_participants_metric_file(
client, proj_folder
)
update_json(
participants_metrics_file,
fl_client.name,
ParticipantStateCols.PROJECT_APPROVED,
True,
)
# Check if the private data is added to the client
check_fl_client_pvt_data_added(
fl_client_running_folder,
fl_client.name,
)
if project_unapproved_clients:
raise StateNotReady(
f"Project {proj_folder.name} is not approved by the clients {project_unapproved_clients}"
)
def load_model_class(model_path: Path, model_class_name: str) -> type:
spec = importlib.util.spec_from_file_location(model_path.stem, model_path)
model_arch = importlib.util.module_from_spec(spec)
spec.loader.exec_module(model_arch)
model_class = getattr(model_arch, model_class_name)
return model_class
def aggregate_model(fl_config, proj_folder, trained_model_paths, current_round) -> Path:
print("Aggregating the trained models")
print(f"Trained model paths: {trained_model_paths}")
global_model_class = load_model_class(
proj_folder / fl_config["model_arch"], fl_config["model_class_name"]
)
global_model: nn.Module = global_model_class()
global_model_state_dict = global_model.state_dict()
aggregated_model_weights = {}
n_peers = len(trained_model_paths)
for model_file in trained_model_paths:
user_model_state = torch.load(str(model_file))
for key in global_model_state_dict.keys():
# If user model has a different architecture than my global model.
# Skip it
if user_model_state.keys() != global_model_state_dict.keys():
raise ValueError(
"User model has a different architecture than the global model"
)
if aggregated_model_weights.get(key, None) is None:
aggregated_model_weights[key] = user_model_state[key] * (1 / n_peers)
else:
aggregated_model_weights[key] += user_model_state[key] * (1 / n_peers)
global_model.load_state_dict(aggregated_model_weights)
global_model_output_path = (
proj_folder / "agg_weights" / f"agg_model_round_{current_round}.pt"
)
torch.save(global_model.state_dict(), str(global_model_output_path))
return global_model_output_path
def shift_project_to_done_folder(
client: Client, proj_folder: Path, total_rounds: int
) -> None:
"""
Moves the project to the `done` folder
a. Create a directory in the `done` folder with the same name as the project
b. moves the agg weights and fl_clients to the done folder
c. delete the project folder from the running folder
"""
done_folder = client.api_data("fl_aggregator") / "done"
done_proj_folder = done_folder / proj_folder.name
done_proj_folder.mkdir(parents=True, exist_ok=True)
# Move the agg weights and round weights folder to the done project folder
# Move the fl_clients folder to the done project folder
shutil.move(proj_folder / "agg_weights", done_proj_folder)
shutil.move(proj_folder / "fl_clients", done_proj_folder)
# Delete the project folder from the running folder
shutil.rmtree(proj_folder)
def evaluate_agg_model(agg_model: nn.Module, dataset_path: Path) -> float:
agg_model.eval()
# load the saved mnist subset
images, labels = torch.load(str(dataset_path))
# create a tensordataset
dataset = TensorDataset(images, labels)
# create a dataloader for the dataset
data_loader = DataLoader(dataset, batch_size=64, shuffle=True)
# dataset = torch.load(str(dataset_path))
# data_loader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=False)
correct = 0
total = 0
with torch.no_grad():
for images, labels in data_loader:
outputs = agg_model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
# Accuracy is returned as a percentage
accuracy = correct / total
return accuracy
def save_model_accuracy_metrics(
client: Client, proj_folder: Path, current_round: int, accuracy: float
):
"""
Saves the model accuracy in the public folder of the datasite under project name
"""
metrics_folder = client.my_datasite / "public" / "fl" / proj_folder.name
if not metrics_folder.is_dir():
raise StateNotReady(
f"Metrics folder not found for the project {proj_folder.name}"
)
metrics_file = metrics_folder / "accuracy_metrics.json"
# Schema of json files
# [ {round: 1, accuracy: 0.98}, {round: 2, accuracy: 0.99} ]
# Append the accuracy and round to the json file
with open(metrics_file, "r") as f:
metrics = json.load(f)
metrics.append({"round": current_round, "accuracy": accuracy})
with open(metrics_file, "w") as f:
json.dump(metrics, f)
def advance_fl_round(client: Client, proj_folder: Path):
"""
1. Wait for the trained model from the clients
3. Aggregate the trained model and place it in the `agg_weights` folder
4. Send the aggregated model to all the clients
5. Repeat until all the rounds are complete
"""
agg_weights_folder = proj_folder / "agg_weights"
current_round = len(list(agg_weights_folder.iterdir()))
with open(proj_folder / "fl_config.json", "r") as f:
fl_config: dict = json.load(f)
total_rounds = fl_config["rounds"]
if current_round >= total_rounds + 1:
print(f"FL project {proj_folder.name} is complete ✅")
shift_project_to_done_folder(client, proj_folder, total_rounds)
return
participants = fl_config["participants"]
test_dataset_dir = get_app_private_data(client, "fl_aggregator")
test_dataset_path = test_dataset_dir / fl_config["test_dataset"]
if not test_dataset_path.exists():
raise StateNotReady(
f"Test dataset not found, please add the test dataset to : {test_dataset_path.resolve()}"
)
check_fl_client_model_training_progress(client, proj_folder)
if current_round == 1:
for participant in participants:
client_app_path = client.datasites / participant / "api_data" / "fl_client"
client_agg_weights_folder = (
client_app_path / "running" / proj_folder.name / "agg_weights"
)
client_round_1_model = client_agg_weights_folder / "agg_model_round_0.pt"
if not client_round_1_model.is_file():
shutil.copy(
proj_folder / "agg_weights" / "agg_model_round_0.pt",
client_agg_weights_folder,
)
pending_clients = []
trained_model_paths = []
for participant in participants:
participant_folder = proj_folder / "fl_clients" / participant
participant_round_folder = (
participant_folder / f"trained_model_round_{current_round}.pt"
)
trained_model_paths.append(participant_round_folder)
if not participant_round_folder.is_file():
pending_clients.append(participant)
else:
# Update the participants.json file with the current round
participants_metrics_file = get_participants_metric_file(
client, proj_folder
)
update_json(
participants_metrics_file,
participant,
ParticipantStateCols.ROUND,
f"{current_round}/{total_rounds}",
)
if pending_clients:
raise StateNotReady(
f"Waiting for trained model from the clients {pending_clients} for round {current_round}"
)
# Aggregate the trained model
agg_model_output_path = aggregate_model(
fl_config, proj_folder, trained_model_paths, current_round
)
# Evaluate the aggregate model
model_class = load_model_class(
proj_folder / fl_config["model_arch"], fl_config["model_class_name"]
)
model: nn.Module = model_class()
model.load_state_dict(torch.load(str(agg_model_output_path), weights_only=True))
accuracy = evaluate_agg_model(model, test_dataset_path)
print(f"Accuracy of the aggregated model for round {current_round}: {accuracy}")
save_model_accuracy_metrics(client, proj_folder, current_round, accuracy)
# Send the aggregated model to all the clients
for participant in participants:
client_app_path = client.datasites / participant / "api_data" / "fl_client"
client_agg_weights_folder = (
client_app_path / "running" / proj_folder.name / "agg_weights"
)
shutil.copy(agg_model_output_path, client_agg_weights_folder)
def _advance_fl_project(client: Client, proj_folder: Path) -> None:
"""
Iterate over all the project folder, it will try to advance its state.
1. Has the client installed the fl_client app or not (api_data/fl_client), if not throw an error message
2. have we submitted the project request to the clients (api_data/fl_client/request)
3. Have all the clients approved the project or not.
4. let assume the round ix x, place agg_model_round_x.pt inside all the clients
5. wait for the trained model from the clients
6. aggregate the trained model
7. repeat d until all the rounds are complete
"""
try:
create_fl_client_request(client, proj_folder)
check_fl_client_installed(client, proj_folder)
check_proj_requests(client, proj_folder)
advance_fl_round(client, proj_folder)
except StateNotReady as e:
print(e)
return
def advance_fl_projects(client: Client) -> None:
"""
Iterates over the `running` folder and tries to advance the FL projects
"""
fl_aggregator_app = client.api_data("fl_aggregator")
running_folder = fl_aggregator_app / "running"
for proj_folder in running_folder.iterdir():
if proj_folder.is_dir():
proj_name = proj_folder.name
print(f"Advancing FL project {proj_name}")
_advance_fl_project(client, proj_folder)
if __name__ == "__main__":
client = Client.load()
# Step 1: Init the FL Aggregator App
init_fl_aggregator_app(client)
# Step 2: Launch the FL Project
# Iterates over the `launch` folder and creates a new FL project
# if the `fl_config.json` is found in the `launch` folder
launch_fl_project(client)
# Step 3: Advance the FL Projects.
# Iterates over the running folder and tries to advance the FL project
advance_fl_projects(client)