-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/SK-523 | Refactor REST and add APIClient (#477)
Showing
84 changed files
with
3,882 additions
and
1,184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import fire | ||
import yaml | ||
|
||
from fedn import APIClient | ||
|
||
|
||
def _download_config(output): | ||
""" Download the client configuration file from the controller. | ||
:param output: The output file path. | ||
:type output: str | ||
""" | ||
client = APIClient(host="localhost", port=8092) | ||
config = client.get_client_config(checksum=True) | ||
with open(output, 'w') as f: | ||
f.write(yaml.dump(config)) | ||
|
||
|
||
def test_api_get_methods(): | ||
client = APIClient(host="localhost", port=8092) | ||
status = client.get_controller_status() | ||
assert status | ||
print("Controller status: ", status, flush=True) | ||
|
||
events = client.get_events() | ||
assert events | ||
print("Events: ", events, flush=True) | ||
|
||
validations = client.list_validations() | ||
assert validations | ||
print("Validations: ", validations, flush=True) | ||
|
||
models = client.get_model_trail() | ||
assert models | ||
print("Models: ", models, flush=True) | ||
|
||
clients = client.list_clients() | ||
assert clients | ||
print("Clients: ", clients, flush=True) | ||
|
||
combiners = client.list_combiners() | ||
assert combiners | ||
print("Combiners: ", combiners, flush=True) | ||
|
||
combiner = client.get_combiner("combiner") | ||
assert combiner | ||
print("Combiner: ", combiner, flush=True) | ||
|
||
first_model = client.get_initial_model() | ||
assert first_model | ||
print("First model: ", first_model, flush=True) | ||
|
||
package = client.get_package() | ||
assert package | ||
print("Package: ", package, flush=True) | ||
|
||
checksum = client.get_package_checksum() | ||
assert checksum | ||
print("Checksum: ", checksum, flush=True) | ||
|
||
rounds = client.list_rounds() | ||
assert rounds | ||
print("Rounds: ", rounds, flush=True) | ||
|
||
round = client.get_round(1) | ||
assert round | ||
print("Round: ", round, flush=True) | ||
|
||
sessions = client.list_sessions() | ||
assert sessions | ||
print("Sessions: ", sessions, flush=True) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
client = APIClient(host="localhost", port=8092) | ||
fire.Fire({ | ||
'set_seed': client.set_initial_model, | ||
'set_package': client.set_package, | ||
'start_session': client.start_session, | ||
'get_client_config': _download_config, | ||
'test_api_get_methods': test_api_get_methods, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
network_id: fedn-network | ||
discover_host: reducer | ||
discover_port: 8090 | ||
discover_host: api-server | ||
discover_port: 8092 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
network_id: fedn-network | ||
controller: | ||
host: api-server | ||
port: 8092 | ||
debug: True | ||
|
||
statestore: | ||
type: MongoDB | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FEDn is using sphinx with reStructuredText. | ||
|
||
sphinx-apidoc --ext-autodoc --module-first -o _source ../fedn/fedn ../*tests* ../*exceptions* ../*common* ../ ../fedn/fedn/network/api/server.py ../fedn/fedn/network/controller/controlbase.py | ||
sphinx-build . _build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,63 @@ | ||
Architecture overview | ||
===================== | ||
|
||
Constructing a federated model with FEDn amounts to a) specifying the details of the client-side training code and data integrations, and b) deploying the reducer-combiner network. A FEDn network, as illustrated in the picture below, is made up of three main components: the *Reducer*, one or more *Combiners*, and a number of *Clients*. The combiner network forms the backbone of the FedML orchestration mechanism, while the Reducer provides discovery services and provides controls to coordinate training over the combiner network. By horizontally scaling the combiner network, one can meet the needs of a growing number of clients. | ||
Constructing a federated model with FEDn amounts to a) specifying the details of the client-side training code and data integrations, and b) deploying the federated network. A FEDn network, as illustrated in the picture below, is made up of components into three different tiers: the *Controller* tier (3), one or more *Combiners* in second tier (2), and a number of *Clients* in tier (1). | ||
The combiners forms the backbone of the federated ML orchestration mechanism, while the Controller tier provides discovery services and controls to coordinate training over the federated network. | ||
By horizontally scaling the number of combiners, one can meet the needs of a growing number of clients. | ||
|
||
.. image:: img/overview.png | ||
.. image:: img/FEDn_network.png | ||
:alt: FEDn network | ||
:width: 100% | ||
:align: center | ||
|
||
Main components | ||
--------------- | ||
|
||
Client | ||
...... | ||
|
||
A Client is a data node, holding private data and connecting to a Combiner to receive model update requests and model validation requests during training rounds. Importantly, clients do not require any open ingress ports. A client receives the code to be executed from the Reducer upon connecting to the network, and thus they only need to be configured prior to connection to read the local datasets during training and validation. Python3 client implementation is provided out of the box, and it is possible to write clients in a variety of languages to target different software and hardware requirements. | ||
|
||
Combiner | ||
........ | ||
The clients: tier 1 | ||
................... | ||
|
||
A combiner is an actor whose main role is to orchestrate and aggregate model updates from a number of clients during a training round. When and how to trigger such orchestration rounds are specified in the overall *compute plan* laid out by the Reducer. Each combiner in the network runs an independent gRPC server, providing RPCs for interacting with the alliance subsystem it controls. Hence, the total number of clients that can be accommodated in a FEDn network is proportional to the number of active combiners in the FEDn network. Combiners can be deployed anywhere, e.g. in a cloud or on a fog node to provide aggregation services near the cloud edge. | ||
A Client (gRPC client) is a data node, holding private data and connecting to a Combiner (gRPC server) to receive model update requests and model validation requests during training sessions. | ||
Importantly, clients uses remote procedure calls (RPC) to ask for model updates tasks, thus the clients not require any open ingress ports! A client receives the code (called package or compute package) to be executed from the *Controller* | ||
upon connecting to the network, and thus they only need to be configured prior to connection to read the local datasets during training and validation. The package is based on entry points in the client code, and can be customized to fit the needs of the user. | ||
This allows for a high degree of flexibility in terms of what kind of training and validation tasks that can be performed on the client side. Such as different types of machine learning models and framework, and even programming languages. | ||
A python3 client implementation is provided out of the box, and it is possible to write clients in a variety of languages to target different software and hardware requirements. | ||
|
||
Reducer | ||
....... | ||
The combiners: tier 2 | ||
..................... | ||
|
||
The reducer fills three main roles in the FEDn network: 1.) it lays out the overall, global training strategy and communicates that to the combiner network. It also dictates the strategy to aggregate model updates from individual combiners into a single global model, 2.) it handles global state and maintains the *model trail* - an immutable trail of global model updates uniquely defining the FedML training timeline, and 3.) it provides discovery services, mediating connections between clients and combiners. For this purpose, the Reducer exposes a standard REST API. | ||
A combiner is an actor whose main role is to orchestrate and aggregate model updates from a number of clients during a training session. | ||
When and how to trigger such orchestration are specified in the overall *compute plan* laid out by the *Controller*. | ||
Each combiner in the network runs an independent gRPC server, providing RPCs for interacting with the federated network it controls. | ||
Hence, the total number of clients that can be accommodated in a FEDn network is proportional to the number of active combiners in the FEDn network. | ||
Combiners can be deployed anywhere, e.g. in a cloud or on a fog node to provide aggregation services near the cloud edge. | ||
|
||
Services and communication | ||
-------------------------- | ||
The controller: tier 3 | ||
...................... | ||
|
||
The figure below provides a logical architecture view of the services provided by each agent and how they interact. | ||
Tier 3 does actually contain several components and services, but we tend to associate it with the *Controller* the most. The *Controller* fills three main roles in the FEDn network: | ||
|
||
.. image:: img/FEDn-arch-overview.png | ||
:alt: FEDn architecture overview | ||
:width: 100% | ||
:align: center | ||
1. it lays out the overall, global training strategy and communicates that to the combiner network. | ||
It also dictates the strategy to aggregate model updates from individual combiners into a single global model, | ||
2. it handles global state and maintains the *model trail* - an immutable trail of global model updates uniquely defining the federated ML training timeline, and | ||
3. it provides discovery services, mediating connections between clients and combiners. For this purpose, the *Controller* exposes a standard REST API both for RPC clients and servers, but also for user interfaces and other services. | ||
|
||
Tier 3 also contain a *Reducer* component, which is responsible for aggregating combiner-level models into a single global model. Further, it contains a *StateStore* database, | ||
which is responsible for storing various states of the network and training sessions. The final global model trail from a traning session is stored in the *ModelRegistry* database. | ||
|
||
|
||
Control flows and algorithms | ||
---------------------------- | ||
Notes on aggregating algorithms | ||
............................... | ||
|
||
FEDn is designed to allow customization of the FedML algorithm, following a specified pattern, or programming model. Model aggregation happens on two levels in the system. First, each Combiner can be configured with a custom orchestration and aggregation implementation, that reduces model updates from Clients into a single, *combiner level* model. Then, a configurable aggregation protocol on the Reducer level is responsible for combining the combiner-level models into a global model. By varying the aggregation schemes on the two levels in the system, many different possible outcomes can be achieved. Good staring configurations are provided out-of-the-box to help the user get started. | ||
FEDn is designed to allow customization of the FedML algorithm, following a specified pattern, or programming model. | ||
Model aggregation happens on two levels in the network. First, each Combiner can be configured with a custom orchestration and aggregation implementation, that reduces model updates from Clients into a single, *combiner level* model. | ||
Then, a configurable aggregation protocol on the *Controller* level is responsible for combining the combiner-level models into a global model. By varying the aggregation schemes on the two levels in the system, | ||
many different possible outcomes can be achieved. Good starting configurations are provided out-of-the-box to help the user get started. See API reference for more details. | ||
|
||
Hierarchical Federated Averaging | ||
................................ | ||
|
||
The currently implemented default scheme uses a local SGD strategy on the Combiner level aggregation and a simple average of models on the reducer level. This results in a highly horizontally scalable FedAvg scheme. The strategy works well with most artificial neural network (ANNs) models, and can in general be applied to models where it is possible and makes sense to form mean values of model parameters (for example SVMs). Additional FedML training protocols, including support for various types of federated ensemble models, are in active development. | ||
The currently implemented default scheme uses a local SGD strategy on the Combiner level aggregation and a simple average of models on the reducer level. | ||
This results in a highly horizontally scalable FedAvg scheme. The strategy works well with most artificial neural network (ANNs) models, | ||
and can in general be applied to models where it is possible and makes sense to form mean values of model parameters (for example SVMs). | ||
|
||
|
||
.. image:: img/HFedAvg.png | ||
:alt: FEDn architecture overview | ||
:width: 100% | ||
:align: center |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.