nvflare.app_common.workflows.model_controller module¶
- class ModelController(*args, persistor_id: str = 'persistor', **kwargs)[source]¶
Bases:
BaseModelController
,ABC
Model Controller API for FLModel-based Controller.
- Parameters:
persistor_id (str, optional) – ID of the persistor component. Defaults to AppConstants.DEFAULT_PERSISTOR_ID (“persistor”).
- load_model() FLModel [source]¶
Load initial model from persistor. If persistor is not configured, returns empty FLModel.
- Returns:
FLModel
- sample_clients(num_clients: int | None = None) List[str] [source]¶
Returns a list of num_clients clients.
- Parameters:
num_clients – number of clients to return. If None or > number available clients, returns all available clients. Defaults to None.
- Returns:
A list of clients names.
- save_model(model: FLModel) None [source]¶
Saves model with persistor. If persistor is not configured, does not save.
- Parameters:
model (FLModel) – model to save.
- Returns:
None
- send_model(task_name: str = 'train', data: FLModel | None = None, targets: List[str] | None = None, min_responses: int | None = None, timeout: int = 0, callback: Callable[[FLModel], None] | None = None) None [source]¶
Send a task with data to targets (non-blocking). Callback is called when a result is received.
- Parameters:
task_name (str, optional) – name of the task. Defaults to “train”.
data (FLModel, optional) – FLModel to be sent to clients. Defaults to None.
targets (List[str], optional) – the list of target client names or None (all clients). Defaults to None.
min_responses (int, optional) – the minimum number of responses expected. If None, must receive responses from all clients that the task has been sent to. Defaults to None.
timeout (int, optional) – time to wait for clients to perform task. Defaults to 0 (never time out).
callback (Callable[[FLModel], None], optional) – callback when a result is received. Defaults to None.
- Returns:
None
- send_model_and_wait(task_name: str = 'train', data: FLModel | None = None, targets: List[str] | None = None, min_responses: int | None = None, timeout: int = 0) List[FLModel] [source]¶
Send a task with data to targets and wait for results.
Returns list of FLModel results from clients once task is completed (min_responses received or timed out). Results received from any clients after task is completed will be discarded.
- Parameters:
task_name (str, optional) – name of the task. Defaults to “train”.
data (FLModel, optional) – FLModel to be sent to clients. Defaults to None.
targets (List[str], optional) – the list of target client names or None (all clients). Defaults to None.
min_responses (int, optional) – the minimum number of responses expected. If None, must receive responses from all clients that the task has been sent to. Defaults to None.
timeout (int, optional) – time to wait for clients to perform task. Defaults to 0 (never time out).
- Returns:
List[FLModel]