nvflare.collab.api package

Submodules

Module contents

Public programming surface of the collab API layer.

User code should import these names from this package rather than from the implementation modules. Exports are resolved lazily (PEP 562).

class App(obj, name: str)[source]

Bases: object

add_collab_object(name: str, obj)[source]
property backend
property client_hierarchy
property client_proxies
finalize(context: Context)[source]
find_collab_method(target_obj, method_name)[source]
find_method(target_obj, method_name)[source]
property fqn
get_children()[source]
get_collab_interface() dict[str, dict[str, list[str]]][source]
get_collab_objects()[source]
get_leaf_clients()[source]
get_prop(name: str, default=None)[source]
get_props()[source]
get_target_object_publish_interface(target_name: str)[source]
has_children()[source]
initialize(context: Context)[source]
property my_site: Proxy
new_context(caller: str, callee: str, target_group=None, set_call_ctx=True)[source]
property server_proxy
set_fqn(fqn)[source]
set_prop(name: str, value)[source]
setup(workspace: Workspace, server: Proxy, clients: list[Proxy], abort_signal)[source]
update_props(props: dict)[source]
property workspace
class CallOption(expect_result: bool = True, blocking: bool = True, timeout: float = 60.0, secure: bool = False, optional: bool = False, target=None, parallel=0)[source]

Bases: object

CallOption defines behavior of a collab call.

Parameters:
  • expect_result – whether result is expected from the remote object.

  • blocking – whether the call is blocking. Only for group calls.

  • timeout – when expecting result, the max number of secs to wait for result.

  • secure – whether to use P2P secure messaging.

  • optional – whether the call is optional.

  • target – name of the collab object to be called.

  • parallel – maximum number of calls that may remain in flight at once.

class ClientApp(obj, name: str = 'client')[source]

Bases: App

get_children()[source]
has_children()[source]
exception CollabCallError(site: str, func_name: str, cause, cause_type: str | None = None, remote_traceback: str | None = None)[source]

Bases: Exception

Raised when a call to a Collab function fails.

class Context(app, caller: str, callee: str, abort_signal: Signal, target_group=None)[source]

Bases: object

property backend
property client_hierarchy
property clients
get_prop(name: str, default=None)[source]
is_aborted()[source]
property server
set_prop(name: str, value)[source]
property target_group_size
property workspace
class ContextKey[source]

Bases: object

RESULT = 'result'
class GroupCallContext(app, target_name: str, call_opt: CallOption, func_name: str, process_cb, cb_kwargs, context: Context, waiter: ResultWaiter)[source]

Bases: object

GroupCallContext contains contextual information about a group call to a target.

Parameters:
  • app – the calling app.

  • target_name – name of the target to be called in the remote app.

  • call_opt – call options.

  • func_name – name of the function to be called in the remote app.

  • process_cb – the callback function to be called to process response from the remote app.

  • cb_kwargs – kwargs passed to the callback function.

  • context – call context.

  • waiter – the waiter to wait for result

add_partial_result(partial_result)[source]
call_completed()[source]
set_completion_cb(cb, **cb_kwargs)[source]
set_exception(ex)[source]

This is called by the backend to set the exception received from the remote app. The process_cb will NOT be called.

Parameters:

ex – the exception received from the remote app.

Returns:

set_result(result)[source]

This is called by the backend to set the result received from the remote app. If process_cb is available, it will be called with the result from the remote app.

Parameters:

result – the result received from the remote app.

Returns: None

class ModuleWrapper(module: ModuleType | str | None = None)[source]

Bases: object

Wrap a module’s publish, main, init, and final functions for Collab.

Collab creates this wrapper automatically whenever a module is supplied as a primary or named app object. This allows callers to pass modules containing standalone functions anywhere they could pass class instances:

# my_module.py from nvflare.collab import collab

@collab.publish def train(weights=None):

@collab.main def fed_avg():

# main.py import my_module

recipe = CollabRecipe(server=my_module, client=my_module, …)

For a distributed deployment, the module must be importable on all client machines (i.e., part of the installed package or included in job resources).

Initialize wrapper with a module containing @collab.publish/@collab.main functions.

Parameters:

module – A Python module object OR a fully qualified module name string. When a string is passed, the module will be imported. When no argument is passed (None), the wrapper is in an uninitialized state and will be set up by __setstate__ during unpickling.

Note

For job config serialization, we store the module name as self._module which matches the ‘module’ parameter. FLARE’s _get_args() looks for param or _param in __dict__, so _module matches ‘module’.

When running as __main__, we convert to an importable module path so that remote processes can import the same module.

property module_name: str

Get the module name.

class PublishInterface(methods: Mapping[str, Sequence] | None = None)[source]

Bases: Mapping[str, MethodInterface]

Immutable description of an object’s published Collab methods.

classmethod from_dict(methods: Mapping[str, Sequence] | None) PublishInterface[source]
get_method(method_name: str) MethodInterface | None[source]

Return a method interface, preserving an empty interface for zero-argument methods.

to_dict() dict[source]

Return the plain-dict representation used for synchronization.

class ServerApp(obj, name: str = 'server')[source]

Bases: App

get_children()[source]
has_children()[source]