flwr 1.21.0__py3-none-any.whl → 1.22.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flwr/cli/app.py +2 -0
- flwr/cli/new/new.py +9 -7
- flwr/cli/new/templates/app/README.flowertune.md.tpl +1 -1
- flwr/cli/new/templates/app/code/client.baseline.py.tpl +64 -47
- flwr/cli/new/templates/app/code/client.huggingface.py.tpl +68 -30
- flwr/cli/new/templates/app/code/client.jax.py.tpl +63 -42
- flwr/cli/new/templates/app/code/client.mlx.py.tpl +80 -51
- flwr/cli/new/templates/app/code/client.numpy.py.tpl +36 -13
- flwr/cli/new/templates/app/code/client.pytorch.py.tpl +71 -46
- flwr/cli/new/templates/app/code/client.pytorch_legacy_api.py.tpl +55 -0
- flwr/cli/new/templates/app/code/client.sklearn.py.tpl +75 -30
- flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +69 -44
- flwr/cli/new/templates/app/code/client.xgboost.py.tpl +110 -0
- flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +56 -90
- flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +1 -23
- flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +37 -58
- flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +39 -44
- flwr/cli/new/templates/app/code/model.baseline.py.tpl +0 -14
- flwr/cli/new/templates/app/code/server.baseline.py.tpl +27 -29
- flwr/cli/new/templates/app/code/server.huggingface.py.tpl +23 -19
- flwr/cli/new/templates/app/code/server.jax.py.tpl +27 -14
- flwr/cli/new/templates/app/code/server.mlx.py.tpl +29 -19
- flwr/cli/new/templates/app/code/server.numpy.py.tpl +30 -17
- flwr/cli/new/templates/app/code/server.pytorch.py.tpl +36 -26
- flwr/cli/new/templates/app/code/server.pytorch_legacy_api.py.tpl +31 -0
- flwr/cli/new/templates/app/code/server.sklearn.py.tpl +29 -21
- flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +28 -19
- flwr/cli/new/templates/app/code/server.xgboost.py.tpl +56 -0
- flwr/cli/new/templates/app/code/task.huggingface.py.tpl +16 -20
- flwr/cli/new/templates/app/code/task.jax.py.tpl +1 -1
- flwr/cli/new/templates/app/code/task.numpy.py.tpl +1 -1
- flwr/cli/new/templates/app/code/task.pytorch.py.tpl +14 -27
- flwr/cli/new/templates/app/code/{task.pytorch_msg_api.py.tpl → task.pytorch_legacy_api.py.tpl} +27 -14
- flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +1 -2
- flwr/cli/new/templates/app/code/task.xgboost.py.tpl +67 -0
- flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +4 -4
- flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +2 -2
- flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +4 -4
- flwr/cli/new/templates/app/pyproject.jax.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +2 -2
- flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +3 -3
- flwr/cli/new/templates/app/{pyproject.pytorch_msg_api.toml.tpl → pyproject.pytorch_legacy_api.toml.tpl} +3 -3
- flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +1 -1
- flwr/cli/new/templates/app/pyproject.xgboost.toml.tpl +61 -0
- flwr/cli/pull.py +100 -0
- flwr/cli/utils.py +17 -0
- flwr/clientapp/mod/__init__.py +4 -1
- flwr/clientapp/mod/centraldp_mods.py +156 -40
- flwr/clientapp/mod/localdp_mod.py +169 -0
- flwr/clientapp/typing.py +22 -0
- flwr/common/constant.py +3 -0
- flwr/common/exit/exit_code.py +4 -0
- flwr/common/record/typeddict.py +12 -0
- flwr/proto/control_pb2.py +7 -3
- flwr/proto/control_pb2.pyi +24 -0
- flwr/proto/control_pb2_grpc.py +34 -0
- flwr/proto/control_pb2_grpc.pyi +13 -0
- flwr/server/app.py +13 -0
- flwr/serverapp/strategy/__init__.py +26 -0
- flwr/serverapp/strategy/bulyan.py +238 -0
- flwr/serverapp/strategy/dp_adaptive_clipping.py +335 -0
- flwr/serverapp/strategy/dp_fixed_clipping.py +71 -49
- flwr/serverapp/strategy/fedadagrad.py +0 -3
- flwr/serverapp/strategy/fedadam.py +0 -3
- flwr/serverapp/strategy/fedavg.py +89 -64
- flwr/serverapp/strategy/fedavgm.py +198 -0
- flwr/serverapp/strategy/fedmedian.py +105 -0
- flwr/serverapp/strategy/fedprox.py +174 -0
- flwr/serverapp/strategy/fedtrimmedavg.py +176 -0
- flwr/serverapp/strategy/fedxgb_bagging.py +117 -0
- flwr/serverapp/strategy/fedxgb_cyclic.py +220 -0
- flwr/serverapp/strategy/fedyogi.py +0 -3
- flwr/serverapp/strategy/krum.py +112 -0
- flwr/serverapp/strategy/multikrum.py +247 -0
- flwr/serverapp/strategy/qfedavg.py +252 -0
- flwr/serverapp/strategy/strategy_utils.py +48 -0
- flwr/simulation/app.py +1 -1
- flwr/simulation/run_simulation.py +25 -30
- flwr/supercore/cli/flower_superexec.py +26 -1
- flwr/supercore/constant.py +19 -0
- flwr/supercore/superexec/plugin/exec_plugin.py +11 -1
- flwr/supercore/superexec/run_superexec.py +16 -2
- flwr/superlink/artifact_provider/__init__.py +22 -0
- flwr/superlink/artifact_provider/artifact_provider.py +37 -0
- flwr/superlink/servicer/control/control_grpc.py +3 -0
- flwr/superlink/servicer/control/control_servicer.py +59 -2
- {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/METADATA +6 -16
- {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/RECORD +93 -74
- flwr/cli/new/templates/app/code/client.pytorch_msg_api.py.tpl +0 -80
- flwr/cli/new/templates/app/code/server.pytorch_msg_api.py.tpl +0 -41
- flwr/serverapp/dp_fixed_clipping.py +0 -352
- flwr/serverapp/strategy/strategy_utils_tests.py +0 -304
- /flwr/cli/new/templates/app/code/{__init__.pytorch_msg_api.py.tpl → __init__.pytorch_legacy_api.py.tpl} +0 -0
- {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/WHEEL +0 -0
- {flwr-1.21.0.dist-info → flwr-1.22.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,31 +1,41 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
-
from flwr.
|
|
4
|
-
from flwr.
|
|
5
|
-
from flwr.
|
|
6
|
-
from $import_name.task import MLP, get_params
|
|
3
|
+
from flwr.app import ArrayRecord, Context
|
|
4
|
+
from flwr.serverapp import Grid, ServerApp
|
|
5
|
+
from flwr.serverapp.strategy import FedAvg
|
|
7
6
|
|
|
7
|
+
from $import_name.task import MLP, get_params, set_params
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
# Create ServerApp
|
|
10
|
+
app = ServerApp()
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@app.main()
|
|
14
|
+
def main(grid: Grid, context: Context) -> None:
|
|
15
|
+
"""Main entry point for the ServerApp."""
|
|
10
16
|
# Read from config
|
|
11
17
|
num_rounds = context.run_config["num-server-rounds"]
|
|
12
|
-
|
|
13
|
-
num_classes = 10
|
|
14
18
|
num_layers = context.run_config["num-layers"]
|
|
15
19
|
input_dim = context.run_config["input-dim"]
|
|
16
20
|
hidden_dim = context.run_config["hidden-dim"]
|
|
17
21
|
|
|
18
22
|
# Initialize global model
|
|
19
|
-
model = MLP(num_layers, input_dim, hidden_dim,
|
|
23
|
+
model = MLP(num_layers, input_dim, hidden_dim, output_dim=10)
|
|
20
24
|
params = get_params(model)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
strategy = FedAvg(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
arrays = ArrayRecord(params)
|
|
26
|
+
|
|
27
|
+
# Initialize FedAvg strategy
|
|
28
|
+
strategy = FedAvg()
|
|
29
|
+
|
|
30
|
+
# Start strategy, run FedAvg for `num_rounds`
|
|
31
|
+
result = strategy.start(
|
|
32
|
+
grid=grid,
|
|
33
|
+
initial_arrays=arrays,
|
|
34
|
+
num_rounds=num_rounds,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Save final model to disk
|
|
38
|
+
print("\nSaving final model to disk...")
|
|
39
|
+
ndarrays = result.arrays.to_numpy_ndarrays()
|
|
40
|
+
set_params(model, ndarrays)
|
|
41
|
+
model.save_weights("final_model.npz")
|
|
@@ -1,25 +1,38 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
from flwr.
|
|
5
|
-
from flwr.
|
|
6
|
-
from
|
|
7
|
-
|
|
3
|
+
import numpy as np
|
|
4
|
+
from flwr.app import ArrayRecord, Context
|
|
5
|
+
from flwr.serverapp import Grid, ServerApp
|
|
6
|
+
from flwr.serverapp.strategy import FedAvg
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
# Read from config
|
|
11
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
8
|
+
from $import_name.task import get_dummy_model
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
dummy_parameters = ndarrays_to_parameters([model])
|
|
10
|
+
# Create ServerApp
|
|
11
|
+
app = ServerApp()
|
|
16
12
|
|
|
17
|
-
# Define strategy
|
|
18
|
-
strategy = FedAvg(initial_parameters=dummy_parameters)
|
|
19
|
-
config = ServerConfig(num_rounds=num_rounds)
|
|
20
13
|
|
|
21
|
-
|
|
14
|
+
@app.main()
|
|
15
|
+
def main(grid: Grid, context: Context) -> None:
|
|
16
|
+
"""Main entry point for the ServerApp."""
|
|
22
17
|
|
|
18
|
+
# Read run config
|
|
19
|
+
num_rounds: int = context.run_config["num-server-rounds"]
|
|
23
20
|
|
|
24
|
-
#
|
|
25
|
-
|
|
21
|
+
# Load global model
|
|
22
|
+
model = get_dummy_model()
|
|
23
|
+
arrays = ArrayRecord(model)
|
|
24
|
+
|
|
25
|
+
# Initialize FedAvg strategy
|
|
26
|
+
strategy = FedAvg()
|
|
27
|
+
|
|
28
|
+
# Start strategy, run FedAvg for `num_rounds`
|
|
29
|
+
result = strategy.start(
|
|
30
|
+
grid=grid,
|
|
31
|
+
initial_arrays=arrays,
|
|
32
|
+
num_rounds=num_rounds,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# Save final model to disk
|
|
36
|
+
print("\nSaving final model to disk...")
|
|
37
|
+
ndarrays = result.arrays.to_numpy_ndarrays()
|
|
38
|
+
np.savez("final_model", *ndarrays)
|
|
@@ -1,31 +1,41 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
from flwr.
|
|
5
|
-
from flwr.
|
|
6
|
-
from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def server_fn(context: Context):
|
|
10
|
-
# Read from config
|
|
11
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
12
|
-
fraction_fit = context.run_config["fraction-fit"]
|
|
13
|
-
|
|
14
|
-
# Initialize model parameters
|
|
15
|
-
ndarrays = get_weights(Net())
|
|
16
|
-
parameters = ndarrays_to_parameters(ndarrays)
|
|
17
|
-
|
|
18
|
-
# Define strategy
|
|
19
|
-
strategy = FedAvg(
|
|
20
|
-
fraction_fit=fraction_fit,
|
|
21
|
-
fraction_evaluate=1.0,
|
|
22
|
-
min_available_clients=2,
|
|
23
|
-
initial_parameters=parameters,
|
|
24
|
-
)
|
|
25
|
-
config = ServerConfig(num_rounds=num_rounds)
|
|
26
|
-
|
|
27
|
-
return ServerAppComponents(strategy=strategy, config=config)
|
|
3
|
+
import torch
|
|
4
|
+
from flwr.app import ArrayRecord, ConfigRecord, Context
|
|
5
|
+
from flwr.serverapp import Grid, ServerApp
|
|
6
|
+
from flwr.serverapp.strategy import FedAvg
|
|
28
7
|
|
|
8
|
+
from $import_name.task import Net
|
|
29
9
|
|
|
30
10
|
# Create ServerApp
|
|
31
|
-
app = ServerApp(
|
|
11
|
+
app = ServerApp()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.main()
|
|
15
|
+
def main(grid: Grid, context: Context) -> None:
|
|
16
|
+
"""Main entry point for the ServerApp."""
|
|
17
|
+
|
|
18
|
+
# Read run config
|
|
19
|
+
fraction_train: float = context.run_config["fraction-train"]
|
|
20
|
+
num_rounds: int = context.run_config["num-server-rounds"]
|
|
21
|
+
lr: float = context.run_config["lr"]
|
|
22
|
+
|
|
23
|
+
# Load global model
|
|
24
|
+
global_model = Net()
|
|
25
|
+
arrays = ArrayRecord(global_model.state_dict())
|
|
26
|
+
|
|
27
|
+
# Initialize FedAvg strategy
|
|
28
|
+
strategy = FedAvg(fraction_train=fraction_train)
|
|
29
|
+
|
|
30
|
+
# Start strategy, run FedAvg for `num_rounds`
|
|
31
|
+
result = strategy.start(
|
|
32
|
+
grid=grid,
|
|
33
|
+
initial_arrays=arrays,
|
|
34
|
+
train_config=ConfigRecord({"lr": lr}),
|
|
35
|
+
num_rounds=num_rounds,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Save final model to disk
|
|
39
|
+
print("\nSaving final model to disk...")
|
|
40
|
+
state_dict = result.arrays.to_torch_state_dict()
|
|
41
|
+
torch.save(state_dict, "final_model.pt")
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
+
|
|
3
|
+
from flwr.common import Context, ndarrays_to_parameters
|
|
4
|
+
from flwr.server import ServerApp, ServerAppComponents, ServerConfig
|
|
5
|
+
from flwr.server.strategy import FedAvg
|
|
6
|
+
from $import_name.task import Net, get_weights
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def server_fn(context: Context):
|
|
10
|
+
# Read from config
|
|
11
|
+
num_rounds = context.run_config["num-server-rounds"]
|
|
12
|
+
fraction_fit = context.run_config["fraction-fit"]
|
|
13
|
+
|
|
14
|
+
# Initialize model parameters
|
|
15
|
+
ndarrays = get_weights(Net())
|
|
16
|
+
parameters = ndarrays_to_parameters(ndarrays)
|
|
17
|
+
|
|
18
|
+
# Define strategy
|
|
19
|
+
strategy = FedAvg(
|
|
20
|
+
fraction_fit=fraction_fit,
|
|
21
|
+
fraction_evaluate=1.0,
|
|
22
|
+
min_available_clients=2,
|
|
23
|
+
initial_parameters=parameters,
|
|
24
|
+
)
|
|
25
|
+
config = ServerConfig(num_rounds=num_rounds)
|
|
26
|
+
|
|
27
|
+
return ServerAppComponents(strategy=strategy, config=config)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# Create ServerApp
|
|
31
|
+
app = ServerApp(server_fn=server_fn)
|
|
@@ -1,36 +1,44 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
from flwr.
|
|
5
|
-
from flwr.
|
|
6
|
-
from
|
|
3
|
+
import joblib
|
|
4
|
+
from flwr.app import ArrayRecord, Context
|
|
5
|
+
from flwr.serverapp import Grid, ServerApp
|
|
6
|
+
from flwr.serverapp.strategy import FedAvg
|
|
7
7
|
|
|
8
|
+
from $import_name.task import get_model, get_model_params, set_initial_params, set_model_params
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
# Create ServerApp
|
|
11
|
+
app = ServerApp()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@app.main()
|
|
15
|
+
def main(grid: Grid, context: Context) -> None:
|
|
16
|
+
"""Main entry point for the ServerApp."""
|
|
17
|
+
|
|
18
|
+
# Read run config
|
|
19
|
+
num_rounds: int = context.run_config["num-server-rounds"]
|
|
12
20
|
|
|
13
21
|
# Create LogisticRegression Model
|
|
14
22
|
penalty = context.run_config["penalty"]
|
|
15
23
|
local_epochs = context.run_config["local-epochs"]
|
|
16
24
|
model = get_model(penalty, local_epochs)
|
|
17
|
-
|
|
18
25
|
# Setting initial parameters, akin to model.compile for keras models
|
|
19
26
|
set_initial_params(model)
|
|
27
|
+
# Construct ArrayRecord representation
|
|
28
|
+
arrays = ArrayRecord(get_model_params(model))
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
# Initialize FedAvg strategy
|
|
31
|
+
strategy = FedAvg(fraction_train=1.0, fraction_evaluate=1.0)
|
|
22
32
|
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
initial_parameters=initial_parameters,
|
|
33
|
+
# Start strategy, run FedAvg for `num_rounds`
|
|
34
|
+
result = strategy.start(
|
|
35
|
+
grid=grid,
|
|
36
|
+
initial_arrays=arrays,
|
|
37
|
+
num_rounds=num_rounds,
|
|
29
38
|
)
|
|
30
|
-
config = ServerConfig(num_rounds=num_rounds)
|
|
31
|
-
|
|
32
|
-
return ServerAppComponents(strategy=strategy, config=config)
|
|
33
39
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
# Save final model parameters
|
|
41
|
+
print("\nSaving final model to disk...")
|
|
42
|
+
ndarrays = result.arrays.to_numpy_ndarrays()
|
|
43
|
+
set_model_params(model, ndarrays)
|
|
44
|
+
joblib.dump(model, "logreg_model.pkl")
|
|
@@ -1,29 +1,38 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
-
from flwr.
|
|
4
|
-
from flwr.
|
|
5
|
-
from flwr.
|
|
3
|
+
from flwr.app import ArrayRecord, Context
|
|
4
|
+
from flwr.serverapp import Grid, ServerApp
|
|
5
|
+
from flwr.serverapp.strategy import FedAvg
|
|
6
6
|
|
|
7
7
|
from $import_name.task import load_model
|
|
8
8
|
|
|
9
|
+
# Create ServerApp
|
|
10
|
+
app = ServerApp()
|
|
9
11
|
|
|
10
|
-
def server_fn(context: Context):
|
|
11
|
-
# Read from config
|
|
12
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
@app.main()
|
|
14
|
+
def main(grid: Grid, context: Context) -> None:
|
|
15
|
+
"""Main entry point for the ServerApp."""
|
|
16
16
|
|
|
17
|
-
#
|
|
18
|
-
|
|
19
|
-
fraction_fit=1.0,
|
|
20
|
-
fraction_evaluate=1.0,
|
|
21
|
-
min_available_clients=2,
|
|
22
|
-
initial_parameters=parameters,
|
|
23
|
-
)
|
|
24
|
-
config = ServerConfig(num_rounds=num_rounds)
|
|
17
|
+
# Read run config
|
|
18
|
+
num_rounds: int = context.run_config["num-server-rounds"]
|
|
25
19
|
|
|
26
|
-
|
|
20
|
+
# Load global model
|
|
21
|
+
model = load_model()
|
|
22
|
+
arrays = ArrayRecord(model.get_weights())
|
|
27
23
|
|
|
28
|
-
#
|
|
29
|
-
|
|
24
|
+
# Initialize FedAvg strategy
|
|
25
|
+
strategy = FedAvg(fraction_train=1.0, fraction_evaluate=1.0)
|
|
26
|
+
|
|
27
|
+
# Start strategy, run FedAvg for `num_rounds`
|
|
28
|
+
result = strategy.start(
|
|
29
|
+
grid=grid,
|
|
30
|
+
initial_arrays=arrays,
|
|
31
|
+
num_rounds=num_rounds,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# Save final model to disk
|
|
35
|
+
print("\nSaving final model to disk...")
|
|
36
|
+
ndarrays = result.arrays.to_numpy_ndarrays()
|
|
37
|
+
model.set_weights(ndarrays)
|
|
38
|
+
model.save("final_model.keras")
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import xgboost as xgb
|
|
5
|
+
from flwr.app import ArrayRecord, Context
|
|
6
|
+
from flwr.common.config import unflatten_dict
|
|
7
|
+
from flwr.serverapp import Grid, ServerApp
|
|
8
|
+
from flwr.serverapp.strategy import FedXgbBagging
|
|
9
|
+
|
|
10
|
+
from $import_name.task import replace_keys
|
|
11
|
+
|
|
12
|
+
# Create ServerApp
|
|
13
|
+
app = ServerApp()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@app.main()
|
|
17
|
+
def main(grid: Grid, context: Context) -> None:
|
|
18
|
+
# Read run config
|
|
19
|
+
num_rounds = context.run_config["num-server-rounds"]
|
|
20
|
+
fraction_train = context.run_config["fraction-train"]
|
|
21
|
+
fraction_evaluate = context.run_config["fraction-evaluate"]
|
|
22
|
+
# Flatted config dict and replace "-" with "_"
|
|
23
|
+
cfg = replace_keys(unflatten_dict(context.run_config))
|
|
24
|
+
params = cfg["params"]
|
|
25
|
+
|
|
26
|
+
# Init global model
|
|
27
|
+
# Init with an empty object; the XGBooster will be created
|
|
28
|
+
# and trained on the client side.
|
|
29
|
+
global_model = b""
|
|
30
|
+
# Note: we store the model as the first item in a list into ArrayRecord,
|
|
31
|
+
# which can be accessed using index ["0"].
|
|
32
|
+
arrays = ArrayRecord([np.frombuffer(global_model, dtype=np.uint8)])
|
|
33
|
+
|
|
34
|
+
# Initialize FedXgbBagging strategy
|
|
35
|
+
strategy = FedXgbBagging(
|
|
36
|
+
fraction_train=fraction_train,
|
|
37
|
+
fraction_evaluate=fraction_evaluate,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# Start strategy, run FedXgbBagging for `num_rounds`
|
|
41
|
+
result = strategy.start(
|
|
42
|
+
grid=grid,
|
|
43
|
+
initial_arrays=arrays,
|
|
44
|
+
num_rounds=num_rounds,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# Save final model to disk
|
|
48
|
+
bst = xgb.Booster(params=params)
|
|
49
|
+
global_model = bytearray(result.arrays["0"].numpy().tobytes())
|
|
50
|
+
|
|
51
|
+
# Load global model into booster
|
|
52
|
+
bst.load_model(global_model)
|
|
53
|
+
|
|
54
|
+
# Save model
|
|
55
|
+
print("\nSaving final model to disk...")
|
|
56
|
+
bst.save_model("final_model.json")
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
|
-
from collections import OrderedDict
|
|
5
4
|
|
|
6
5
|
import torch
|
|
7
6
|
import transformers
|
|
@@ -62,17 +61,24 @@ def load_data(partition_id: int, num_partitions: int, model_name: str):
|
|
|
62
61
|
return trainloader, testloader
|
|
63
62
|
|
|
64
63
|
|
|
65
|
-
def train(net, trainloader,
|
|
64
|
+
def train(net, trainloader, num_steps, device):
|
|
66
65
|
optimizer = AdamW(net.parameters(), lr=5e-5)
|
|
67
66
|
net.train()
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
running_loss = 0.0
|
|
68
|
+
step_cnt = 0
|
|
69
|
+
for batch in trainloader:
|
|
70
|
+
batch = {k: v.to(device) for k, v in batch.items()}
|
|
71
|
+
outputs = net(**batch)
|
|
72
|
+
loss = outputs.loss
|
|
73
|
+
loss.backward()
|
|
74
|
+
optimizer.step()
|
|
75
|
+
optimizer.zero_grad()
|
|
76
|
+
running_loss += loss.item()
|
|
77
|
+
step_cnt += 1
|
|
78
|
+
if step_cnt >= num_steps:
|
|
79
|
+
break
|
|
80
|
+
avg_trainloss = running_loss / step_cnt
|
|
81
|
+
return avg_trainloss
|
|
76
82
|
|
|
77
83
|
|
|
78
84
|
def test(net, testloader, device):
|
|
@@ -90,13 +96,3 @@ def test(net, testloader, device):
|
|
|
90
96
|
loss /= len(testloader.dataset)
|
|
91
97
|
accuracy = metric.compute()["accuracy"]
|
|
92
98
|
return loss, accuracy
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def get_weights(net):
|
|
96
|
-
return [val.cpu().numpy() for _, val in net.state_dict().items()]
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def set_weights(net, parameters):
|
|
100
|
-
params_dict = zip(net.state_dict().keys(), parameters)
|
|
101
|
-
state_dict = OrderedDict({k: torch.tensor(v) for k, v in params_dict})
|
|
102
|
-
net.load_state_dict(state_dict, strict=True)
|
|
@@ -31,7 +31,7 @@ def loss_fn(params, X, y):
|
|
|
31
31
|
def train(params, grad_fn, X, y):
|
|
32
32
|
loss = 1_000_000
|
|
33
33
|
num_examples = X.shape[0]
|
|
34
|
-
for
|
|
34
|
+
for _ in range(50):
|
|
35
35
|
grads = grad_fn(params, X, y)
|
|
36
36
|
params = jax.tree.map(lambda p, g: p - 0.05 * g, params, grads)
|
|
37
37
|
loss = loss_fn(params, X, y)
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
-
from collections import OrderedDict
|
|
4
|
-
|
|
5
3
|
import torch
|
|
6
4
|
import torch.nn as nn
|
|
7
5
|
import torch.nn.functional as F
|
|
@@ -34,6 +32,14 @@ class Net(nn.Module):
|
|
|
34
32
|
|
|
35
33
|
fds = None # Cache FederatedDataset
|
|
36
34
|
|
|
35
|
+
pytorch_transforms = Compose([ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def apply_transforms(batch):
|
|
39
|
+
"""Apply transforms to the partition from FederatedDataset."""
|
|
40
|
+
batch["img"] = [pytorch_transforms(img) for img in batch["img"]]
|
|
41
|
+
return batch
|
|
42
|
+
|
|
37
43
|
|
|
38
44
|
def load_data(partition_id: int, num_partitions: int):
|
|
39
45
|
"""Load partition CIFAR10 data."""
|
|
@@ -48,38 +54,29 @@ def load_data(partition_id: int, num_partitions: int):
|
|
|
48
54
|
partition = fds.load_partition(partition_id)
|
|
49
55
|
# Divide data on each node: 80% train, 20% test
|
|
50
56
|
partition_train_test = partition.train_test_split(test_size=0.2, seed=42)
|
|
51
|
-
|
|
52
|
-
[ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
def apply_transforms(batch):
|
|
56
|
-
"""Apply transforms to the partition from FederatedDataset."""
|
|
57
|
-
batch["img"] = [pytorch_transforms(img) for img in batch["img"]]
|
|
58
|
-
return batch
|
|
59
|
-
|
|
57
|
+
# Construct dataloaders
|
|
60
58
|
partition_train_test = partition_train_test.with_transform(apply_transforms)
|
|
61
59
|
trainloader = DataLoader(partition_train_test["train"], batch_size=32, shuffle=True)
|
|
62
60
|
testloader = DataLoader(partition_train_test["test"], batch_size=32)
|
|
63
61
|
return trainloader, testloader
|
|
64
62
|
|
|
65
63
|
|
|
66
|
-
def train(net, trainloader, epochs, device):
|
|
64
|
+
def train(net, trainloader, epochs, lr, device):
|
|
67
65
|
"""Train the model on the training set."""
|
|
68
66
|
net.to(device) # move model to GPU if available
|
|
69
67
|
criterion = torch.nn.CrossEntropyLoss().to(device)
|
|
70
|
-
optimizer = torch.optim.Adam(net.parameters(), lr=
|
|
68
|
+
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
|
|
71
69
|
net.train()
|
|
72
70
|
running_loss = 0.0
|
|
73
71
|
for _ in range(epochs):
|
|
74
72
|
for batch in trainloader:
|
|
75
|
-
images = batch["img"]
|
|
76
|
-
labels = batch["label"]
|
|
73
|
+
images = batch["img"].to(device)
|
|
74
|
+
labels = batch["label"].to(device)
|
|
77
75
|
optimizer.zero_grad()
|
|
78
|
-
loss = criterion(net(images
|
|
76
|
+
loss = criterion(net(images), labels)
|
|
79
77
|
loss.backward()
|
|
80
78
|
optimizer.step()
|
|
81
79
|
running_loss += loss.item()
|
|
82
|
-
|
|
83
80
|
avg_trainloss = running_loss / len(trainloader)
|
|
84
81
|
return avg_trainloss
|
|
85
82
|
|
|
@@ -99,13 +96,3 @@ def test(net, testloader, device):
|
|
|
99
96
|
accuracy = correct / len(testloader.dataset)
|
|
100
97
|
loss = loss / len(testloader)
|
|
101
98
|
return loss, accuracy
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def get_weights(net):
|
|
105
|
-
return [val.cpu().numpy() for _, val in net.state_dict().items()]
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
def set_weights(net, parameters):
|
|
109
|
-
params_dict = zip(net.state_dict().keys(), parameters)
|
|
110
|
-
state_dict = OrderedDict({k: torch.tensor(v) for k, v in params_dict})
|
|
111
|
-
net.load_state_dict(state_dict, strict=True)
|
flwr/cli/new/templates/app/code/{task.pytorch_msg_api.py.tpl → task.pytorch_legacy_api.py.tpl}
RENAMED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""$project_name: A Flower / $framework_str app."""
|
|
2
2
|
|
|
3
|
+
from collections import OrderedDict
|
|
4
|
+
|
|
3
5
|
import torch
|
|
4
6
|
import torch.nn as nn
|
|
5
7
|
import torch.nn.functional as F
|
|
@@ -32,14 +34,6 @@ class Net(nn.Module):
|
|
|
32
34
|
|
|
33
35
|
fds = None # Cache FederatedDataset
|
|
34
36
|
|
|
35
|
-
pytorch_transforms = Compose([ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def apply_transforms(batch):
|
|
39
|
-
"""Apply transforms to the partition from FederatedDataset."""
|
|
40
|
-
batch["img"] = [pytorch_transforms(img) for img in batch["img"]]
|
|
41
|
-
return batch
|
|
42
|
-
|
|
43
37
|
|
|
44
38
|
def load_data(partition_id: int, num_partitions: int):
|
|
45
39
|
"""Load partition CIFAR10 data."""
|
|
@@ -54,29 +48,38 @@ def load_data(partition_id: int, num_partitions: int):
|
|
|
54
48
|
partition = fds.load_partition(partition_id)
|
|
55
49
|
# Divide data on each node: 80% train, 20% test
|
|
56
50
|
partition_train_test = partition.train_test_split(test_size=0.2, seed=42)
|
|
57
|
-
|
|
51
|
+
pytorch_transforms = Compose(
|
|
52
|
+
[ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def apply_transforms(batch):
|
|
56
|
+
"""Apply transforms to the partition from FederatedDataset."""
|
|
57
|
+
batch["img"] = [pytorch_transforms(img) for img in batch["img"]]
|
|
58
|
+
return batch
|
|
59
|
+
|
|
58
60
|
partition_train_test = partition_train_test.with_transform(apply_transforms)
|
|
59
61
|
trainloader = DataLoader(partition_train_test["train"], batch_size=32, shuffle=True)
|
|
60
62
|
testloader = DataLoader(partition_train_test["test"], batch_size=32)
|
|
61
63
|
return trainloader, testloader
|
|
62
64
|
|
|
63
65
|
|
|
64
|
-
def train(net, trainloader, epochs,
|
|
66
|
+
def train(net, trainloader, epochs, device):
|
|
65
67
|
"""Train the model on the training set."""
|
|
66
68
|
net.to(device) # move model to GPU if available
|
|
67
69
|
criterion = torch.nn.CrossEntropyLoss().to(device)
|
|
68
|
-
optimizer = torch.optim.Adam(net.parameters(), lr=
|
|
70
|
+
optimizer = torch.optim.Adam(net.parameters(), lr=0.01)
|
|
69
71
|
net.train()
|
|
70
72
|
running_loss = 0.0
|
|
71
73
|
for _ in range(epochs):
|
|
72
74
|
for batch in trainloader:
|
|
73
|
-
images = batch["img"]
|
|
74
|
-
labels = batch["label"]
|
|
75
|
+
images = batch["img"]
|
|
76
|
+
labels = batch["label"]
|
|
75
77
|
optimizer.zero_grad()
|
|
76
|
-
loss = criterion(net(images), labels)
|
|
78
|
+
loss = criterion(net(images.to(device)), labels.to(device))
|
|
77
79
|
loss.backward()
|
|
78
80
|
optimizer.step()
|
|
79
81
|
running_loss += loss.item()
|
|
82
|
+
|
|
80
83
|
avg_trainloss = running_loss / len(trainloader)
|
|
81
84
|
return avg_trainloss
|
|
82
85
|
|
|
@@ -96,3 +99,13 @@ def test(net, testloader, device):
|
|
|
96
99
|
accuracy = correct / len(testloader.dataset)
|
|
97
100
|
loss = loss / len(testloader)
|
|
98
101
|
return loss, accuracy
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get_weights(net):
|
|
105
|
+
return [val.cpu().numpy() for _, val in net.state_dict().items()]
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def set_weights(net, parameters):
|
|
109
|
+
params_dict = zip(net.state_dict().keys(), parameters)
|
|
110
|
+
state_dict = OrderedDict({k: torch.tensor(v) for k, v in params_dict})
|
|
111
|
+
net.load_state_dict(state_dict, strict=True)
|
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
import os
|
|
4
4
|
|
|
5
5
|
import keras
|
|
6
|
-
from keras import layers
|
|
7
6
|
from flwr_datasets import FederatedDataset
|
|
8
7
|
from flwr_datasets.partitioner import IidPartitioner
|
|
9
|
-
|
|
8
|
+
from keras import layers
|
|
10
9
|
|
|
11
10
|
# Make TensorFlow log less verbose
|
|
12
11
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|