Skip to content

Commit

Permalink
Cleaned up new example/test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Hellander committed Jan 29, 2024
1 parent 215f748 commit 09bc999
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 29 deletions.
6 changes: 6 additions & 0 deletions examples/async-simulation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data
*.npz
*.tgz
*.tar.gz
.async-simulation
client.yaml
333 changes: 333 additions & 0 deletions examples/async-simulation/Experiment.ipynb

Large diffs are not rendered by default.

36 changes: 11 additions & 25 deletions examples/async-simulation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,37 @@ This example is intended as a test for asynchronous clients.
Clone FEDn and locate into this directory.
```sh
git clone https://github.com/scaleoutsystems/fedn.git
cd fedn/examples/mnist-pytorch
cd fedn/examples/async-simulation
```

### Preparing the environment, the local data, the compute package and seed model
Start by initializing a virtual enviroment with all of the required dependencies.
```
bin/init_venv.sh
```

Then, to get the data you can run the following script.
```
bin/get_data
```
Install FEDn and dependencies (we recommend using a virtual environment):

The next command splits the data in 2 parts for the clients.
```
bin/split_data
pip install -e ../../fedn
pip install -r requirements.txt
```
> **Note**: run with `--n_splits=N` to split in *N* parts.

Create the compute package and a seed model that you will be asked to upload in the next step.
```
bin/build.sh
tar -cvzf package.tgz
```
> The files location will be `package/package.tgz` and `seed.npz`.

### Deploy FEDn
Now we are ready to deploy FEDn with `docker-compose`.
```
docker-compose -f ../../docker-compose.yaml up -d minio mongo mongo-express reducer combiner
python client/entrypoint init_seed
```

### Initialize the federated model
Now navigate to http://localhost:8090 to see the reducer UI. You will be asked to upload the compute package and the seed model that you created in the previous step. Make sure to choose the "PyTorch" helper.
### Deploy FEDn and two clients
docker-compose -f ../../docker-compose.yaml -f docker-compose.override.yaml up

### Attach clients
To attach clients to the network, use the docker-compose.override.yaml template to start 2 clients:
### Initialize the federated model
See 'Experiments.pynb' or 'launch_client.py' to set the package and seed model.

```
docker-compose -f ../../docker-compose.yaml -f docker-compose.override.yaml up client
```
> **Note**: run with `--scale client=N` to start *N* clients.
### Run federated training
Finally, you can start the experiment from the "control" tab of the UI.
See 'Experiment.ipynb'.

## Clean up
You can clean up by running `docker-compose down`.
4 changes: 2 additions & 2 deletions examples/async-simulation/init_fedn.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from fedn import APIClient

DISCOVER_HOST = '54.208.105.152'
DISCOVER_HOST = '127.0.0.1'
DISCOVER_PORT = 8092

client = APIClient(DISCOVER_HOST, DISCOVER_PORT)
client.set_package('package.tar.gz', 'numpyarrayhelper')
client.set_package('package.tgz', 'numpyhelper')
client.set_initial_model('seed.npz')
21 changes: 19 additions & 2 deletions examples/async-simulation/launch_clients.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
"""This scripts starts N_CLIENTS using the SDK.
If you are running with a local deploy of FEDn
using docker compose, you need to make sure that clients
are able to resolver the name "combiner" to 127.0.0.1
One way to accomplish this is to edit your /etc/host,
adding the line:
combiner 127.0.0.1
"""


import copy
import time

from fedn.network.clients.client import Client

DISCOVER_HOST = '127.0.0.1'
DISCOVER_PORT = 8092
N_CLIENTS = 1
N_CLIENTS = 5
CLIENTS_AVAILABLE_TIME = 120

config = {'discover_host': DISCOVER_HOST, 'discover_port': DISCOVER_PORT, 'token': None, 'name': 'testclient',
'client_id': 1, 'remote_compute_context': True, 'force_ssl': False, 'dry_run': False, 'secure': False,
'preshared_cert': False, 'verify': False, 'preferred_combiner': False,
'validator': True, 'trainer': True, 'init': None, 'logfile': 'test.log', 'heartbeat_interval': 2,
'reconnect_after_missed_heartbeat': 30}

# Start up N_CLIENTS clients
clients = []
for i in range(N_CLIENTS):
config_i = copy.deepcopy(config)
config['name'] = 'client{}'.format(i)
clients.append(Client(config))

time.sleep(120)
# Disconnect clients after some time
time.sleep(CLIENTS_AVAILABLE_TIME)
for client in clients:
client._detach()
Binary file removed examples/async-simulation/seed.npz
Binary file not shown.
10 changes: 10 additions & 0 deletions examples/mnist-keras/bin/init_venv_macm1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

# Init venv
python3 -m venv .mnist-keras

# Pip deps
.mnist-keras/bin/pip install --upgrade pip
.mnist-keras/bin/pip install -e ../../fedn
.mnist-keras/bin/pip install -r requirements-macos.txt
4 changes: 4 additions & 0 deletions examples/mnist-keras/requirements-macos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tensorflow-macos
tensorflow-metal
fire==0.3.1
docker==5.0.2

0 comments on commit 09bc999

Please sign in to comment.