-
Notifications
You must be signed in to change notification settings - Fork 942
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fds-add-typing-module
- Loading branch information
Showing
55 changed files
with
850 additions
and
818 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ authors = ["The Flower Authors <[email protected]>"] | |
python = ">=3.8,<3.11" | ||
flwr-datasets = { path = "./../../", extras = ["vision"] } | ||
tensorflow-cpu = "^2.9.1, !=2.11.1" | ||
tensorflow-io-gcs-filesystem = "<0.35.0" | ||
parameterized = "==0.9.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2024 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. | ||
# ============================================================================== | ||
"""ConfigsRecord.""" | ||
|
||
|
||
from dataclasses import dataclass, field | ||
from typing import Dict, Optional, get_args | ||
|
||
from .typing import ConfigsRecordValues, ConfigsScalar | ||
|
||
|
||
@dataclass | ||
class ConfigsRecord: | ||
"""Configs record.""" | ||
|
||
keep_input: bool | ||
data: Dict[str, ConfigsRecordValues] = field(default_factory=dict) | ||
|
||
def __init__( | ||
self, | ||
configs_dict: Optional[Dict[str, ConfigsRecordValues]] = None, | ||
keep_input: bool = True, | ||
): | ||
"""Construct a ConfigsRecord object. | ||
Parameters | ||
---------- | ||
configs_dict : Optional[Dict[str, ConfigsRecordValues]] | ||
A dictionary that stores basic types (i.e. `str`, `int`, `float`, `bytes` as | ||
defined in `ConfigsScalar`) and lists of such types (see | ||
`ConfigsScalarList`). | ||
keep_input : bool (default: True) | ||
A boolean indicating whether config passed should be deleted from the input | ||
dictionary immediately after adding them to the record. When set | ||
to True, the data is duplicated in memory. If memory is a concern, set | ||
it to False. | ||
""" | ||
self.keep_input = keep_input | ||
self.data = {} | ||
if configs_dict: | ||
self.set_configs(configs_dict) | ||
|
||
def set_configs(self, configs_dict: Dict[str, ConfigsRecordValues]) -> None: | ||
"""Add configs to the record. | ||
Parameters | ||
---------- | ||
configs_dict : Dict[str, ConfigsRecordValues] | ||
A dictionary that stores basic types (i.e. `str`,`int`, `float`, `bytes` as | ||
defined in `ConfigsRecordValues`) and list of such types (see | ||
`ConfigsScalarList`). | ||
""" | ||
if any(not isinstance(k, str) for k in configs_dict.keys()): | ||
raise TypeError(f"Not all keys are of valid type. Expected {str}") | ||
|
||
def is_valid(value: ConfigsScalar) -> None: | ||
"""Check if value is of expected type.""" | ||
if not isinstance(value, get_args(ConfigsScalar)): | ||
raise TypeError( | ||
"Not all values are of valid type." | ||
f" Expected {ConfigsRecordValues} but you passed {type(value)}." | ||
) | ||
|
||
# Check types of values | ||
# Split between those values that are list and those that aren't | ||
# then process in the same way | ||
for value in configs_dict.values(): | ||
if isinstance(value, list): | ||
# If your lists are large (e.g. 1M+ elements) this will be slow | ||
# 1s to check 10M element list on a M2 Pro | ||
# In such settings, you'd be better of treating such config as | ||
# an array and pass it to a ParametersRecord. | ||
for list_value in value: | ||
is_valid(list_value) | ||
else: | ||
is_valid(value) | ||
|
||
# Add configs to record | ||
if self.keep_input: | ||
# Copy | ||
self.data = configs_dict.copy() | ||
else: | ||
# Add entries to dataclass without duplicating memory | ||
for key in list(configs_dict.keys()): | ||
self.data[key] = configs_dict[key] | ||
del configs_dict[key] |
Oops, something went wrong.