-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
235 lines (192 loc) · 7.36 KB
/
Copy pathserver.py
File metadata and controls
235 lines (192 loc) · 7.36 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
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
import warnings
from logging import INFO
from flwr.common import FitRes, Parameters, Scalar
from typing import Dict, List, Tuple, Union, Optional, cast
import json
import flwr as fl
from flwr.common.logger import log
from flwr.server.client_proxy import ClientProxy
from flwr_datasets import FederatedDataset, partitioner
from flwr.server.strategy import FedXgbBagging, FedXgbCyclic
import numpy as np
from utils import generic_args_parser
import xgboost as xgb
import math
import os
import random
warnings.filterwarnings("ignore", category=UserWarning)
class CustomFedXgbBagging(FedXgbBagging):
def aggregate_fit(
self,
server_round: int,
results: List[Tuple[ClientProxy, FitRes]],
failures: List[Union[Tuple[ClientProxy, FitRes], BaseException]],
) -> Tuple[Optional[Parameters], Dict[str, Scalar]]:
"""Aggregate fit results using bagging."""
if not results:
return None, {}
# Do not aggregate if there are failures and failures are not accepted
if not self.accept_failures and failures:
return None, {}
# Aggregate all the client trees
global_model = self.global_model
for _, fit_res in results:
update = fit_res.parameters.tensors
for bst in update:
random_number = random.random()
print(random_number)
if random_number > 0.5:
global_model = aggregate(global_model, bst)
self.global_model = global_model
return (
Parameters(tensor_type="", tensors=[cast(bytes, global_model)]),
{},
)
def aggregate(
bst_prev_org: Optional[bytes],
bst_curr_org: bytes,
) -> bytes:
"""Conduct bagging aggregation for given trees."""
if not bst_prev_org:
return bst_curr_org
# Get the tree numbers
tree_num_prev, _ = _get_tree_nums(bst_prev_org)
_, paral_tree_num_curr = _get_tree_nums(bst_curr_org)
bst_prev = json.loads(bytearray(bst_prev_org))
bst_curr = json.loads(bytearray(bst_curr_org))
bst_prev["learner"]["gradient_booster"]["model"]["gbtree_model_param"][
"num_trees"
] = str(tree_num_prev + paral_tree_num_curr)
iteration_indptr = bst_prev["learner"]["gradient_booster"]["model"][
"iteration_indptr"
]
bst_prev["learner"]["gradient_booster"]["model"]["iteration_indptr"].append(
iteration_indptr[-1] + paral_tree_num_curr
)
# Aggregate new trees
trees_curr = bst_curr["learner"]["gradient_booster"]["model"]["trees"]
for tree_count in range(paral_tree_num_curr):
trees_curr[tree_count]["id"] = tree_num_prev + tree_count
bst_prev["learner"]["gradient_booster"]["model"]["trees"].append(
trees_curr[tree_count]
)
bst_prev["learner"]["gradient_booster"]["model"]["tree_info"].append(0)
bst_prev_bytes = bytes(json.dumps(bst_prev), "utf-8")
return bst_prev_bytes
def _get_tree_nums(xgb_model_org: bytes) -> Tuple[int, int]:
xgb_model = json.loads(bytearray(xgb_model_org))
# Get the number of trees
tree_num = int(
xgb_model["learner"]["gradient_booster"]["model"]["gbtree_model_param"][
"num_trees"
]
)
# Get the number of parallel trees
paral_tree_num = int(
xgb_model["learner"]["gradient_booster"]["model"]["gbtree_model_param"][
"num_parallel_tree"
]
)
return tree_num, paral_tree_num
# Parse arguments for experimental settings
args = generic_args_parser()
pool_size = args.pool_size
num_rounds = args.num_rounds
num_clients_per_round = args.num_clients_per_round
dataloader_str = args.dataloader
sample_rate = args.sample_rate
sampling_method = args.sampling_method
partitioner_type = args.partitioner_type
def eval_config(rnd: int) -> Dict[str, str]:
"""Return a configuration with global epochs."""
config = {
"global_round": str(rnd),
}
return config
def fit_config(rnd: int) -> Dict[str, str]:
"""Return a configuration with global epochs."""
config = {
"global_round": str(rnd),
}
return config
evals = []
num_rounds_elapsed = 1
def evaluate_metrics_aggregation(eval_metrics):
"""Return an aggregated metric (AUC) for evaluation."""
global num_rounds_elapsed, evals
filtered_metrics = [(num, metrics) for num, metrics in eval_metrics if not math.isnan(metrics["AUC"])]
total_num = sum([num for num, _ in filtered_metrics])
auc_aggregated = (
sum([metrics["AUC"] * num for num, metrics in filtered_metrics]) / total_num
)
evals.append(round(auc_aggregated, 4))
if num_rounds_elapsed == num_rounds:
print(evals)
print("Writing to a file")
with open(f'_static/{dataloader_str}_{sampling_method}_{partitioner_type}_{sample_rate}_{num_clients_per_round}.txt', 'w') as file:
file.write(','.join(map(str, evals)))
if sampling_method == 'mvs':
print("writing model sizes to a file")
model_sizes = []
for i in range(1, num_rounds+1):
file_path = f'_static/{i}.csv'
data = np.loadtxt(file_path, delimiter=',')
average_size = np.mean(data)
model_sizes.append(round(average_size, 3))
os.remove(file_path)
with open(f'_static/{dataloader_str}_{sampling_method}_{partitioner_type}_{sample_rate}_{num_clients_per_round}_size.txt', 'w') as file:
file.write(','.join(map(str, model_sizes)))
num_rounds_elapsed += 1
print("Aggregate evals: ", evals)
metrics_aggregated = {"AUC": auc_aggregated}
return metrics_aggregated
def get_evaluate_fn(test_data, params):
"""Return a function for centralised evaluation."""
def evaluate_fn(
server_round: int, parameters: Parameters, config: Dict[str, Scalar]
):
# If at the first round, skip the evaluation
global rate
if server_round == 0:
return 0, {}
else:
bst = xgb.Booster(params=params)
for para in parameters.tensors:
para_b = bytearray(para)
# Load global model
bst.load_model(para_b)
# Run evaluation
eval_results = bst.eval_set(
evals=[(test_data, "valid")],
iteration=bst.num_boosted_rounds() - 1,
)
auc = round(float(eval_results.split("\t")[1].split(":")[1]), 4)
evals[rate].append(auc)
if server_round % 150 == 0:
rate += 0.1
evals[rate] = []
log(INFO, f"Eval results: {evals}")
return 0, {"AUC": auc}
return evaluate_fn
print("Min available clients: ", pool_size)
print("Min fit clients: ", num_clients_per_round)
strategy = FedXgbBagging(
evaluate_function=None,
fraction_fit=(float(num_clients_per_round) / pool_size),
min_fit_clients=num_clients_per_round,
min_available_clients=pool_size,
min_evaluate_clients=2,
fraction_evaluate=1.0,
on_evaluate_config_fn=eval_config,
on_fit_config_fn=fit_config,
evaluate_metrics_aggregation_fn=(
evaluate_metrics_aggregation
),
)
# Start Flower server
fl.server.start_server(
server_address="0.0.0.0:8080",
config=fl.server.ServerConfig(num_rounds=num_rounds),
strategy=strategy,
client_manager=None,
)