flwr-nightly 1.8.0.dev20240314__py3-none-any.whl → 1.15.0.dev20250114__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (311) hide show
  1. flwr/cli/app.py +16 -2
  2. flwr/cli/build.py +181 -0
  3. flwr/cli/cli_user_auth_interceptor.py +90 -0
  4. flwr/cli/config_utils.py +343 -0
  5. flwr/cli/example.py +4 -1
  6. flwr/cli/install.py +253 -0
  7. flwr/cli/log.py +182 -0
  8. flwr/{server/superlink/state → cli/login}/__init__.py +4 -10
  9. flwr/cli/login/login.py +88 -0
  10. flwr/cli/ls.py +327 -0
  11. flwr/cli/new/__init__.py +1 -0
  12. flwr/cli/new/new.py +210 -66
  13. flwr/cli/new/templates/app/.gitignore.tpl +163 -0
  14. flwr/cli/new/templates/app/LICENSE.tpl +202 -0
  15. flwr/cli/new/templates/app/README.baseline.md.tpl +127 -0
  16. flwr/cli/new/templates/app/README.flowertune.md.tpl +66 -0
  17. flwr/cli/new/templates/app/README.md.tpl +16 -32
  18. flwr/cli/new/templates/app/code/__init__.baseline.py.tpl +1 -0
  19. flwr/cli/new/templates/app/code/__init__.py.tpl +1 -1
  20. flwr/cli/new/templates/app/code/client.baseline.py.tpl +58 -0
  21. flwr/cli/new/templates/app/code/client.huggingface.py.tpl +55 -0
  22. flwr/cli/new/templates/app/code/client.jax.py.tpl +50 -0
  23. flwr/cli/new/templates/app/code/client.mlx.py.tpl +73 -0
  24. flwr/cli/new/templates/app/code/client.numpy.py.tpl +7 -7
  25. flwr/cli/new/templates/app/code/client.pytorch.py.tpl +30 -21
  26. flwr/cli/new/templates/app/code/client.sklearn.py.tpl +63 -0
  27. flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +57 -1
  28. flwr/cli/new/templates/app/code/dataset.baseline.py.tpl +36 -0
  29. flwr/cli/new/templates/app/code/flwr_tune/__init__.py +15 -0
  30. flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +126 -0
  31. flwr/cli/new/templates/app/code/flwr_tune/dataset.py.tpl +87 -0
  32. flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +78 -0
  33. flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +94 -0
  34. flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +83 -0
  35. flwr/cli/new/templates/app/code/model.baseline.py.tpl +80 -0
  36. flwr/cli/new/templates/app/code/server.baseline.py.tpl +46 -0
  37. flwr/cli/new/templates/app/code/server.huggingface.py.tpl +38 -0
  38. flwr/cli/new/templates/app/code/server.jax.py.tpl +26 -0
  39. flwr/cli/new/templates/app/code/server.mlx.py.tpl +31 -0
  40. flwr/cli/new/templates/app/code/server.numpy.py.tpl +22 -9
  41. flwr/cli/new/templates/app/code/server.pytorch.py.tpl +21 -18
  42. flwr/cli/new/templates/app/code/server.sklearn.py.tpl +36 -0
  43. flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +29 -1
  44. flwr/cli/new/templates/app/code/strategy.baseline.py.tpl +1 -0
  45. flwr/cli/new/templates/app/code/task.huggingface.py.tpl +102 -0
  46. flwr/cli/new/templates/app/code/task.jax.py.tpl +57 -0
  47. flwr/cli/new/templates/app/code/task.mlx.py.tpl +102 -0
  48. flwr/cli/new/templates/app/code/task.numpy.py.tpl +7 -0
  49. flwr/cli/new/templates/app/code/task.pytorch.py.tpl +29 -24
  50. flwr/cli/new/templates/app/code/task.sklearn.py.tpl +67 -0
  51. flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +53 -0
  52. flwr/cli/new/templates/app/code/utils.baseline.py.tpl +1 -0
  53. flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +138 -0
  54. flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +68 -0
  55. flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +46 -0
  56. flwr/cli/new/templates/app/pyproject.jax.toml.tpl +35 -0
  57. flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +39 -0
  58. flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +25 -12
  59. flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +29 -14
  60. flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +35 -0
  61. flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +29 -14
  62. flwr/cli/run/__init__.py +1 -0
  63. flwr/cli/run/run.py +212 -34
  64. flwr/cli/stop.py +130 -0
  65. flwr/cli/utils.py +240 -5
  66. flwr/client/__init__.py +3 -2
  67. flwr/client/app.py +432 -255
  68. flwr/client/client.py +1 -11
  69. flwr/client/client_app.py +74 -13
  70. flwr/client/clientapp/__init__.py +22 -0
  71. flwr/client/clientapp/app.py +259 -0
  72. flwr/client/clientapp/clientappio_servicer.py +244 -0
  73. flwr/client/clientapp/utils.py +115 -0
  74. flwr/client/dpfedavg_numpy_client.py +7 -8
  75. flwr/client/grpc_adapter_client/__init__.py +15 -0
  76. flwr/client/grpc_adapter_client/connection.py +98 -0
  77. flwr/client/grpc_client/connection.py +21 -7
  78. flwr/client/grpc_rere_client/__init__.py +1 -1
  79. flwr/client/grpc_rere_client/client_interceptor.py +176 -0
  80. flwr/client/grpc_rere_client/connection.py +163 -56
  81. flwr/client/grpc_rere_client/grpc_adapter.py +167 -0
  82. flwr/client/heartbeat.py +74 -0
  83. flwr/client/message_handler/__init__.py +1 -1
  84. flwr/client/message_handler/message_handler.py +10 -11
  85. flwr/client/mod/__init__.py +5 -5
  86. flwr/client/mod/centraldp_mods.py +4 -2
  87. flwr/client/mod/comms_mods.py +5 -4
  88. flwr/client/mod/localdp_mod.py +10 -5
  89. flwr/client/mod/secure_aggregation/__init__.py +1 -1
  90. flwr/client/mod/secure_aggregation/secaggplus_mod.py +26 -26
  91. flwr/client/mod/utils.py +2 -4
  92. flwr/client/nodestate/__init__.py +26 -0
  93. flwr/client/nodestate/in_memory_nodestate.py +38 -0
  94. flwr/client/nodestate/nodestate.py +31 -0
  95. flwr/client/nodestate/nodestate_factory.py +38 -0
  96. flwr/client/numpy_client.py +8 -31
  97. flwr/client/rest_client/__init__.py +1 -1
  98. flwr/client/rest_client/connection.py +199 -176
  99. flwr/client/run_info_store.py +112 -0
  100. flwr/client/supernode/__init__.py +24 -0
  101. flwr/client/supernode/app.py +321 -0
  102. flwr/client/typing.py +1 -0
  103. flwr/common/__init__.py +17 -11
  104. flwr/common/address.py +47 -3
  105. flwr/common/args.py +153 -0
  106. flwr/common/auth_plugin/__init__.py +24 -0
  107. flwr/common/auth_plugin/auth_plugin.py +121 -0
  108. flwr/common/config.py +243 -0
  109. flwr/common/constant.py +132 -1
  110. flwr/common/context.py +32 -2
  111. flwr/common/date.py +22 -4
  112. flwr/common/differential_privacy.py +2 -2
  113. flwr/common/dp.py +2 -4
  114. flwr/common/exit_handlers.py +3 -3
  115. flwr/common/grpc.py +164 -5
  116. flwr/common/logger.py +230 -12
  117. flwr/common/message.py +191 -106
  118. flwr/common/object_ref.py +179 -44
  119. flwr/common/pyproject.py +1 -0
  120. flwr/common/record/__init__.py +2 -1
  121. flwr/common/record/configsrecord.py +58 -18
  122. flwr/common/record/metricsrecord.py +57 -17
  123. flwr/common/record/parametersrecord.py +88 -20
  124. flwr/common/record/recordset.py +153 -30
  125. flwr/common/record/typeddict.py +30 -55
  126. flwr/common/recordset_compat.py +31 -12
  127. flwr/common/retry_invoker.py +123 -30
  128. flwr/common/secure_aggregation/__init__.py +1 -1
  129. flwr/common/secure_aggregation/crypto/__init__.py +1 -1
  130. flwr/common/secure_aggregation/crypto/shamir.py +11 -11
  131. flwr/common/secure_aggregation/crypto/symmetric_encryption.py +68 -4
  132. flwr/common/secure_aggregation/ndarrays_arithmetic.py +17 -17
  133. flwr/common/secure_aggregation/quantization.py +8 -8
  134. flwr/common/secure_aggregation/secaggplus_constants.py +1 -1
  135. flwr/common/secure_aggregation/secaggplus_utils.py +10 -12
  136. flwr/common/serde.py +298 -19
  137. flwr/common/telemetry.py +65 -29
  138. flwr/common/typing.py +120 -19
  139. flwr/common/version.py +17 -3
  140. flwr/proto/clientappio_pb2.py +45 -0
  141. flwr/proto/clientappio_pb2.pyi +132 -0
  142. flwr/proto/clientappio_pb2_grpc.py +135 -0
  143. flwr/proto/clientappio_pb2_grpc.pyi +53 -0
  144. flwr/proto/exec_pb2.py +62 -0
  145. flwr/proto/exec_pb2.pyi +212 -0
  146. flwr/proto/exec_pb2_grpc.py +237 -0
  147. flwr/proto/exec_pb2_grpc.pyi +93 -0
  148. flwr/proto/fab_pb2.py +31 -0
  149. flwr/proto/fab_pb2.pyi +65 -0
  150. flwr/proto/fab_pb2_grpc.py +4 -0
  151. flwr/proto/fab_pb2_grpc.pyi +4 -0
  152. flwr/proto/fleet_pb2.py +42 -23
  153. flwr/proto/fleet_pb2.pyi +123 -1
  154. flwr/proto/fleet_pb2_grpc.py +170 -0
  155. flwr/proto/fleet_pb2_grpc.pyi +61 -0
  156. flwr/proto/grpcadapter_pb2.py +32 -0
  157. flwr/proto/grpcadapter_pb2.pyi +43 -0
  158. flwr/proto/grpcadapter_pb2_grpc.py +66 -0
  159. flwr/proto/grpcadapter_pb2_grpc.pyi +24 -0
  160. flwr/proto/log_pb2.py +29 -0
  161. flwr/proto/log_pb2.pyi +39 -0
  162. flwr/proto/log_pb2_grpc.py +4 -0
  163. flwr/proto/log_pb2_grpc.pyi +4 -0
  164. flwr/proto/message_pb2.py +41 -0
  165. flwr/proto/message_pb2.pyi +128 -0
  166. flwr/proto/message_pb2_grpc.py +4 -0
  167. flwr/proto/message_pb2_grpc.pyi +4 -0
  168. flwr/proto/node_pb2.py +1 -1
  169. flwr/proto/recordset_pb2.py +35 -33
  170. flwr/proto/recordset_pb2.pyi +40 -14
  171. flwr/proto/run_pb2.py +64 -0
  172. flwr/proto/run_pb2.pyi +268 -0
  173. flwr/proto/run_pb2_grpc.py +4 -0
  174. flwr/proto/run_pb2_grpc.pyi +4 -0
  175. flwr/proto/serverappio_pb2.py +52 -0
  176. flwr/proto/{driver_pb2.pyi → serverappio_pb2.pyi} +62 -20
  177. flwr/proto/serverappio_pb2_grpc.py +410 -0
  178. flwr/proto/serverappio_pb2_grpc.pyi +160 -0
  179. flwr/proto/simulationio_pb2.py +38 -0
  180. flwr/proto/simulationio_pb2.pyi +65 -0
  181. flwr/proto/simulationio_pb2_grpc.py +239 -0
  182. flwr/proto/simulationio_pb2_grpc.pyi +94 -0
  183. flwr/proto/task_pb2.py +7 -8
  184. flwr/proto/task_pb2.pyi +8 -5
  185. flwr/proto/transport_pb2.py +8 -8
  186. flwr/proto/transport_pb2.pyi +9 -6
  187. flwr/server/__init__.py +2 -10
  188. flwr/server/app.py +579 -402
  189. flwr/server/client_manager.py +8 -6
  190. flwr/server/compat/app.py +6 -62
  191. flwr/server/compat/app_utils.py +14 -8
  192. flwr/server/compat/driver_client_proxy.py +25 -58
  193. flwr/server/compat/legacy_context.py +5 -4
  194. flwr/server/driver/__init__.py +2 -0
  195. flwr/server/driver/driver.py +36 -131
  196. flwr/server/driver/grpc_driver.py +217 -81
  197. flwr/server/driver/inmemory_driver.py +182 -0
  198. flwr/server/history.py +28 -29
  199. flwr/server/run_serverapp.py +15 -126
  200. flwr/server/server.py +50 -44
  201. flwr/server/server_app.py +59 -10
  202. flwr/server/serverapp/__init__.py +22 -0
  203. flwr/server/serverapp/app.py +256 -0
  204. flwr/server/serverapp_components.py +52 -0
  205. flwr/server/strategy/__init__.py +2 -2
  206. flwr/server/strategy/aggregate.py +37 -23
  207. flwr/server/strategy/bulyan.py +9 -9
  208. flwr/server/strategy/dp_adaptive_clipping.py +25 -25
  209. flwr/server/strategy/dp_fixed_clipping.py +23 -22
  210. flwr/server/strategy/dpfedavg_adaptive.py +8 -8
  211. flwr/server/strategy/dpfedavg_fixed.py +13 -12
  212. flwr/server/strategy/fault_tolerant_fedavg.py +11 -11
  213. flwr/server/strategy/fedadagrad.py +9 -9
  214. flwr/server/strategy/fedadam.py +20 -10
  215. flwr/server/strategy/fedavg.py +16 -16
  216. flwr/server/strategy/fedavg_android.py +17 -17
  217. flwr/server/strategy/fedavgm.py +9 -9
  218. flwr/server/strategy/fedmedian.py +5 -5
  219. flwr/server/strategy/fedopt.py +6 -6
  220. flwr/server/strategy/fedprox.py +7 -7
  221. flwr/server/strategy/fedtrimmedavg.py +8 -8
  222. flwr/server/strategy/fedxgb_bagging.py +12 -12
  223. flwr/server/strategy/fedxgb_cyclic.py +10 -10
  224. flwr/server/strategy/fedxgb_nn_avg.py +6 -6
  225. flwr/server/strategy/fedyogi.py +9 -9
  226. flwr/server/strategy/krum.py +9 -9
  227. flwr/server/strategy/qfedavg.py +16 -16
  228. flwr/server/strategy/strategy.py +10 -10
  229. flwr/server/superlink/driver/__init__.py +2 -2
  230. flwr/server/superlink/driver/serverappio_grpc.py +61 -0
  231. flwr/server/superlink/driver/serverappio_servicer.py +363 -0
  232. flwr/server/superlink/ffs/__init__.py +24 -0
  233. flwr/server/superlink/ffs/disk_ffs.py +108 -0
  234. flwr/server/superlink/ffs/ffs.py +79 -0
  235. flwr/server/superlink/ffs/ffs_factory.py +47 -0
  236. flwr/server/superlink/fleet/__init__.py +1 -1
  237. flwr/server/superlink/fleet/grpc_adapter/__init__.py +15 -0
  238. flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +162 -0
  239. flwr/server/superlink/fleet/grpc_bidi/__init__.py +1 -1
  240. flwr/server/superlink/fleet/grpc_bidi/flower_service_servicer.py +4 -2
  241. flwr/server/superlink/fleet/grpc_bidi/grpc_bridge.py +3 -2
  242. flwr/server/superlink/fleet/grpc_bidi/grpc_client_proxy.py +1 -1
  243. flwr/server/superlink/fleet/grpc_bidi/grpc_server.py +5 -154
  244. flwr/server/superlink/fleet/grpc_rere/__init__.py +1 -1
  245. flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +120 -13
  246. flwr/server/superlink/fleet/grpc_rere/server_interceptor.py +228 -0
  247. flwr/server/superlink/fleet/message_handler/__init__.py +1 -1
  248. flwr/server/superlink/fleet/message_handler/message_handler.py +153 -9
  249. flwr/server/superlink/fleet/rest_rere/__init__.py +1 -1
  250. flwr/server/superlink/fleet/rest_rere/rest_api.py +119 -81
  251. flwr/server/superlink/fleet/vce/__init__.py +1 -0
  252. flwr/server/superlink/fleet/vce/backend/__init__.py +4 -4
  253. flwr/server/superlink/fleet/vce/backend/backend.py +8 -9
  254. flwr/server/superlink/fleet/vce/backend/raybackend.py +87 -68
  255. flwr/server/superlink/fleet/vce/vce_api.py +208 -146
  256. flwr/server/superlink/linkstate/__init__.py +28 -0
  257. flwr/server/superlink/linkstate/in_memory_linkstate.py +581 -0
  258. flwr/server/superlink/linkstate/linkstate.py +389 -0
  259. flwr/server/superlink/{state/state_factory.py → linkstate/linkstate_factory.py} +19 -10
  260. flwr/server/superlink/linkstate/sqlite_linkstate.py +1236 -0
  261. flwr/server/superlink/linkstate/utils.py +389 -0
  262. flwr/server/superlink/simulation/__init__.py +15 -0
  263. flwr/server/superlink/simulation/simulationio_grpc.py +65 -0
  264. flwr/server/superlink/simulation/simulationio_servicer.py +186 -0
  265. flwr/server/superlink/utils.py +65 -0
  266. flwr/server/typing.py +2 -0
  267. flwr/server/utils/__init__.py +1 -1
  268. flwr/server/utils/tensorboard.py +5 -5
  269. flwr/server/utils/validator.py +31 -11
  270. flwr/server/workflow/default_workflows.py +70 -26
  271. flwr/server/workflow/secure_aggregation/secagg_workflow.py +1 -0
  272. flwr/server/workflow/secure_aggregation/secaggplus_workflow.py +40 -27
  273. flwr/simulation/__init__.py +12 -5
  274. flwr/simulation/app.py +247 -315
  275. flwr/simulation/legacy_app.py +402 -0
  276. flwr/simulation/ray_transport/__init__.py +1 -1
  277. flwr/simulation/ray_transport/ray_actor.py +42 -67
  278. flwr/simulation/ray_transport/ray_client_proxy.py +37 -17
  279. flwr/simulation/ray_transport/utils.py +1 -0
  280. flwr/simulation/run_simulation.py +306 -163
  281. flwr/simulation/simulationio_connection.py +89 -0
  282. flwr/superexec/__init__.py +15 -0
  283. flwr/superexec/app.py +59 -0
  284. flwr/superexec/deployment.py +188 -0
  285. flwr/superexec/exec_grpc.py +80 -0
  286. flwr/superexec/exec_servicer.py +231 -0
  287. flwr/superexec/exec_user_auth_interceptor.py +101 -0
  288. flwr/superexec/executor.py +96 -0
  289. flwr/superexec/simulation.py +124 -0
  290. {flwr_nightly-1.8.0.dev20240314.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/METADATA +33 -26
  291. flwr_nightly-1.15.0.dev20250114.dist-info/RECORD +328 -0
  292. flwr_nightly-1.15.0.dev20250114.dist-info/entry_points.txt +12 -0
  293. flwr/cli/flower_toml.py +0 -140
  294. flwr/cli/new/templates/app/flower.toml.tpl +0 -13
  295. flwr/cli/new/templates/app/requirements.numpy.txt.tpl +0 -2
  296. flwr/cli/new/templates/app/requirements.pytorch.txt.tpl +0 -4
  297. flwr/cli/new/templates/app/requirements.tensorflow.txt.tpl +0 -4
  298. flwr/client/node_state.py +0 -48
  299. flwr/client/node_state_tests.py +0 -65
  300. flwr/proto/driver_pb2.py +0 -44
  301. flwr/proto/driver_pb2_grpc.py +0 -169
  302. flwr/proto/driver_pb2_grpc.pyi +0 -66
  303. flwr/server/superlink/driver/driver_grpc.py +0 -54
  304. flwr/server/superlink/driver/driver_servicer.py +0 -129
  305. flwr/server/superlink/state/in_memory_state.py +0 -230
  306. flwr/server/superlink/state/sqlite_state.py +0 -630
  307. flwr/server/superlink/state/state.py +0 -154
  308. flwr_nightly-1.8.0.dev20240314.dist-info/RECORD +0 -211
  309. flwr_nightly-1.8.0.dev20240314.dist-info/entry_points.txt +0 -9
  310. {flwr_nightly-1.8.0.dev20240314.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/LICENSE +0 -0
  311. {flwr_nightly-1.8.0.dev20240314.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/WHEEL +0 -0
@@ -0,0 +1,58 @@
1
+ """$project_name: A Flower Baseline."""
2
+
3
+ import torch
4
+
5
+ from flwr.client import ClientApp, NumPyClient
6
+ from flwr.common import Context
7
+ from $import_name.dataset import load_data
8
+ from $import_name.model import Net, get_weights, set_weights, test, train
9
+
10
+
11
+ class FlowerClient(NumPyClient):
12
+ """A class defining the client."""
13
+
14
+ def __init__(self, net, trainloader, valloader, local_epochs):
15
+ self.net = net
16
+ self.trainloader = trainloader
17
+ self.valloader = valloader
18
+ self.local_epochs = local_epochs
19
+ self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
20
+ self.net.to(self.device)
21
+
22
+ def fit(self, parameters, config):
23
+ """Traim model using this client's data."""
24
+ set_weights(self.net, parameters)
25
+ train_loss = train(
26
+ self.net,
27
+ self.trainloader,
28
+ self.local_epochs,
29
+ self.device,
30
+ )
31
+ return (
32
+ get_weights(self.net),
33
+ len(self.trainloader.dataset),
34
+ {"train_loss": train_loss},
35
+ )
36
+
37
+ def evaluate(self, parameters, config):
38
+ """Evaluate model using this client's data."""
39
+ set_weights(self.net, parameters)
40
+ loss, accuracy = test(self.net, self.valloader, self.device)
41
+ return loss, len(self.valloader.dataset), {"accuracy": accuracy}
42
+
43
+
44
+ def client_fn(context: Context):
45
+ """Construct a Client that will be run in a ClientApp."""
46
+ # Load model and data
47
+ net = Net()
48
+ partition_id = int(context.node_config["partition-id"])
49
+ num_partitions = int(context.node_config["num-partitions"])
50
+ trainloader, valloader = load_data(partition_id, num_partitions)
51
+ local_epochs = context.run_config["local-epochs"]
52
+
53
+ # Return Client instance
54
+ return FlowerClient(net, trainloader, valloader, local_epochs).to_client()
55
+
56
+
57
+ # Flower ClientApp
58
+ app = ClientApp(client_fn)
@@ -0,0 +1,55 @@
1
+ """$project_name: A Flower / $framework_str app."""
2
+
3
+ import torch
4
+ from flwr.client import ClientApp, NumPyClient
5
+ from flwr.common import Context
6
+ from transformers import AutoModelForSequenceClassification
7
+
8
+ from $import_name.task import get_weights, load_data, set_weights, test, train
9
+
10
+
11
+ # Flower client
12
+ class FlowerClient(NumPyClient):
13
+ def __init__(self, net, trainloader, testloader, local_epochs):
14
+ self.net = net
15
+ self.trainloader = trainloader
16
+ self.testloader = testloader
17
+ self.local_epochs = local_epochs
18
+ self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
19
+ self.net.to(self.device)
20
+
21
+ def fit(self, parameters, config):
22
+ set_weights(self.net, parameters)
23
+ train(self.net, self.trainloader, epochs=self.local_epochs, device=self.device)
24
+ return get_weights(self.net), len(self.trainloader), {}
25
+
26
+ def evaluate(self, parameters, config):
27
+ set_weights(self.net, parameters)
28
+ loss, accuracy = test(self.net, self.testloader, self.device)
29
+ return float(loss), len(self.testloader), {"accuracy": accuracy}
30
+
31
+
32
+ def client_fn(context: Context):
33
+
34
+ # Get this client's dataset partition
35
+ partition_id = context.node_config["partition-id"]
36
+ num_partitions = context.node_config["num-partitions"]
37
+ model_name = context.run_config["model-name"]
38
+ trainloader, valloader = load_data(partition_id, num_partitions, model_name)
39
+
40
+ # Load model
41
+ num_labels = context.run_config["num-labels"]
42
+ net = AutoModelForSequenceClassification.from_pretrained(
43
+ model_name, num_labels=num_labels
44
+ )
45
+
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
+ )
@@ -0,0 +1,50 @@
1
+ """$project_name: A Flower / $framework_str app."""
2
+
3
+ import jax
4
+
5
+ from flwr.client import ClientApp, NumPyClient
6
+ from flwr.common import Context
7
+ from $import_name.task import (
8
+ evaluation,
9
+ get_params,
10
+ load_data,
11
+ load_model,
12
+ loss_fn,
13
+ set_params,
14
+ train,
15
+ )
16
+
17
+
18
+ # Define Flower Client and client_fn
19
+ class FlowerClient(NumPyClient):
20
+ def __init__(self, input_dim):
21
+ self.train_x, self.train_y, self.test_x, self.test_y = load_data()
22
+ self.grad_fn = jax.grad(loss_fn)
23
+ self.params = load_model((input_dim,))
24
+
25
+ def fit(self, parameters, config):
26
+ set_params(self.params, parameters)
27
+ self.params, loss, num_examples = train(
28
+ self.params, self.grad_fn, self.train_x, self.train_y
29
+ )
30
+ return get_params(self.params), num_examples, {"loss": float(loss)}
31
+
32
+ def evaluate(self, parameters, config):
33
+ set_params(self.params, parameters)
34
+ loss, num_examples = evaluation(
35
+ self.params, self.grad_fn, self.test_x, self.test_y
36
+ )
37
+ return float(loss), num_examples, {"loss": float(loss)}
38
+
39
+
40
+ def client_fn(context: Context):
41
+ input_dim = context.run_config["input-dim"]
42
+
43
+ # Return Client instance
44
+ return FlowerClient(input_dim).to_client()
45
+
46
+
47
+ # Flower ClientApp
48
+ app = ClientApp(
49
+ client_fn,
50
+ )
@@ -0,0 +1,73 @@
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
+
7
+ from flwr.client import ClientApp, NumPyClient
8
+ from flwr.common import Context
9
+ from flwr.common.config import UserConfig
10
+ from $import_name.task import (
11
+ MLP,
12
+ batch_iterate,
13
+ eval_fn,
14
+ get_params,
15
+ load_data,
16
+ loss_fn,
17
+ set_params,
18
+ )
19
+
20
+
21
+ # Define Flower Client and client_fn
22
+ class FlowerClient(NumPyClient):
23
+ def __init__(
24
+ self,
25
+ data,
26
+ run_config: UserConfig,
27
+ num_classes,
28
+ ):
29
+ num_layers = run_config["num-layers"]
30
+ hidden_dim = run_config["hidden-dim"]
31
+ input_dim = run_config["input-dim"]
32
+ batch_size = run_config["batch-size"]
33
+ learning_rate = run_config["lr"]
34
+ self.num_epochs = run_config["local-epochs"]
35
+
36
+ self.train_images, self.train_labels, self.test_images, self.test_labels = data
37
+ self.model = MLP(num_layers, input_dim, hidden_dim, num_classes)
38
+ self.optimizer = optim.SGD(learning_rate=learning_rate)
39
+ self.loss_and_grad_fn = nn.value_and_grad(self.model, loss_fn)
40
+ self.batch_size = batch_size
41
+
42
+ def fit(self, parameters, config):
43
+ set_params(self.model, parameters)
44
+ for _ in range(self.num_epochs):
45
+ for X, y in batch_iterate(
46
+ self.batch_size, self.train_images, self.train_labels
47
+ ):
48
+ _, grads = self.loss_and_grad_fn(self.model, X, y)
49
+ self.optimizer.update(self.model, grads)
50
+ mx.eval(self.model.parameters(), self.optimizer.state)
51
+ return get_params(self.model), len(self.train_images), {}
52
+
53
+ def evaluate(self, parameters, config):
54
+ set_params(self.model, parameters)
55
+ accuracy = eval_fn(self.model, self.test_images, self.test_labels)
56
+ loss = loss_fn(self.model, self.test_images, self.test_labels)
57
+ return loss.item(), len(self.test_images), {"accuracy": accuracy.item()}
58
+
59
+
60
+ def client_fn(context: Context):
61
+ partition_id = context.node_config["partition-id"]
62
+ num_partitions = context.node_config["num-partitions"]
63
+ data = load_data(partition_id, num_partitions)
64
+ num_classes = 10
65
+
66
+ # Return Client instance
67
+ return FlowerClient(data, context.run_config, num_classes).to_client()
68
+
69
+
70
+ # Flower ClientApp
71
+ app = ClientApp(
72
+ client_fn,
73
+ )
@@ -1,21 +1,21 @@
1
- """$project_name: A Flower / NumPy app."""
1
+ """$project_name: A Flower / $framework_str app."""
2
2
 
3
- from flwr.client import NumPyClient, ClientApp
4
- import numpy as np
3
+ from flwr.client import ClientApp, NumPyClient
4
+ from flwr.common import Context
5
+ from $import_name.task import get_dummy_model
5
6
 
6
7
 
7
8
  class FlowerClient(NumPyClient):
8
- def get_parameters(self, config):
9
- return [np.ones((1, 1))]
10
9
 
11
10
  def fit(self, parameters, config):
12
- return ([np.ones((1, 1))], 1, {})
11
+ model = get_dummy_model()
12
+ return [model], 1, {}
13
13
 
14
14
  def evaluate(self, parameters, config):
15
15
  return float(0.0), 1, {"accuracy": float(1.0)}
16
16
 
17
17
 
18
- def client_fn(cid: str):
18
+ def client_fn(context: Context):
19
19
  return FlowerClient().to_client()
20
20
 
21
21
 
@@ -1,43 +1,52 @@
1
- """$project_name: A Flower / PyTorch app."""
2
-
3
- from flwr.client import NumPyClient, ClientApp
4
-
5
- from $project_name.task import (
6
- Net,
7
- DEVICE,
8
- load_data,
9
- get_weights,
10
- set_weights,
11
- train,
12
- test,
13
- )
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
14
8
 
15
9
 
16
10
  # Define Flower Client and client_fn
17
11
  class FlowerClient(NumPyClient):
18
- def __init__(self, net, trainloader, valloader):
12
+ def __init__(self, net, trainloader, valloader, local_epochs):
19
13
  self.net = net
20
14
  self.trainloader = trainloader
21
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)
22
19
 
23
20
  def fit(self, parameters, config):
24
21
  set_weights(self.net, parameters)
25
- results = train(self.net, self.trainloader, self.valloader, 1, DEVICE)
26
- return get_weights(self.net), len(self.trainloader.dataset), results
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
+ )
27
33
 
28
34
  def evaluate(self, parameters, config):
29
35
  set_weights(self.net, parameters)
30
- loss, accuracy = test(self.net, self.valloader)
36
+ loss, accuracy = test(self.net, self.valloader, self.device)
31
37
  return loss, len(self.valloader.dataset), {"accuracy": accuracy}
32
38
 
33
39
 
34
- def client_fn(cid):
40
+ def client_fn(context: Context):
35
41
  # Load model and data
36
- net = Net().to(DEVICE)
37
- trainloader, valloader = load_data(int(cid), 2)
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"]
38
47
 
39
48
  # Return Client instance
40
- return FlowerClient(net, trainloader, valloader).to_client()
49
+ return FlowerClient(net, trainloader, valloader, local_epochs).to_client()
41
50
 
42
51
 
43
52
  # Flower ClientApp
@@ -0,0 +1,63 @@
1
+ """$project_name: A Flower / $framework_str app."""
2
+
3
+ import warnings
4
+
5
+ from sklearn.metrics import log_loss
6
+
7
+ from flwr.client import ClientApp, NumPyClient
8
+ from flwr.common import Context
9
+ from $import_name.task import (
10
+ get_model,
11
+ get_model_params,
12
+ load_data,
13
+ set_initial_params,
14
+ set_model_params,
15
+ )
16
+
17
+
18
+ class FlowerClient(NumPyClient):
19
+ def __init__(self, model, X_train, X_test, y_train, y_test):
20
+ self.model = model
21
+ self.X_train = X_train
22
+ self.X_test = X_test
23
+ self.y_train = y_train
24
+ self.y_test = y_test
25
+
26
+ def fit(self, parameters, config):
27
+ set_model_params(self.model, parameters)
28
+
29
+ # Ignore convergence failure due to low local epochs
30
+ with warnings.catch_warnings():
31
+ warnings.simplefilter("ignore")
32
+ self.model.fit(self.X_train, self.y_train)
33
+
34
+ return get_model_params(self.model), len(self.X_train), {}
35
+
36
+ def evaluate(self, parameters, config):
37
+ set_model_params(self.model, parameters)
38
+
39
+ loss = log_loss(self.y_test, self.model.predict_proba(self.X_test))
40
+ accuracy = self.model.score(self.X_test, self.y_test)
41
+
42
+ return loss, len(self.X_test), {"accuracy": accuracy}
43
+
44
+
45
+ def client_fn(context: Context):
46
+ partition_id = context.node_config["partition-id"]
47
+ num_partitions = context.node_config["num-partitions"]
48
+
49
+ X_train, X_test, y_train, y_test = load_data(partition_id, num_partitions)
50
+
51
+ # Create LogisticRegression Model
52
+ penalty = context.run_config["penalty"]
53
+ local_epochs = context.run_config["local-epochs"]
54
+ model = get_model(penalty, local_epochs)
55
+
56
+ # Setting initial parameters, akin to model.compile for keras models
57
+ set_initial_params(model)
58
+
59
+ return FlowerClient(model, X_train, X_test, y_train, y_test).to_client()
60
+
61
+
62
+ # Flower ClientApp
63
+ app = ClientApp(client_fn=client_fn)
@@ -1 +1,57 @@
1
- """$project_name: A Flower / TensorFlow app."""
1
+ """$project_name: A Flower / $framework_str app."""
2
+
3
+ from flwr.client import NumPyClient, ClientApp
4
+ from flwr.common import Context
5
+
6
+ from $import_name.task import load_data, load_model
7
+
8
+
9
+ # Define Flower Client and client_fn
10
+ class FlowerClient(NumPyClient):
11
+ def __init__(
12
+ self, model, data, epochs, batch_size, verbose
13
+ ):
14
+ self.model = model
15
+ self.x_train, self.y_train, self.x_test, self.y_test = data
16
+ self.epochs = epochs
17
+ self.batch_size = batch_size
18
+ self.verbose = verbose
19
+
20
+ def fit(self, parameters, config):
21
+ self.model.set_weights(parameters)
22
+ self.model.fit(
23
+ self.x_train,
24
+ self.y_train,
25
+ epochs=self.epochs,
26
+ batch_size=self.batch_size,
27
+ verbose=self.verbose,
28
+ )
29
+ return self.model.get_weights(), len(self.x_train), {}
30
+
31
+ def evaluate(self, parameters, config):
32
+ self.model.set_weights(parameters)
33
+ loss, accuracy = self.model.evaluate(self.x_test, self.y_test, verbose=0)
34
+ return loss, len(self.x_test), {"accuracy": accuracy}
35
+
36
+
37
+ def client_fn(context: Context):
38
+ # Load model and data
39
+ net = load_model()
40
+
41
+ partition_id = context.node_config["partition-id"]
42
+ num_partitions = context.node_config["num-partitions"]
43
+ data = load_data(partition_id, num_partitions)
44
+ epochs = context.run_config["local-epochs"]
45
+ batch_size = context.run_config["batch-size"]
46
+ verbose = context.run_config.get("verbose")
47
+
48
+ # Return Client instance
49
+ return FlowerClient(
50
+ net, data, epochs, batch_size, verbose
51
+ ).to_client()
52
+
53
+
54
+ # Flower ClientApp
55
+ app = ClientApp(
56
+ client_fn=client_fn,
57
+ )
@@ -0,0 +1,36 @@
1
+ """$project_name: A Flower Baseline."""
2
+
3
+ from flwr_datasets import FederatedDataset
4
+ from flwr_datasets.partitioner import IidPartitioner
5
+ from torch.utils.data import DataLoader
6
+ from torchvision.transforms import Compose, Normalize, ToTensor
7
+
8
+ FDS = None # Cache FederatedDataset
9
+
10
+
11
+ def load_data(partition_id: int, num_partitions: int):
12
+ """Load partition CIFAR10 data."""
13
+ # Only initialize `FederatedDataset` once
14
+ global FDS # pylint: disable=global-statement
15
+ if FDS is None:
16
+ partitioner = IidPartitioner(num_partitions=num_partitions)
17
+ FDS = FederatedDataset(
18
+ dataset="uoft-cs/cifar10",
19
+ partitioners={"train": partitioner},
20
+ )
21
+ partition = FDS.load_partition(partition_id)
22
+ # Divide data on each node: 80% train, 20% test
23
+ partition_train_test = partition.train_test_split(test_size=0.2, seed=42)
24
+ pytorch_transforms = Compose(
25
+ [ToTensor(), Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
26
+ )
27
+
28
+ def apply_transforms(batch):
29
+ """Apply transforms to the partition from FederatedDataset."""
30
+ batch["img"] = [pytorch_transforms(img) for img in batch["img"]]
31
+ return batch
32
+
33
+ partition_train_test = partition_train_test.with_transform(apply_transforms)
34
+ trainloader = DataLoader(partition_train_test["train"], batch_size=32, shuffle=True)
35
+ testloader = DataLoader(partition_train_test["test"], batch_size=32)
36
+ return trainloader, testloader
@@ -0,0 +1,15 @@
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+ """Flower CLI `new` command app / code / flwr_tune templates."""
@@ -0,0 +1,126 @@
1
+ """$project_name: A Flower / FlowerTune app."""
2
+
3
+ import os
4
+ import warnings
5
+ from typing import Dict, Tuple
6
+
7
+ import torch
8
+ from flwr.client import ClientApp, NumPyClient
9
+ from flwr.common import Context
10
+ from flwr.common.config import unflatten_dict
11
+ from flwr.common.typing import NDArrays, Scalar
12
+ from omegaconf import DictConfig
13
+
14
+ from transformers import TrainingArguments
15
+ from trl import SFTTrainer
16
+
17
+ from $import_name.dataset import (
18
+ get_tokenizer_and_data_collator_and_propt_formatting,
19
+ load_data,
20
+ replace_keys,
21
+ )
22
+ from $import_name.models import (
23
+ cosine_annealing,
24
+ get_model,
25
+ set_parameters,
26
+ get_parameters,
27
+ )
28
+
29
+ # Avoid warnings
30
+ os.environ["TOKENIZERS_PARALLELISM"] = "true"
31
+ os.environ["RAY_DISABLE_DOCKER_CPU_WARNING"] = "1"
32
+ warnings.filterwarnings("ignore", category=UserWarning)
33
+
34
+
35
+ # pylint: disable=too-many-arguments
36
+ # pylint: disable=too-many-instance-attributes
37
+ class FlowerClient(NumPyClient):
38
+ """Standard Flower client for CNN training."""
39
+
40
+ def __init__(
41
+ self,
42
+ model_cfg: DictConfig,
43
+ train_cfg: DictConfig,
44
+ trainset,
45
+ tokenizer,
46
+ formatting_prompts_func,
47
+ data_collator,
48
+ num_rounds,
49
+ ): # pylint: disable=too-many-arguments
50
+ self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
51
+ self.train_cfg = train_cfg
52
+ self.training_argumnets = TrainingArguments(**train_cfg.training_arguments)
53
+ self.tokenizer = tokenizer
54
+ self.formatting_prompts_func = formatting_prompts_func
55
+ self.data_collator = data_collator
56
+ self.num_rounds = num_rounds
57
+ self.trainset = trainset
58
+
59
+ # instantiate model
60
+ self.model = get_model(model_cfg)
61
+
62
+ def fit(
63
+ self, parameters: NDArrays, config: Dict[str, Scalar]
64
+ ) -> Tuple[NDArrays, int, Dict]:
65
+ """Implement distributed fit function for a given client."""
66
+ set_parameters(self.model, parameters)
67
+
68
+ new_lr = cosine_annealing(
69
+ int(config["current_round"]),
70
+ self.num_rounds,
71
+ self.train_cfg.learning_rate_max,
72
+ self.train_cfg.learning_rate_min,
73
+ )
74
+
75
+ self.training_argumnets.learning_rate = new_lr
76
+ self.training_argumnets.output_dir = config["save_path"]
77
+
78
+ # Construct trainer
79
+ trainer = SFTTrainer(
80
+ model=self.model,
81
+ tokenizer=self.tokenizer,
82
+ args=self.training_argumnets,
83
+ max_seq_length=self.train_cfg.seq_length,
84
+ train_dataset=self.trainset,
85
+ formatting_func=self.formatting_prompts_func,
86
+ data_collator=self.data_collator,
87
+ )
88
+
89
+ # Do local training
90
+ results = trainer.train()
91
+
92
+ return (
93
+ get_parameters(self.model),
94
+ len(self.trainset),
95
+ {"train_loss": results.training_loss},
96
+ )
97
+
98
+
99
+ def client_fn(context: Context) -> FlowerClient:
100
+ """Create a Flower client representing a single organization."""
101
+ partition_id = context.node_config["partition-id"]
102
+ num_partitions = context.node_config["num-partitions"]
103
+ num_rounds = context.run_config["num-server-rounds"]
104
+ cfg = DictConfig(replace_keys(unflatten_dict(context.run_config)))
105
+
106
+ # Let's get the client partition
107
+ client_trainset = load_data(partition_id, num_partitions, cfg.static.dataset.name)
108
+ (
109
+ tokenizer,
110
+ data_collator,
111
+ formatting_prompts_func,
112
+ ) = get_tokenizer_and_data_collator_and_propt_formatting(cfg.model.name)
113
+
114
+ return FlowerClient(
115
+ cfg.model,
116
+ cfg.train,
117
+ client_trainset,
118
+ tokenizer,
119
+ formatting_prompts_func,
120
+ data_collator,
121
+ num_rounds,
122
+ ).to_client()
123
+
124
+
125
+ # Flower ClientApp
126
+ app = ClientApp(client_fn)