nvflare.app_common.workflows.base_model_controller module

class BaseModelController(persistor_id: str = 'persistor', ignore_result_error: bool = False, allow_empty_global_weights: bool = False, task_check_period: float = 0.5)[source]

Bases: Controller, FLComponentWrapper, ABC

FLModel based controller.

Parameters:
  • persistor_id (str, optional) – ID of the persistor component. Defaults to AppConstants.DEFAULT_PERSISTOR_ID (“persistor”).

  • ignore_result_error (bool, optional) – whether this controller can proceed if client result has errors. Defaults to False.

  • allow_empty_global_weights (bool, optional) – whether to allow empty global weights. Some pipelines can have empty global weights at first round, such that clients start training from scratch without any global info. Defaults to False.

  • task_check_period (float, optional) – interval for checking status of tasks. Defaults to 0.5.

broadcast_model(data, task_name: str = 'train', targets: List[Client] | List[str] | None = None, min_responses: int | None = None, timeout: int = 0, wait_time_after_min_received: int = 0, blocking: bool = True, callback: Callable[[FLModel], None] | None = None) List[source]

Send a task with data to a list of targets.

Parameters:
  • data – FLModel to be sent to clients. It must be a FLModel object. It will raise an exception if None.

  • task_name (str, optional) – name of the task. Defaults to “train”.

  • 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, i.e., never time out.

  • wait_time_after_min_received (int, optional) – time to wait after minimum number of clients responses has been received. Defaults to 0.

  • blocking (bool, optional) – whether to block to wait for task result. Defaults to True.

  • callback (Callable[[FLModel], None], optional) – callback when a result is received, only called when blocking=False. Defaults to None.

Returns:

List[FLModel] if blocking=True else None

build_component(config_dict: dict)[source]
control_flow(abort_signal: Signal, fl_ctx: FLContext) None[source]

This is the control logic for the RUN.

NOTE: this is running in a separate thread, and its life is the duration of the RUN.

Parameters:
  • fl_ctx – the FL context

  • abort_signal – the abort signal. If triggered, this method stops waiting and returns to the caller.

get_app_dir()[source]

Get current app directory.

get_component(component_id: str)[source]

Get the specified component from the context

Parameters:

component_id – ID of the component

Returns: the specified component if it is defined; or None if not.

get_run_dir()[source]

Get current run directory.

load_model()[source]
process_result_of_unknown_task(client: Client, task_name: str, client_task_id: str, result: Shareable, fl_ctx: FLContext) None[source]

Process result when no task is found for it.

This is called when a result submission is received from a client, but no standing task can be found for it (from the task queue)

This could happen when: - the client’s submission is too late - the task is already completed - the Controller lost the task, e.g. the Server is restarted

Parameters:
  • client – the client that the result comes from

  • task_name – the name of the task

  • client_task_id – ID of the task

  • result – the result from the client

  • fl_ctx – the FL context that comes with the client’s submission

abstract run()[source]

Main run routine called by the Controller’s control_flow to execute the workflow.

Returns: None.

sample_clients(num_clients: int | None = None) List[str][source]
save_model(model)[source]
set_fl_context(data: FLModel)[source]

Set up the fl_ctx information based on the passed in FLModel data.

start_controller(fl_ctx: FLContext) None[source]

Starts the controller.

This method is called at the beginning of the RUN.

Parameters:
  • fl_ctx – the FL context. You can use this context to access services provided by the

  • example (framework. For)

  • your (you can get Command Register from it and register)

  • modules. (admin command)

stop_controller(fl_ctx: FLContext)[source]

Stops the controller.

This method is called right before the RUN is ended.

Parameters:
  • fl_ctx – the FL context. You can use this context to access services provided by the

  • example (framework. For)

  • your (you can get Command Register from it and unregister)

  • modules. (admin command)