forked from NVIDIA/NVFlare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_engine_spec.py
265 lines (204 loc) · 7.66 KB
/
server_engine_spec.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
# Copyright (c) 2021, 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.
from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Tuple
from nvflare.apis.shareable import Shareable
from nvflare.widgets.widget import Widget
from .client import Client
from .engine_spec import EngineSpec
from .fl_context import FLContext
from .fl_snapshot import RunSnapshot
from .job_def import Job
from .workspace import Workspace
class ServerEngineSpec(EngineSpec, ABC):
@abstractmethod
def fire_event(self, event_type: str, fl_ctx: FLContext):
pass
@abstractmethod
def get_clients(self) -> List[Client]:
pass
@abstractmethod
def sync_clients_from_main_process(self):
"""To fetch the participating clients from the main parent process
Returns: clients
"""
pass
@abstractmethod
def update_job_run_status(self):
"""To update the job run status to parent process."""
pass
@abstractmethod
def new_context(self) -> FLContext:
# the engine must use FLContextManager to create a new context!
pass
@abstractmethod
def get_workspace(self) -> Workspace:
pass
@abstractmethod
def add_component(self, component_id: str, component):
"""Add a component into the system.
Args:
component_id: component ID
component: component object
Returns:
"""
pass
@abstractmethod
def get_component(self, component_id: str) -> object:
"""Retrieve the system component from the engine.
Args:
component_id: component ID
Returns:
component object
"""
pass
@abstractmethod
def register_aux_message_handler(self, topic: str, message_handle_func):
"""Register aux message handling function with specified topics.
Exception is raised when:
a handler is already registered for the topic;
bad topic - must be a non-empty string
bad message_handle_func - must be callable
Implementation Note:
This method should simply call the ServerAuxRunner's register_aux_message_handler method.
Args:
topic: the topic to be handled by the func
message_handle_func: the func to handle the message. Must follow aux_message_handle_func_signature.
"""
pass
@abstractmethod
def send_aux_request(
self,
targets: [],
topic: str,
request: Shareable,
timeout: float,
fl_ctx: FLContext,
optional=False,
secure=False,
) -> dict:
"""Send a request to specified clients via the aux channel.
Implementation: simply calls the AuxRunner's send_aux_request method.
Args:
targets: target clients. None or empty list means all clients.
topic: topic of the request.
request: request to be sent
timeout: number of secs to wait for replies. 0 means fire-and-forget.
fl_ctx: FL context
optional: whether this message is optional
secure: send the aux request in a secure way
Returns: a dict of replies (client name => reply Shareable)
"""
pass
@abstractmethod
def multicast_aux_requests(
self,
topic: str,
target_requests: Dict[str, Shareable],
timeout: float,
fl_ctx: FLContext,
optional: bool = False,
secure: bool = False,
) -> dict:
"""Send requests to specified clients via the aux channel.
Implementation: simply calls the AuxRunner's multicast_aux_requests method.
Args:
topic: topic of the request
target_requests: requests of the target clients. Different target can have different request.
timeout: amount of time to wait for responses. 0 means fire and forget.
fl_ctx: FL context
optional: whether this request is optional
secure: whether to send the aux request in P2P secure
Returns: a dict of replies (client name => reply Shareable)
"""
pass
def fire_and_forget_aux_request(
self, targets: [], topic: str, request: Shareable, fl_ctx: FLContext, optional=False, secure=False
) -> dict:
return self.send_aux_request(targets, topic, request, 0.0, fl_ctx, optional, secure=secure)
@abstractmethod
def get_widget(self, widget_id: str) -> Widget:
"""Get the widget with the specified ID.
Args:
widget_id: ID of the widget
Returns: the widget or None if not found
"""
pass
@abstractmethod
def persist_components(self, fl_ctx: FLContext, completed: bool):
"""To persist the FL running components
Args:
fl_ctx: FLContext
completed: flag to indicate where the run is complete
Returns:
"""
pass
@abstractmethod
def restore_components(self, snapshot: RunSnapshot, fl_ctx: FLContext):
"""To restore the FL components from the saved snapshot
Args:
snapshot: RunSnapshot
fl_ctx: FLContext
Returns:
"""
pass
@abstractmethod
def start_client_job(self, job_id, client_sites, fl_ctx: FLContext):
"""To send the start client run commands to the clients
Args:
client_sites: client sites
job_id: job_id
fl_ctx: FLContext
Returns:
"""
pass
@abstractmethod
def check_client_resources(
self, job: Job, resource_reqs: Dict[str, dict], fl_ctx: FLContext
) -> Dict[str, Tuple[bool, Optional[str]]]:
"""Sends the check_client_resources requests to the clients.
Args:
job: job object
resource_reqs: A dict of {client_name: resource requirements dict}
fl_ctx: FLContext
Returns:
A dict of {client_name: client_check_result}.
client_check_result is a tuple of (is_resource_enough, token);
is_resource_enough is a bool indicates whether there is enough resources;
token is for resource reservation / cancellation for this check request.
"""
pass
@abstractmethod
def cancel_client_resources(
self, resource_check_results: Dict[str, Tuple[bool, str]], resource_reqs: Dict[str, dict], fl_ctx: FLContext
):
"""Cancels the request resources for the job.
Args:
resource_check_results: A dict of {client_name: client_check_result}
where client_check_result is a tuple of (is_resource_enough, resource reserve token if any)
resource_reqs: A dict of {client_name: resource requirements dict}
fl_ctx: FLContext
"""
pass
@abstractmethod
def get_client_name_from_token(self, token: str) -> str:
"""Gets the client name from client login token.
Args:
token: client login token
Returns:
Client name
"""
pass
def register_app_command(self, topic: str, cmd_func, *args, **kwargs):
pass