Skip to content
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

Feature/SK-707 | Flower client example #537

Merged
merged 40 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
50557cd
add flower client example
viktorvaladi Feb 28, 2024
d290402
clarify readme
viktorvaladi Feb 29, 2024
a1b6982
exclude dir floating imports
viktorvaladi Feb 29, 2024
b748904
gh wf
viktorvaladi Feb 29, 2024
5a4b1be
new flower adapter
viktorvaladi Mar 12, 2024
f83e7fd
formatting
viktorvaladi Mar 12, 2024
8e45c2a
formatting
viktorvaladi Mar 12, 2024
fcb4689
formatting...
viktorvaladi Mar 12, 2024
8f17732
Updated readme, added Dockerfile, added init file
Mar 19, 2024
936e7ae
Updated readme
Mar 20, 2024
2d148b8
point at stable
viktorvaladi Mar 20, 2024
8803bd3
Language fixes
Mar 20, 2024
acf6c04
move adapter to fedn utils
viktorvaladi Mar 21, 2024
1e978b6
add adapter in fedn utils
viktorvaladi Mar 21, 2024
e4df154
readme fix
viktorvaladi Mar 26, 2024
5954bfd
Merge branch 'master' into feature/SK-707
Apr 1, 2024
489f15a
Example updated to run natively in the venv
Apr 2, 2024
43aabb4
fix readme
Apr 2, 2024
2a29cec
python3.9 -> python3 in venv
Apr 3, 2024
5335477
Updated dependcies
Apr 3, 2024
0de5e18
add results for flower compat
viktorvaladi Apr 3, 2024
53705e7
Deps
Apr 3, 2024
61d09c0
change to fit new clientapp
viktorvaladi Apr 3, 2024
ce75cfb
Revert venv changes
Apr 3, 2024
b64bd31
Update run.sh
ahellander Apr 4, 2024
a2c7e29
Code checks
Apr 4, 2024
56e109f
Updated README, removed build.sh, instructions in readme instead
Apr 4, 2024
bc423eb
Polish readme
Apr 6, 2024
a0ee701
Fix
Apr 6, 2024
e6e6cb1
update gitignore
viktorvaladi Apr 8, 2024
d4c5721
update gitignore
viktorvaladi Apr 8, 2024
eb75a8e
updater adapter
viktorvaladi Apr 8, 2024
9c4a424
update adapter
viktorvaladi Apr 8, 2024
8ba7cbc
linting
viktorvaladi Apr 8, 2024
1854451
linting
viktorvaladi Apr 8, 2024
afe46f6
linting
viktorvaladi Apr 8, 2024
3c0006d
get_parameters and assertion added
viktorvaladi Apr 8, 2024
79c60c3
linting
viktorvaladi Apr 8, 2024
02dc0b6
linting
viktorvaladi Apr 8, 2024
30b82f2
readme typo fix
viktorvaladi Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/flower-client/client/flwr_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ def client_fn(cid: str):
# Flower ClientApp
app = ClientApp(
client_fn=client_fn,
)
)
2 changes: 1 addition & 1 deletion examples/flower-client/client/flwr_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ def get_weights(net):
def set_weights(net, parameters):
params_dict = zip(net.state_dict().keys(), parameters)
state_dict = OrderedDict({k: torch.tensor(v) for k, v in params_dict})
net.load_state_dict(state_dict, strict=True)
net.load_state_dict(state_dict, strict=True)
29 changes: 23 additions & 6 deletions fedn/fedn/utils/flowercompat/client_app_adapter.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from typing import Tuple

from flwr.client import ClientApp
from flwr.common import (Context, EvaluateIns, FitIns, Message, MessageType,
Metadata, NDArrays, ndarrays_to_parameters,
from flwr.common import (Context, EvaluateIns, GetParametersIns, FitIns,
Message, MessageType, MessageTypeLegacy, Metadata,
NDArrays, ndarrays_to_parameters,
parameters_to_ndarrays)
from flwr.common.recordset_compat import (evaluateins_to_recordset,
fitins_to_recordset,
getparametersins_to_recordset,
recordset_to_evaluateres,
recordset_to_fitres)
from flwr_task import Net, get_weights
recordset_to_fitres,
recordset_to_getparametersres)


class FlwrClientAppAdapter:
Expand All @@ -17,8 +19,16 @@ class FlwrClientAppAdapter:
def __init__(self, app: ClientApp) -> None:
self.app = app

def init_parameters(self):
return get_weights(net=Net())
def init_parameters(self, partition_id: int):
# Construct a get_parameters message for the ClientApp
message, context = self._construct_message(
MessageTypeLegacy.GET_PARAMETERS, [], partition_id
)
# Call client app with train message
client_return_message = self.app(message, context)
# return NDArrays of clients parameters
parameters = self._parse_get_parameters_message(client_return_message)
return parameters

def train(self, parameters: NDArrays, partition_id: int):
# Construct a train message for the ClientApp with given parameters
Expand All @@ -41,6 +51,10 @@ def evaluate(self, parameters: NDArrays, partition_id: int):
# Parse return message
loss, accuracy = self._parse_evaluate_message(client_return_message)
return loss, accuracy

def _parse_get_parameters_message(self, message: Message) -> NDArrays:
get_parameters_res = recordset_to_getparametersres(message.content, keep_input=False)
return parameters_to_ndarrays(get_parameters_res.parameters)

def _parse_train_message(self, message: Message) -> Tuple[NDArrays, int]:
fitres = recordset_to_fitres(message.content, keep_input=False)
Expand All @@ -67,6 +81,9 @@ def _construct_message(
if message_type == MessageType.EVALUATE:
ev_ins: EvaluateIns = EvaluateIns(parameters=parameters, config={})
recordset = evaluateins_to_recordset(evaluateins=ev_ins, keep_input=False)
if message_type == MessageTypeLegacy.GET_PARAMETERS:
get_parameters_ins: GetParametersIns = GetParametersIns({})
recordset = getparametersins_to_recordset(getparameters_ins=get_parameters_ins)

metadata = Metadata(
run_id=0,
Expand Down
Loading