-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathpt_np.py
More file actions
148 lines (118 loc) · 4.67 KB
/
pt_np.py
File metadata and controls
148 lines (118 loc) · 4.67 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
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import threading
from nvflare.fox import fox
from nvflare.fox.api.utils import simple_logging
from nvflare.fox.examples import get_experiment_root
from nvflare.fox.examples.np.algos.utils import add as add_np
from nvflare.fox.examples.np.algos.utils import div as div_np
from nvflare.fox.examples.np.algos.utils import parse_state_dict as parse_np
from nvflare.fox.examples.pt.utils import add as add_pt
from nvflare.fox.examples.pt.utils import div as div_pt
from nvflare.fox.examples.pt.utils import parse_state_dict as parse_pt
from nvflare.fox.sim.simulator import Simulator
from nvflare.fuel.utils.log_utils import get_obj_logger
class _AggrResult:
def __init__(self):
self.pt_total = {}
self.np_total = {}
self.count = 0
self.lock = threading.Lock() # ensure update integrity
class PTFedAvgMixed:
def __init__(self, pt_model, np_model, num_rounds=10, timeout=2.0):
self.num_rounds = num_rounds
self.pt_model = pt_model
self.np_model = np_model
self.timeout = timeout
self.name = "PTFedAvg"
self.logger = get_obj_logger(self)
self._pt_model = parse_pt(pt_model)
self._np_model = parse_np(np_model)
@fox.algo
def execute(self):
self.logger.info(f"[{fox.call_info}] Start training for {self.num_rounds} rounds")
pt_model, np_model = self._pt_model, self._np_model
for i in range(self.num_rounds):
pt_model, np_model = self._do_one_round(i, pt_model, np_model)
self.logger.info(f"FINAL MODEL: {pt_model=} {np_model=}")
return pt_model, np_model
def _do_one_round(self, r, pt_model, np_model):
aggr_result = _AggrResult()
fox.clients(
process_resp_cb=self._accept_train_result,
aggr_result=aggr_result,
).train(r, pt_model, np_model)
if aggr_result.count == 0:
return None, None
else:
pt_result = aggr_result.pt_total
div_pt(pt_result, aggr_result.count)
self.logger.info(
f"[{fox.call_info}] round {r}: aggr PT result from {aggr_result.count} clients: {pt_result}"
)
np_result = aggr_result.np_total
div_np(np_result, aggr_result.count)
self.logger.info(
f"[{fox.call_info}] round {r}: aggr NP result from {aggr_result.count} clients: {np_result}"
)
return pt_result, np_result
def _accept_train_result(self, gcc, result, aggr_result: _AggrResult):
self.logger.info(f"[{fox.call_info}] got train result from {fox.caller}: {result}")
pt_result, np_result = result
with aggr_result.lock:
add_pt(pt_result, aggr_result.pt_total)
add_np(np_result, aggr_result.np_total)
aggr_result.count += 1
return None
class PTTrainer:
def __init__(self, delta: float):
self.delta = delta
self.logger = get_obj_logger(self)
@fox.collab
def train(self, current_round, pt_model, np_model):
if fox.is_aborted:
self.logger.debug("training aborted")
return 0
self.logger.debug(f"[{fox.call_info}] training round {current_round}: {pt_model=} {np_model=}")
pt_result = {}
for k, v in pt_model.items():
pt_result[k] = v + self.delta
np_result = {}
for k, v in np_model.items():
np_result[k] = v + self.delta
return pt_result, np_result
def main():
simple_logging(logging.DEBUG)
init_model = {
"x": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
"y": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
"z": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
}
server = PTFedAvgMixed(
pt_model=init_model,
np_model=init_model,
num_rounds=4,
)
simulator = Simulator(
root_dir=get_experiment_root(),
experiment_name="pt_np",
server=server,
client=PTTrainer(delta=1.0),
num_clients=2,
)
result = simulator.run()
print(f"Final result: {result}")
if __name__ == "__main__":
main()