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
@@ -0,0 +1,389 @@
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
+ """Utility functions for State."""
16
+
17
+
18
+ from logging import ERROR
19
+ from os import urandom
20
+ from typing import Optional, Union
21
+ from uuid import UUID, uuid4
22
+
23
+ from flwr.common import ConfigsRecord, Context, log, now, serde
24
+ from flwr.common.constant import ErrorCode, Status, SubStatus
25
+ from flwr.common.typing import RunStatus
26
+
27
+ # pylint: disable=E0611
28
+ from flwr.proto.error_pb2 import Error
29
+ from flwr.proto.message_pb2 import Context as ProtoContext
30
+ from flwr.proto.node_pb2 import Node
31
+ from flwr.proto.recordset_pb2 import ConfigsRecord as ProtoConfigsRecord
32
+ from flwr.proto.task_pb2 import Task, TaskIns, TaskRes
33
+
34
+ # pylint: enable=E0611
35
+
36
+ NODE_UNAVAILABLE_ERROR_REASON = (
37
+ "Error: Node Unavailable - The destination node is currently unavailable. "
38
+ "It exceeds the time limit specified in its last ping."
39
+ )
40
+
41
+ VALID_RUN_STATUS_TRANSITIONS = {
42
+ (Status.PENDING, Status.STARTING),
43
+ (Status.STARTING, Status.RUNNING),
44
+ (Status.RUNNING, Status.FINISHED),
45
+ # Any non-FINISHED status can transition to FINISHED
46
+ (Status.PENDING, Status.FINISHED),
47
+ (Status.STARTING, Status.FINISHED),
48
+ }
49
+ VALID_RUN_SUB_STATUSES = {
50
+ SubStatus.COMPLETED,
51
+ SubStatus.FAILED,
52
+ SubStatus.STOPPED,
53
+ }
54
+ MESSAGE_UNAVAILABLE_ERROR_REASON = (
55
+ "Error: Message Unavailable - The requested message could not be found in the "
56
+ "database. It may have expired due to its TTL or never existed."
57
+ )
58
+ REPLY_MESSAGE_UNAVAILABLE_ERROR_REASON = (
59
+ "Error: Reply Message Unavailable - The reply message has expired."
60
+ )
61
+
62
+
63
+ def generate_rand_int_from_bytes(num_bytes: int) -> int:
64
+ """Generate a random unsigned integer from `num_bytes` bytes."""
65
+ return int.from_bytes(urandom(num_bytes), "little", signed=False)
66
+
67
+
68
+ def convert_uint64_to_sint64(u: int) -> int:
69
+ """Convert a uint64 value to a sint64 value with the same bit sequence.
70
+
71
+ Parameters
72
+ ----------
73
+ u : int
74
+ The unsigned 64-bit integer to convert.
75
+
76
+ Returns
77
+ -------
78
+ int
79
+ The signed 64-bit integer equivalent.
80
+
81
+ The signed 64-bit integer will have the same bit pattern as the
82
+ unsigned 64-bit integer but may have a different decimal value.
83
+
84
+ For numbers within the range [0, `sint64` max value], the decimal
85
+ value remains the same. However, for numbers greater than the `sint64`
86
+ max value, the decimal value will differ due to the wraparound caused
87
+ by the sign bit.
88
+ """
89
+ if u >= (1 << 63):
90
+ return u - (1 << 64)
91
+ return u
92
+
93
+
94
+ def convert_sint64_to_uint64(s: int) -> int:
95
+ """Convert a sint64 value to a uint64 value with the same bit sequence.
96
+
97
+ Parameters
98
+ ----------
99
+ s : int
100
+ The signed 64-bit integer to convert.
101
+
102
+ Returns
103
+ -------
104
+ int
105
+ The unsigned 64-bit integer equivalent.
106
+
107
+ The unsigned 64-bit integer will have the same bit pattern as the
108
+ signed 64-bit integer but may have a different decimal value.
109
+
110
+ For negative `sint64` values, the conversion adds 2^64 to the
111
+ signed value to obtain the equivalent `uint64` value. For non-negative
112
+ `sint64` values, the decimal value remains unchanged in the `uint64`
113
+ representation.
114
+ """
115
+ if s < 0:
116
+ return s + (1 << 64)
117
+ return s
118
+
119
+
120
+ def convert_uint64_values_in_dict_to_sint64(
121
+ data_dict: dict[str, int], keys: list[str]
122
+ ) -> None:
123
+ """Convert uint64 values to sint64 in the given dictionary.
124
+
125
+ Parameters
126
+ ----------
127
+ data_dict : dict[str, int]
128
+ A dictionary where the values are integers to be converted.
129
+ keys : list[str]
130
+ A list of keys in the dictionary whose values need to be converted.
131
+ """
132
+ for key in keys:
133
+ if key in data_dict:
134
+ data_dict[key] = convert_uint64_to_sint64(data_dict[key])
135
+
136
+
137
+ def convert_sint64_values_in_dict_to_uint64(
138
+ data_dict: dict[str, int], keys: list[str]
139
+ ) -> None:
140
+ """Convert sint64 values to uint64 in the given dictionary.
141
+
142
+ Parameters
143
+ ----------
144
+ data_dict : dict[str, int]
145
+ A dictionary where the values are integers to be converted.
146
+ keys : list[str]
147
+ A list of keys in the dictionary whose values need to be converted.
148
+ """
149
+ for key in keys:
150
+ if key in data_dict:
151
+ data_dict[key] = convert_sint64_to_uint64(data_dict[key])
152
+
153
+
154
+ def context_to_bytes(context: Context) -> bytes:
155
+ """Serialize `Context` to bytes."""
156
+ return serde.context_to_proto(context).SerializeToString()
157
+
158
+
159
+ def context_from_bytes(context_bytes: bytes) -> Context:
160
+ """Deserialize `Context` from bytes."""
161
+ return serde.context_from_proto(ProtoContext.FromString(context_bytes))
162
+
163
+
164
+ def configsrecord_to_bytes(configs_record: ConfigsRecord) -> bytes:
165
+ """Serialize a `ConfigsRecord` to bytes."""
166
+ return serde.configs_record_to_proto(configs_record).SerializeToString()
167
+
168
+
169
+ def configsrecord_from_bytes(configsrecord_bytes: bytes) -> ConfigsRecord:
170
+ """Deserialize `ConfigsRecord` from bytes."""
171
+ return serde.configs_record_from_proto(
172
+ ProtoConfigsRecord.FromString(configsrecord_bytes)
173
+ )
174
+
175
+
176
+ def is_valid_transition(current_status: RunStatus, new_status: RunStatus) -> bool:
177
+ """Check if a transition between two run statuses is valid.
178
+
179
+ Parameters
180
+ ----------
181
+ current_status : RunStatus
182
+ The current status of the run.
183
+ new_status : RunStatus
184
+ The new status to transition to.
185
+
186
+ Returns
187
+ -------
188
+ bool
189
+ True if the transition is valid, False otherwise.
190
+ """
191
+ # Transition to FINISHED from a non-RUNNING status is only allowed
192
+ # if the sub-status is not COMPLETED
193
+ if (
194
+ current_status.status in [Status.PENDING, Status.STARTING]
195
+ and new_status.status == Status.FINISHED
196
+ ):
197
+ return new_status.sub_status != SubStatus.COMPLETED
198
+
199
+ return (
200
+ current_status.status,
201
+ new_status.status,
202
+ ) in VALID_RUN_STATUS_TRANSITIONS
203
+
204
+
205
+ def has_valid_sub_status(status: RunStatus) -> bool:
206
+ """Check if the 'sub_status' field of the given status is valid.
207
+
208
+ Parameters
209
+ ----------
210
+ status : RunStatus
211
+ The status object to be checked.
212
+
213
+ Returns
214
+ -------
215
+ bool
216
+ True if the status object has a valid sub-status, False otherwise.
217
+
218
+ Notes
219
+ -----
220
+ Only an empty string (i.e., "") is considered a valid sub-status for
221
+ non-finished statuses. The sub-status of a finished status cannot be empty.
222
+ """
223
+ if status.status == Status.FINISHED:
224
+ return status.sub_status in VALID_RUN_SUB_STATUSES
225
+ return status.sub_status == ""
226
+
227
+
228
+ def create_taskres_for_unavailable_taskins(taskins_id: Union[str, UUID]) -> TaskRes:
229
+ """Generate a TaskRes with a TaskIns unavailable error.
230
+
231
+ Parameters
232
+ ----------
233
+ taskins_id : Union[str, UUID]
234
+ The ID of the unavailable TaskIns.
235
+
236
+ Returns
237
+ -------
238
+ TaskRes
239
+ A TaskRes with an error code MESSAGE_UNAVAILABLE to indicate that the
240
+ inquired TaskIns ID cannot be found (due to non-existence or expiration).
241
+ """
242
+ current_time = now().timestamp()
243
+ return TaskRes(
244
+ task_id=str(uuid4()),
245
+ group_id="", # Unknown group ID
246
+ run_id=0, # Unknown run ID
247
+ task=Task(
248
+ # This function is only called by SuperLink, and thus it's the producer.
249
+ producer=Node(node_id=0, anonymous=False),
250
+ consumer=Node(node_id=0, anonymous=False),
251
+ created_at=current_time,
252
+ ttl=0,
253
+ ancestry=[str(taskins_id)],
254
+ task_type="", # Unknown message type
255
+ error=Error(
256
+ code=ErrorCode.MESSAGE_UNAVAILABLE,
257
+ reason=MESSAGE_UNAVAILABLE_ERROR_REASON,
258
+ ),
259
+ ),
260
+ )
261
+
262
+
263
+ def create_taskres_for_unavailable_taskres(ref_taskins: TaskIns) -> TaskRes:
264
+ """Generate a TaskRes with a reply message unavailable error from a TaskIns.
265
+
266
+ Parameters
267
+ ----------
268
+ ref_taskins : TaskIns
269
+ The reference TaskIns object.
270
+
271
+ Returns
272
+ -------
273
+ TaskRes
274
+ The generated TaskRes with an error code REPLY_MESSAGE_UNAVAILABLE_ERROR_REASON,
275
+ indicating that the original TaskRes has expired.
276
+ """
277
+ current_time = now().timestamp()
278
+ ttl = ref_taskins.task.ttl - (current_time - ref_taskins.task.created_at)
279
+ if ttl < 0:
280
+ log(ERROR, "Creating TaskRes for TaskIns that exceeds its TTL.")
281
+ ttl = 0
282
+ return TaskRes(
283
+ task_id=str(uuid4()),
284
+ group_id=ref_taskins.group_id,
285
+ run_id=ref_taskins.run_id,
286
+ task=Task(
287
+ # This function is only called by SuperLink, and thus it's the producer.
288
+ producer=Node(node_id=0, anonymous=False),
289
+ consumer=Node(node_id=0, anonymous=False),
290
+ created_at=current_time,
291
+ ttl=ttl,
292
+ ancestry=[ref_taskins.task_id],
293
+ task_type=ref_taskins.task.task_type,
294
+ error=Error(
295
+ code=ErrorCode.REPLY_MESSAGE_UNAVAILABLE,
296
+ reason=REPLY_MESSAGE_UNAVAILABLE_ERROR_REASON,
297
+ ),
298
+ ),
299
+ )
300
+
301
+
302
+ def has_expired(task_ins_or_res: Union[TaskIns, TaskRes], current_time: float) -> bool:
303
+ """Check if the TaskIns/TaskRes has expired."""
304
+ return task_ins_or_res.task.ttl + task_ins_or_res.task.created_at < current_time
305
+
306
+
307
+ def verify_taskins_ids(
308
+ inquired_taskins_ids: set[UUID],
309
+ found_taskins_dict: dict[UUID, TaskIns],
310
+ current_time: Optional[float] = None,
311
+ update_set: bool = True,
312
+ ) -> dict[UUID, TaskRes]:
313
+ """Verify found TaskIns and generate error TaskRes for invalid ones.
314
+
315
+ Parameters
316
+ ----------
317
+ inquired_taskins_ids : set[UUID]
318
+ Set of TaskIns IDs for which to generate error TaskRes if invalid.
319
+ found_taskins_dict : dict[UUID, TaskIns]
320
+ Dictionary containing all found TaskIns indexed by their IDs.
321
+ current_time : Optional[float] (default: None)
322
+ The current time to check for expiration. If set to `None`, the current time
323
+ will automatically be set to the current timestamp using `now().timestamp()`.
324
+ update_set : bool (default: True)
325
+ If True, the `inquired_taskins_ids` will be updated to remove invalid ones,
326
+ by default True.
327
+
328
+ Returns
329
+ -------
330
+ dict[UUID, TaskRes]
331
+ A dictionary of error TaskRes indexed by the corresponding TaskIns ID.
332
+ """
333
+ ret_dict = {}
334
+ current = current_time if current_time else now().timestamp()
335
+ for taskins_id in list(inquired_taskins_ids):
336
+ # Generate error TaskRes if the task_ins doesn't exist or has expired
337
+ taskins = found_taskins_dict.get(taskins_id)
338
+ if taskins is None or has_expired(taskins, current):
339
+ if update_set:
340
+ inquired_taskins_ids.remove(taskins_id)
341
+ taskres = create_taskres_for_unavailable_taskins(taskins_id)
342
+ ret_dict[taskins_id] = taskres
343
+ return ret_dict
344
+
345
+
346
+ def verify_found_taskres(
347
+ inquired_taskins_ids: set[UUID],
348
+ found_taskins_dict: dict[UUID, TaskIns],
349
+ found_taskres_list: list[TaskRes],
350
+ current_time: Optional[float] = None,
351
+ update_set: bool = True,
352
+ ) -> dict[UUID, TaskRes]:
353
+ """Verify found TaskRes and generate error TaskRes for invalid ones.
354
+
355
+ Parameters
356
+ ----------
357
+ inquired_taskins_ids : set[UUID]
358
+ Set of TaskIns IDs for which to generate error TaskRes if invalid.
359
+ found_taskins_dict : dict[UUID, TaskIns]
360
+ Dictionary containing all found TaskIns indexed by their IDs.
361
+ found_taskres_list : dict[TaskIns, TaskRes]
362
+ List of found TaskRes to be verified.
363
+ current_time : Optional[float] (default: None)
364
+ The current time to check for expiration. If set to `None`, the current time
365
+ will automatically be set to the current timestamp using `now().timestamp()`.
366
+ update_set : bool (default: True)
367
+ If True, the `inquired_taskins_ids` will be updated to remove ones
368
+ that have a TaskRes, by default True.
369
+
370
+ Returns
371
+ -------
372
+ dict[UUID, TaskRes]
373
+ A dictionary of TaskRes indexed by the corresponding TaskIns ID.
374
+ """
375
+ ret_dict: dict[UUID, TaskRes] = {}
376
+ current = current_time if current_time else now().timestamp()
377
+ for taskres in found_taskres_list:
378
+ taskins_id = UUID(taskres.task.ancestry[0])
379
+ if update_set:
380
+ inquired_taskins_ids.remove(taskins_id)
381
+ # Check if the TaskRes has expired
382
+ if has_expired(taskres, current):
383
+ # No need to insert the error TaskRes
384
+ taskres = create_taskres_for_unavailable_taskres(
385
+ found_taskins_dict[taskins_id]
386
+ )
387
+ taskres.task.delivered_at = now().isoformat()
388
+ ret_dict[taskins_id] = taskres
389
+ return ret_dict
@@ -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 SimulationIo service."""
@@ -0,0 +1,65 @@
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
+ """SimulationIo gRPC API."""
16
+
17
+
18
+ from logging import INFO
19
+ from typing import Optional
20
+
21
+ import grpc
22
+
23
+ from flwr.common import GRPC_MAX_MESSAGE_LENGTH
24
+ from flwr.common.grpc import generic_create_grpc_server
25
+ from flwr.common.logger import log
26
+ from flwr.proto.simulationio_pb2_grpc import ( # pylint: disable=E0611
27
+ add_SimulationIoServicer_to_server,
28
+ )
29
+ from flwr.server.superlink.ffs.ffs_factory import FfsFactory
30
+ from flwr.server.superlink.linkstate import LinkStateFactory
31
+
32
+ from .simulationio_servicer import SimulationIoServicer
33
+
34
+
35
+ def run_simulationio_api_grpc(
36
+ address: str,
37
+ state_factory: LinkStateFactory,
38
+ ffs_factory: FfsFactory,
39
+ certificates: Optional[tuple[bytes, bytes, bytes]],
40
+ ) -> grpc.Server:
41
+ """Run SimulationIo API (gRPC, request-response)."""
42
+ # Create SimulationIo API gRPC server
43
+ simulationio_servicer: grpc.Server = SimulationIoServicer(
44
+ state_factory=state_factory,
45
+ ffs_factory=ffs_factory,
46
+ )
47
+ simulationio_add_servicer_to_server_fn = add_SimulationIoServicer_to_server
48
+ simulationio_grpc_server = generic_create_grpc_server(
49
+ servicer_and_add_fn=(
50
+ simulationio_servicer,
51
+ simulationio_add_servicer_to_server_fn,
52
+ ),
53
+ server_address=address,
54
+ max_message_length=GRPC_MAX_MESSAGE_LENGTH,
55
+ certificates=certificates,
56
+ )
57
+
58
+ log(
59
+ INFO,
60
+ "Flower Simulation Engine: Starting SimulationIo API on %s",
61
+ address,
62
+ )
63
+ simulationio_grpc_server.start()
64
+
65
+ return simulationio_grpc_server
@@ -0,0 +1,186 @@
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
+ """SimulationIo API servicer."""
16
+
17
+
18
+ import threading
19
+ from logging import DEBUG, INFO
20
+
21
+ import grpc
22
+ from grpc import ServicerContext
23
+
24
+ from flwr.common.constant import Status
25
+ from flwr.common.logger import log
26
+ from flwr.common.serde import (
27
+ configs_record_to_proto,
28
+ context_from_proto,
29
+ context_to_proto,
30
+ fab_to_proto,
31
+ run_status_from_proto,
32
+ run_status_to_proto,
33
+ run_to_proto,
34
+ )
35
+ from flwr.common.typing import Fab, RunStatus
36
+ from flwr.proto import simulationio_pb2_grpc
37
+ from flwr.proto.log_pb2 import ( # pylint: disable=E0611
38
+ PushLogsRequest,
39
+ PushLogsResponse,
40
+ )
41
+ from flwr.proto.run_pb2 import ( # pylint: disable=E0611
42
+ GetFederationOptionsRequest,
43
+ GetFederationOptionsResponse,
44
+ GetRunStatusRequest,
45
+ GetRunStatusResponse,
46
+ UpdateRunStatusRequest,
47
+ UpdateRunStatusResponse,
48
+ )
49
+ from flwr.proto.simulationio_pb2 import ( # pylint: disable=E0611
50
+ PullSimulationInputsRequest,
51
+ PullSimulationInputsResponse,
52
+ PushSimulationOutputsRequest,
53
+ PushSimulationOutputsResponse,
54
+ )
55
+ from flwr.server.superlink.ffs.ffs_factory import FfsFactory
56
+ from flwr.server.superlink.linkstate import LinkStateFactory
57
+ from flwr.server.superlink.utils import abort_if
58
+
59
+
60
+ class SimulationIoServicer(simulationio_pb2_grpc.SimulationIoServicer):
61
+ """SimulationIo API servicer."""
62
+
63
+ def __init__(
64
+ self, state_factory: LinkStateFactory, ffs_factory: FfsFactory
65
+ ) -> None:
66
+ self.state_factory = state_factory
67
+ self.ffs_factory = ffs_factory
68
+ self.lock = threading.RLock()
69
+
70
+ def PullSimulationInputs(
71
+ self, request: PullSimulationInputsRequest, context: ServicerContext
72
+ ) -> PullSimulationInputsResponse:
73
+ """Pull SimultionIo process inputs."""
74
+ log(DEBUG, "SimultionIoServicer.SimultionIoInputs")
75
+ # Init access to LinkState and Ffs
76
+ state = self.state_factory.state()
77
+ ffs = self.ffs_factory.ffs()
78
+
79
+ # Lock access to LinkState, preventing obtaining the same pending run_id
80
+ with self.lock:
81
+ # Attempt getting the run_id of a pending run
82
+ run_id = state.get_pending_run_id()
83
+ # If there's no pending run, return an empty response
84
+ if run_id is None:
85
+ return PullSimulationInputsResponse()
86
+
87
+ # Retrieve Context, Run and Fab for the run_id
88
+ serverapp_ctxt = state.get_serverapp_context(run_id)
89
+ run = state.get_run(run_id)
90
+ fab = None
91
+ if run and run.fab_hash:
92
+ if result := ffs.get(run.fab_hash):
93
+ fab = Fab(run.fab_hash, result[0])
94
+ if run and fab and serverapp_ctxt:
95
+ # Update run status to STARTING
96
+ if state.update_run_status(run_id, RunStatus(Status.STARTING, "", "")):
97
+ log(INFO, "Starting run %d", run_id)
98
+ return PullSimulationInputsResponse(
99
+ context=context_to_proto(serverapp_ctxt),
100
+ run=run_to_proto(run),
101
+ fab=fab_to_proto(fab),
102
+ )
103
+
104
+ # Raise an exception if the Run or Fab is not found,
105
+ # or if the status cannot be updated to STARTING
106
+ raise RuntimeError(f"Failed to start run {run_id}")
107
+
108
+ def PushSimulationOutputs(
109
+ self, request: PushSimulationOutputsRequest, context: ServicerContext
110
+ ) -> PushSimulationOutputsResponse:
111
+ """Push Simulation process outputs."""
112
+ log(DEBUG, "SimultionIoServicer.PushSimulationOutputs")
113
+ state = self.state_factory.state()
114
+
115
+ # Abort if the run is not running
116
+ abort_if(
117
+ request.run_id,
118
+ [Status.PENDING, Status.STARTING, Status.FINISHED],
119
+ state,
120
+ context,
121
+ )
122
+
123
+ state.set_serverapp_context(request.run_id, context_from_proto(request.context))
124
+ return PushSimulationOutputsResponse()
125
+
126
+ def UpdateRunStatus(
127
+ self, request: UpdateRunStatusRequest, context: grpc.ServicerContext
128
+ ) -> UpdateRunStatusResponse:
129
+ """Update the status of a run."""
130
+ log(DEBUG, "SimultionIoServicer.UpdateRunStatus")
131
+ state = self.state_factory.state()
132
+
133
+ # Abort if the run is finished
134
+ abort_if(request.run_id, [Status.FINISHED], state, context)
135
+
136
+ # Update the run status
137
+ state.update_run_status(
138
+ run_id=request.run_id, new_status=run_status_from_proto(request.run_status)
139
+ )
140
+ return UpdateRunStatusResponse()
141
+
142
+ def GetRunStatus(
143
+ self, request: GetRunStatusRequest, context: ServicerContext
144
+ ) -> GetRunStatusResponse:
145
+ """Get status of requested runs."""
146
+ log(DEBUG, "SimultionIoServicer.GetRunStatus")
147
+ state = self.state_factory.state()
148
+
149
+ statuses = state.get_run_status(set(request.run_ids))
150
+
151
+ return GetRunStatusResponse(
152
+ run_status_dict={
153
+ run_id: run_status_to_proto(status)
154
+ for run_id, status in statuses.items()
155
+ }
156
+ )
157
+
158
+ def PushLogs(
159
+ self, request: PushLogsRequest, context: grpc.ServicerContext
160
+ ) -> PushLogsResponse:
161
+ """Push logs."""
162
+ log(DEBUG, "SimultionIoServicer.PushLogs")
163
+ state = self.state_factory.state()
164
+
165
+ # Add logs to LinkState
166
+ merged_logs = "".join(request.logs)
167
+ state.add_serverapp_log(request.run_id, merged_logs)
168
+ return PushLogsResponse()
169
+
170
+ def GetFederationOptions(
171
+ self, request: GetFederationOptionsRequest, context: ServicerContext
172
+ ) -> GetFederationOptionsResponse:
173
+ """Get Federation Options associated with a run."""
174
+ log(DEBUG, "SimultionIoServicer.GetFederationOptions")
175
+ state = self.state_factory.state()
176
+
177
+ federation_options = state.get_federation_options(request.run_id)
178
+ if federation_options is None:
179
+ context.abort(
180
+ grpc.StatusCode.FAILED_PRECONDITION,
181
+ "Expected federation options to be set, but none available.",
182
+ )
183
+ return GetFederationOptionsResponse()
184
+ return GetFederationOptionsResponse(
185
+ federation_options=configs_record_to_proto(federation_options)
186
+ )