-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for Secure Aggregation with low-level API #4967
Draft
panh99
wants to merge
2
commits into
sa-aggregator
Choose a base branch
from
sa-low-level-example
base: sa-aggregator
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions
21
examples/flower-secure-aggregation/secaggexample/client_app_low_level.py
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,21 @@ | ||
"""secaggexample: A Flower with SecAgg+ app.""" | ||
|
||
import numpy as np | ||
|
||
from flwr.client import ClientApp | ||
from flwr.client.mod import secaggplus_base_mod | ||
from flwr.common import Context, Message, ParametersRecord, RecordSet, array_from_numpy | ||
|
||
# Flower ClientApp | ||
app = ClientApp(mods=[secaggplus_base_mod]) | ||
|
||
|
||
@app.query() | ||
def simple_query(msg: Message, ctxt: Context) -> Message: | ||
"""Simple query function.""" | ||
pr = ParametersRecord() | ||
pr["simple_array"] = array_from_numpy(np.array([0.5, 1, 2])) | ||
print(f"Sending simple array: {pr['simple_array'].numpy()}") | ||
content = RecordSet() | ||
content.parameters_records["simple_pr"] = pr | ||
return msg.create_reply(content=content) |
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
74 changes: 74 additions & 0 deletions
74
examples/flower-secure-aggregation/secaggexample/server_app_low_level.py
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,74 @@ | ||
"""secaggexample: A Flower with SecAgg+ app.""" | ||
|
||
import time | ||
from collections.abc import Iterable | ||
from logging import DEBUG, INFO | ||
|
||
from flwr.common import Context, Message, RecordSet, log | ||
from flwr.common.constant import MessageType | ||
from flwr.common.logger import update_console_handler | ||
from flwr.server import Driver, ServerApp | ||
from flwr.server.workflow import SecAggPlusAggregator | ||
from flwr.server.workflow.secure_aggregation.secaggplus_aggregator import ( | ||
SecAggPlusAggregatorState, | ||
) | ||
|
||
# Flower ServerApp | ||
app = ServerApp() | ||
|
||
|
||
@app.main() | ||
def main(driver: Driver, context: Context) -> None: | ||
# Show debug logs | ||
update_console_handler(DEBUG, True, True) | ||
|
||
# Sample at least 5 clients | ||
log(INFO, "Waiting for at least 5 clients to connect...") | ||
while True: | ||
nids = driver.get_node_ids() | ||
if len(nids) >= 5: | ||
break | ||
time.sleep(0.1) | ||
|
||
# Create the SecAgg+ aggregator | ||
sa_aggregator = SecAggPlusAggregator( | ||
driver=driver, | ||
context=context, | ||
num_shares=context.run_config["num-shares"], | ||
reconstruction_threshold=context.run_config["reconstruction-threshold"], | ||
timeout=context.run_config["timeout"], | ||
clipping_range=8.0, | ||
on_send=on_send, | ||
on_receive=on_receive, | ||
on_stage_complete=on_stage_complete, | ||
) | ||
msgs = [ | ||
driver.create_message( | ||
content=RecordSet(), # Empty message | ||
message_type=MessageType.QUERY, | ||
dst_node_id=nid, | ||
group_id="", | ||
) | ||
for nid in nids | ||
] | ||
msgs[0].metadata.group_id = "drop" | ||
msg = sa_aggregator.aggregate(msgs) | ||
|
||
arr = msg.content.parameters_records["simple_pr"]["simple_array"] | ||
log(INFO, f"Received aggregated array: {arr.numpy()}") | ||
|
||
|
||
# Example `on_send`/`on_receive`/`on_stage_complete` callback functions | ||
def on_send(msgs: Iterable[Message], state: SecAggPlusAggregatorState) -> None: | ||
"""Intercept messages before sending.""" | ||
log(INFO, "Intercepted messages before sending.") | ||
|
||
|
||
def on_receive(msgs: Iterable[Message], state: SecAggPlusAggregatorState) -> None: | ||
"""Intercept reply messages after receiving.""" | ||
log(INFO, "Intercepted reply messages after receiving.") | ||
|
||
|
||
def on_stage_complete(success: bool, state: SecAggPlusAggregatorState) -> None: | ||
"""Handle stage completion event.""" | ||
log(INFO, "Handled stage completion event.") | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you see these functions to be useful for (in addition to logging purposes)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be a bit useful for some advanced user to adjust the protocol, but generally it's more for me to implement things like weighted averaging based on the original
SecAggPlusInsAggregator
.