forked from royson/fedl2p
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (47 loc) · 1.89 KB
/
Copy pathmain.py
File metadata and controls
59 lines (47 loc) · 1.89 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
import sys
from pprint import pformat
from config import get_args
from pathlib import Path
from src.data import prepare_fl_partitioned_dataset
from src.apps import get_app
from src.server import get_server
from src.simulation import start_simulation
from src.log import Checkpoint
import numpy as np
import random
import torch
import logging
logger = logging.getLogger(__name__)
def main():
# YAML & Arguments Config Passing
config = get_args()
# Initialize logger
ckp = Checkpoint(config)
logger.info(f'Command ran: {" ".join(sys.argv)}')
logger.info(pformat(config))
if config['run']:
# Initialize wanDB. You can access ckp.config as attributes.
ckp.init_wandb()
logger.info(f'Run ID: {ckp.config.run_id}')
# Setting seed for reproducibility
if hasattr(ckp.config, 'seed'):
logger.info(f'Setting fixed seed: {ckp.config.seed}')
torch.manual_seed(ckp.config.seed)
random.seed(ckp.config.seed)
np.random.seed(ckp.config.seed)
torch.set_num_threads(ckp.config.cpu_threads)
# Downloads and partitions dataset federated-ly.
# Federated directory is accessible via ckp.config.data.fed_dir
prepare_fl_partitioned_dataset(ckp)
# Application-specific code
# Contains 1) client pipeline, 2) how it's evaluated on the server, 3) FL main pipeline
my_app = get_app(ckp)
# Get Server, which consists of Strategy & ClientManager, using App's strategy_fns namely,
# on_fit_config_fn, on_evaluate_config_fn, eval_fn
server = get_server(ckp, strategy_fns=my_app.get_strategy_fns())
# Initialize ray & flwr annd call app.run()
start_simulation(ckp, server=server, app=my_app)
# Sync local checkpoint config with wandb config
ckp.update_wandb_config()
if __name__ == "__main__":
main()