flwr-nightly 1.8.0.dev20240315__py3-none-any.whl → 1.15.0.dev20250115__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
Files changed (312) 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 +135 -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 +304 -23
  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 +2 -2
  169. flwr/proto/node_pb2.pyi +1 -4
  170. flwr/proto/recordset_pb2.py +35 -33
  171. flwr/proto/recordset_pb2.pyi +40 -14
  172. flwr/proto/run_pb2.py +64 -0
  173. flwr/proto/run_pb2.pyi +268 -0
  174. flwr/proto/run_pb2_grpc.py +4 -0
  175. flwr/proto/run_pb2_grpc.pyi +4 -0
  176. flwr/proto/serverappio_pb2.py +52 -0
  177. flwr/proto/{driver_pb2.pyi → serverappio_pb2.pyi} +62 -20
  178. flwr/proto/serverappio_pb2_grpc.py +410 -0
  179. flwr/proto/serverappio_pb2_grpc.pyi +160 -0
  180. flwr/proto/simulationio_pb2.py +38 -0
  181. flwr/proto/simulationio_pb2.pyi +65 -0
  182. flwr/proto/simulationio_pb2_grpc.py +239 -0
  183. flwr/proto/simulationio_pb2_grpc.pyi +94 -0
  184. flwr/proto/task_pb2.py +7 -8
  185. flwr/proto/task_pb2.pyi +8 -5
  186. flwr/proto/transport_pb2.py +8 -8
  187. flwr/proto/transport_pb2.pyi +9 -6
  188. flwr/server/__init__.py +2 -10
  189. flwr/server/app.py +579 -402
  190. flwr/server/client_manager.py +8 -6
  191. flwr/server/compat/app.py +6 -62
  192. flwr/server/compat/app_utils.py +14 -9
  193. flwr/server/compat/driver_client_proxy.py +25 -59
  194. flwr/server/compat/legacy_context.py +5 -4
  195. flwr/server/driver/__init__.py +2 -0
  196. flwr/server/driver/driver.py +36 -131
  197. flwr/server/driver/grpc_driver.py +220 -81
  198. flwr/server/driver/inmemory_driver.py +183 -0
  199. flwr/server/history.py +28 -29
  200. flwr/server/run_serverapp.py +15 -126
  201. flwr/server/server.py +50 -44
  202. flwr/server/server_app.py +59 -10
  203. flwr/server/serverapp/__init__.py +22 -0
  204. flwr/server/serverapp/app.py +256 -0
  205. flwr/server/serverapp_components.py +52 -0
  206. flwr/server/strategy/__init__.py +2 -2
  207. flwr/server/strategy/aggregate.py +37 -23
  208. flwr/server/strategy/bulyan.py +9 -9
  209. flwr/server/strategy/dp_adaptive_clipping.py +25 -25
  210. flwr/server/strategy/dp_fixed_clipping.py +23 -22
  211. flwr/server/strategy/dpfedavg_adaptive.py +8 -8
  212. flwr/server/strategy/dpfedavg_fixed.py +13 -12
  213. flwr/server/strategy/fault_tolerant_fedavg.py +11 -11
  214. flwr/server/strategy/fedadagrad.py +9 -9
  215. flwr/server/strategy/fedadam.py +20 -10
  216. flwr/server/strategy/fedavg.py +16 -16
  217. flwr/server/strategy/fedavg_android.py +17 -17
  218. flwr/server/strategy/fedavgm.py +9 -9
  219. flwr/server/strategy/fedmedian.py +5 -5
  220. flwr/server/strategy/fedopt.py +6 -6
  221. flwr/server/strategy/fedprox.py +7 -7
  222. flwr/server/strategy/fedtrimmedavg.py +8 -8
  223. flwr/server/strategy/fedxgb_bagging.py +12 -12
  224. flwr/server/strategy/fedxgb_cyclic.py +10 -10
  225. flwr/server/strategy/fedxgb_nn_avg.py +6 -6
  226. flwr/server/strategy/fedyogi.py +9 -9
  227. flwr/server/strategy/krum.py +9 -9
  228. flwr/server/strategy/qfedavg.py +16 -16
  229. flwr/server/strategy/strategy.py +10 -10
  230. flwr/server/superlink/driver/__init__.py +2 -2
  231. flwr/server/superlink/driver/serverappio_grpc.py +61 -0
  232. flwr/server/superlink/driver/serverappio_servicer.py +361 -0
  233. flwr/server/superlink/ffs/__init__.py +24 -0
  234. flwr/server/superlink/ffs/disk_ffs.py +108 -0
  235. flwr/server/superlink/ffs/ffs.py +79 -0
  236. flwr/server/superlink/ffs/ffs_factory.py +47 -0
  237. flwr/server/superlink/fleet/__init__.py +1 -1
  238. flwr/server/superlink/fleet/grpc_adapter/__init__.py +15 -0
  239. flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +162 -0
  240. flwr/server/superlink/fleet/grpc_bidi/__init__.py +1 -1
  241. flwr/server/superlink/fleet/grpc_bidi/flower_service_servicer.py +4 -2
  242. flwr/server/superlink/fleet/grpc_bidi/grpc_bridge.py +3 -2
  243. flwr/server/superlink/fleet/grpc_bidi/grpc_client_proxy.py +1 -1
  244. flwr/server/superlink/fleet/grpc_bidi/grpc_server.py +5 -154
  245. flwr/server/superlink/fleet/grpc_rere/__init__.py +1 -1
  246. flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +120 -13
  247. flwr/server/superlink/fleet/grpc_rere/server_interceptor.py +228 -0
  248. flwr/server/superlink/fleet/message_handler/__init__.py +1 -1
  249. flwr/server/superlink/fleet/message_handler/message_handler.py +156 -13
  250. flwr/server/superlink/fleet/rest_rere/__init__.py +1 -1
  251. flwr/server/superlink/fleet/rest_rere/rest_api.py +119 -81
  252. flwr/server/superlink/fleet/vce/__init__.py +1 -0
  253. flwr/server/superlink/fleet/vce/backend/__init__.py +4 -4
  254. flwr/server/superlink/fleet/vce/backend/backend.py +8 -9
  255. flwr/server/superlink/fleet/vce/backend/raybackend.py +87 -68
  256. flwr/server/superlink/fleet/vce/vce_api.py +208 -146
  257. flwr/server/superlink/linkstate/__init__.py +28 -0
  258. flwr/server/superlink/linkstate/in_memory_linkstate.py +569 -0
  259. flwr/server/superlink/linkstate/linkstate.py +376 -0
  260. flwr/server/superlink/{state/state_factory.py → linkstate/linkstate_factory.py} +19 -10
  261. flwr/server/superlink/linkstate/sqlite_linkstate.py +1196 -0
  262. flwr/server/superlink/linkstate/utils.py +399 -0
  263. flwr/server/superlink/simulation/__init__.py +15 -0
  264. flwr/server/superlink/simulation/simulationio_grpc.py +65 -0
  265. flwr/server/superlink/simulation/simulationio_servicer.py +186 -0
  266. flwr/server/superlink/utils.py +65 -0
  267. flwr/server/typing.py +2 -0
  268. flwr/server/utils/__init__.py +1 -1
  269. flwr/server/utils/tensorboard.py +5 -5
  270. flwr/server/utils/validator.py +40 -45
  271. flwr/server/workflow/default_workflows.py +70 -26
  272. flwr/server/workflow/secure_aggregation/secagg_workflow.py +1 -0
  273. flwr/server/workflow/secure_aggregation/secaggplus_workflow.py +40 -27
  274. flwr/simulation/__init__.py +12 -5
  275. flwr/simulation/app.py +247 -315
  276. flwr/simulation/legacy_app.py +404 -0
  277. flwr/simulation/ray_transport/__init__.py +1 -1
  278. flwr/simulation/ray_transport/ray_actor.py +42 -67
  279. flwr/simulation/ray_transport/ray_client_proxy.py +37 -17
  280. flwr/simulation/ray_transport/utils.py +1 -0
  281. flwr/simulation/run_simulation.py +306 -163
  282. flwr/simulation/simulationio_connection.py +89 -0
  283. flwr/superexec/__init__.py +15 -0
  284. flwr/superexec/app.py +59 -0
  285. flwr/superexec/deployment.py +188 -0
  286. flwr/superexec/exec_grpc.py +80 -0
  287. flwr/superexec/exec_servicer.py +231 -0
  288. flwr/superexec/exec_user_auth_interceptor.py +101 -0
  289. flwr/superexec/executor.py +96 -0
  290. flwr/superexec/simulation.py +124 -0
  291. {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250115.dist-info}/METADATA +33 -26
  292. flwr_nightly-1.15.0.dev20250115.dist-info/RECORD +328 -0
  293. flwr_nightly-1.15.0.dev20250115.dist-info/entry_points.txt +12 -0
  294. flwr/cli/flower_toml.py +0 -140
  295. flwr/cli/new/templates/app/flower.toml.tpl +0 -13
  296. flwr/cli/new/templates/app/requirements.numpy.txt.tpl +0 -2
  297. flwr/cli/new/templates/app/requirements.pytorch.txt.tpl +0 -4
  298. flwr/cli/new/templates/app/requirements.tensorflow.txt.tpl +0 -4
  299. flwr/client/node_state.py +0 -48
  300. flwr/client/node_state_tests.py +0 -65
  301. flwr/proto/driver_pb2.py +0 -44
  302. flwr/proto/driver_pb2_grpc.py +0 -169
  303. flwr/proto/driver_pb2_grpc.pyi +0 -66
  304. flwr/server/superlink/driver/driver_grpc.py +0 -54
  305. flwr/server/superlink/driver/driver_servicer.py +0 -129
  306. flwr/server/superlink/state/in_memory_state.py +0 -230
  307. flwr/server/superlink/state/sqlite_state.py +0 -630
  308. flwr/server/superlink/state/state.py +0 -154
  309. flwr_nightly-1.8.0.dev20240315.dist-info/RECORD +0 -211
  310. flwr_nightly-1.8.0.dev20240315.dist-info/entry_points.txt +0 -9
  311. {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250115.dist-info}/LICENSE +0 -0
  312. {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250115.dist-info}/WHEEL +0 -0
@@ -0,0 +1,244 @@
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
+ """ClientAppIo API servicer."""
16
+
17
+
18
+ from dataclasses import dataclass
19
+ from logging import DEBUG, ERROR
20
+ from typing import Optional, cast
21
+
22
+ import grpc
23
+
24
+ from flwr.common import Context, Message, typing
25
+ from flwr.common.logger import log
26
+ from flwr.common.serde import (
27
+ clientappstatus_to_proto,
28
+ context_from_proto,
29
+ context_to_proto,
30
+ fab_to_proto,
31
+ message_from_proto,
32
+ message_to_proto,
33
+ run_to_proto,
34
+ )
35
+ from flwr.common.typing import Fab, Run
36
+
37
+ # pylint: disable=E0611
38
+ from flwr.proto import clientappio_pb2_grpc
39
+ from flwr.proto.clientappio_pb2 import ( # pylint: disable=E0401
40
+ GetTokenRequest,
41
+ GetTokenResponse,
42
+ PullClientAppInputsRequest,
43
+ PullClientAppInputsResponse,
44
+ PushClientAppOutputsRequest,
45
+ PushClientAppOutputsResponse,
46
+ )
47
+
48
+
49
+ @dataclass
50
+ class ClientAppInputs:
51
+ """Specify the inputs to the ClientApp."""
52
+
53
+ message: Message
54
+ context: Context
55
+ run: Run
56
+ fab: Optional[Fab]
57
+ token: int
58
+
59
+
60
+ @dataclass
61
+ class ClientAppOutputs:
62
+ """Specify the outputs from the ClientApp."""
63
+
64
+ message: Message
65
+ context: Context
66
+
67
+
68
+ # pylint: disable=C0103,W0613,W0201
69
+ class ClientAppIoServicer(clientappio_pb2_grpc.ClientAppIoServicer):
70
+ """ClientAppIo API servicer."""
71
+
72
+ def __init__(self) -> None:
73
+ self.clientapp_input: Optional[ClientAppInputs] = None
74
+ self.clientapp_output: Optional[ClientAppOutputs] = None
75
+ self.token_returned: bool = False
76
+ self.inputs_returned: bool = False
77
+
78
+ def GetToken(
79
+ self, request: GetTokenRequest, context: grpc.ServicerContext
80
+ ) -> GetTokenResponse:
81
+ """Get token."""
82
+ log(DEBUG, "ClientAppIo.GetToken")
83
+
84
+ # Fail if no ClientAppInputs are available
85
+ if self.clientapp_input is None:
86
+ context.abort(
87
+ grpc.StatusCode.FAILED_PRECONDITION,
88
+ "No inputs available.",
89
+ )
90
+ clientapp_input = cast(ClientAppInputs, self.clientapp_input)
91
+
92
+ # Fail if token was already returned in a previous call
93
+ if self.token_returned:
94
+ context.abort(
95
+ grpc.StatusCode.FAILED_PRECONDITION,
96
+ "Token already returned. A token can be returned only once.",
97
+ )
98
+
99
+ # If
100
+ # - ClientAppInputs is set, and
101
+ # - token hasn't been returned before,
102
+ # return token
103
+ self.token_returned = True
104
+ return GetTokenResponse(token=clientapp_input.token)
105
+
106
+ def PullClientAppInputs(
107
+ self, request: PullClientAppInputsRequest, context: grpc.ServicerContext
108
+ ) -> PullClientAppInputsResponse:
109
+ """Pull Message, Context, and Run."""
110
+ log(DEBUG, "ClientAppIo.PullClientAppInputs")
111
+
112
+ # Fail if no ClientAppInputs are available
113
+ if self.clientapp_input is None:
114
+ context.abort(
115
+ grpc.StatusCode.FAILED_PRECONDITION,
116
+ "No inputs available.",
117
+ )
118
+ clientapp_input = cast(ClientAppInputs, self.clientapp_input)
119
+
120
+ # Fail if token wasn't returned in a previous call
121
+ if not self.token_returned:
122
+ context.abort(
123
+ grpc.StatusCode.FAILED_PRECONDITION,
124
+ "Token hasn't been returned."
125
+ "Token must be returned before can be returned only once.",
126
+ )
127
+
128
+ # Fail if token isn't matching
129
+ if request.token != clientapp_input.token:
130
+ context.abort(
131
+ grpc.StatusCode.INVALID_ARGUMENT,
132
+ "Mismatch between ClientApp and SuperNode token",
133
+ )
134
+
135
+ # Success
136
+ self.inputs_returned = True
137
+ return PullClientAppInputsResponse(
138
+ message=message_to_proto(clientapp_input.message),
139
+ context=context_to_proto(clientapp_input.context),
140
+ run=run_to_proto(clientapp_input.run),
141
+ fab=fab_to_proto(clientapp_input.fab) if clientapp_input.fab else None,
142
+ )
143
+
144
+ def PushClientAppOutputs(
145
+ self, request: PushClientAppOutputsRequest, context: grpc.ServicerContext
146
+ ) -> PushClientAppOutputsResponse:
147
+ """Push Message and Context."""
148
+ log(DEBUG, "ClientAppIo.PushClientAppOutputs")
149
+
150
+ # Fail if no ClientAppInputs are available
151
+ if not self.clientapp_input:
152
+ context.abort(
153
+ grpc.StatusCode.FAILED_PRECONDITION,
154
+ "No inputs available.",
155
+ )
156
+ clientapp_input = cast(ClientAppInputs, self.clientapp_input)
157
+
158
+ # Fail if token wasn't returned in a previous call
159
+ if not self.token_returned:
160
+ context.abort(
161
+ grpc.StatusCode.FAILED_PRECONDITION,
162
+ "Token hasn't been returned."
163
+ "Token must be returned before can be returned only once.",
164
+ )
165
+
166
+ # Fail if inputs weren't delivered in a previous call
167
+ if not self.inputs_returned:
168
+ context.abort(
169
+ grpc.StatusCode.FAILED_PRECONDITION,
170
+ "Inputs haven't been delivered."
171
+ "Inputs must be delivered before can be returned only once.",
172
+ )
173
+
174
+ # Fail if token isn't matching
175
+ if request.token != clientapp_input.token:
176
+ context.abort(
177
+ grpc.StatusCode.INVALID_ARGUMENT,
178
+ "Mismatch between ClientApp and SuperNode token",
179
+ )
180
+
181
+ # Preconditions met
182
+ try:
183
+ # Update Message and Context
184
+ self.clientapp_output = ClientAppOutputs(
185
+ message=message_from_proto(request.message),
186
+ context=context_from_proto(request.context),
187
+ )
188
+
189
+ # Set status
190
+ code = typing.ClientAppOutputCode.SUCCESS
191
+ status = typing.ClientAppOutputStatus(code=code, message="Success")
192
+ except Exception as e: # pylint: disable=broad-exception-caught
193
+ log(ERROR, "ClientApp failed to push message to SuperNode, %s", e)
194
+ code = typing.ClientAppOutputCode.UNKNOWN_ERROR
195
+ status = typing.ClientAppOutputStatus(code=code, message="Unkonwn error")
196
+
197
+ # Return status to ClientApp process
198
+ proto_status = clientappstatus_to_proto(status=status)
199
+ return PushClientAppOutputsResponse(status=proto_status)
200
+
201
+ def set_inputs(
202
+ self, clientapp_input: ClientAppInputs, token_returned: bool
203
+ ) -> None:
204
+ """Set ClientApp inputs.
205
+
206
+ Parameters
207
+ ----------
208
+ clientapp_input : ClientAppInputs
209
+ The inputs to the ClientApp.
210
+ token_returned : bool
211
+ A boolean indicating if the token has been returned.
212
+ Set to `True` when passing the token to `flwr-clientap`
213
+ and `False` otherwise.
214
+ """
215
+ if (
216
+ self.clientapp_input is not None
217
+ or self.clientapp_output is not None
218
+ or self.token_returned
219
+ ):
220
+ raise ValueError(
221
+ "ClientAppInputs and ClientAppOutputs must not be set before "
222
+ "calling `set_inputs`."
223
+ )
224
+ log(DEBUG, "ClientAppInputs set (token: %s)", clientapp_input.token)
225
+ self.clientapp_input = clientapp_input
226
+ self.token_returned = token_returned
227
+
228
+ def has_outputs(self) -> bool:
229
+ """Check if ClientAppOutputs are available."""
230
+ return self.clientapp_output is not None
231
+
232
+ def get_outputs(self) -> ClientAppOutputs:
233
+ """Get ClientApp outputs."""
234
+ if self.clientapp_output is None:
235
+ raise ValueError("ClientAppOutputs not set before calling `get_outputs`.")
236
+
237
+ # Set outputs to a local variable and clear state
238
+ output: ClientAppOutputs = self.clientapp_output
239
+ self.clientapp_input = None
240
+ self.clientapp_output = None
241
+ self.token_returned = False
242
+ self.inputs_returned = False
243
+
244
+ return output
@@ -0,0 +1,115 @@
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 ClientApp loading utils."""
16
+
17
+
18
+ from logging import DEBUG
19
+ from pathlib import Path
20
+ from typing import Callable, Optional
21
+
22
+ from flwr.client.client_app import ClientApp, LoadClientAppError
23
+ from flwr.common.config import (
24
+ get_flwr_dir,
25
+ get_metadata_from_config,
26
+ get_project_config,
27
+ get_project_dir,
28
+ )
29
+ from flwr.common.logger import log
30
+ from flwr.common.object_ref import load_app, validate
31
+
32
+
33
+ def get_load_client_app_fn(
34
+ default_app_ref: str,
35
+ app_path: Optional[str],
36
+ multi_app: bool,
37
+ flwr_dir: Optional[str] = None,
38
+ ) -> Callable[[str, str, str], ClientApp]:
39
+ """Get the load_client_app_fn function.
40
+
41
+ If `multi_app` is True, this function loads the specified ClientApp
42
+ based on `fab_id` and `fab_version`. If `fab_id` is empty, a default
43
+ ClientApp will be loaded.
44
+
45
+ If `multi_app` is False, it ignores `fab_id` and `fab_version` and
46
+ loads a default ClientApp.
47
+ """
48
+ if not multi_app:
49
+ log(
50
+ DEBUG,
51
+ "Flower SuperNode will load and validate ClientApp `%s`",
52
+ default_app_ref,
53
+ )
54
+
55
+ valid, error_msg = validate(default_app_ref, project_dir=app_path)
56
+ if not valid and error_msg:
57
+ raise LoadClientAppError(error_msg) from None
58
+
59
+ def _load(fab_id: str, fab_version: str, fab_hash: str) -> ClientApp:
60
+ runtime_app_dir = Path(app_path if app_path else "").absolute()
61
+ # If multi-app feature is disabled
62
+ if not multi_app:
63
+ # Set app reference
64
+ client_app_ref = default_app_ref
65
+ # If multi-app feature is enabled but app directory is provided.
66
+ # `fab_hash` is not required since the app is loaded from `runtime_app_dir`.
67
+ elif app_path is not None:
68
+ config = get_project_config(runtime_app_dir)
69
+ this_fab_version, this_fab_id = get_metadata_from_config(config)
70
+
71
+ if this_fab_version != fab_version or this_fab_id != fab_id:
72
+ raise LoadClientAppError(
73
+ f"FAB ID or version mismatch: Expected FAB ID '{this_fab_id}' and "
74
+ f"FAB version '{this_fab_version}', but received FAB ID '{fab_id}' "
75
+ f"and FAB version '{fab_version}'.",
76
+ ) from None
77
+
78
+ # log(WARN, "FAB ID is not provided; the default ClientApp will be loaded.")
79
+
80
+ # Set app reference
81
+ client_app_ref = config["tool"]["flwr"]["app"]["components"]["clientapp"]
82
+ # If multi-app feature is enabled
83
+ else:
84
+ try:
85
+ runtime_app_dir = get_project_dir(
86
+ fab_id, fab_version, fab_hash, get_flwr_dir(flwr_dir)
87
+ )
88
+ config = get_project_config(runtime_app_dir)
89
+ except Exception as e:
90
+ raise LoadClientAppError(
91
+ "Failed to load ClientApp."
92
+ "Possible reasons for error include mismatched "
93
+ "`fab_id`, `fab_version`, or `fab_hash` in "
94
+ f"{str(get_flwr_dir(flwr_dir).resolve())}."
95
+ ) from e
96
+
97
+ # Set app reference
98
+ client_app_ref = config["tool"]["flwr"]["app"]["components"]["clientapp"]
99
+
100
+ # Load ClientApp
101
+ log(
102
+ DEBUG,
103
+ "Loading ClientApp `%s`",
104
+ client_app_ref,
105
+ )
106
+ client_app = load_app(client_app_ref, LoadClientAppError, runtime_app_dir)
107
+
108
+ if not isinstance(client_app, ClientApp):
109
+ raise LoadClientAppError(
110
+ f"Attribute {client_app_ref} is not of type {ClientApp}",
111
+ ) from None
112
+
113
+ return client_app
114
+
115
+ return _load
@@ -1,4 +1,4 @@
1
- # Copyright 2020 Flower Labs GmbH. All Rights Reserved.
1
+ # Copyright 2022 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.
@@ -16,7 +16,6 @@
16
16
 
17
17
 
18
18
  import copy
19
- from typing import Dict, Tuple
20
19
 
21
20
  import numpy as np
22
21
 
@@ -39,7 +38,7 @@ class DPFedAvgNumPyClient(NumPyClient):
39
38
  super().__init__()
40
39
  self.client = client
41
40
 
42
- def get_properties(self, config: Config) -> Dict[str, Scalar]:
41
+ def get_properties(self, config: Config) -> dict[str, Scalar]:
43
42
  """Get client properties using the given Numpy client.
44
43
 
45
44
  Parameters
@@ -58,7 +57,7 @@ class DPFedAvgNumPyClient(NumPyClient):
58
57
  """
59
58
  return self.client.get_properties(config)
60
59
 
61
- def get_parameters(self, config: Dict[str, Scalar]) -> NDArrays:
60
+ def get_parameters(self, config: dict[str, Scalar]) -> NDArrays:
62
61
  """Return the current local model parameters.
63
62
 
64
63
  Parameters
@@ -76,8 +75,8 @@ class DPFedAvgNumPyClient(NumPyClient):
76
75
  return self.client.get_parameters(config)
77
76
 
78
77
  def fit(
79
- self, parameters: NDArrays, config: Dict[str, Scalar]
80
- ) -> Tuple[NDArrays, int, Dict[str, Scalar]]:
78
+ self, parameters: NDArrays, config: dict[str, Scalar]
79
+ ) -> tuple[NDArrays, int, dict[str, Scalar]]:
81
80
  """Train the provided parameters using the locally held dataset.
82
81
 
83
82
  This method first updates the local model using the original parameters
@@ -153,8 +152,8 @@ class DPFedAvgNumPyClient(NumPyClient):
153
152
  return updated_params, num_examples, metrics
154
153
 
155
154
  def evaluate(
156
- self, parameters: NDArrays, config: Dict[str, Scalar]
157
- ) -> Tuple[float, int, Dict[str, Scalar]]:
155
+ self, parameters: NDArrays, config: dict[str, Scalar]
156
+ ) -> tuple[float, int, dict[str, Scalar]]:
158
157
  """Evaluate the provided parameters using the locally held dataset.
159
158
 
160
159
  Parameters
@@ -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
+ """Client-side part of the GrpcAdapter transport layer."""
@@ -0,0 +1,98 @@
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
+ """Contextmanager for a GrpcAdapter channel to the Flower server."""
16
+
17
+
18
+ from collections.abc import Iterator
19
+ from contextlib import contextmanager
20
+ from logging import ERROR
21
+ from typing import Callable, Optional, Union
22
+
23
+ from cryptography.hazmat.primitives.asymmetric import ec
24
+
25
+ from flwr.client.grpc_rere_client.connection import grpc_request_response
26
+ from flwr.client.grpc_rere_client.grpc_adapter import GrpcAdapter
27
+ from flwr.common import GRPC_MAX_MESSAGE_LENGTH
28
+ from flwr.common.logger import log
29
+ from flwr.common.message import Message
30
+ from flwr.common.retry_invoker import RetryInvoker
31
+ from flwr.common.typing import Fab, Run
32
+
33
+
34
+ @contextmanager
35
+ def grpc_adapter( # pylint: disable=R0913,too-many-positional-arguments
36
+ server_address: str,
37
+ insecure: bool,
38
+ retry_invoker: RetryInvoker,
39
+ max_message_length: int = GRPC_MAX_MESSAGE_LENGTH, # pylint: disable=W0613
40
+ root_certificates: Optional[Union[bytes, str]] = None,
41
+ authentication_keys: Optional[ # pylint: disable=unused-argument
42
+ tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
43
+ ] = None,
44
+ ) -> Iterator[
45
+ tuple[
46
+ Callable[[], Optional[Message]],
47
+ Callable[[Message], None],
48
+ Optional[Callable[[], Optional[int]]],
49
+ Optional[Callable[[], None]],
50
+ Optional[Callable[[int], Run]],
51
+ Optional[Callable[[str, int], Fab]],
52
+ ]
53
+ ]:
54
+ """Primitives for request/response-based interaction with a server via GrpcAdapter.
55
+
56
+ Parameters
57
+ ----------
58
+ server_address : str
59
+ The IPv6 address of the server with `http://` or `https://`.
60
+ If the Flower server runs on the same machine
61
+ on port 8080, then `server_address` would be `"http://[::]:8080"`.
62
+ insecure : bool
63
+ Starts an insecure gRPC connection when True. Enables HTTPS connection
64
+ when False, using system certificates if `root_certificates` is None.
65
+ retry_invoker: RetryInvoker
66
+ `RetryInvoker` object that will try to reconnect the client to the server
67
+ after gRPC errors. If None, the client will only try to
68
+ reconnect once after a failure.
69
+ max_message_length : int
70
+ Ignored, only present to preserve API-compatibility.
71
+ root_certificates : Optional[Union[bytes, str]] (default: None)
72
+ Path of the root certificate. If provided, a secure
73
+ connection using the certificates will be established to an SSL-enabled
74
+ Flower server. Bytes won't work for the REST API.
75
+ authentication_keys : Optional[Tuple[PrivateKey, PublicKey]] (default: None)
76
+ Client authentication is not supported for this transport type.
77
+
78
+ Returns
79
+ -------
80
+ receive : Callable
81
+ send : Callable
82
+ create_node : Optional[Callable]
83
+ delete_node : Optional[Callable]
84
+ get_run : Optional[Callable]
85
+ get_fab : Optional[Callable]
86
+ """
87
+ if authentication_keys is not None:
88
+ log(ERROR, "Client authentication is not supported for this transport type.")
89
+ with grpc_request_response(
90
+ server_address=server_address,
91
+ insecure=insecure,
92
+ retry_invoker=retry_invoker,
93
+ max_message_length=max_message_length,
94
+ root_certificates=root_certificates,
95
+ authentication_keys=None, # Authentication is not supported
96
+ adapter_cls=GrpcAdapter,
97
+ ) as conn:
98
+ yield conn
@@ -16,13 +16,17 @@
16
16
 
17
17
 
18
18
  import uuid
19
+ from collections.abc import Iterator
19
20
  from contextlib import contextmanager
20
- from logging import DEBUG
21
+ from logging import DEBUG, ERROR
21
22
  from pathlib import Path
22
23
  from queue import Queue
23
- from typing import Callable, Iterator, Optional, Tuple, Union, cast
24
+ from typing import Callable, Optional, Union, cast
25
+
26
+ from cryptography.hazmat.primitives.asymmetric import ec
24
27
 
25
28
  from flwr.common import (
29
+ DEFAULT_TTL,
26
30
  GRPC_MAX_MESSAGE_LENGTH,
27
31
  ConfigsRecord,
28
32
  Message,
@@ -35,6 +39,7 @@ from flwr.common.constant import MessageType, MessageTypeLegacy
35
39
  from flwr.common.grpc import create_channel
36
40
  from flwr.common.logger import log
37
41
  from flwr.common.retry_invoker import RetryInvoker
42
+ from flwr.common.typing import Fab, Run
38
43
  from flwr.proto.transport_pb2 import ( # pylint: disable=E0611
39
44
  ClientMessage,
40
45
  Reason,
@@ -55,18 +60,23 @@ def on_channel_state_change(channel_connectivity: str) -> None:
55
60
 
56
61
 
57
62
  @contextmanager
58
- def grpc_connection( # pylint: disable=R0915
63
+ def grpc_connection( # pylint: disable=R0913,R0915,too-many-positional-arguments
59
64
  server_address: str,
60
65
  insecure: bool,
61
66
  retry_invoker: RetryInvoker, # pylint: disable=unused-argument
62
67
  max_message_length: int = GRPC_MAX_MESSAGE_LENGTH,
63
68
  root_certificates: Optional[Union[bytes, str]] = None,
69
+ authentication_keys: Optional[ # pylint: disable=unused-argument
70
+ tuple[ec.EllipticCurvePrivateKey, ec.EllipticCurvePublicKey]
71
+ ] = None,
64
72
  ) -> Iterator[
65
- Tuple[
73
+ tuple[
66
74
  Callable[[], Optional[Message]],
67
75
  Callable[[Message], None],
76
+ Optional[Callable[[], Optional[int]]],
68
77
  Optional[Callable[[], None]],
69
- Optional[Callable[[], None]],
78
+ Optional[Callable[[int], Run]],
79
+ Optional[Callable[[str, int], Fab]],
70
80
  ]
71
81
  ]:
72
82
  """Establish a gRPC connection to a gRPC server.
@@ -94,6 +104,8 @@ def grpc_connection( # pylint: disable=R0915
94
104
  The PEM-encoded root certificates as a byte string or a path string.
95
105
  If provided, a secure connection using the certificates will be
96
106
  established to an SSL-enabled Flower server.
107
+ authentication_keys : Optional[Tuple[PrivateKey, PublicKey]] (default: None)
108
+ Client authentication is not supported for this transport type.
97
109
 
98
110
  Returns
99
111
  -------
@@ -116,6 +128,8 @@ def grpc_connection( # pylint: disable=R0915
116
128
  """
117
129
  if isinstance(root_certificates, str):
118
130
  root_certificates = Path(root_certificates).read_bytes()
131
+ if authentication_keys is not None:
132
+ log(ERROR, "Client authentication is not supported for this transport type.")
119
133
 
120
134
  channel = create_channel(
121
135
  server_address=server_address,
@@ -180,7 +194,7 @@ def grpc_connection( # pylint: disable=R0915
180
194
  dst_node_id=0,
181
195
  reply_to_message="",
182
196
  group_id="",
183
- ttl="",
197
+ ttl=DEFAULT_TTL,
184
198
  message_type=message_type,
185
199
  ),
186
200
  content=recordset,
@@ -223,7 +237,7 @@ def grpc_connection( # pylint: disable=R0915
223
237
 
224
238
  try:
225
239
  # Yield methods
226
- yield (receive, send, None, None)
240
+ yield (receive, send, None, None, None, None)
227
241
  finally:
228
242
  # Make sure to have a final
229
243
  channel.close()
@@ -1,4 +1,4 @@
1
- # Copyright 2020 Flower Labs GmbH. All Rights Reserved.
1
+ # Copyright 2023 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.