flwr 1.24.0__py3-none-any.whl → 1.25.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_cmd/review.py +13 -3
- flwr/cli/federation/show.py +4 -3
- flwr/cli/ls.py +44 -3
- flwr/cli/new/new.py +106 -297
- flwr/cli/run/run.py +12 -17
- flwr/cli/run_utils.py +23 -5
- flwr/cli/stop.py +1 -1
- flwr/cli/supernode/ls.py +10 -5
- flwr/cli/utils.py +0 -137
- flwr/client/grpc_adapter_client/connection.py +2 -2
- flwr/client/grpc_rere_client/connection.py +6 -3
- flwr/client/rest_client/connection.py +6 -4
- flwr/common/serde.py +6 -0
- flwr/common/typing.py +6 -0
- flwr/proto/fleet_pb2.py +10 -10
- flwr/proto/fleet_pb2.pyi +5 -1
- flwr/proto/run_pb2.py +24 -24
- flwr/proto/run_pb2.pyi +10 -1
- flwr/server/app.py +1 -0
- flwr/server/superlink/fleet/message_handler/message_handler.py +41 -2
- flwr/server/superlink/linkstate/in_memory_linkstate.py +34 -0
- flwr/server/superlink/linkstate/linkstate.py +32 -0
- flwr/server/superlink/linkstate/sqlite_linkstate.py +60 -3
- flwr/supercore/constant.py +3 -0
- flwr/supercore/utils.py +190 -0
- flwr/superlink/servicer/control/control_grpc.py +2 -0
- flwr/superlink/servicer/control/control_servicer.py +88 -5
- flwr/supernode/nodestate/in_memory_nodestate.py +62 -1
- flwr/supernode/nodestate/nodestate.py +45 -0
- flwr/supernode/servicer/clientappio/clientappio_servicer.py +7 -1
- flwr/supernode/start_client_internal.py +7 -4
- {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/METADATA +2 -4
- {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/RECORD +35 -96
- flwr/cli/new/templates/__init__.py +0 -15
- flwr/cli/new/templates/app/.gitignore.tpl +0 -163
- flwr/cli/new/templates/app/LICENSE.tpl +0 -202
- flwr/cli/new/templates/app/README.baseline.md.tpl +0 -127
- flwr/cli/new/templates/app/README.flowertune.md.tpl +0 -68
- flwr/cli/new/templates/app/README.md.tpl +0 -37
- flwr/cli/new/templates/app/__init__.py +0 -15
- flwr/cli/new/templates/app/code/__init__.baseline.py.tpl +0 -1
- flwr/cli/new/templates/app/code/__init__.py +0 -15
- flwr/cli/new/templates/app/code/__init__.py.tpl +0 -1
- flwr/cli/new/templates/app/code/__init__.pytorch_legacy_api.py.tpl +0 -1
- flwr/cli/new/templates/app/code/client.baseline.py.tpl +0 -75
- flwr/cli/new/templates/app/code/client.huggingface.py.tpl +0 -93
- flwr/cli/new/templates/app/code/client.jax.py.tpl +0 -71
- flwr/cli/new/templates/app/code/client.mlx.py.tpl +0 -102
- flwr/cli/new/templates/app/code/client.numpy.py.tpl +0 -46
- flwr/cli/new/templates/app/code/client.pytorch.py.tpl +0 -80
- flwr/cli/new/templates/app/code/client.pytorch_legacy_api.py.tpl +0 -55
- flwr/cli/new/templates/app/code/client.sklearn.py.tpl +0 -108
- flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +0 -82
- flwr/cli/new/templates/app/code/client.xgboost.py.tpl +0 -110
- flwr/cli/new/templates/app/code/dataset.baseline.py.tpl +0 -36
- flwr/cli/new/templates/app/code/flwr_tune/__init__.py +0 -15
- flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +0 -92
- flwr/cli/new/templates/app/code/flwr_tune/dataset.py.tpl +0 -87
- flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +0 -56
- flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +0 -73
- flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +0 -78
- flwr/cli/new/templates/app/code/model.baseline.py.tpl +0 -66
- flwr/cli/new/templates/app/code/server.baseline.py.tpl +0 -43
- flwr/cli/new/templates/app/code/server.huggingface.py.tpl +0 -42
- flwr/cli/new/templates/app/code/server.jax.py.tpl +0 -39
- flwr/cli/new/templates/app/code/server.mlx.py.tpl +0 -41
- flwr/cli/new/templates/app/code/server.numpy.py.tpl +0 -38
- flwr/cli/new/templates/app/code/server.pytorch.py.tpl +0 -41
- flwr/cli/new/templates/app/code/server.pytorch_legacy_api.py.tpl +0 -31
- flwr/cli/new/templates/app/code/server.sklearn.py.tpl +0 -44
- flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +0 -38
- flwr/cli/new/templates/app/code/server.xgboost.py.tpl +0 -56
- flwr/cli/new/templates/app/code/strategy.baseline.py.tpl +0 -1
- flwr/cli/new/templates/app/code/task.huggingface.py.tpl +0 -98
- flwr/cli/new/templates/app/code/task.jax.py.tpl +0 -57
- flwr/cli/new/templates/app/code/task.mlx.py.tpl +0 -102
- flwr/cli/new/templates/app/code/task.numpy.py.tpl +0 -7
- flwr/cli/new/templates/app/code/task.pytorch.py.tpl +0 -99
- flwr/cli/new/templates/app/code/task.pytorch_legacy_api.py.tpl +0 -111
- flwr/cli/new/templates/app/code/task.sklearn.py.tpl +0 -67
- flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +0 -52
- flwr/cli/new/templates/app/code/task.xgboost.py.tpl +0 -67
- flwr/cli/new/templates/app/code/utils.baseline.py.tpl +0 -1
- flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +0 -146
- flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +0 -80
- flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +0 -65
- flwr/cli/new/templates/app/pyproject.jax.toml.tpl +0 -52
- flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +0 -56
- flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +0 -49
- flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +0 -53
- flwr/cli/new/templates/app/pyproject.pytorch_legacy_api.toml.tpl +0 -53
- flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +0 -52
- flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +0 -53
- flwr/cli/new/templates/app/pyproject.xgboost.toml.tpl +0 -61
- {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/WHEEL +0 -0
- {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower Baseline."""
|
|
2
|
-
|
|
3
|
-
import torch
|
|
4
|
-
import torch.nn.functional as F
|
|
5
|
-
from torch import nn
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class Net(nn.Module):
|
|
9
|
-
"""Model (simple CNN adapted from 'PyTorch: A 60 Minute Blitz')."""
|
|
10
|
-
|
|
11
|
-
def __init__(self):
|
|
12
|
-
super().__init__()
|
|
13
|
-
self.conv1 = nn.Conv2d(3, 6, 5)
|
|
14
|
-
self.pool = nn.MaxPool2d(2, 2)
|
|
15
|
-
self.conv2 = nn.Conv2d(6, 16, 5)
|
|
16
|
-
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
|
17
|
-
self.fc2 = nn.Linear(120, 84)
|
|
18
|
-
self.fc3 = nn.Linear(84, 10)
|
|
19
|
-
|
|
20
|
-
def forward(self, x):
|
|
21
|
-
"""Do forward."""
|
|
22
|
-
x = self.pool(F.relu(self.conv1(x)))
|
|
23
|
-
x = self.pool(F.relu(self.conv2(x)))
|
|
24
|
-
x = x.view(-1, 16 * 5 * 5)
|
|
25
|
-
x = F.relu(self.fc1(x))
|
|
26
|
-
x = F.relu(self.fc2(x))
|
|
27
|
-
return self.fc3(x)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def train(net, trainloader, epochs, device):
|
|
31
|
-
"""Train the model on the training set."""
|
|
32
|
-
net.to(device) # move model to GPU if available
|
|
33
|
-
criterion = torch.nn.CrossEntropyLoss()
|
|
34
|
-
criterion.to(device)
|
|
35
|
-
optimizer = torch.optim.SGD(net.parameters(), lr=0.1, momentum=0.9)
|
|
36
|
-
net.train()
|
|
37
|
-
running_loss = 0.0
|
|
38
|
-
for _ in range(epochs):
|
|
39
|
-
for batch in trainloader:
|
|
40
|
-
images = batch["img"]
|
|
41
|
-
labels = batch["label"]
|
|
42
|
-
optimizer.zero_grad()
|
|
43
|
-
loss = criterion(net(images.to(device)), labels.to(device))
|
|
44
|
-
loss.backward()
|
|
45
|
-
optimizer.step()
|
|
46
|
-
running_loss += loss.item()
|
|
47
|
-
|
|
48
|
-
avg_trainloss = running_loss / len(trainloader)
|
|
49
|
-
return avg_trainloss
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
def test(net, testloader, device):
|
|
53
|
-
"""Validate the model on the test set."""
|
|
54
|
-
net.to(device)
|
|
55
|
-
criterion = torch.nn.CrossEntropyLoss()
|
|
56
|
-
correct, loss = 0, 0.0
|
|
57
|
-
with torch.no_grad():
|
|
58
|
-
for batch in testloader:
|
|
59
|
-
images = batch["img"].to(device)
|
|
60
|
-
labels = batch["label"].to(device)
|
|
61
|
-
outputs = net(images)
|
|
62
|
-
loss += criterion(outputs, labels).item()
|
|
63
|
-
correct += (torch.max(outputs.data, 1)[1] == labels).sum().item()
|
|
64
|
-
accuracy = correct / len(testloader.dataset)
|
|
65
|
-
loss = loss / len(testloader)
|
|
66
|
-
return loss, accuracy
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower Baseline."""
|
|
2
|
-
|
|
3
|
-
import torch
|
|
4
|
-
from flwr.app import ArrayRecord, Context
|
|
5
|
-
from flwr.serverapp import Grid, ServerApp
|
|
6
|
-
from flwr.serverapp.strategy import FedAvg
|
|
7
|
-
|
|
8
|
-
from $import_name.model import Net
|
|
9
|
-
|
|
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 from config
|
|
19
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
20
|
-
fraction_train = context.run_config["fraction-train"]
|
|
21
|
-
|
|
22
|
-
# Load global model
|
|
23
|
-
global_model = Net()
|
|
24
|
-
arrays = ArrayRecord(global_model.state_dict())
|
|
25
|
-
|
|
26
|
-
# Initialize FedAvg strategy
|
|
27
|
-
strategy = FedAvg(
|
|
28
|
-
fraction_train=fraction_train,
|
|
29
|
-
fraction_evaluate=1.0,
|
|
30
|
-
min_available_nodes=2,
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
# Start strategy, run FedAvg for `num_rounds`
|
|
34
|
-
result = strategy.start(
|
|
35
|
-
grid=grid,
|
|
36
|
-
initial_arrays=arrays,
|
|
37
|
-
num_rounds=num_rounds,
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
# Save final model to disk
|
|
41
|
-
print("\nSaving final model to disk...")
|
|
42
|
-
state_dict = result.arrays.to_torch_state_dict()
|
|
43
|
-
torch.save(state_dict, "final_model.pt")
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
3
|
-
import torch
|
|
4
|
-
from flwr.app import ArrayRecord, Context
|
|
5
|
-
from flwr.serverapp import Grid, ServerApp
|
|
6
|
-
from flwr.serverapp.strategy import FedAvg
|
|
7
|
-
from transformers import AutoModelForSequenceClassification
|
|
8
|
-
|
|
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."""
|
|
16
|
-
|
|
17
|
-
# Read from config
|
|
18
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
19
|
-
fraction_train = context.run_config["fraction-train"]
|
|
20
|
-
|
|
21
|
-
# Initialize global model
|
|
22
|
-
model_name = context.run_config["model-name"]
|
|
23
|
-
num_labels = context.run_config["num-labels"]
|
|
24
|
-
net = AutoModelForSequenceClassification.from_pretrained(
|
|
25
|
-
model_name, num_labels=num_labels
|
|
26
|
-
)
|
|
27
|
-
arrays = ArrayRecord(net.state_dict())
|
|
28
|
-
|
|
29
|
-
# Initialize FedAvg strategy
|
|
30
|
-
strategy = FedAvg(fraction_train=fraction_train)
|
|
31
|
-
|
|
32
|
-
# Start strategy, run FedAvg for `num_rounds`
|
|
33
|
-
result = strategy.start(
|
|
34
|
-
grid=grid,
|
|
35
|
-
initial_arrays=arrays,
|
|
36
|
-
num_rounds=num_rounds,
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
# Save final model to disk
|
|
40
|
-
print("\nSaving final model to disk...")
|
|
41
|
-
state_dict = result.arrays.to_torch_state_dict()
|
|
42
|
-
torch.save(state_dict, "final_model.pt")
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
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
|
|
7
|
-
|
|
8
|
-
from $import_name.task import get_params, load_model
|
|
9
|
-
|
|
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 from config
|
|
19
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
20
|
-
input_dim = context.run_config["input-dim"]
|
|
21
|
-
|
|
22
|
-
# Load global model
|
|
23
|
-
model = load_model((input_dim,))
|
|
24
|
-
arrays = ArrayRecord(get_params(model))
|
|
25
|
-
|
|
26
|
-
# Initialize FedAvg strategy
|
|
27
|
-
strategy = FedAvg()
|
|
28
|
-
|
|
29
|
-
# Start strategy, run FedAvg for `num_rounds`
|
|
30
|
-
result = strategy.start(
|
|
31
|
-
grid=grid,
|
|
32
|
-
initial_arrays=arrays,
|
|
33
|
-
num_rounds=num_rounds,
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
# Save final model to disk
|
|
37
|
-
print("\nSaving final model to disk...")
|
|
38
|
-
ndarrays = result.arrays.to_numpy_ndarrays()
|
|
39
|
-
np.savez("final_model.npz", *ndarrays)
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
3
|
-
from flwr.app import ArrayRecord, Context
|
|
4
|
-
from flwr.serverapp import Grid, ServerApp
|
|
5
|
-
from flwr.serverapp.strategy import FedAvg
|
|
6
|
-
|
|
7
|
-
from $import_name.task import MLP, get_params, set_params
|
|
8
|
-
|
|
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."""
|
|
16
|
-
# Read from config
|
|
17
|
-
num_rounds = context.run_config["num-server-rounds"]
|
|
18
|
-
num_layers = context.run_config["num-layers"]
|
|
19
|
-
input_dim = context.run_config["input-dim"]
|
|
20
|
-
hidden_dim = context.run_config["hidden-dim"]
|
|
21
|
-
|
|
22
|
-
# Initialize global model
|
|
23
|
-
model = MLP(num_layers, input_dim, hidden_dim, output_dim=10)
|
|
24
|
-
params = get_params(model)
|
|
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,38 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
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
|
|
7
|
-
|
|
8
|
-
from $import_name.task import get_dummy_model
|
|
9
|
-
|
|
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"]
|
|
20
|
-
|
|
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,41 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
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
|
|
7
|
-
|
|
8
|
-
from $import_name.task import Net
|
|
9
|
-
|
|
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
|
-
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")
|
|
@@ -1,31 +0,0 @@
|
|
|
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,44 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
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
|
-
|
|
8
|
-
from $import_name.task import get_model, get_model_params, set_initial_params, set_model_params
|
|
9
|
-
|
|
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"]
|
|
20
|
-
|
|
21
|
-
# Create LogisticRegression Model
|
|
22
|
-
penalty = context.run_config["penalty"]
|
|
23
|
-
local_epochs = context.run_config["local-epochs"]
|
|
24
|
-
model = get_model(penalty, local_epochs)
|
|
25
|
-
# Setting initial parameters, akin to model.compile for keras models
|
|
26
|
-
set_initial_params(model)
|
|
27
|
-
# Construct ArrayRecord representation
|
|
28
|
-
arrays = ArrayRecord(get_model_params(model))
|
|
29
|
-
|
|
30
|
-
# Initialize FedAvg strategy
|
|
31
|
-
strategy = FedAvg(fraction_train=1.0, fraction_evaluate=1.0)
|
|
32
|
-
|
|
33
|
-
# Start strategy, run FedAvg for `num_rounds`
|
|
34
|
-
result = strategy.start(
|
|
35
|
-
grid=grid,
|
|
36
|
-
initial_arrays=arrays,
|
|
37
|
-
num_rounds=num_rounds,
|
|
38
|
-
)
|
|
39
|
-
|
|
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,38 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
3
|
-
from flwr.app import ArrayRecord, Context
|
|
4
|
-
from flwr.serverapp import Grid, ServerApp
|
|
5
|
-
from flwr.serverapp.strategy import FedAvg
|
|
6
|
-
|
|
7
|
-
from $import_name.task import load_model
|
|
8
|
-
|
|
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."""
|
|
16
|
-
|
|
17
|
-
# Read run config
|
|
18
|
-
num_rounds: int = context.run_config["num-server-rounds"]
|
|
19
|
-
|
|
20
|
-
# Load global model
|
|
21
|
-
model = load_model()
|
|
22
|
-
arrays = ArrayRecord(model.get_weights())
|
|
23
|
-
|
|
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")
|
|
@@ -1,56 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower Baseline."""
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
3
|
-
import warnings
|
|
4
|
-
|
|
5
|
-
import torch
|
|
6
|
-
import transformers
|
|
7
|
-
from datasets.utils.logging import disable_progress_bar
|
|
8
|
-
from evaluate import load as load_metric
|
|
9
|
-
from flwr_datasets import FederatedDataset
|
|
10
|
-
from flwr_datasets.partitioner import IidPartitioner
|
|
11
|
-
from torch.optim import AdamW
|
|
12
|
-
from torch.utils.data import DataLoader
|
|
13
|
-
from transformers import AutoTokenizer, DataCollatorWithPadding
|
|
14
|
-
|
|
15
|
-
warnings.filterwarnings("ignore", category=UserWarning)
|
|
16
|
-
warnings.filterwarnings("ignore", category=FutureWarning)
|
|
17
|
-
disable_progress_bar()
|
|
18
|
-
transformers.logging.set_verbosity_error()
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
fds = None # Cache FederatedDataset
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def load_data(partition_id: int, num_partitions: int, model_name: str):
|
|
25
|
-
"""Load IMDB data (training and eval)"""
|
|
26
|
-
# Only initialize `FederatedDataset` once
|
|
27
|
-
global fds
|
|
28
|
-
if fds is None:
|
|
29
|
-
partitioner = IidPartitioner(num_partitions=num_partitions)
|
|
30
|
-
fds = FederatedDataset(
|
|
31
|
-
dataset="stanfordnlp/imdb",
|
|
32
|
-
partitioners={"train": partitioner},
|
|
33
|
-
)
|
|
34
|
-
partition = fds.load_partition(partition_id)
|
|
35
|
-
# Divide data: 80% train, 20% test
|
|
36
|
-
partition_train_test = partition.train_test_split(test_size=0.2, seed=42)
|
|
37
|
-
|
|
38
|
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
39
|
-
|
|
40
|
-
def tokenize_function(examples):
|
|
41
|
-
return tokenizer(
|
|
42
|
-
examples["text"], truncation=True, add_special_tokens=True, max_length=512
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
partition_train_test = partition_train_test.map(tokenize_function, batched=True)
|
|
46
|
-
partition_train_test = partition_train_test.remove_columns("text")
|
|
47
|
-
partition_train_test = partition_train_test.rename_column("label", "labels")
|
|
48
|
-
|
|
49
|
-
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
|
|
50
|
-
trainloader = DataLoader(
|
|
51
|
-
partition_train_test["train"],
|
|
52
|
-
shuffle=True,
|
|
53
|
-
batch_size=32,
|
|
54
|
-
collate_fn=data_collator,
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
testloader = DataLoader(
|
|
58
|
-
partition_train_test["test"], batch_size=32, collate_fn=data_collator
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
return trainloader, testloader
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def train(net, trainloader, num_steps, device):
|
|
65
|
-
optimizer = AdamW(net.parameters(), lr=5e-5)
|
|
66
|
-
net.train()
|
|
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
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
def test(net, testloader, device):
|
|
85
|
-
metric = load_metric("accuracy")
|
|
86
|
-
loss = 0
|
|
87
|
-
net.eval()
|
|
88
|
-
for batch in testloader:
|
|
89
|
-
batch = {k: v.to(device) for k, v in batch.items()}
|
|
90
|
-
with torch.no_grad():
|
|
91
|
-
outputs = net(**batch)
|
|
92
|
-
logits = outputs.logits
|
|
93
|
-
loss += outputs.loss.item()
|
|
94
|
-
predictions = torch.argmax(logits, dim=-1)
|
|
95
|
-
metric.add_batch(predictions=predictions, references=batch["labels"])
|
|
96
|
-
loss /= len(testloader.dataset)
|
|
97
|
-
accuracy = metric.compute()["accuracy"]
|
|
98
|
-
return loss, accuracy
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
2
|
-
|
|
3
|
-
import jax
|
|
4
|
-
import jax.numpy as jnp
|
|
5
|
-
import numpy as np
|
|
6
|
-
from sklearn.datasets import make_regression
|
|
7
|
-
from sklearn.model_selection import train_test_split
|
|
8
|
-
|
|
9
|
-
key = jax.random.PRNGKey(0)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def load_data():
|
|
13
|
-
# Load dataset
|
|
14
|
-
X, y = make_regression(n_features=3, random_state=0)
|
|
15
|
-
X, X_test, y, y_test = train_test_split(X, y)
|
|
16
|
-
return X, y, X_test, y_test
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def load_model(model_shape):
|
|
20
|
-
# Extract model parameters
|
|
21
|
-
params = {"b": jax.random.uniform(key), "w": jax.random.uniform(key, model_shape)}
|
|
22
|
-
return params
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def loss_fn(params, X, y):
|
|
26
|
-
# Return MSE as loss
|
|
27
|
-
err = jnp.dot(X, params["w"]) + params["b"] - y
|
|
28
|
-
return jnp.mean(jnp.square(err))
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def train(params, grad_fn, X, y):
|
|
32
|
-
loss = 1_000_000
|
|
33
|
-
num_examples = X.shape[0]
|
|
34
|
-
for _ in range(50):
|
|
35
|
-
grads = grad_fn(params, X, y)
|
|
36
|
-
params = jax.tree.map(lambda p, g: p - 0.05 * g, params, grads)
|
|
37
|
-
loss = loss_fn(params, X, y)
|
|
38
|
-
return params, loss, num_examples
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
def evaluation(params, grad_fn, X_test, y_test):
|
|
42
|
-
num_examples = X_test.shape[0]
|
|
43
|
-
err_test = loss_fn(params, X_test, y_test)
|
|
44
|
-
loss_test = jnp.mean(jnp.square(err_test))
|
|
45
|
-
return loss_test, num_examples
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
def get_params(params):
|
|
49
|
-
parameters = []
|
|
50
|
-
for _, val in params.items():
|
|
51
|
-
parameters.append(np.array(val))
|
|
52
|
-
return parameters
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
def set_params(local_params, global_params):
|
|
56
|
-
for key, value in list(zip(local_params.keys(), global_params)):
|
|
57
|
-
local_params[key] = value
|