-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathserde_test.py
More file actions
575 lines (474 loc) · 16.9 KB
/
serde_test.py
File metadata and controls
575 lines (474 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# Copyright 2021 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""(De-)serialization tests."""
import random
import string
from collections import OrderedDict
from typing import Any, Callable, Optional, TypeVar, Union, cast
import pytest
# pylint: disable=E0611
from flwr.proto import clientappio_pb2
from flwr.proto import transport_pb2 as pb2
from flwr.proto.fab_pb2 import Fab as ProtoFab
from flwr.proto.message_pb2 import Context as ProtoContext
from flwr.proto.message_pb2 import Message as ProtoMessage
from flwr.proto.recordset_pb2 import Array as ProtoArray
from flwr.proto.recordset_pb2 import ConfigsRecord as ProtoConfigsRecord
from flwr.proto.recordset_pb2 import MetricsRecord as ProtoMetricsRecord
from flwr.proto.recordset_pb2 import ParametersRecord as ProtoParametersRecord
from flwr.proto.recordset_pb2 import RecordSet as ProtoRecordSet
from flwr.proto.run_pb2 import Run as ProtoRun
# pylint: enable=E0611
from . import (
Array,
ConfigsRecord,
Context,
MetricsRecord,
ParametersRecord,
RecordSet,
typing,
)
from .constant import SUPERLINK_NODE_ID
from .message import Error, Message, Metadata
from .serde import (
array_from_proto,
array_to_proto,
clientappstatus_from_proto,
clientappstatus_to_proto,
configs_record_from_proto,
configs_record_to_proto,
context_from_proto,
context_to_proto,
fab_from_proto,
fab_to_proto,
message_from_proto,
message_from_taskins,
message_from_taskres,
message_to_proto,
message_to_taskins,
message_to_taskres,
metrics_record_from_proto,
metrics_record_to_proto,
parameters_record_from_proto,
parameters_record_to_proto,
recordset_from_proto,
recordset_to_proto,
run_from_proto,
run_to_proto,
scalar_from_proto,
scalar_to_proto,
status_from_proto,
status_to_proto,
)
def test_serialisation_deserialisation() -> None:
"""Test if the np.ndarray is identical after (de-)serialization."""
# Prepare
scalars = [True, b"bytestr", 3.14, 9000, "Hello", (1 << 63) + 1]
for scalar in scalars:
# Execute
scalar = cast(Union[bool, bytes, float, int, str], scalar)
serialized = scalar_to_proto(scalar)
actual = scalar_from_proto(serialized)
# Assert
assert actual == scalar
def test_status_to_proto() -> None:
"""Test status message (de-)serialization."""
# Prepare
code_msg = pb2.Code.OK # pylint: disable=E1101
status_msg = pb2.Status(code=code_msg, message="Success") # pylint: disable=E1101
code = typing.Code.OK
status = typing.Status(code=code, message="Success")
# Execute
actual_status_msg = status_to_proto(status=status)
# Assert
assert actual_status_msg == status_msg
def test_status_from_proto() -> None:
"""Test status message (de-)serialization."""
# Prepare
code_msg = pb2.Code.OK # pylint: disable=E1101
status_msg = pb2.Status(code=code_msg, message="Success") # pylint: disable=E1101
code = typing.Code.OK
status = typing.Status(code=code, message="Success")
# Execute
actual_status = status_from_proto(msg=status_msg)
# Assert
assert actual_status == status
def test_fab_to_proto() -> None:
"""Test Fab serialization."""
proto_fab = ProtoFab(hash_str="fab_test_hash", content=b"fab_test_content")
py_fab = typing.Fab(hash_str="fab_test_hash", content=b"fab_test_content")
converted_fab = fab_to_proto(py_fab)
# Assert
assert converted_fab == proto_fab
def test_fab_from_proto() -> None:
"""Test Fab deserialization."""
proto_fab = ProtoFab(hash_str="fab_test_hash", content=b"fab_test_content")
py_fab = typing.Fab(hash_str="fab_test_hash", content=b"fab_test_content")
converted_fab = fab_from_proto(proto_fab)
# Assert
assert converted_fab == py_fab
T = TypeVar("T")
class RecordMaker:
"""A record maker based on a seeded random number generator."""
def __init__(self, state: int = 42) -> None:
self.rng = random.Random(state)
def randbytes(self, n: int) -> bytes:
"""Create a bytes."""
return self.rng.getrandbits(n * 8).to_bytes(n, "little")
def get_str(self, length: Optional[int] = None) -> str:
"""Create a string."""
char_pool = (
string.ascii_letters + string.digits + " !@#$%^&*()_-+=[]|;':,./<>?{}"
)
if length is None:
length = self.rng.randint(1, 10)
return "".join(self.rng.choices(char_pool, k=length))
def get_value(self, dtype: Union[type[T], str]) -> T:
"""Create a value of a given type."""
ret: Any = None
if dtype == bool:
ret = self.rng.random() < 0.5
elif dtype == str:
ret = self.get_str(self.rng.randint(10, 100))
elif dtype == int:
ret = self.rng.randint(-1 << 63, (1 << 63) - 1)
elif dtype == float:
ret = (self.rng.random() - 0.5) * (2.0 ** self.rng.randint(0, 50))
elif dtype == bytes:
ret = self.randbytes(self.rng.randint(10, 100))
elif dtype == "uint":
ret = self.rng.randint(0, (1 << 64) - 1)
else:
raise NotImplementedError(f"Unsupported dtype: {dtype}")
return cast(T, ret)
def array(self) -> Array:
"""Create a Array."""
dtypes = ("float", "int")
stypes = ("torch", "tf", "numpy")
max_shape_size = 100
max_shape_dim = 10
min_max_bytes_size = (10, 1000)
dtype = self.rng.choice(dtypes)
shape = [
self.rng.randint(1, max_shape_size)
for _ in range(self.rng.randint(1, max_shape_dim))
]
stype = self.rng.choice(stypes)
data = self.randbytes(self.rng.randint(*min_max_bytes_size))
return Array(dtype=dtype, shape=shape, stype=stype, data=data)
def parameters_record(self) -> ParametersRecord:
"""Create a ParametersRecord."""
num_arrays = self.rng.randint(1, 5)
arrays = OrderedDict(
[(self.get_str(), self.array()) for i in range(num_arrays)]
)
return ParametersRecord(arrays, keep_input=False)
def metrics_record(self) -> MetricsRecord:
"""Create a MetricsRecord."""
num_entries = self.rng.randint(1, 5)
types = (float, int)
return MetricsRecord(
metrics_dict={
self.get_str(): self.get_value(self.rng.choice(types))
for _ in range(num_entries)
},
keep_input=False,
)
def configs_record(self) -> ConfigsRecord:
"""Create a ConfigsRecord."""
num_entries = self.rng.randint(1, 5)
types = (str, int, float, bytes, bool)
return ConfigsRecord(
configs_dict={
self.get_str(): self.get_value(self.rng.choice(types))
for _ in range(num_entries)
},
keep_input=False,
)
def recordset(
self,
num_params_records: int,
num_metrics_records: int,
num_configs_records: int,
) -> RecordSet:
"""Create a RecordSet."""
ret = RecordSet()
for _ in range(num_params_records):
ret[self.get_str()] = self.parameters_record()
for _ in range(num_metrics_records):
ret[self.get_str()] = self.metrics_record()
for _ in range(num_configs_records):
ret[self.get_str()] = self.configs_record()
return ret
def metadata(self) -> Metadata:
"""Create a Metadata."""
return Metadata(
run_id=self.rng.randint(0, 1 << 30),
message_id=self.get_str(64),
group_id=self.get_str(30),
src_node_id=self.rng.randint(0, 1 << 63),
dst_node_id=self.rng.randint(0, 1 << 63),
reply_to_message=self.get_str(64),
ttl=self.rng.randint(1, 1 << 30),
message_type=self.get_str(10),
)
def user_config(self) -> typing.UserConfig:
"""Create a UserConfig."""
return {
"key1": self.rng.randint(0, 1 << 30),
"key2": self.get_str(10),
"key3": self.rng.random(),
"key4": bool(self.rng.getrandbits(1)),
}
def test_array_serialization_deserialization() -> None:
"""Test serialization and deserialization of Array."""
# Prepare
maker = RecordMaker()
original = maker.array()
# Execute
proto = array_to_proto(original)
deserialized = array_from_proto(proto)
# Assert
assert isinstance(proto, ProtoArray)
assert original == deserialized
def test_parameters_record_serialization_deserialization() -> None:
"""Test serialization and deserialization of ParametersRecord."""
# Prepare
maker = RecordMaker()
original = maker.parameters_record()
# Execute
proto = parameters_record_to_proto(original)
deserialized = parameters_record_from_proto(proto)
# Assert
assert isinstance(proto, ProtoParametersRecord)
assert original == deserialized
def test_metrics_record_serialization_deserialization() -> None:
"""Test serialization and deserialization of MetricsRecord."""
# Prepare
maker = RecordMaker()
original = maker.metrics_record()
original["uint64"] = (1 << 63) + 321
original["list of uint64"] = [maker.get_value("uint") for _ in range(30)]
# Execute
proto = metrics_record_to_proto(original)
deserialized = metrics_record_from_proto(proto)
# Assert
assert isinstance(proto, ProtoMetricsRecord)
assert original == deserialized
def test_configs_record_serialization_deserialization() -> None:
"""Test serialization and deserialization of ConfigsRecord."""
# Prepare
maker = RecordMaker()
original = maker.configs_record()
original["uint64"] = (1 << 63) + 101
original["list of uint64"] = [maker.get_value("uint") for _ in range(100)]
# Execute
proto = configs_record_to_proto(original)
deserialized = configs_record_from_proto(proto)
# Assert
assert isinstance(proto, ProtoConfigsRecord)
assert original == deserialized
def test_recordset_serialization_deserialization() -> None:
"""Test serialization and deserialization of RecordSet."""
# Prepare
maker = RecordMaker(state=0)
original = maker.recordset(2, 2, 1)
# Execute
proto = recordset_to_proto(original)
deserialized = recordset_from_proto(proto)
# Assert
assert isinstance(proto, ProtoRecordSet)
assert original == deserialized
@pytest.mark.parametrize(
"content_fn, error_fn",
[
(
lambda maker: maker.recordset(1, 1, 1),
None,
), # check when only content is set
(None, lambda code: Error(code=code)), # check when only error is set
],
)
def test_message_to_and_from_taskins(
content_fn: Callable[
[
RecordMaker,
],
RecordSet,
],
error_fn: Callable[[int], Error],
) -> None:
"""Test Message to and from TaskIns."""
# Prepare
maker = RecordMaker(state=1)
metadata = maker.metadata()
# pylint: disable-next=protected-access
metadata.__dict__["_src_node_id"] = SUPERLINK_NODE_ID # Assume driver node
original = Message(
metadata=metadata,
content=None if content_fn is None else content_fn(maker),
error=None if error_fn is None else error_fn(0),
)
# Execute
taskins = message_to_taskins(original)
taskins.task_id = metadata.message_id
deserialized = message_from_taskins(taskins)
# Assert
if original.has_content():
assert original.content == deserialized.content
if original.has_error():
assert original.error == deserialized.error
assert metadata == deserialized.metadata
@pytest.mark.parametrize(
"content_fn, error_fn",
[
(
lambda maker: maker.recordset(1, 1, 1),
None,
), # check when only content is set
(None, lambda code: Error(code=code)), # check when only error is set
],
)
def test_message_to_and_from_taskres(
content_fn: Callable[
[
RecordMaker,
],
RecordSet,
],
error_fn: Callable[[int], Error],
) -> None:
"""Test Message to and from TaskRes."""
# Prepare
maker = RecordMaker(state=2)
metadata = maker.metadata()
metadata.dst_node_id = SUPERLINK_NODE_ID # Assume driver node
original = Message(
metadata=metadata,
content=None if content_fn is None else content_fn(maker),
error=None if error_fn is None else error_fn(0),
)
# Execute
taskres = message_to_taskres(original)
taskres.task_id = metadata.message_id
deserialized = message_from_taskres(taskres)
# Assert
if original.has_content():
assert original.content == deserialized.content
if original.has_error():
assert original.error == deserialized.error
assert metadata == deserialized.metadata
@pytest.mark.parametrize(
"content_fn, error_fn",
[
(
lambda maker: maker.recordset(1, 1, 1),
None,
), # check when only content is set
(None, lambda code: Error(code=code)), # check when only error is set
],
)
def test_message_serialization_deserialization(
content_fn: Callable[
[
RecordMaker,
],
RecordSet,
],
error_fn: Callable[[int], Error],
) -> None:
"""Test serialization and deserialization of Message."""
# Prepare
maker = RecordMaker(state=2)
metadata = maker.metadata()
metadata.dst_node_id = 0 # Assume driver node
original = Message(
metadata=metadata,
content=None if content_fn is None else content_fn(maker),
error=None if error_fn is None else error_fn(0),
)
# Execute
proto = message_to_proto(original)
deserialized = message_from_proto(proto)
# Assert
assert isinstance(proto, ProtoMessage)
if original.has_content():
assert original.content == deserialized.content
if original.has_error():
assert original.error == deserialized.error
assert original.metadata == deserialized.metadata
def test_context_serialization_deserialization() -> None:
"""Test serialization and deserialization of Context."""
# Prepare
maker = RecordMaker()
original = Context(
run_id=0,
node_id=1,
node_config=maker.user_config(),
state=maker.recordset(1, 1, 1),
run_config=maker.user_config(),
)
# Execute
proto = context_to_proto(original)
deserialized = context_from_proto(proto)
# Assert
assert isinstance(proto, ProtoContext)
assert original == deserialized
def test_run_serialization_deserialization() -> None:
"""Test serialization and deserialization of Run."""
# Prepare
maker = RecordMaker()
original = typing.Run(
run_id=1,
fab_id="lorem",
fab_version="ipsum",
fab_hash="hash",
override_config=maker.user_config(),
pending_at="2021-01-01T00:00:00Z",
starting_at="2021-01-02T23:02:11Z",
running_at="2021-01-03T12:00:50Z",
finished_at="",
status=typing.RunStatus(status="running", sub_status="", details="OK"),
)
# Execute
proto = run_to_proto(original)
deserialized = run_from_proto(proto)
# Assert
assert isinstance(proto, ProtoRun)
assert original == deserialized
def test_clientappstatus_to_proto() -> None:
"""Test ClientApp status message (de-)serialization."""
# Prepare
# pylint: disable=E1101
code_msg = clientappio_pb2.ClientAppOutputCode.SUCCESS
status_msg = clientappio_pb2.ClientAppOutputStatus(code=code_msg, message="Success")
code = typing.ClientAppOutputCode.SUCCESS
status = typing.ClientAppOutputStatus(code=code, message="Success")
# Execute
actual_status_msg = clientappstatus_to_proto(status=status)
# Assert
assert actual_status_msg == status_msg
def test_clientappstatus_from_proto() -> None:
"""Test ClientApp status message (de-)serialization."""
# Prepare
# pylint: disable=E1101
code_msg = clientappio_pb2.ClientAppOutputCode.SUCCESS
status_msg = clientappio_pb2.ClientAppOutputStatus(code=code_msg, message="Success")
code = typing.ClientAppOutputCode.SUCCESS
status = typing.ClientAppOutputStatus(code=code, message="Success")
# Execute
actual_status = clientappstatus_from_proto(msg=status_msg)
# Assert
assert actual_status == status