-
Notifications
You must be signed in to change notification settings - Fork 942
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fds-add-concatenate-divisions
- Loading branch information
Showing
38 changed files
with
897 additions
and
874 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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Secure aggregation with Flower (the SecAgg+ protocol) 🧪 | ||
|
||
> 🧪 = This example covers experimental features that might change in future versions of Flower | ||
> Please consult the regular PyTorch code examples ([quickstart](https://github.com/adap/flower/tree/main/examples/quickstart-pytorch), [advanced](https://github.com/adap/flower/tree/main/examples/advanced-pytorch)) to learn how to use Flower with PyTorch. | ||
The following steps describe how to use Secure Aggregation in flower, with `ClientApp` using `secaggplus_mod` and `ServerApp` using `SecAggPlusWorkflow`. | ||
|
||
## Preconditions | ||
|
||
Let's assume the following project structure: | ||
|
||
```bash | ||
$ tree . | ||
. | ||
├── client.py # Client application using `secaggplus_mod` | ||
├── server.py # Server application using `SecAggPlusWorkflow` | ||
├── workflow_with_log.py # Augmented `SecAggPlusWorkflow` | ||
├── run.sh # Quick start script | ||
├── pyproject.toml # Project dependencies (poetry) | ||
└── requirements.txt # Project dependencies (pip) | ||
``` | ||
|
||
## Installing dependencies | ||
|
||
Project dependencies (such as and `flwr`) are defined in `pyproject.toml`. We recommend [Poetry](https://python-poetry.org/docs/) to install those dependencies and manage your virtual environment ([Poetry installation](https://python-poetry.org/docs/#installation)), but feel free to use a different way of installing dependencies and managing virtual environments if you have other preferences. | ||
|
||
### Poetry | ||
|
||
```shell | ||
poetry install | ||
poetry shell | ||
``` | ||
|
||
Poetry will install all your dependencies in a newly created virtual environment. To verify that everything works correctly you can run the following command: | ||
|
||
```shell | ||
poetry run python3 -c "import flwr" | ||
``` | ||
|
||
### pip | ||
|
||
Write the command below in your terminal to install the dependencies according to the configuration file requirements.txt. | ||
|
||
```shell | ||
pip install -r requirements.txt | ||
``` | ||
|
||
If you don't see any errors you're good to go! | ||
|
||
## Run the example with one command (recommended) | ||
|
||
```bash | ||
./run.sh | ||
``` | ||
|
||
## Run the example with the simulation engine | ||
|
||
```bash | ||
flower-simulation --server-app server:app --client-app client:app --num-supernodes 5 | ||
``` | ||
|
||
## Alternatively, run the example (in 7 terminal windows) | ||
|
||
Start the Flower Superlink in one terminal window: | ||
|
||
```bash | ||
flower-superlink --insecure | ||
``` | ||
|
||
Start 5 Flower `ClientApp` in 5 separate terminal windows: | ||
|
||
```bash | ||
flower-client-app client:app --insecure | ||
``` | ||
|
||
Start the Flower `ServerApp`: | ||
|
||
```bash | ||
flower-server-app server:app --insecure --verbose | ||
``` | ||
|
||
## Amend the example for practical usage | ||
|
||
For real-world applications, modify the `workflow` in `server.py` as follows: | ||
|
||
```python | ||
workflow = fl.server.workflow.DefaultWorkflow( | ||
fit_workflow=SecAggPlusWorkflow( | ||
num_shares=<number of shares>, | ||
reconstruction_threshold=<reconstruction threshold>, | ||
) | ||
) | ||
``` |
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,34 @@ | ||
import time | ||
|
||
from flwr.client import ClientApp, NumPyClient | ||
from flwr.client.mod import secaggplus_mod | ||
import numpy as np | ||
|
||
|
||
# Define FlowerClient and client_fn | ||
class FlowerClient(NumPyClient): | ||
def fit(self, parameters, config): | ||
# Instead of training and returning model parameters, | ||
# the client directly returns [1.0, 1.0, 1.0] for demonstration purposes. | ||
ret_vec = [np.ones(3)] | ||
# Force a significant delay for testing purposes | ||
if "drop" in config and config["drop"]: | ||
print(f"Client dropped for testing purposes.") | ||
time.sleep(8) | ||
else: | ||
print(f"Client uploading {ret_vec[0]}...") | ||
return ret_vec, 1, {} | ||
|
||
|
||
def client_fn(cid: str): | ||
"""Create and return an instance of Flower `Client`.""" | ||
return FlowerClient().to_client() | ||
|
||
|
||
# Flower ClientApp | ||
app = ClientApp( | ||
client_fn=client_fn, | ||
mods=[ | ||
secaggplus_mod, | ||
], | ||
) |
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 |
---|---|---|
|
@@ -3,11 +3,12 @@ requires = ["poetry-core>=1.4.0"] | |
build-backend = "poetry.core.masonry.api" | ||
|
||
[tool.poetry] | ||
name = "secaggplus-mt" | ||
name = "app-secure-aggregation" | ||
version = "0.1.0" | ||
description = "Secure Aggregation with Driver API" | ||
description = "Flower Secure Aggregation example." | ||
authors = ["The Flower Authors <[email protected]>"] | ||
|
||
[tool.poetry.dependencies] | ||
python = ">=3.8,<3.11" | ||
flwr-nightly = { version = "^1.5.0.dev20230629", extras = ["simulation", "rest"] } | ||
python = "^3.8" | ||
# Mandatory dependencies | ||
flwr-nightly = { version = "1.8.0.dev20240309", extras = ["simulation"] } |
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 @@ | ||
flwr-nightly[simulation]==1.8.0.dev20240309 |
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,45 @@ | ||
from flwr.common import Context | ||
from flwr.server import Driver, LegacyContext, ServerApp, ServerConfig | ||
from flwr.server.strategy import FedAvg | ||
from flwr.server.workflow import DefaultWorkflow, SecAggPlusWorkflow | ||
|
||
from workflow_with_log import SecAggPlusWorkflowWithLogs | ||
|
||
|
||
# Define strategy | ||
strategy = FedAvg( | ||
fraction_fit=1.0, # Select all available clients | ||
fraction_evaluate=0.0, # Disable evaluation | ||
min_available_clients=5, | ||
) | ||
|
||
|
||
# Flower ServerApp | ||
app = ServerApp() | ||
|
||
|
||
@app.main() | ||
def main(driver: Driver, context: Context) -> None: | ||
# Construct the LegacyContext | ||
context = LegacyContext( | ||
state=context.state, | ||
config=ServerConfig(num_rounds=3), | ||
strategy=strategy, | ||
) | ||
|
||
# Create the workflow | ||
workflow = DefaultWorkflow( | ||
fit_workflow=SecAggPlusWorkflowWithLogs( | ||
num_shares=3, | ||
reconstruction_threshold=2, | ||
timeout=5, | ||
) | ||
# # For real-world applications, use the following code instead | ||
# fit_workflow=SecAggPlusWorkflow( | ||
# num_shares=<number of shares>, | ||
# reconstruction_threshold=<reconstruction threshold>, | ||
# ) | ||
) | ||
|
||
# Execute | ||
workflow(driver, context) |
Oops, something went wrong.