-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
from typing import List, Tuple | ||
import numpy as np | ||
|
||
import flwr as fl | ||
from flwr.common import Metrics | ||
|
||
from client import client_fn | ||
STATE_VAR = 'timestamp' | ||
|
||
|
||
# Define metric aggregation function | ||
def record_state_metrics(metrics: List[Tuple[int, Metrics]]) -> Metrics: | ||
"""Ensure that timestamps are monotonically increasing.""" | ||
states = [] | ||
for _, m in metrics: | ||
# split string and covert timestamps to float | ||
states.append([float(tt) for tt in m[STATE_VAR].split(',')]) | ||
|
||
for client_state in states: | ||
if len(client_state) == 1: | ||
continue | ||
deltas = np.diff(client_state) | ||
assert np.all(deltas > 0), f"Timestamps are not monotonically increasing: {client_state}" | ||
|
||
return {STATE_VAR: states} | ||
|
||
|
||
strategy = fl.server.strategy.FedAvg(evaluate_metrics_aggregation_fn=record_state_metrics) | ||
|
||
hist = fl.simulation.start_simulation( | ||
client_fn=client_fn, | ||
num_clients=2, | ||
config=fl.server.ServerConfig(num_rounds=3), | ||
strategy=strategy, | ||
) | ||
|
||
assert hist.losses_distributed[-1][1] == 0 or (hist.losses_distributed[0][1] / hist.losses_distributed[-1][1]) >= 0.98 | ||
|
||
# The checks in record_state_metrics don't do anythinng if client's state has a single entry | ||
state_metrics_last_round = hist.metrics_distributed[STATE_VAR][-1] | ||
assert len(state_metrics_last_round[1][0]) == 2*state_metrics_last_round[0], f"There should be twice as many entries in the client state as rounds" |
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,11 +1,42 @@ | ||
from typing import List, Tuple | ||
import numpy as np | ||
|
||
import flwr as fl | ||
from flwr.common import Metrics | ||
|
||
|
||
from client import client_fn | ||
STATE_VAR = 'timestamp' | ||
|
||
|
||
# Define metric aggregation function | ||
def record_state_metrics(metrics: List[Tuple[int, Metrics]]) -> Metrics: | ||
"""Ensure that timestamps are monotonically increasing.""" | ||
states = [] | ||
for _, m in metrics: | ||
# split string and covert timestamps to float | ||
states.append([float(tt) for tt in m[STATE_VAR].split(',')]) | ||
|
||
for client_state in states: | ||
if len(client_state) == 1: | ||
continue | ||
deltas = np.diff(client_state) | ||
assert np.all(deltas > 0), f"Timestamps are not monotonically increasing: {client_state}" | ||
|
||
return {STATE_VAR: states} | ||
|
||
|
||
strategy = fl.server.strategy.FedAvg(evaluate_metrics_aggregation_fn=record_state_metrics) | ||
|
||
hist = fl.simulation.start_simulation( | ||
client_fn=client_fn, | ||
num_clients=2, | ||
config=fl.server.ServerConfig(num_rounds=3), | ||
strategy=strategy, | ||
) | ||
|
||
assert hist.losses_distributed[-1][1] == 0 or (hist.losses_distributed[0][1] / hist.losses_distributed[-1][1]) >= 0.98 | ||
|
||
# The checks in record_state_metrics don't do anythinng if client's state has a single entry | ||
state_metrics_last_round = hist.metrics_distributed[STATE_VAR][-1] | ||
assert len(state_metrics_last_round[1][0]) == 2*state_metrics_last_round[0], f"There should be twice as many entries in the client state as rounds" |
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,8 +1,47 @@ | ||
from typing import List, Tuple | ||
import numpy as np | ||
|
||
|
||
import flwr as fl | ||
from flwr.common import Metrics | ||
STATE_VAR = 'timestamp' | ||
|
||
|
||
# Define metric aggregation function | ||
def record_state_metrics(metrics: List[Tuple[int, Metrics]]) -> Metrics: | ||
"""Ensure that timestamps are monotonically increasing.""" | ||
if not metrics: | ||
return {} | ||
|
||
if STATE_VAR not in metrics[0][1]: | ||
# Do nothing if keyword is not present | ||
return {} | ||
|
||
states = [] | ||
for _, m in metrics: | ||
# split string and covert timestamps to float | ||
states.append([float(tt) for tt in m[STATE_VAR].split(',')]) | ||
|
||
for client_state in states: | ||
if len(client_state) == 1: | ||
continue | ||
deltas = np.diff(client_state) | ||
assert np.all(deltas > 0), f"Timestamps are not monotonically increasing: {client_state}" | ||
|
||
return {STATE_VAR: states} | ||
|
||
|
||
strategy = fl.server.strategy.FedAvg(evaluate_metrics_aggregation_fn=record_state_metrics) | ||
|
||
hist = fl.server.start_server( | ||
server_address="0.0.0.0:8080", | ||
config=fl.server.ServerConfig(num_rounds=3), | ||
strategy=strategy, | ||
) | ||
|
||
assert hist.losses_distributed[-1][1] == 0 or (hist.losses_distributed[0][1] / hist.losses_distributed[-1][1]) >= 0.98 | ||
|
||
if STATE_VAR in hist.metrics_distributed: | ||
# The checks in record_state_metrics don't do anythinng if client's state has a single entry | ||
state_metrics_last_round = hist.metrics_distributed[STATE_VAR][-1] | ||
assert len(state_metrics_last_round[1][0]) == 2*state_metrics_last_round[0], f"There should be twice as many entries in the client state as rounds" |