Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(framework) Extend ConfigRecordValue serde #3739

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/py/flwr/common/serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@

from collections import OrderedDict
from collections.abc import MutableMapping
from typing import Any, TypeVar, cast
from typing import (
Any,
MutableMapping,
OrderedDict,
Type,
TypeVar,
Union,
cast,
)

from google.protobuf.message import Message as GrpcMessage

Expand All @@ -30,7 +38,7 @@
from flwr.proto.message_pb2 import Metadata as ProtoMetadata
from flwr.proto.node_pb2 import Node
from flwr.proto.recordset_pb2 import Array as ProtoArray
from flwr.proto.recordset_pb2 import BoolList, BytesList
from flwr.proto.recordset_pb2 import Bool, BytesList
from flwr.proto.recordset_pb2 import ConfigsRecord as ProtoConfigsRecord
from flwr.proto.recordset_pb2 import ConfigsRecordValue as ProtoConfigsRecordValue
from flwr.proto.recordset_pb2 import DoubleList
Expand Down Expand Up @@ -63,6 +71,7 @@
)
from .message import Error, Message, Metadata
from .record.typeddict import TypedDict
from .typing import ConfigsRecordValues

# === Parameters message ===

Expand Down Expand Up @@ -427,10 +436,10 @@ def _record_value_from_proto(value_proto: GrpcMessage) -> Any:
return value


def _record_value_dict_to_proto(
value_dict: TypedDict[str, Any],
def record_value_dict_to_proto(
value_dict: Union[TypedDict[str, Any], dict[str, ConfigsRecordValues]],
allowed_types: list[type],
value_proto_class: type[T],
value_proto_class: Type[T],
) -> dict[str, T]:
"""Serialize the record value dict to ProtoBuf.

Expand All @@ -447,7 +456,7 @@ def proto(_v: Any) -> T:
return {k: proto(v) for k, v in value_dict.items()}


def _record_value_dict_from_proto(
def record_value_dict_from_proto(
value_dict_proto: MutableMapping[str, Any]
) -> dict[str, Any]:
"""Deserialize the record value dict from ProtoBuf."""
Expand Down Expand Up @@ -492,7 +501,7 @@ def parameters_record_from_proto(
def metrics_record_to_proto(record: MetricsRecord) -> ProtoMetricsRecord:
"""Serialize MetricsRecord to ProtoBuf."""
return ProtoMetricsRecord(
data=_record_value_dict_to_proto(record, [float, int], ProtoMetricsRecordValue)
data=record_value_dict_to_proto(record, [float, int], ProtoMetricsRecordValue)
)


Expand All @@ -501,7 +510,7 @@ def metrics_record_from_proto(record_proto: ProtoMetricsRecord) -> MetricsRecord
return MetricsRecord(
metrics_dict=cast(
dict[str, typing.MetricsRecordValues],
_record_value_dict_from_proto(record_proto.data),
record_value_dict_from_proto(record_proto.data),
),
keep_input=False,
)
Expand All @@ -510,7 +519,7 @@ def metrics_record_from_proto(record_proto: ProtoMetricsRecord) -> MetricsRecord
def configs_record_to_proto(record: ConfigsRecord) -> ProtoConfigsRecord:
"""Serialize ConfigsRecord to ProtoBuf."""
return ProtoConfigsRecord(
data=_record_value_dict_to_proto(
data=record_value_dict_to_proto(
record,
[bool, int, float, str, bytes],
ProtoConfigsRecordValue,
Expand All @@ -523,7 +532,7 @@ def configs_record_from_proto(record_proto: ProtoConfigsRecord) -> ConfigsRecord
return ConfigsRecord(
configs_dict=cast(
dict[str, typing.ConfigsRecordValues],
_record_value_dict_from_proto(record_proto.data),
record_value_dict_from_proto(record_proto.data),
),
keep_input=False,
)
Expand Down
12 changes: 12 additions & 0 deletions src/py/flwr/common/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
list[int],
list[str],
]
ValueList = [
bool,
bytes,
float,
int,
str,
List[bool],
List[bytes],
List[float],
List[int],
List[str],
]

# Value types for common.MetricsRecord
MetricsScalar = Union[int, float]
Expand Down
Loading