[BUG] Dirichlet partition fallback assigns samples to already-full sites
Describe the bug
In the proposed dirichlet_label_partition helper in #5503, the zero-weight fallback discards the site-capacity mask. When a small-alpha Dirichlet draw assigns all nonzero weight to an already-full site, masked.sum() is zero and masked = proportions restores that site's weight. The accepted partition can therefore send an entire later class to a site that should receive no further samples.
Affected code: partition.py, lines 187–195.
This concerns the unmerged implementation in PR #5503 at commit 0a126c144177aa2466fe4ef416804b478eff4d4c, not a released NVFlare version. Related work: #5265.
To reproduce
On the PR branch, with NumPy 2.4.1:
import numpy as np
from nvflare.algos.data.partition import dirichlet_label_partition
labels = np.repeat(np.arange(10), 1000)
result = dirichlet_label_partition(
labels, num_sites=10, alpha=0.01, seed=118
)
counts = result.class_counts[0]
print(counts)
print("Samples before class 6:", sum(counts.get(c, 0) for c in range(6)))
print("Class 6 added:", counts.get(6, 0))
Observed output:
{2: 238, 5: 987, 6: 1000}
Samples before class 6: 1225
Class 6 added: 1000
The capacity threshold is len(labels) / num_sites == 1000. Replaying the sampling loop shows that, on the first accepted attempt, the class-6 draw is [1.0, 0.0, ..., 0.0]; sites 1, 3, 4, 5, 6, and 8 still have room. Removing the mask sends all 1,000 class-6 samples to site 0 instead.
Expected behavior
A site that already holds at least the average shard size should remain excluded from subsequent class allocations, including the zero-weight fallback. A single class allocation may cross the threshold; this report concerns allocations after it has already been crossed.
Suggested fix and validation
Resample or distribute fallback weights only among sites for which has_room is true. Add a regression covering zero total weight on eligible sites, and retain checks for determinism, sample coverage, and minimum shard size.
During review, all 54 existing partition tests and scoped Black/isort/flake8 checks passed; the existing tests do not catch this case.
I'd like to take this — opening a PR against feat/5265-data-partition-utility (#5503) that redistributes the zero-weight Dirichlet fallback only among sites with remaining room, plus a regression for seed=118.
cc @holgerroth @akshat-lakhera