fedn.network.combiner.hooks package

The FEDn Hooks package responsible for executing user defined code on the server.

Submodules

fedn.network.combiner.hooks.allowed_import module

fedn.network.combiner.hooks.hook_client module

class fedn.network.combiner.hooks.hook_client.CombinerHookInterface[source]

Bases: object

Combiner to server function hooks client.

__init__()[source]

Initialize CombinerHookInterface client.

aggregate(previous_global, update_handler: UpdateHandler, helper, delete_models: bool)[source]

Aggregation call to the hook functions. Sends models in chunks, then asks for aggregation.

Parameters:

global_model – The global model that will be distributed to clients.

Returns:

config that will be distributed to clients.

Return type:

dict

client_selection(clients: list) list[source]
client_settings(global_model) dict[source]

Communicates to hook container to get a client config.

Parameters:

global_model – The global model that will be distributed to clients.

Returns:

config that will be distributed to clients.

Return type:

dict

provided_functions(server_functions: str)[source]

Communicates to hook container and asks which functions are available.

Parameters:

server_functions – String version of an implementation of the ServerFunctionsBase interface.

Returns:

dictionary specifing which functions are implemented.

Return type:

dict

fedn.network.combiner.hooks.hooks module

class fedn.network.combiner.hooks.hooks.FunctionServiceServicer[source]

Bases: FunctionServiceServicer

Function service running in an environment combined with each combiner.

Receiving requests from the combiner.

HandleAggregation(request, context)[source]

Receive and store models and aggregate based on user-defined code when specified in the request.

Parameters:
  • request_iterator (fedn.network.grpc.fedn_pb2.fedn.AggregationRequest) – the aggregation request

  • context (grpc._server._Context) – the context (unused)

Returns:

the aggregation response (aggregated model or None)

Return type:

fedn.network.grpc.fedn_pb2.AggregationResponse

HandleClientConfig(request_iterator: ClientConfigRequest, context)[source]

Distribute client configs to clients from user defined code.

Parameters:
  • request_iterator (fedn.network.grpc.fedn_pb2.ClientConfigRequest) – the client config request

  • context (grpc._server._Context) – the context (unused)

Returns:

the client config response

Return type:

fedn.network.grpc.fedn_pb2.ClientConfigResponse

HandleClientSelection(request: ClientSelectionRequest, context)[source]

Handle client selection from user defined code.

Parameters:
  • request (fedn.network.grpc.fedn_pb2.fedn.ClientSelectionRequest) – the client selection request

  • context (grpc._server._Context) – the context (unused)

Returns:

the client selection response

Return type:

fedn.network.grpc.fedn_pb2.ClientSelectionResponse

HandleMetadata(request: ClientMetaRequest, context)[source]

Store client metadata from a request.

Parameters:
  • request (fedn.network.grpc.fedn_pb2.fedn.ClientMetaRequest) – the client meta request

  • context (grpc._server._Context) – the context (unused)

Returns:

the client meta response

Return type:

fedn.network.grpc.fedn_pb2.ClientMetaResponse

HandleProvidedFunctions(request: ProvidedFunctionsResponse, context)[source]

Handles the ‘provided_functions’ request. Sends back which functions are available.

Parameters:
  • request (fedn.network.grpc.fedn_pb2.fedn.ProvidedFunctionsRequest) – the provided function request

  • context (grpc._server._Context) – the context (unused)

Returns:

dict with str -> bool for which functions are available

Return type:

fedn.network.grpc.fedn_pb2.ProvidedFunctionsResponse

HandleStoreModel(request_iterator, context)[source]

Missing associated documentation comment in .proto file.

__init__() None[source]

Initialize long-running Function server.

check_incremental_aggregate(client_id)[source]
fedn.network.combiner.hooks.hooks.serve()[source]

Start the hooks service.

fedn.network.combiner.hooks.serverfunctionsbase module

class fedn.network.combiner.hooks.serverfunctionsbase.ServerFunctions[source]

Bases: ServerFunctionsBase

class fedn.network.combiner.hooks.serverfunctionsbase.ServerFunctionsBase[source]

Bases: ABC

Base class that defines the structure for the Server Functions. Override these functions to add to the server workflow.

__init__() None[source]

Initialize the ServerFunctionsBase class. This method can be overridden by subclasses if initialization logic is required.

aggregate(previous_global: List[ndarray], client_updates: Dict[str, Tuple[List[ndarray], Dict]]) List[ndarray][source]

Aggregates a list of parameters from clients.

Args:

previous_global (list[np.ndarray]): A list of parameters representing the global model from the previous round.

client_updates (Dict[str, Tuple[List[np.ndarray], Dict]]): A dictionary where the key is client ID, pointing to a tuple with the first element being client parameter and second element being the clients metadata.

Returns:

list[np.ndarray]: A list of numpy arrays representing the aggregated parameters across all clients.

client_selection(client_ids: List[str]) List[source]

Returns a list of client_id’s of which clients to be used for the next training request.

Args:

client_ids (list[str]): A list of client_ids for all connected clients.

Returns:

list[str]: A list of client ids for which clients should be chosen for the next training round.

client_settings(global_model: List[ndarray]) Dict[source]

Returns metadata related to the model, which gets distributed to the clients. The dictionary may only contain primitive types.

Args:

global_model (list[np.ndarray]): A list of parameters representing the global model for the upcomming round.

Returns:

dict: A dictionary containing metadata information, supporting only primitive python types.

get_incremental_aggregate_model() List[ndarray][source]

Returns the current running model.

Returns:

  • list[np.ndarray] (A list of numpy arrays representing the aggregated)

  • parameters across all clients.

incremental_aggregate(client_id: str, model: List[ndarray], client_metadata: Dict, previous_global: List[ndarray])[source]

Aggregates a list of parameters from clients.

Args:

client_id: str: the id of the client sending the model.

model (list[np.ndarray]): A list of parameters representing a model as numpy arrays.

client_metadata (Dict): A dictionary containing metadata from the client update.

previous_global (list[np.ndarray]): A list of parameters representing the previous global model as numpy arrays.

Returns:

list[np.ndarray]: A list of numpy arrays representing the aggregated parameters across all clients.

fedn.network.combiner.hooks.serverfunctionstest module

Helper function to test if your server functions implementation runs correctly.

fedn.network.combiner.hooks.serverfunctionstest.test_server_functions(server_functions: ServerFunctionsBase, parameters_np: List[ndarray], client_metadata: Dict, rounds=5, num_clients=20)[source]

Test if your functionalities are working on your server functions implementation. :param server_functions: An implementation of ServerFunctionsBase. :type server_functions: ServerFunctionsBase :param parameters: Model parameters in a list of numpy arrays. :type parameters: List[np.ndarray]