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(datasets) Add a check for same object partitioners #4335

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
19 changes: 19 additions & 0 deletions datasets/flwr_datasets/federated_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def __init__(
self._partitioners: Dict[str, Partitioner] = _instantiate_partitioners(
partitioners
)
self._check_partitioners_correctness()
self._shuffle = shuffle
self._seed = seed
# _dataset is prepared lazily on the first call to `load_partition`
Expand Down Expand Up @@ -336,3 +337,21 @@ def _check_if_no_split_keyword_possible(self) -> None:
"Please set the `split` argument. You can only omit the split keyword "
"if there is exactly one partitioner specified."
)

def _check_partitioners_correctness(self) -> None:
"""Check if the partitioners are correctly specified.

Check if the multiple partitioner objects are not the same Python object, which
is not allowed, as the partitioner objects should be independent (one
partitioner per split).
"""
partitioners_keys = list(self._partitioners.keys())
for i, first_split in enumerate(partitioners_keys):
for j in range(i + 1, len(partitioners_keys)):
second_split = partitioners_keys[j]
if self._partitioners[first_split] is self._partitioners[second_split]:
raise ValueError(
f"The same partitioner object is used for multiple splits: "
f"('{first_split}', '{second_split}'). "
"Each partitioner should be a separate object."
)
52 changes: 52 additions & 0 deletions datasets/flwr_datasets/federated_dataset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
_load_mocked_dataset_dict_by_partial_download,
)
from flwr_datasets.partitioner import IidPartitioner, NaturalIdPartitioner, Partitioner
from flwr_datasets.preprocessor.divider import Divider

mocked_datasets = ["cifar100", "svhn", "sentiment140", "speech_commands"]

Expand Down Expand Up @@ -568,6 +569,57 @@ def test_use_load_dataset_kwargs(self) -> None:
with self.assertRaises(ValueError):
_ = fds.load_partition(0)

def test_incorrect_two_partitioners(self) -> None:
"""Test if the method raises ValueError with incorrect partitioners."""
partitioner = IidPartitioner(num_partitions=10)
partitioners: dict[str, Partitioner | int] = {
"train": partitioner,
"test": partitioner,
}
first_split = "train"
second_split = "test"
with self.assertRaises(ValueError) as context:
FederatedDataset(
dataset="mnist",
partitioners=partitioners,
)
self.assertIn(
f"The same partitioner object is used for multiple splits: "
f"('{first_split}', '{second_split}'). "
"Each partitioner should be a separate object.",
str(context.exception),
)

def test_incorrect_three_partitioners(self) -> None:
"""Test if the method raises ValueError with incorrect partitioners."""
partitioner = IidPartitioner(num_partitions=10)
partitioners: dict[str, int | Partitioner] = {
"train1": partitioner,
"train2": 10,
"test": partitioner,
}
divider = Divider(
divide_config={
"train1": 0.5,
"train2": 0.5,
},
divide_split="train",
)

with self.assertRaises(
ValueError,
) as context:

FederatedDataset(
dataset="mnist", partitioners=partitioners, preprocessor=divider
)

self.assertIn(
"The same partitioner object is used for multiple splits: "
"('train1', 'test'). Each partitioner should be a separate object.",
str(context.exception),
)


def datasets_are_equal(ds1: Dataset, ds2: Dataset) -> bool:
"""Check if two Datasets have the same values."""
Expand Down
Loading