flwr 1.24.0__py3-none-any.whl → 1.26.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/__init__.py +1 -1
- flwr/app/__init__.py +4 -1
- flwr/app/message_type.py +29 -0
- flwr/app/metadata.py +5 -2
- flwr/app/user_config.py +19 -0
- flwr/cli/app.py +37 -19
- flwr/cli/app_cmd/publish.py +25 -75
- flwr/cli/app_cmd/review.py +25 -66
- flwr/cli/auth_plugin/auth_plugin.py +5 -10
- flwr/cli/auth_plugin/noop_auth_plugin.py +1 -2
- flwr/cli/auth_plugin/oidc_cli_plugin.py +38 -38
- flwr/cli/build.py +15 -28
- flwr/cli/config/__init__.py +21 -0
- flwr/cli/config/ls.py +71 -0
- flwr/cli/config_migration.py +297 -0
- flwr/cli/config_utils.py +63 -156
- flwr/cli/constant.py +71 -0
- flwr/cli/federation/__init__.py +0 -2
- flwr/cli/federation/ls.py +256 -64
- flwr/cli/flower_config.py +429 -0
- flwr/cli/install.py +23 -62
- flwr/cli/log.py +23 -37
- flwr/cli/login/login.py +29 -63
- flwr/cli/ls.py +72 -61
- flwr/cli/new/new.py +98 -309
- flwr/cli/pull.py +19 -37
- flwr/cli/run/run.py +87 -100
- flwr/cli/run_utils.py +23 -5
- flwr/cli/stop.py +33 -74
- flwr/cli/supernode/ls.py +35 -62
- flwr/cli/supernode/register.py +31 -80
- flwr/cli/supernode/unregister.py +24 -70
- flwr/cli/typing.py +200 -0
- flwr/cli/utils.py +160 -412
- flwr/client/grpc_adapter_client/connection.py +2 -2
- flwr/client/grpc_rere_client/connection.py +9 -6
- flwr/client/grpc_rere_client/grpc_adapter.py +1 -1
- flwr/client/message_handler/message_handler.py +2 -1
- flwr/client/mod/centraldp_mods.py +1 -1
- flwr/client/mod/localdp_mod.py +1 -1
- flwr/client/mod/secure_aggregation/secaggplus_mod.py +1 -1
- flwr/client/rest_client/connection.py +6 -4
- flwr/client/run_info_store.py +2 -1
- flwr/clientapp/client_app.py +2 -1
- flwr/common/__init__.py +3 -2
- flwr/common/args.py +5 -5
- flwr/common/config.py +12 -17
- flwr/common/constant.py +3 -16
- flwr/common/context.py +2 -1
- flwr/common/exit/exit.py +4 -4
- flwr/common/exit/exit_code.py +6 -0
- flwr/common/grpc.py +2 -1
- flwr/common/logger.py +1 -1
- flwr/common/message.py +1 -1
- flwr/common/retry_invoker.py +13 -5
- flwr/common/secure_aggregation/ndarrays_arithmetic.py +5 -2
- flwr/common/serde.py +13 -5
- flwr/common/telemetry.py +1 -1
- flwr/common/typing.py +10 -3
- flwr/compat/client/app.py +6 -9
- flwr/compat/client/grpc_client/connection.py +2 -1
- flwr/compat/common/constant.py +29 -0
- flwr/compat/server/app.py +1 -1
- flwr/proto/clientappio_pb2.py +2 -2
- flwr/proto/clientappio_pb2_grpc.py +104 -88
- flwr/proto/clientappio_pb2_grpc.pyi +140 -80
- flwr/proto/federation_pb2.py +5 -3
- flwr/proto/federation_pb2.pyi +32 -2
- flwr/proto/fleet_pb2.py +10 -10
- flwr/proto/fleet_pb2.pyi +5 -1
- flwr/proto/run_pb2.py +18 -26
- flwr/proto/run_pb2.pyi +10 -58
- flwr/proto/serverappio_pb2.py +2 -2
- flwr/proto/serverappio_pb2_grpc.py +138 -207
- flwr/proto/serverappio_pb2_grpc.pyi +189 -155
- flwr/proto/simulationio_pb2.py +2 -2
- flwr/proto/simulationio_pb2_grpc.py +62 -90
- flwr/proto/simulationio_pb2_grpc.pyi +95 -55
- flwr/server/app.py +7 -13
- flwr/server/compat/grid_client_proxy.py +2 -1
- flwr/server/grid/grpc_grid.py +5 -5
- flwr/server/serverapp/app.py +11 -4
- flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +1 -1
- flwr/server/superlink/fleet/grpc_rere/node_auth_server_interceptor.py +13 -12
- flwr/server/superlink/fleet/message_handler/message_handler.py +42 -2
- flwr/server/superlink/linkstate/__init__.py +2 -2
- flwr/server/superlink/linkstate/in_memory_linkstate.py +36 -10
- flwr/server/superlink/linkstate/linkstate.py +34 -21
- flwr/server/superlink/linkstate/linkstate_factory.py +16 -8
- flwr/server/superlink/linkstate/{sqlite_linkstate.py → sql_linkstate.py} +471 -516
- flwr/server/superlink/linkstate/utils.py +49 -2
- flwr/server/superlink/serverappio/serverappio_servicer.py +1 -33
- flwr/server/superlink/simulation/simulationio_servicer.py +0 -19
- flwr/server/utils/validator.py +1 -1
- flwr/server/workflow/default_workflows.py +2 -1
- flwr/server/workflow/secure_aggregation/secaggplus_workflow.py +1 -1
- flwr/serverapp/strategy/bulyan.py +7 -1
- flwr/serverapp/strategy/dp_fixed_clipping.py +9 -1
- flwr/serverapp/strategy/fedavg.py +1 -1
- flwr/serverapp/strategy/fedxgb_cyclic.py +1 -1
- flwr/simulation/ray_transport/ray_client_proxy.py +2 -6
- flwr/simulation/run_simulation.py +3 -12
- flwr/simulation/simulationio_connection.py +3 -3
- flwr/{common → supercore}/address.py +7 -33
- flwr/supercore/app_utils.py +2 -1
- flwr/supercore/constant.py +27 -2
- flwr/supercore/corestate/{sqlite_corestate.py → sql_corestate.py} +19 -23
- flwr/supercore/credential_store/__init__.py +33 -0
- flwr/supercore/credential_store/credential_store.py +34 -0
- flwr/supercore/credential_store/file_credential_store.py +76 -0
- flwr/{common → supercore}/date.py +0 -11
- flwr/supercore/ffs/disk_ffs.py +1 -1
- flwr/supercore/object_store/object_store_factory.py +14 -6
- flwr/supercore/object_store/{sqlite_object_store.py → sql_object_store.py} +115 -117
- flwr/supercore/sql_mixin.py +315 -0
- flwr/{cli/new/templates → supercore/state}/__init__.py +2 -2
- flwr/{cli/new/templates/app/code/flwr_tune → supercore/state/alembic}/__init__.py +2 -2
- flwr/supercore/state/alembic/env.py +103 -0
- flwr/supercore/state/alembic/script.py.mako +43 -0
- flwr/supercore/state/alembic/utils.py +239 -0
- flwr/{cli/new/templates/app → supercore/state/alembic/versions}/__init__.py +2 -2
- flwr/supercore/state/alembic/versions/rev_2026_01_28_initialize_migration_of_state_tables.py +200 -0
- flwr/supercore/state/schema/README.md +121 -0
- flwr/{cli/new/templates/app/code → supercore/state/schema}/__init__.py +2 -2
- flwr/supercore/state/schema/corestate_tables.py +36 -0
- flwr/supercore/state/schema/linkstate_tables.py +152 -0
- flwr/supercore/state/schema/objectstore_tables.py +90 -0
- flwr/supercore/superexec/run_superexec.py +2 -2
- flwr/supercore/utils.py +225 -0
- flwr/superlink/federation/federation_manager.py +2 -2
- flwr/superlink/federation/noop_federation_manager.py +8 -6
- flwr/superlink/servicer/control/control_grpc.py +2 -0
- flwr/superlink/servicer/control/control_servicer.py +106 -21
- flwr/supernode/cli/flower_supernode.py +2 -1
- flwr/supernode/nodestate/in_memory_nodestate.py +62 -1
- flwr/supernode/nodestate/nodestate.py +45 -0
- flwr/supernode/runtime/run_clientapp.py +14 -14
- flwr/supernode/servicer/clientappio/clientappio_servicer.py +13 -5
- flwr/supernode/start_client_internal.py +17 -10
- {flwr-1.24.0.dist-info → flwr-1.26.0.dist-info}/METADATA +8 -8
- {flwr-1.24.0.dist-info → flwr-1.26.0.dist-info}/RECORD +144 -184
- flwr/cli/federation/show.py +0 -317
- 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/code/__init__.baseline.py.tpl +0 -1
- 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/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/common/pyproject.py +0 -42
- flwr/supercore/sqlite_mixin.py +0 -159
- /flwr/{common → supercore}/version.py +0 -0
- {flwr-1.24.0.dist-info → flwr-1.26.0.dist-info}/WHEEL +0 -0
- {flwr-1.24.0.dist-info → flwr-1.26.0.dist-info}/entry_points.txt +0 -0
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: title of the paper # TODO
|
|
3
|
-
url: https://arxiv.org/abs/2007.14390 # TODO: update with the link to your paper
|
|
4
|
-
labels: [label1, label2] # TODO: please add between 4 and 10 single-word (maybe two-words) labels (e.g. system heterogeneity, image classification, asynchronous, weight sharing, cross-silo). Do not use "". Remove this comment once you are done.
|
|
5
|
-
dataset: [dataset1, dataset2] # TODO: list of datasets you include in your baseline. Do not use "". Remove this comment once you are done.
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
> [!IMPORTANT]
|
|
9
|
-
> This is the template for your `README.md`. Please fill-in the information in all areas with a :warning: symbol.
|
|
10
|
-
> Please refer to the [Flower Baselines contribution](https://flower.ai/docs/baselines/how-to-contribute-baselines.html) and [Flower Baselines usage](https://flower.ai/docs/baselines/how-to-use-baselines.html) guides for more details.
|
|
11
|
-
> Please complete the metadata section at the very top of this README. This generates a table at the top of the file that will facilitate indexing baselines.
|
|
12
|
-
> Please remove this [!IMPORTANT] block once you are done with your `README.md` as well as all the `:warning:` symbols and the comments next to them.
|
|
13
|
-
|
|
14
|
-
> [!IMPORTANT]
|
|
15
|
-
> To help having all baselines similarly formatted and structured, we have included two scripts in `baselines/dev` that when run will format your code and run some tests checking if it's formatted.
|
|
16
|
-
> These checks use standard packages such as `isort`, `black`, `pylint` and others. You as a baseline creator will need to install additional packages. These are already specified in the `pyproject.toml` of
|
|
17
|
-
> your baseline. Follow these steps:
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
# Create a python env
|
|
21
|
-
pyenv virtualenv 3.10.14 $project_name
|
|
22
|
-
|
|
23
|
-
# Activate it
|
|
24
|
-
pyenv activate $project_name
|
|
25
|
-
|
|
26
|
-
# Install project including developer packages
|
|
27
|
-
# Note the `-e` this means you install it in editable mode
|
|
28
|
-
# so even if you change the code you don't need to do `pip install`
|
|
29
|
-
# again. However, if you add a new dependency to `pyproject.toml` you
|
|
30
|
-
# will need to re-run the command below
|
|
31
|
-
pip install -e ".[dev]"
|
|
32
|
-
|
|
33
|
-
# Even without modifying or adding new code, you can run your baseline
|
|
34
|
-
# with the placeholder code generated when you did `flwr new`. If you
|
|
35
|
-
# want to test this to familiarise yourself with how flower apps are
|
|
36
|
-
# executed, execute this from the directory where you `pyproject.toml` is:
|
|
37
|
-
flwr run .
|
|
38
|
-
|
|
39
|
-
# At anypoint during the process of creating your baseline you can
|
|
40
|
-
# run the formatting script. For this do:
|
|
41
|
-
cd .. # so you are in the `flower/baselines` directory
|
|
42
|
-
|
|
43
|
-
# Run the formatting script (it will auto-correct issues if possible)
|
|
44
|
-
./dev/format-baseline.sh $project_name
|
|
45
|
-
|
|
46
|
-
# Then, if the above is all good, run the tests.
|
|
47
|
-
./dev/test-baseline.sh $project_name
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
> [!IMPORTANT]
|
|
51
|
-
> When you open a PR to get the baseline merged into the main Flower repository, the `./dev/test-baseline.sh` script will run. Only if test pass, the baseline can be merged.
|
|
52
|
-
> Some issues highlighted by the tests script are easier than others to fix. Do not hesitate in reaching out for help to us (e.g. as a comment in your PR) if you are stuck with these.
|
|
53
|
-
> Before opening your PR, please remove the code snippet above as well all the [!IMPORTANT] message blocks. Yes, including this one.
|
|
54
|
-
|
|
55
|
-
# :warning: *_Title of your baseline_* # Also copy this title to the `description` in the `[project]` section of your `pyproject.toml`.
|
|
56
|
-
|
|
57
|
-
> [!NOTE]
|
|
58
|
-
> If you use this baseline in your work, please remember to cite the original authors of the paper as well as the Flower paper.
|
|
59
|
-
|
|
60
|
-
**Paper:** :warning: *_add the URL of the paper page (not to the .pdf). For instance if you link a paper on ArXiv, add here the URL to the abstract page (e.g. [paper](https://arxiv.org/abs/1512.03385)). If your paper is in from a journal or conference proceedings, please follow the same logic._*
|
|
61
|
-
|
|
62
|
-
**Authors:** :warning: *_list authors of the paper_*
|
|
63
|
-
|
|
64
|
-
**Abstract:** :warning: *_add here the abstract of the paper you are implementing_*
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
## About this baseline
|
|
68
|
-
|
|
69
|
-
**What’s implemented:** :warning: *_Concisely describe what experiment(s) (e.g. Figure 1, Table 2, etc.) in the publication can be replicated by running the code. Please only use a few sentences. ”_*
|
|
70
|
-
|
|
71
|
-
**Datasets:** :warning: *_List the datasets you used (if you used a medium to large dataset, >10GB please also include the sizes of the dataset). We highly recommend using [FlowerDatasets](https://flower.ai/docs/datasets/index.html) to download and partition your dataset. If you have other ways to download the data, you can also use `FlowerDatasets` to partition it._*
|
|
72
|
-
|
|
73
|
-
**Hardware Setup:** :warning: *_Give some details about the hardware (e.g. a server with 8x V100 32GB and 256GB of RAM) you used to run the experiments for this baseline. Indicate how long it took to run the experiments. Someone out there might not have access to the same resources you have so, could you list the absolute minimum hardware needed to run the experiment in a reasonable amount of time ? (e.g. minimum is 1x 16GB GPU otherwise a client model can’t be trained with a sufficiently large batch size). Could you test this works too?_*
|
|
74
|
-
|
|
75
|
-
**Contributors:** :warning: *_let the world know who contributed to this baseline. This could be either your name, your name and affiliation at the time, or your GitHub profile name if you prefer. If multiple contributors signed up for this baseline, please list yourself and your colleagues_*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
## Experimental Setup
|
|
79
|
-
|
|
80
|
-
**Task:** :warning: *_what’s the primary task that is being federated? (e.g. image classification, next-word prediction). If you have experiments for several, please list them_*
|
|
81
|
-
|
|
82
|
-
**Model:** :warning: *_provide details about the model you used in your experiments (if more than use a list). If your model is small, describing it as a table would be :100:. Some FL methods do not use an off-the-shelve model (e.g. ResNet18) instead they create your own. If this is your case, please provide a summary here and give pointers to where in the paper (e.g. Appendix B.4) is detailed._*
|
|
83
|
-
|
|
84
|
-
**Dataset:** :warning: *_Earlier you listed already the datasets that your baseline uses. Now you should include a breakdown of the details about each of them. Please include information about: how the dataset is partitioned (e.g. LDA with alpha 0.1 as default and all clients have the same number of training examples; or each client gets assigned a different number of samples following a power-law distribution with each client only instances of 2 classes)? if your dataset is naturally partitioned just state “naturally partitioned”; how many partitions there are (i.e. how many clients)? Please include this an all information relevant about the dataset and its partitioning into a table._*
|
|
85
|
-
|
|
86
|
-
**Training Hyperparameters:** :warning: *_Include a table with all the main hyperparameters in your baseline. Please show them with their default value._*
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
## Environment Setup
|
|
90
|
-
|
|
91
|
-
:warning: _Specify the steps to create and activate your environment and install the baseline project. Most baselines are expected to require minimal steps as shown below. These instructions should be comprehensive enough so anyone can run them (if non standard, describe them step-by-step)._
|
|
92
|
-
|
|
93
|
-
:warning: _The dependencies for your baseline are listed in the `pyproject.toml`, extend it with additional packages needed for your baseline._
|
|
94
|
-
|
|
95
|
-
:warning: _Baselines should use Python 3.10, [pyenv](https://github.com/pyenv/pyenv), and the [virtualenv](https://github.com/pyenv/pyenv-virtualenv) plugging.
|
|
96
|
-
|
|
97
|
-
```bash
|
|
98
|
-
# Create the virtual environment
|
|
99
|
-
pyenv virtualenv 3.10.14 <name-of-your-baseline-env>
|
|
100
|
-
|
|
101
|
-
# Activate it
|
|
102
|
-
pyenv activate <name-of-your-baseline-env>
|
|
103
|
-
|
|
104
|
-
# Install the baseline
|
|
105
|
-
pip install -e .
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
:warning: _If your baseline requires running some script before starting an experiment, please indicate so here_.
|
|
109
|
-
|
|
110
|
-
## Running the Experiments
|
|
111
|
-
|
|
112
|
-
:warning: _Make sure you have adjusted the `client-resources` in the federation in `pyproject.toml` so your simulation makes the best use of the system resources available._
|
|
113
|
-
|
|
114
|
-
:warning: _Your baseline implementation should replicate several of the experiments in the original paper. Please include here the exact command(s) needed to run each of those experiments followed by a figure (e.g. a line plot) or table showing the results you obtained when you ran the code. Below is an example of how you can present this. Please add command followed by results for all your experiments._
|
|
115
|
-
|
|
116
|
-
:warning: _You might want to add more hyperparameters and settings for your baseline. You can do so by extending `[tool.flwr.app.config]` in `pyproject.toml`. In addition, you can create a new `.toml` file that can be passed with the `--run-config` command (see below an example) to override several config values **already present** in `pyproject.toml`._
|
|
117
|
-
```bash
|
|
118
|
-
# it is likely that for one experiment you need to override some arguments.
|
|
119
|
-
flwr run . --run-config learning-rate=0.1,coefficient=0.123
|
|
120
|
-
|
|
121
|
-
# or you might want to load different `.toml` configs all together:
|
|
122
|
-
flwr run . --run-config <my-big-experiment-config>.toml
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
:warning: _It is preferable to show a single command (or multiple commands if they belong to the same experiment) and then a table/plot with the expected results, instead of showing all the commands first and then all the results/plots._
|
|
126
|
-
:warning: _If you present plots or other figures, please include either a Jupyter notebook showing how to create them or include a utility function that can be called after the experiments finish running._
|
|
127
|
-
:warning: If you include plots or figures, save them in `.png` format and place them in a new directory named `_static` at the same level as your `README.md`.
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
# FlowerTune LLM on $challenge_name Dataset
|
|
2
|
-
|
|
3
|
-
This directory conducts federated instruction tuning with a pretrained [Mistral-7B](https://huggingface.co/mistralai/Mistral-7B-v0.3) model on a [$challenge_name dataset](https://huggingface.co/datasets/$dataset_name).
|
|
4
|
-
We use [Flower Datasets](https://flower.dev/docs/datasets/) to download, partition and preprocess the dataset.
|
|
5
|
-
Flower's Simulation Engine is used to simulate the LLM fine-tuning process in federated way,
|
|
6
|
-
which allows users to perform the training on a single GPU.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## Methodology
|
|
10
|
-
|
|
11
|
-
This baseline performs federated LLM fine-tuning with [LoRA](https://arxiv.org/pdf/2106.09685) using the [🤗PEFT](https://huggingface.co/docs/peft/en/index) library.
|
|
12
|
-
The clients' models are aggregated with FedAvg strategy.
|
|
13
|
-
This provides a baseline performance for the leaderboard of $challenge_name challenge.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
## Environments setup
|
|
17
|
-
|
|
18
|
-
Project dependencies are defined in `pyproject.toml`. Install them in an activated Python environment with:
|
|
19
|
-
|
|
20
|
-
```shell
|
|
21
|
-
pip install -e .
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
> **Tip:** Learn how to configure your `pyproject.toml` file for Flower apps in [this guide](https://flower.ai/docs/framework/how-to-configure-pyproject-toml.html).
|
|
25
|
-
|
|
26
|
-
## Experimental setup
|
|
27
|
-
|
|
28
|
-
The dataset is divided into $num_clients partitions in an IID fashion, a partition is assigned to each ClientApp.
|
|
29
|
-
We randomly sample a fraction ($fraction_train) of the total nodes to participate in each round, for a total of `200` rounds.
|
|
30
|
-
All settings are defined in `pyproject.toml`.
|
|
31
|
-
|
|
32
|
-
> [!IMPORTANT]
|
|
33
|
-
> Please note that `[tool.flwr.app.config.static]` and `options.num-supernodes` under `[tool.flwr.federations.local-simulation]` are not allowed to be modified for fair competition if you plan to participated in the [LLM leaderboard](https://flower.ai/benchmarks/llm-leaderboard).
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
## Running the challenge
|
|
37
|
-
|
|
38
|
-
First make sure that you have got the access to [Mistral-7B](https://huggingface.co/mistralai/Mistral-7B-v0.3) model with your Hugging-Face account. You can request access directly from the Hugging-Face website.
|
|
39
|
-
Then, follow the instruction [here](https://huggingface.co/docs/huggingface_hub/en/quick-start#login-command) to log in your account. Note you only need to complete this stage once in your development machine:
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
huggingface-cli login
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Run the challenge with default config values.
|
|
46
|
-
The configs are defined in `[tool.flwr.app.config]` entry of `pyproject.toml`, and are loaded automatically.
|
|
47
|
-
|
|
48
|
-
```bash
|
|
49
|
-
flwr run
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## VRAM consumption
|
|
53
|
-
|
|
54
|
-
We use Mistral-7B model with 4-bit quantization as default. The estimated VRAM consumption per client for each challenge is shown below:
|
|
55
|
-
|
|
56
|
-
| Challenges | GeneralNLP | Finance | Medical | Code |
|
|
57
|
-
| :--------: | :--------: | :--------: | :--------: | :--------: |
|
|
58
|
-
| VRAM | ~25.50 GB | ~17.30 GB | ~22.80 GB | ~17.40 GB |
|
|
59
|
-
|
|
60
|
-
You can adjust the CPU/GPU resources you assign to each of the clients based on your device, which are specified with `options.backend.client-resources.num-cpus` and `options.backend.client-resources.num-gpus` under `[tool.flwr.federations.local-simulation]` entry in `pyproject.toml`.
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
## Model saving
|
|
64
|
-
|
|
65
|
-
The global PEFT model checkpoints are saved every 5 rounds after aggregation on the sever side as default, which can be specified with `train.save-every-round` under [tool.flwr.app.config] entry in `pyproject.toml`.
|
|
66
|
-
|
|
67
|
-
> [!NOTE]
|
|
68
|
-
> Please provide the last PEFT checkpoint if you plan to participated in the [LLM leaderboard](https://flower.ai/benchmarks/llm-leaderboard).
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
# $project_name: A Flower / $framework_str app
|
|
2
|
-
|
|
3
|
-
## Install dependencies and project
|
|
4
|
-
|
|
5
|
-
The dependencies are listed in the `pyproject.toml` and you can install them as follows:
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pip install -e .
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
> **Tip:** Your `pyproject.toml` file can define more than just the dependencies of your Flower app. You can also use it to specify hyperparameters for your runs and control which Flower Runtime is used. By default, it uses the Simulation Runtime, but you can switch to the Deployment Runtime when needed.
|
|
12
|
-
> Learn more in the [TOML configuration guide](https://flower.ai/docs/framework/how-to-configure-pyproject-toml.html).
|
|
13
|
-
|
|
14
|
-
## Run with the Simulation Engine
|
|
15
|
-
|
|
16
|
-
In the `$project_name` directory, use `flwr run` to run a local simulation:
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
flwr run .
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Refer to the [How to Run Simulations](https://flower.ai/docs/framework/how-to-run-simulations.html) guide in the documentation for advice on how to optimize your simulations.
|
|
23
|
-
|
|
24
|
-
## Run with the Deployment Engine
|
|
25
|
-
|
|
26
|
-
Follow this [how-to guide](https://flower.ai/docs/framework/how-to-run-flower-with-deployment-engine.html) to run the same app in this example but with Flower's Deployment Engine. After that, you might be interested in setting up [secure TLS-enabled communications](https://flower.ai/docs/framework/how-to-enable-tls-connections.html) and [SuperNode authentication](https://flower.ai/docs/framework/how-to-authenticate-supernodes.html) in your federation.
|
|
27
|
-
|
|
28
|
-
You can run Flower on Docker too! Check out the [Flower with Docker](https://flower.ai/docs/framework/docker/index.html) documentation.
|
|
29
|
-
|
|
30
|
-
## Resources
|
|
31
|
-
|
|
32
|
-
- Flower website: [flower.ai](https://flower.ai/)
|
|
33
|
-
- Check the documentation: [flower.ai/docs](https://flower.ai/docs/)
|
|
34
|
-
- Give Flower a ⭐️ on GitHub: [GitHub](https://github.com/adap/flower)
|
|
35
|
-
- Join the Flower community!
|
|
36
|
-
- [Flower Slack](https://flower.ai/join-slack/)
|
|
37
|
-
- [Flower Discuss](https://discuss.flower.ai/)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower Baseline."""
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / $framework_str app."""
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"""$project_name: A Flower / PyTorch app."""
|
|
@@ -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)
|