flwr-nightly 1.8.0.dev20240315__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.dev20240315.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.dev20240315.dist-info/RECORD +0 -211
  309. flwr_nightly-1.8.0.dev20240315.dist-info/entry_points.txt +0 -9
  310. {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/LICENSE +0 -0
  311. {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/WHEEL +0 -0
@@ -1,4 +1,4 @@
1
- # Copyright 2020 Flower Labs GmbH. All Rights Reserved.
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -15,68 +15,175 @@
15
15
  """Fleet API gRPC request-response servicer."""
16
16
 
17
17
 
18
- from logging import INFO
18
+ from logging import DEBUG, INFO
19
19
 
20
20
  import grpc
21
21
 
22
22
  from flwr.common.logger import log
23
+ from flwr.common.typing import InvalidRunStatusException
23
24
  from flwr.proto import fleet_pb2_grpc # pylint: disable=E0611
25
+ from flwr.proto.fab_pb2 import GetFabRequest, GetFabResponse # pylint: disable=E0611
24
26
  from flwr.proto.fleet_pb2 import ( # pylint: disable=E0611
25
27
  CreateNodeRequest,
26
28
  CreateNodeResponse,
27
29
  DeleteNodeRequest,
28
30
  DeleteNodeResponse,
31
+ PingRequest,
32
+ PingResponse,
33
+ PullMessagesRequest,
34
+ PullMessagesResponse,
29
35
  PullTaskInsRequest,
30
36
  PullTaskInsResponse,
37
+ PushMessagesRequest,
38
+ PushMessagesResponse,
31
39
  PushTaskResRequest,
32
40
  PushTaskResResponse,
33
41
  )
42
+ from flwr.proto.run_pb2 import GetRunRequest, GetRunResponse # pylint: disable=E0611
43
+ from flwr.server.superlink.ffs.ffs_factory import FfsFactory
34
44
  from flwr.server.superlink.fleet.message_handler import message_handler
35
- from flwr.server.superlink.state import StateFactory
45
+ from flwr.server.superlink.linkstate import LinkStateFactory
46
+ from flwr.server.superlink.utils import abort_grpc_context
36
47
 
37
48
 
38
49
  class FleetServicer(fleet_pb2_grpc.FleetServicer):
39
50
  """Fleet API servicer."""
40
51
 
41
- def __init__(self, state_factory: StateFactory) -> None:
52
+ def __init__(
53
+ self, state_factory: LinkStateFactory, ffs_factory: FfsFactory
54
+ ) -> None:
42
55
  self.state_factory = state_factory
56
+ self.ffs_factory = ffs_factory
43
57
 
44
58
  def CreateNode(
45
59
  self, request: CreateNodeRequest, context: grpc.ServicerContext
46
60
  ) -> CreateNodeResponse:
47
61
  """."""
48
- log(INFO, "FleetServicer.CreateNode")
49
- return message_handler.create_node(
62
+ log(INFO, "[Fleet.CreateNode] Request ping_interval=%s", request.ping_interval)
63
+ log(DEBUG, "[Fleet.CreateNode] Request: %s", request)
64
+ response = message_handler.create_node(
50
65
  request=request,
51
66
  state=self.state_factory.state(),
52
67
  )
68
+ log(INFO, "[Fleet.CreateNode] Created node_id=%s", response.node.node_id)
69
+ log(DEBUG, "[Fleet.CreateNode] Response: %s", response)
70
+ return response
53
71
 
54
72
  def DeleteNode(
55
73
  self, request: DeleteNodeRequest, context: grpc.ServicerContext
56
74
  ) -> DeleteNodeResponse:
57
75
  """."""
58
- log(INFO, "FleetServicer.DeleteNode")
76
+ log(INFO, "[Fleet.DeleteNode] Delete node_id=%s", request.node.node_id)
77
+ log(DEBUG, "[Fleet.DeleteNode] Request: %s", request)
59
78
  return message_handler.delete_node(
60
79
  request=request,
61
80
  state=self.state_factory.state(),
62
81
  )
63
82
 
83
+ def Ping(self, request: PingRequest, context: grpc.ServicerContext) -> PingResponse:
84
+ """."""
85
+ log(DEBUG, "[Fleet.Ping] Request: %s", request)
86
+ return message_handler.ping(
87
+ request=request,
88
+ state=self.state_factory.state(),
89
+ )
90
+
64
91
  def PullTaskIns(
65
92
  self, request: PullTaskInsRequest, context: grpc.ServicerContext
66
93
  ) -> PullTaskInsResponse:
67
94
  """Pull TaskIns."""
68
- log(INFO, "FleetServicer.PullTaskIns")
95
+ log(INFO, "[Fleet.PullTaskIns] node_id=%s", request.node.node_id)
96
+ log(DEBUG, "[Fleet.PullTaskIns] Request: %s", request)
69
97
  return message_handler.pull_task_ins(
70
98
  request=request,
71
99
  state=self.state_factory.state(),
72
100
  )
73
101
 
102
+ def PullMessages(
103
+ self, request: PullMessagesRequest, context: grpc.ServicerContext
104
+ ) -> PullMessagesResponse:
105
+ """Pull Messages."""
106
+ log(INFO, "[Fleet.PullMessages] node_id=%s", request.node.node_id)
107
+ log(DEBUG, "[Fleet.PullMessages] Request: %s", request)
108
+ return message_handler.pull_messages(
109
+ request=request,
110
+ state=self.state_factory.state(),
111
+ )
112
+
74
113
  def PushTaskRes(
75
114
  self, request: PushTaskResRequest, context: grpc.ServicerContext
76
115
  ) -> PushTaskResResponse:
77
116
  """Push TaskRes."""
78
- log(INFO, "FleetServicer.PushTaskRes")
79
- return message_handler.push_task_res(
80
- request=request,
81
- state=self.state_factory.state(),
82
- )
117
+ if request.task_res_list:
118
+ log(
119
+ INFO,
120
+ "[Fleet.PushTaskRes] Push results from node_id=%s",
121
+ request.task_res_list[0].task.producer.node_id,
122
+ )
123
+ else:
124
+ log(INFO, "[Fleet.PushTaskRes] No task results to push")
125
+
126
+ try:
127
+ res = message_handler.push_task_res(
128
+ request=request,
129
+ state=self.state_factory.state(),
130
+ )
131
+ except InvalidRunStatusException as e:
132
+ abort_grpc_context(e.message, context)
133
+
134
+ return res
135
+
136
+ def PushMessages(
137
+ self, request: PushMessagesRequest, context: grpc.ServicerContext
138
+ ) -> PushMessagesResponse:
139
+ """Push Messages."""
140
+ if request.messages_list:
141
+ log(
142
+ INFO,
143
+ "[Fleet.PushMessages] Push results from node_id=%s",
144
+ request.messages_list[0].metadata.src_node_id,
145
+ )
146
+ else:
147
+ log(INFO, "[Fleet.PushMessages] No task results to push")
148
+
149
+ try:
150
+ res = message_handler.push_messages(
151
+ request=request,
152
+ state=self.state_factory.state(),
153
+ )
154
+ except InvalidRunStatusException as e:
155
+ abort_grpc_context(e.message, context)
156
+
157
+ return res
158
+
159
+ def GetRun(
160
+ self, request: GetRunRequest, context: grpc.ServicerContext
161
+ ) -> GetRunResponse:
162
+ """Get run information."""
163
+ log(INFO, "[Fleet.GetRun] Requesting `Run` for run_id=%s", request.run_id)
164
+
165
+ try:
166
+ res = message_handler.get_run(
167
+ request=request,
168
+ state=self.state_factory.state(),
169
+ )
170
+ except InvalidRunStatusException as e:
171
+ abort_grpc_context(e.message, context)
172
+
173
+ return res
174
+
175
+ def GetFab(
176
+ self, request: GetFabRequest, context: grpc.ServicerContext
177
+ ) -> GetFabResponse:
178
+ """Get FAB."""
179
+ log(INFO, "[Fleet.GetFab] Requesting FAB for fab_hash=%s", request.hash_str)
180
+ try:
181
+ res = message_handler.get_fab(
182
+ request=request,
183
+ ffs=self.ffs_factory.ffs(),
184
+ state=self.state_factory.state(),
185
+ )
186
+ except InvalidRunStatusException as e:
187
+ abort_grpc_context(e.message, context)
188
+
189
+ return res
@@ -0,0 +1,228 @@
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 server interceptor."""
16
+
17
+
18
+ import base64
19
+ from collections.abc import Sequence
20
+ from logging import INFO, WARNING
21
+ from typing import Any, Callable, Optional, Union
22
+
23
+ import grpc
24
+ from cryptography.hazmat.primitives.asymmetric import ec
25
+
26
+ from flwr.common.logger import log
27
+ from flwr.common.secure_aggregation.crypto.symmetric_encryption import (
28
+ bytes_to_private_key,
29
+ bytes_to_public_key,
30
+ generate_shared_key,
31
+ verify_hmac,
32
+ )
33
+ from flwr.proto.fab_pb2 import GetFabRequest, GetFabResponse # pylint: disable=E0611
34
+ from flwr.proto.fleet_pb2 import ( # pylint: disable=E0611
35
+ CreateNodeRequest,
36
+ CreateNodeResponse,
37
+ DeleteNodeRequest,
38
+ DeleteNodeResponse,
39
+ PingRequest,
40
+ PingResponse,
41
+ PullTaskInsRequest,
42
+ PullTaskInsResponse,
43
+ PushTaskResRequest,
44
+ PushTaskResResponse,
45
+ )
46
+ from flwr.proto.node_pb2 import Node # pylint: disable=E0611
47
+ from flwr.proto.run_pb2 import GetRunRequest, GetRunResponse # pylint: disable=E0611
48
+ from flwr.server.superlink.linkstate import LinkStateFactory
49
+
50
+ _PUBLIC_KEY_HEADER = "public-key"
51
+ _AUTH_TOKEN_HEADER = "auth-token"
52
+
53
+ Request = Union[
54
+ CreateNodeRequest,
55
+ DeleteNodeRequest,
56
+ PullTaskInsRequest,
57
+ PushTaskResRequest,
58
+ GetRunRequest,
59
+ PingRequest,
60
+ GetFabRequest,
61
+ ]
62
+
63
+ Response = Union[
64
+ CreateNodeResponse,
65
+ DeleteNodeResponse,
66
+ PullTaskInsResponse,
67
+ PushTaskResResponse,
68
+ GetRunResponse,
69
+ PingResponse,
70
+ GetFabResponse,
71
+ ]
72
+
73
+
74
+ def _get_value_from_tuples(
75
+ key_string: str, tuples: Sequence[tuple[str, Union[str, bytes]]]
76
+ ) -> bytes:
77
+ value = next((value for key, value in tuples if key == key_string), "")
78
+ if isinstance(value, str):
79
+ return value.encode()
80
+
81
+ return value
82
+
83
+
84
+ class AuthenticateServerInterceptor(grpc.ServerInterceptor): # type: ignore
85
+ """Server interceptor for node authentication."""
86
+
87
+ def __init__(self, state_factory: LinkStateFactory):
88
+ self.state_factory = state_factory
89
+ state = self.state_factory.state()
90
+
91
+ self.node_public_keys = state.get_node_public_keys()
92
+ if len(self.node_public_keys) == 0:
93
+ log(WARNING, "Authentication enabled, but no known public keys configured")
94
+
95
+ private_key = state.get_server_private_key()
96
+ public_key = state.get_server_public_key()
97
+
98
+ if private_key is None or public_key is None:
99
+ raise ValueError("Error loading authentication keys")
100
+
101
+ self.server_private_key = bytes_to_private_key(private_key)
102
+ self.encoded_server_public_key = base64.urlsafe_b64encode(public_key)
103
+
104
+ def intercept_service(
105
+ self,
106
+ continuation: Callable[[Any], Any],
107
+ handler_call_details: grpc.HandlerCallDetails,
108
+ ) -> grpc.RpcMethodHandler:
109
+ """Flower server interceptor authentication logic.
110
+
111
+ Intercept all unary calls from nodes and authenticate nodes by validating auth
112
+ metadata sent by the node. Continue RPC call if node is authenticated, else,
113
+ terminate RPC call by setting context to abort.
114
+ """
115
+ # One of the method handlers in
116
+ # `flwr.server.superlink.fleet.grpc_rere.fleet_server.FleetServicer`
117
+ method_handler: grpc.RpcMethodHandler = continuation(handler_call_details)
118
+ return self._generic_auth_unary_method_handler(method_handler)
119
+
120
+ def _generic_auth_unary_method_handler(
121
+ self, method_handler: grpc.RpcMethodHandler
122
+ ) -> grpc.RpcMethodHandler:
123
+ def _generic_method_handler(
124
+ request: Request,
125
+ context: grpc.ServicerContext,
126
+ ) -> Response:
127
+ node_public_key_bytes = base64.urlsafe_b64decode(
128
+ _get_value_from_tuples(
129
+ _PUBLIC_KEY_HEADER, context.invocation_metadata()
130
+ )
131
+ )
132
+ if node_public_key_bytes not in self.node_public_keys:
133
+ context.abort(grpc.StatusCode.UNAUTHENTICATED, "Access denied")
134
+
135
+ if isinstance(request, CreateNodeRequest):
136
+ response = self._create_authenticated_node(
137
+ node_public_key_bytes, request, context
138
+ )
139
+ log(
140
+ INFO,
141
+ "AuthenticateServerInterceptor: Created node_id=%s",
142
+ response.node.node_id,
143
+ )
144
+ return response
145
+
146
+ # Verify hmac value
147
+ hmac_value = base64.urlsafe_b64decode(
148
+ _get_value_from_tuples(
149
+ _AUTH_TOKEN_HEADER, context.invocation_metadata()
150
+ )
151
+ )
152
+ public_key = bytes_to_public_key(node_public_key_bytes)
153
+
154
+ if not self._verify_hmac(public_key, request, hmac_value):
155
+ context.abort(grpc.StatusCode.UNAUTHENTICATED, "Access denied")
156
+
157
+ # Verify node_id
158
+ node_id = self.state_factory.state().get_node_id(node_public_key_bytes)
159
+
160
+ if not self._verify_node_id(node_id, request):
161
+ context.abort(grpc.StatusCode.UNAUTHENTICATED, "Access denied")
162
+
163
+ return method_handler.unary_unary(request, context) # type: ignore
164
+
165
+ return grpc.unary_unary_rpc_method_handler(
166
+ _generic_method_handler,
167
+ request_deserializer=method_handler.request_deserializer,
168
+ response_serializer=method_handler.response_serializer,
169
+ )
170
+
171
+ def _verify_node_id(
172
+ self,
173
+ node_id: Optional[int],
174
+ request: Union[
175
+ DeleteNodeRequest,
176
+ PullTaskInsRequest,
177
+ PushTaskResRequest,
178
+ GetRunRequest,
179
+ PingRequest,
180
+ GetFabRequest,
181
+ ],
182
+ ) -> bool:
183
+ if node_id is None:
184
+ return False
185
+ if isinstance(request, PushTaskResRequest):
186
+ if len(request.task_res_list) == 0:
187
+ return False
188
+ return request.task_res_list[0].task.producer.node_id == node_id
189
+ if isinstance(request, GetRunRequest):
190
+ return node_id in self.state_factory.state().get_nodes(request.run_id)
191
+ return request.node.node_id == node_id
192
+
193
+ def _verify_hmac(
194
+ self, public_key: ec.EllipticCurvePublicKey, request: Request, hmac_value: bytes
195
+ ) -> bool:
196
+ shared_secret = generate_shared_key(self.server_private_key, public_key)
197
+ message_bytes = request.SerializeToString(deterministic=True)
198
+ return verify_hmac(shared_secret, message_bytes, hmac_value)
199
+
200
+ def _create_authenticated_node(
201
+ self,
202
+ public_key_bytes: bytes,
203
+ request: CreateNodeRequest,
204
+ context: grpc.ServicerContext,
205
+ ) -> CreateNodeResponse:
206
+ context.send_initial_metadata(
207
+ (
208
+ (
209
+ _PUBLIC_KEY_HEADER,
210
+ self.encoded_server_public_key,
211
+ ),
212
+ )
213
+ )
214
+ state = self.state_factory.state()
215
+ node_id = state.get_node_id(public_key_bytes)
216
+
217
+ # Handle `CreateNode` here instead of calling the default method handler
218
+ # Return previously assigned `node_id` for the provided `public_key`
219
+ if node_id is not None:
220
+ state.acknowledge_ping(node_id, request.ping_interval)
221
+ return CreateNodeResponse(node=Node(node_id=node_id, anonymous=False))
222
+
223
+ # No `node_id` exists for the provided `public_key`
224
+ # Handle `CreateNode` here instead of calling the default method handler
225
+ # Note: the innermost `CreateNode` method will never be called
226
+ node_id = state.create_node(request.ping_interval)
227
+ state.set_node_public_key(node_id, public_key_bytes)
228
+ return CreateNodeResponse(node=Node(node_id=node_id, anonymous=False))
@@ -1,4 +1,4 @@
1
- # Copyright 2020 Flower Labs GmbH. All Rights Reserved.
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- # Copyright 2020 Flower Labs GmbH. All Rights Reserved.
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
@@ -15,36 +15,61 @@
15
15
  """Fleet API message handlers."""
16
16
 
17
17
 
18
- from typing import List, Optional
18
+ import time
19
+ from typing import Optional
19
20
  from uuid import UUID
20
21
 
22
+ from flwr.common.constant import Status
23
+ from flwr.common.serde import (
24
+ fab_to_proto,
25
+ message_from_proto,
26
+ message_from_taskins,
27
+ message_to_proto,
28
+ message_to_taskres,
29
+ user_config_to_proto,
30
+ )
31
+ from flwr.common.typing import Fab, InvalidRunStatusException
32
+ from flwr.proto.fab_pb2 import GetFabRequest, GetFabResponse # pylint: disable=E0611
21
33
  from flwr.proto.fleet_pb2 import ( # pylint: disable=E0611
22
34
  CreateNodeRequest,
23
35
  CreateNodeResponse,
24
36
  DeleteNodeRequest,
25
37
  DeleteNodeResponse,
38
+ PingRequest,
39
+ PingResponse,
40
+ PullMessagesRequest,
41
+ PullMessagesResponse,
26
42
  PullTaskInsRequest,
27
43
  PullTaskInsResponse,
44
+ PushMessagesRequest,
45
+ PushMessagesResponse,
28
46
  PushTaskResRequest,
29
47
  PushTaskResResponse,
30
48
  Reconnect,
31
49
  )
32
50
  from flwr.proto.node_pb2 import Node # pylint: disable=E0611
51
+ from flwr.proto.run_pb2 import ( # pylint: disable=E0611
52
+ GetRunRequest,
53
+ GetRunResponse,
54
+ Run,
55
+ )
33
56
  from flwr.proto.task_pb2 import TaskIns, TaskRes # pylint: disable=E0611
34
- from flwr.server.superlink.state import State
57
+ from flwr.server.superlink.ffs.ffs import Ffs
58
+ from flwr.server.superlink.linkstate import LinkState
59
+ from flwr.server.superlink.utils import check_abort
35
60
 
36
61
 
37
62
  def create_node(
38
63
  request: CreateNodeRequest, # pylint: disable=unused-argument
39
- state: State,
64
+ state: LinkState,
40
65
  ) -> CreateNodeResponse:
41
66
  """."""
42
67
  # Create node
43
- node_id = state.create_node()
68
+ node_id = state.create_node(ping_interval=request.ping_interval)
44
69
  return CreateNodeResponse(node=Node(node_id=node_id, anonymous=False))
45
70
 
46
71
 
47
- def delete_node(request: DeleteNodeRequest, state: State) -> DeleteNodeResponse:
72
+ def delete_node(request: DeleteNodeRequest, state: LinkState) -> DeleteNodeResponse:
48
73
  """."""
49
74
  # Validate node_id
50
75
  if request.node.anonymous or request.node.node_id == 0:
@@ -55,14 +80,23 @@ def delete_node(request: DeleteNodeRequest, state: State) -> DeleteNodeResponse:
55
80
  return DeleteNodeResponse()
56
81
 
57
82
 
58
- def pull_task_ins(request: PullTaskInsRequest, state: State) -> PullTaskInsResponse:
83
+ def ping(
84
+ request: PingRequest, # pylint: disable=unused-argument
85
+ state: LinkState, # pylint: disable=unused-argument
86
+ ) -> PingResponse:
87
+ """."""
88
+ res = state.acknowledge_ping(request.node.node_id, request.ping_interval)
89
+ return PingResponse(success=res)
90
+
91
+
92
+ def pull_task_ins(request: PullTaskInsRequest, state: LinkState) -> PullTaskInsResponse:
59
93
  """Pull TaskIns handler."""
60
94
  # Get node_id if client node is not anonymous
61
95
  node = request.node # pylint: disable=no-member
62
96
  node_id: Optional[int] = None if node.anonymous else node.node_id
63
97
 
64
98
  # Retrieve TaskIns from State
65
- task_ins_list: List[TaskIns] = state.get_task_ins(node_id=node_id, limit=1)
99
+ task_ins_list: list[TaskIns] = state.get_task_ins(node_id=node_id, limit=1)
66
100
 
67
101
  # Build response
68
102
  response = PullTaskInsResponse(
@@ -71,12 +105,44 @@ def pull_task_ins(request: PullTaskInsRequest, state: State) -> PullTaskInsRespo
71
105
  return response
72
106
 
73
107
 
74
- def push_task_res(request: PushTaskResRequest, state: State) -> PushTaskResResponse:
108
+ def pull_messages(
109
+ request: PullMessagesRequest, state: LinkState
110
+ ) -> PullMessagesResponse:
111
+ """Pull Messages handler."""
112
+ # Get node_id if client node is not anonymous
113
+ node = request.node # pylint: disable=no-member
114
+ node_id: Optional[int] = None if node.anonymous else node.node_id
115
+
116
+ # Retrieve TaskIns from State
117
+ task_ins_list: list[TaskIns] = state.get_task_ins(node_id=node_id, limit=1)
118
+
119
+ # Convert to Messages
120
+ msg_proto = []
121
+ for task_ins in task_ins_list:
122
+ msg = message_from_taskins(task_ins)
123
+ msg_proto.append(message_to_proto(msg))
124
+
125
+ return PullMessagesResponse(messages_list=msg_proto)
126
+
127
+
128
+ def push_task_res(request: PushTaskResRequest, state: LinkState) -> PushTaskResResponse:
75
129
  """Push TaskRes handler."""
76
130
  # pylint: disable=no-member
77
131
  task_res: TaskRes = request.task_res_list[0]
78
132
  # pylint: enable=no-member
79
133
 
134
+ # Abort if the run is not running
135
+ abort_msg = check_abort(
136
+ task_res.run_id,
137
+ [Status.PENDING, Status.STARTING, Status.FINISHED],
138
+ state,
139
+ )
140
+ if abort_msg:
141
+ raise InvalidRunStatusException(abort_msg)
142
+
143
+ # Set pushed_at (timestamp in seconds)
144
+ task_res.task.pushed_at = time.time()
145
+
80
146
  # Store TaskRes in State
81
147
  task_id: Optional[UUID] = state.store_task_res(task_res=task_res)
82
148
 
@@ -86,3 +152,81 @@ def push_task_res(request: PushTaskResRequest, state: State) -> PushTaskResRespo
86
152
  results={str(task_id): 0},
87
153
  )
88
154
  return response
155
+
156
+
157
+ def push_messages(
158
+ request: PushMessagesRequest, state: LinkState
159
+ ) -> PushMessagesResponse:
160
+ """Push Messages handler."""
161
+ # Convert Message to TaskRes
162
+ msg = message_from_proto(message_proto=request.messages_list[0])
163
+ task_res = message_to_taskres(msg)
164
+
165
+ # Abort if the run is not running
166
+ abort_msg = check_abort(
167
+ task_res.run_id,
168
+ [Status.PENDING, Status.STARTING, Status.FINISHED],
169
+ state,
170
+ )
171
+ if abort_msg:
172
+ raise InvalidRunStatusException(abort_msg)
173
+
174
+ # Set pushed_at (timestamp in seconds)
175
+ task_res.task.pushed_at = time.time()
176
+
177
+ # Store TaskRes in State
178
+ message_id: Optional[UUID] = state.store_task_res(task_res=task_res)
179
+
180
+ # Build response
181
+ response = PushMessagesResponse(
182
+ reconnect=Reconnect(reconnect=5),
183
+ results={str(message_id): 0},
184
+ )
185
+ return response
186
+
187
+
188
+ def get_run(request: GetRunRequest, state: LinkState) -> GetRunResponse:
189
+ """Get run information."""
190
+ run = state.get_run(request.run_id)
191
+
192
+ if run is None:
193
+ return GetRunResponse()
194
+
195
+ # Abort if the run is not running
196
+ abort_msg = check_abort(
197
+ request.run_id,
198
+ [Status.PENDING, Status.STARTING, Status.FINISHED],
199
+ state,
200
+ )
201
+ if abort_msg:
202
+ raise InvalidRunStatusException(abort_msg)
203
+
204
+ return GetRunResponse(
205
+ run=Run(
206
+ run_id=run.run_id,
207
+ fab_id=run.fab_id,
208
+ fab_version=run.fab_version,
209
+ override_config=user_config_to_proto(run.override_config),
210
+ fab_hash=run.fab_hash,
211
+ )
212
+ )
213
+
214
+
215
+ def get_fab(
216
+ request: GetFabRequest, ffs: Ffs, state: LinkState # pylint: disable=W0613
217
+ ) -> GetFabResponse:
218
+ """Get FAB."""
219
+ # Abort if the run is not running
220
+ abort_msg = check_abort(
221
+ request.run_id,
222
+ [Status.PENDING, Status.STARTING, Status.FINISHED],
223
+ state,
224
+ )
225
+ if abort_msg:
226
+ raise InvalidRunStatusException(abort_msg)
227
+
228
+ if result := ffs.get(request.hash_str):
229
+ fab = Fab(request.hash_str, result[0])
230
+ return GetFabResponse(fab=fab_to_proto(fab))
231
+
232
+ raise ValueError(f"Found no FAB with hash: {request.hash_str}")
@@ -1,4 +1,4 @@
1
- # Copyright 2020 Flower Labs GmbH. All Rights Reserved.
1
+ # Copyright 2024 Flower Labs GmbH. All Rights Reserved.
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.