Source code for flwr.common.typing

# Copyright 2020 Adap 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.
# ==============================================================================
"""Flower type definitions."""


from dataclasses import dataclass
from typing import Dict, List, Optional, Union

import numpy as np

Weights = List[np.ndarray]

# The following union type contains Python types corresponding to ProtoBuf types that
# ProtoBuf considers to be "Scalar Value Types", even though some of them arguably do
# not conform to other definitions of what a scalar is. Source:
# https://developers.google.com/protocol-buffers/docs/overview#scalar
Scalar = Union[bool, bytes, float, int, str]

Metrics = Dict[str, Scalar]


[docs]@dataclass class Parameters: """Model parameters.""" tensors: List[bytes] tensor_type: str
[docs]@dataclass class ParametersRes: """Response when asked to return parameters.""" parameters: Parameters
[docs]@dataclass class FitIns: """Fit instructions for a client.""" parameters: Parameters config: Dict[str, Scalar]
[docs]@dataclass class FitRes: """Fit response from a client.""" parameters: Parameters num_examples: int num_examples_ceil: Optional[int] = None # Deprecated fit_duration: Optional[float] = None # Deprecated metrics: Optional[Metrics] = None
[docs]@dataclass class EvaluateIns: """Evaluate instructions for a client.""" parameters: Parameters config: Dict[str, Scalar]
[docs]@dataclass class EvaluateRes: """Evaluate response from a client.""" loss: float num_examples: int accuracy: Optional[float] = None # Deprecated metrics: Optional[Metrics] = None
[docs]@dataclass class Reconnect: """Reconnect message from server to client.""" seconds: Optional[int]
[docs]@dataclass class Disconnect: """Disconnect message from client to server.""" reason: str