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.
Files changed (96) hide show
  1. flwr/cli/app_cmd/review.py +13 -3
  2. flwr/cli/federation/show.py +4 -3
  3. flwr/cli/ls.py +44 -3
  4. flwr/cli/new/new.py +106 -297
  5. flwr/cli/run/run.py +12 -17
  6. flwr/cli/run_utils.py +23 -5
  7. flwr/cli/stop.py +1 -1
  8. flwr/cli/supernode/ls.py +10 -5
  9. flwr/cli/utils.py +0 -137
  10. flwr/client/grpc_adapter_client/connection.py +2 -2
  11. flwr/client/grpc_rere_client/connection.py +6 -3
  12. flwr/client/rest_client/connection.py +6 -4
  13. flwr/common/serde.py +6 -0
  14. flwr/common/typing.py +6 -0
  15. flwr/proto/fleet_pb2.py +10 -10
  16. flwr/proto/fleet_pb2.pyi +5 -1
  17. flwr/proto/run_pb2.py +24 -24
  18. flwr/proto/run_pb2.pyi +10 -1
  19. flwr/server/app.py +1 -0
  20. flwr/server/superlink/fleet/message_handler/message_handler.py +41 -2
  21. flwr/server/superlink/linkstate/in_memory_linkstate.py +34 -0
  22. flwr/server/superlink/linkstate/linkstate.py +32 -0
  23. flwr/server/superlink/linkstate/sqlite_linkstate.py +60 -3
  24. flwr/supercore/constant.py +3 -0
  25. flwr/supercore/utils.py +190 -0
  26. flwr/superlink/servicer/control/control_grpc.py +2 -0
  27. flwr/superlink/servicer/control/control_servicer.py +88 -5
  28. flwr/supernode/nodestate/in_memory_nodestate.py +62 -1
  29. flwr/supernode/nodestate/nodestate.py +45 -0
  30. flwr/supernode/servicer/clientappio/clientappio_servicer.py +7 -1
  31. flwr/supernode/start_client_internal.py +7 -4
  32. {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/METADATA +2 -4
  33. {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/RECORD +35 -96
  34. flwr/cli/new/templates/__init__.py +0 -15
  35. flwr/cli/new/templates/app/.gitignore.tpl +0 -163
  36. flwr/cli/new/templates/app/LICENSE.tpl +0 -202
  37. flwr/cli/new/templates/app/README.baseline.md.tpl +0 -127
  38. flwr/cli/new/templates/app/README.flowertune.md.tpl +0 -68
  39. flwr/cli/new/templates/app/README.md.tpl +0 -37
  40. flwr/cli/new/templates/app/__init__.py +0 -15
  41. flwr/cli/new/templates/app/code/__init__.baseline.py.tpl +0 -1
  42. flwr/cli/new/templates/app/code/__init__.py +0 -15
  43. flwr/cli/new/templates/app/code/__init__.py.tpl +0 -1
  44. flwr/cli/new/templates/app/code/__init__.pytorch_legacy_api.py.tpl +0 -1
  45. flwr/cli/new/templates/app/code/client.baseline.py.tpl +0 -75
  46. flwr/cli/new/templates/app/code/client.huggingface.py.tpl +0 -93
  47. flwr/cli/new/templates/app/code/client.jax.py.tpl +0 -71
  48. flwr/cli/new/templates/app/code/client.mlx.py.tpl +0 -102
  49. flwr/cli/new/templates/app/code/client.numpy.py.tpl +0 -46
  50. flwr/cli/new/templates/app/code/client.pytorch.py.tpl +0 -80
  51. flwr/cli/new/templates/app/code/client.pytorch_legacy_api.py.tpl +0 -55
  52. flwr/cli/new/templates/app/code/client.sklearn.py.tpl +0 -108
  53. flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +0 -82
  54. flwr/cli/new/templates/app/code/client.xgboost.py.tpl +0 -110
  55. flwr/cli/new/templates/app/code/dataset.baseline.py.tpl +0 -36
  56. flwr/cli/new/templates/app/code/flwr_tune/__init__.py +0 -15
  57. flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +0 -92
  58. flwr/cli/new/templates/app/code/flwr_tune/dataset.py.tpl +0 -87
  59. flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +0 -56
  60. flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +0 -73
  61. flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +0 -78
  62. flwr/cli/new/templates/app/code/model.baseline.py.tpl +0 -66
  63. flwr/cli/new/templates/app/code/server.baseline.py.tpl +0 -43
  64. flwr/cli/new/templates/app/code/server.huggingface.py.tpl +0 -42
  65. flwr/cli/new/templates/app/code/server.jax.py.tpl +0 -39
  66. flwr/cli/new/templates/app/code/server.mlx.py.tpl +0 -41
  67. flwr/cli/new/templates/app/code/server.numpy.py.tpl +0 -38
  68. flwr/cli/new/templates/app/code/server.pytorch.py.tpl +0 -41
  69. flwr/cli/new/templates/app/code/server.pytorch_legacy_api.py.tpl +0 -31
  70. flwr/cli/new/templates/app/code/server.sklearn.py.tpl +0 -44
  71. flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +0 -38
  72. flwr/cli/new/templates/app/code/server.xgboost.py.tpl +0 -56
  73. flwr/cli/new/templates/app/code/strategy.baseline.py.tpl +0 -1
  74. flwr/cli/new/templates/app/code/task.huggingface.py.tpl +0 -98
  75. flwr/cli/new/templates/app/code/task.jax.py.tpl +0 -57
  76. flwr/cli/new/templates/app/code/task.mlx.py.tpl +0 -102
  77. flwr/cli/new/templates/app/code/task.numpy.py.tpl +0 -7
  78. flwr/cli/new/templates/app/code/task.pytorch.py.tpl +0 -99
  79. flwr/cli/new/templates/app/code/task.pytorch_legacy_api.py.tpl +0 -111
  80. flwr/cli/new/templates/app/code/task.sklearn.py.tpl +0 -67
  81. flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +0 -52
  82. flwr/cli/new/templates/app/code/task.xgboost.py.tpl +0 -67
  83. flwr/cli/new/templates/app/code/utils.baseline.py.tpl +0 -1
  84. flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +0 -146
  85. flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +0 -80
  86. flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +0 -65
  87. flwr/cli/new/templates/app/pyproject.jax.toml.tpl +0 -52
  88. flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +0 -56
  89. flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +0 -49
  90. flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +0 -53
  91. flwr/cli/new/templates/app/pyproject.pytorch_legacy_api.toml.tpl +0 -53
  92. flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +0 -52
  93. flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +0 -53
  94. flwr/cli/new/templates/app/pyproject.xgboost.toml.tpl +0 -61
  95. {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/WHEEL +0 -0
  96. {flwr-1.24.0.dist-info → flwr-1.25.0.dist-info}/entry_points.txt +0 -0
@@ -1,75 +0,0 @@
1
- """$project_name: A Flower Baseline."""
2
-
3
- import torch
4
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
5
- from flwr.clientapp import ClientApp
6
-
7
- from $import_name.dataset import load_data
8
- from $import_name.model import Net
9
- from $import_name.model import test as test_fn
10
- from $import_name.model import train as train_fn
11
-
12
- # Flower ClientApp
13
- app = ClientApp()
14
-
15
-
16
- @app.train()
17
- def train(msg: Message, context: Context):
18
- """Train the model on local data."""
19
-
20
- # Load the model and initialize it with the received weights
21
- model = Net()
22
- model.load_state_dict(msg.content["arrays"].to_torch_state_dict())
23
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
24
-
25
- # Load the data
26
- partition_id = int(context.node_config["partition-id"])
27
- num_partitions = int(context.node_config["num-partitions"])
28
- trainloader, _ = load_data(partition_id, num_partitions)
29
- local_epochs = context.run_config["local-epochs"]
30
-
31
- # Call the training function
32
- train_loss = train_fn(
33
- model,
34
- trainloader,
35
- local_epochs,
36
- device,
37
- )
38
-
39
- # Construct and return reply Message
40
- model_record = ArrayRecord(model.state_dict())
41
- metrics = {
42
- "train_loss": train_loss,
43
- "num-examples": len(trainloader.dataset),
44
- }
45
- metric_record = MetricRecord(metrics)
46
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
47
- return Message(content=content, reply_to=msg)
48
-
49
-
50
- @app.evaluate()
51
- def evaluate(msg: Message, context: Context):
52
- """Evaluate the model on local data."""
53
-
54
- # Load the model and initialize it with the received weights
55
- model = Net()
56
- model.load_state_dict(msg.content["arrays"].to_torch_state_dict())
57
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
58
-
59
- # Load the data
60
- partition_id = int(context.node_config["partition-id"])
61
- num_partitions = int(context.node_config["num-partitions"])
62
- _, valloader = load_data(partition_id, num_partitions)
63
-
64
- # Call the evaluation function
65
- eval_loss, eval_acc = test_fn(model, valloader, device)
66
-
67
- # Construct and return reply Message
68
- metrics = {
69
- "eval_loss": eval_loss,
70
- "eval_acc": eval_acc,
71
- "num-examples": len(valloader.dataset),
72
- }
73
- metric_record = MetricRecord(metrics)
74
- content = RecordDict({"metrics": metric_record})
75
- return Message(content=content, reply_to=msg)
@@ -1,93 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import torch
4
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
5
- from flwr.clientapp import ClientApp
6
- from transformers import AutoModelForSequenceClassification
7
-
8
- from $import_name.task import load_data
9
- from $import_name.task import test as test_fn
10
- from $import_name.task import train as train_fn
11
-
12
- # Flower ClientApp
13
- app = ClientApp()
14
-
15
-
16
- @app.train()
17
- def train(msg: Message, context: Context):
18
- """Train the model on local data."""
19
-
20
- # Get this client's dataset partition
21
- partition_id = context.node_config["partition-id"]
22
- num_partitions = context.node_config["num-partitions"]
23
- model_name = context.run_config["model-name"]
24
- trainloader, _ = load_data(partition_id, num_partitions, model_name)
25
-
26
- # Load model
27
- num_labels = context.run_config["num-labels"]
28
- net = AutoModelForSequenceClassification.from_pretrained(
29
- model_name, num_labels=num_labels
30
- )
31
-
32
- # Initialize it with the received weights
33
- net.load_state_dict(msg.content["arrays"].to_torch_state_dict())
34
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
35
- net.to(device)
36
-
37
- # Train the model on local data
38
- train_loss = train_fn(
39
- net,
40
- trainloader,
41
- context.run_config["local-steps"],
42
- device,
43
- )
44
-
45
- # Construct and return reply Message
46
- model_record = ArrayRecord(net.state_dict())
47
- metrics = {
48
- "train_loss": train_loss,
49
- "num-examples": len(trainloader.dataset),
50
- }
51
- metric_record = MetricRecord(metrics)
52
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
53
- return Message(content=content, reply_to=msg)
54
-
55
-
56
- @app.evaluate()
57
- def evaluate(msg: Message, context: Context):
58
- """Evaluate the model on local data."""
59
-
60
- # Get this client's dataset partition
61
- partition_id = context.node_config["partition-id"]
62
- num_partitions = context.node_config["num-partitions"]
63
- model_name = context.run_config["model-name"]
64
- _, valloader = load_data(partition_id, num_partitions, model_name)
65
-
66
- # Load model
67
- num_labels = context.run_config["num-labels"]
68
- net = AutoModelForSequenceClassification.from_pretrained(
69
- model_name, num_labels=num_labels
70
- )
71
-
72
- # Initialize it with the received weights
73
- net.load_state_dict(msg.content["arrays"].to_torch_state_dict())
74
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
75
- net.to(device)
76
-
77
- # Evaluate the model on local data
78
- val_loss, val_accuracy = test_fn(
79
- net,
80
- valloader,
81
- device,
82
- )
83
-
84
- # Construct and return reply Message
85
- model_record = ArrayRecord(net.state_dict())
86
- metrics = {
87
- "val_loss": val_loss,
88
- "val_accuracy": val_accuracy,
89
- "num-examples": len(valloader.dataset),
90
- }
91
- metric_record = MetricRecord(metrics)
92
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
93
- return Message(content=content, reply_to=msg)
@@ -1,71 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import jax
4
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
5
- from flwr.clientapp import ClientApp
6
-
7
- from $import_name.task import evaluation as evaluation_fn
8
- from $import_name.task import get_params, load_data, load_model, loss_fn, set_params
9
- from $import_name.task import train as train_fn
10
-
11
- # Flower ClientApp
12
- app = ClientApp()
13
-
14
-
15
- @app.train()
16
- def train(msg: Message, context: Context):
17
- """Train the model on local data."""
18
-
19
- # Read from config
20
- input_dim = context.run_config["input-dim"]
21
-
22
- # Load data and model
23
- train_x, train_y, _, _ = load_data()
24
- model = load_model((input_dim,))
25
- grad_fn = jax.grad(loss_fn)
26
-
27
- # Set model parameters
28
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
29
- set_params(model, ndarrays)
30
-
31
- # Train the model on local data
32
- model, loss, num_examples = train_fn(model, grad_fn, train_x, train_y)
33
-
34
- # Construct and return reply Message
35
- model_record = ArrayRecord(get_params(model))
36
- metrics = {
37
- "train_loss": float(loss),
38
- "num-examples": num_examples,
39
- }
40
- metric_record = MetricRecord(metrics)
41
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
42
- return Message(content=content, reply_to=msg)
43
-
44
-
45
- @app.evaluate()
46
- def evaluate(msg: Message, context: Context):
47
- """Evaluate the model on local data."""
48
-
49
- # Read from config
50
- input_dim = context.run_config["input-dim"]
51
-
52
- # Load data and model
53
- _, _, test_x, test_y = load_data()
54
- model = load_model((input_dim,))
55
- grad_fn = jax.grad(loss_fn)
56
-
57
- # Set model parameters
58
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
59
- set_params(model, ndarrays)
60
-
61
- # Evaluate the model on local data
62
- loss, num_examples = evaluation_fn(model, grad_fn, test_x, test_y)
63
-
64
- # Construct and return reply Message
65
- metrics = {
66
- "test_loss": float(loss),
67
- "num-examples": num_examples,
68
- }
69
- metric_record = MetricRecord(metrics)
70
- content = RecordDict({"metrics": metric_record})
71
- return Message(content=content, reply_to=msg)
@@ -1,102 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import mlx.core as mx
4
- import mlx.nn as nn
5
- import mlx.optimizers as optim
6
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
7
- from flwr.clientapp import ClientApp
8
-
9
- from $import_name.task import (
10
- MLP,
11
- batch_iterate,
12
- eval_fn,
13
- get_params,
14
- load_data,
15
- loss_fn,
16
- set_params,
17
- )
18
-
19
- # Flower ClientApp
20
- app = ClientApp()
21
-
22
-
23
- @app.train()
24
- def train(msg: Message, context: Context):
25
- """Train the model on local data."""
26
-
27
- # Read config
28
- num_layers = context.run_config["num-layers"]
29
- input_dim = context.run_config["input-dim"]
30
- hidden_dim = context.run_config["hidden-dim"]
31
- batch_size = context.run_config["batch-size"]
32
- learning_rate = context.run_config["lr"]
33
- num_epochs = context.run_config["local-epochs"]
34
-
35
- # Instantiate model and apply global parameters
36
- model = MLP(num_layers, input_dim, hidden_dim, output_dim=10)
37
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
38
- set_params(model, ndarrays)
39
-
40
- # Define optimizer and loss function
41
- optimizer = optim.SGD(learning_rate=learning_rate)
42
- loss_and_grad_fn = nn.value_and_grad(model, loss_fn)
43
-
44
- # Load data
45
- partition_id = context.node_config["partition-id"]
46
- num_partitions = context.node_config["num-partitions"]
47
- train_images, train_labels, _, _ = load_data(partition_id, num_partitions)
48
-
49
- # Train the model on local data
50
- for _ in range(num_epochs):
51
- for X, y in batch_iterate(batch_size, train_images, train_labels):
52
- _, grads = loss_and_grad_fn(model, X, y)
53
- optimizer.update(model, grads)
54
- mx.eval(model.parameters(), optimizer.state)
55
-
56
- # Compute train accuracy and loss
57
- accuracy = eval_fn(model, train_images, train_labels)
58
- loss = loss_fn(model, train_images, train_labels)
59
- # Construct and return reply Message
60
- model_record = ArrayRecord(get_params(model))
61
- metrics = {
62
- "num-examples": len(train_images),
63
- "accuracy": float(accuracy.item()),
64
- "loss": float(loss.item()),
65
- }
66
- metric_record = MetricRecord(metrics)
67
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
68
- return Message(content=content, reply_to=msg)
69
-
70
-
71
- @app.evaluate()
72
- def evaluate(msg: Message, context: Context):
73
- """Evaluate the model on local data."""
74
-
75
- # Read config
76
- num_layers = context.run_config["num-layers"]
77
- input_dim = context.run_config["input-dim"]
78
- hidden_dim = context.run_config["hidden-dim"]
79
-
80
- # Instantiate model and apply global parameters
81
- model = MLP(num_layers, input_dim, hidden_dim, output_dim=10)
82
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
83
- set_params(model, ndarrays)
84
-
85
- # Load data
86
- partition_id = context.node_config["partition-id"]
87
- num_partitions = context.node_config["num-partitions"]
88
- _, _, test_images, test_labels = load_data(partition_id, num_partitions)
89
-
90
- # Evaluate the model on local data
91
- accuracy = eval_fn(model, test_images, test_labels)
92
- loss = loss_fn(model, test_images, test_labels)
93
-
94
- # Construct and return reply Message
95
- metrics = {
96
- "num-examples": len(test_images),
97
- "accuracy": float(accuracy.item()),
98
- "loss": float(loss.item()),
99
- }
100
- metric_record = MetricRecord(metrics)
101
- content = RecordDict({"metrics": metric_record})
102
- return Message(content=content, reply_to=msg)
@@ -1,46 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import numpy as np
4
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
5
- from flwr.clientapp import ClientApp
6
-
7
- # Flower ClientApp
8
- app = ClientApp()
9
-
10
-
11
- @app.train()
12
- def train(msg: Message, context: Context):
13
- """Train the model on local data."""
14
-
15
- # The model is the global arrays
16
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
17
-
18
- # Simulate local training (here we just add random noise to model parameters)
19
- model = [m + np.random.rand(*m.shape) for m in ndarrays]
20
-
21
- # Construct and return reply Message
22
- model_record = ArrayRecord(model)
23
- metrics = {
24
- "random_metric": np.random.rand(),
25
- "num-examples": 1,
26
- }
27
- metric_record = MetricRecord(metrics)
28
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
29
- return Message(content=content, reply_to=msg)
30
-
31
-
32
- @app.evaluate()
33
- def evaluate(msg: Message, context: Context):
34
- """Evaluate the model on local data."""
35
-
36
- # The model is the global arrays
37
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
38
-
39
- # Return reply Message
40
- metrics = {
41
- "random_metric": np.random.rand(3).tolist(),
42
- "num-examples": 1,
43
- }
44
- metric_record = MetricRecord(metrics)
45
- content = RecordDict({"metrics": metric_record})
46
- return Message(content=content, reply_to=msg)
@@ -1,80 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import torch
4
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
5
- from flwr.clientapp import ClientApp
6
-
7
- from $import_name.task import Net, load_data
8
- from $import_name.task import test as test_fn
9
- from $import_name.task import train as train_fn
10
-
11
- # Flower ClientApp
12
- app = ClientApp()
13
-
14
-
15
- @app.train()
16
- def train(msg: Message, context: Context):
17
- """Train the model on local data."""
18
-
19
- # Load the model and initialize it with the received weights
20
- model = Net()
21
- model.load_state_dict(msg.content["arrays"].to_torch_state_dict())
22
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
23
- model.to(device)
24
-
25
- # Load the data
26
- partition_id = context.node_config["partition-id"]
27
- num_partitions = context.node_config["num-partitions"]
28
- trainloader, _ = load_data(partition_id, num_partitions)
29
-
30
- # Call the training function
31
- train_loss = train_fn(
32
- model,
33
- trainloader,
34
- context.run_config["local-epochs"],
35
- msg.content["config"]["lr"],
36
- device,
37
- )
38
-
39
- # Construct and return reply Message
40
- model_record = ArrayRecord(model.state_dict())
41
- metrics = {
42
- "train_loss": train_loss,
43
- "num-examples": len(trainloader.dataset),
44
- }
45
- metric_record = MetricRecord(metrics)
46
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
47
- return Message(content=content, reply_to=msg)
48
-
49
-
50
- @app.evaluate()
51
- def evaluate(msg: Message, context: Context):
52
- """Evaluate the model on local data."""
53
-
54
- # Load the model and initialize it with the received weights
55
- model = Net()
56
- model.load_state_dict(msg.content["arrays"].to_torch_state_dict())
57
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
58
- model.to(device)
59
-
60
- # Load the data
61
- partition_id = context.node_config["partition-id"]
62
- num_partitions = context.node_config["num-partitions"]
63
- _, valloader = load_data(partition_id, num_partitions)
64
-
65
- # Call the evaluation function
66
- eval_loss, eval_acc = test_fn(
67
- model,
68
- valloader,
69
- device,
70
- )
71
-
72
- # Construct and return reply Message
73
- metrics = {
74
- "eval_loss": eval_loss,
75
- "eval_acc": eval_acc,
76
- "num-examples": len(valloader.dataset),
77
- }
78
- metric_record = MetricRecord(metrics)
79
- content = RecordDict({"metrics": metric_record})
80
- return Message(content=content, reply_to=msg)
@@ -1,55 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import torch
4
-
5
- from flwr.client import ClientApp, NumPyClient
6
- from flwr.common import Context
7
- from $import_name.task import Net, get_weights, load_data, set_weights, test, train
8
-
9
-
10
- # Define Flower Client and client_fn
11
- class FlowerClient(NumPyClient):
12
- def __init__(self, net, trainloader, valloader, local_epochs):
13
- self.net = net
14
- self.trainloader = trainloader
15
- self.valloader = valloader
16
- self.local_epochs = local_epochs
17
- self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
18
- self.net.to(self.device)
19
-
20
- def fit(self, parameters, config):
21
- set_weights(self.net, parameters)
22
- train_loss = train(
23
- self.net,
24
- self.trainloader,
25
- self.local_epochs,
26
- self.device,
27
- )
28
- return (
29
- get_weights(self.net),
30
- len(self.trainloader.dataset),
31
- {"train_loss": train_loss},
32
- )
33
-
34
- def evaluate(self, parameters, config):
35
- set_weights(self.net, parameters)
36
- loss, accuracy = test(self.net, self.valloader, self.device)
37
- return loss, len(self.valloader.dataset), {"accuracy": accuracy}
38
-
39
-
40
- def client_fn(context: Context):
41
- # Load model and data
42
- net = Net()
43
- partition_id = context.node_config["partition-id"]
44
- num_partitions = context.node_config["num-partitions"]
45
- trainloader, valloader = load_data(partition_id, num_partitions)
46
- local_epochs = context.run_config["local-epochs"]
47
-
48
- # Return Client instance
49
- return FlowerClient(net, trainloader, valloader, local_epochs).to_client()
50
-
51
-
52
- # Flower ClientApp
53
- app = ClientApp(
54
- client_fn,
55
- )
@@ -1,108 +0,0 @@
1
- """$project_name: A Flower / $framework_str app."""
2
-
3
- import warnings
4
-
5
- from flwr.app import ArrayRecord, Context, Message, MetricRecord, RecordDict
6
- from flwr.clientapp import ClientApp
7
- from sklearn.metrics import (
8
- accuracy_score,
9
- f1_score,
10
- log_loss,
11
- precision_score,
12
- recall_score,
13
- )
14
-
15
- from $import_name.task import (
16
- get_model,
17
- get_model_params,
18
- load_data,
19
- set_initial_params,
20
- set_model_params,
21
- )
22
-
23
- # Flower ClientApp
24
- app = ClientApp()
25
-
26
-
27
- @app.train()
28
- def train(msg: Message, context: Context):
29
- """Train the model on local data."""
30
-
31
- # Create LogisticRegression Model
32
- penalty = context.run_config["penalty"]
33
- local_epochs = context.run_config["local-epochs"]
34
- model = get_model(penalty, local_epochs)
35
- # Setting initial parameters, akin to model.compile for keras models
36
- set_initial_params(model)
37
-
38
- # Apply received pararameters
39
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
40
- set_model_params(model, ndarrays)
41
-
42
- # Load the data
43
- partition_id = context.node_config["partition-id"]
44
- num_partitions = context.node_config["num-partitions"]
45
- X_train, _, y_train, _ = load_data(partition_id, num_partitions)
46
-
47
- # Ignore convergence failure due to low local epochs
48
- with warnings.catch_warnings():
49
- warnings.simplefilter("ignore")
50
- # Train the model on local data
51
- model.fit(X_train, y_train)
52
-
53
- # Let's compute train loss
54
- y_train_pred_proba = model.predict_proba(X_train)
55
- train_logloss = log_loss(y_train, y_train_pred_proba)
56
-
57
- # Construct and return reply Message
58
- ndarrays = get_model_params(model)
59
- model_record = ArrayRecord(ndarrays)
60
- metrics = {"num-examples": len(X_train), "train_logloss": train_logloss}
61
- metric_record = MetricRecord(metrics)
62
- content = RecordDict({"arrays": model_record, "metrics": metric_record})
63
- return Message(content=content, reply_to=msg)
64
-
65
-
66
- @app.evaluate()
67
- def evaluate(msg: Message, context: Context):
68
- """Evaluate the model on test data."""
69
-
70
- # Create LogisticRegression Model
71
- penalty = context.run_config["penalty"]
72
- local_epochs = context.run_config["local-epochs"]
73
- model = get_model(penalty, local_epochs)
74
-
75
- # Setting initial parameters, akin to model.compile for keras models
76
- set_initial_params(model)
77
-
78
- # Apply received pararameters
79
- ndarrays = msg.content["arrays"].to_numpy_ndarrays()
80
- set_model_params(model, ndarrays)
81
-
82
- # Load the data
83
- partition_id = context.node_config["partition-id"]
84
- num_partitions = context.node_config["num-partitions"]
85
- _, X_test, _, y_test = load_data(partition_id, num_partitions)
86
-
87
- # Evaluate the model on local data
88
- y_train_pred = model.predict(X_test)
89
- y_train_pred_proba = model.predict_proba(X_test)
90
-
91
- accuracy = accuracy_score(y_test, y_train_pred)
92
- loss = log_loss(y_test, y_train_pred_proba)
93
- precision = precision_score(y_test, y_train_pred, average="macro", zero_division=0)
94
- recall = recall_score(y_test, y_train_pred, average="macro", zero_division=0)
95
- f1 = f1_score(y_test, y_train_pred, average="macro", zero_division=0)
96
-
97
- # Construct and return reply Message
98
- metrics = {
99
- "num-examples": len(X_test),
100
- "test_logloss": loss,
101
- "accuracy": accuracy,
102
- "precision": precision,
103
- "recall": recall,
104
- "f1": f1,
105
- }
106
- metric_record = MetricRecord(metrics)
107
- content = RecordDict({"metrics": metric_record})
108
- return Message(content=content, reply_to=msg)