From 45acbc2952bb11f6c2ce8668d4ed5f5f2b1f336e Mon Sep 17 00:00:00 2001 From: TATP-233 Date: Thu, 19 Nov 2026 22:18:14 +0800 Subject: [PATCH] refactor(rng): drop the always-default replacement parameter from ManagerRng.choice ManagerRng.choice only ever supported sampling with replacement (replacement=False raised NotImplementedError from the API's introduction), so the parameter misled callers into discovering the limitation at runtime. The narrower signature makes the with-replacement-only contract explicit and removes the NotImplementedError guard plus its test assertion. No in-repo or known downstream caller passed the argument. Follow-up to #2624, building on #2625. --- docs/sphinx/source/changelog.md | 12 ++++++++++++ src/unilab/managers/rng.py | 15 ++++++--------- tests/managers/test_rng.py | 2 -- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/docs/sphinx/source/changelog.md b/docs/sphinx/source/changelog.md index 152c9a3c3..435e1ba5d 100644 --- a/docs/sphinx/source/changelog.md +++ b/docs/sphinx/source/changelog.md @@ -157,6 +157,18 @@ PyPI 版本变更与未发布变更;发布日期采用 PyPI 上传日期。完 覆盖率门槛(`pyproject.toml` 中 `fail-under = 40`) ([#2584](https://github.com/Motphys/UniLab/pull/2584), [#706](https://github.com/Motphys/UniLab/issues/706))。 +- `ManagerRng.choice` drops the `replacement` parameter: sampling was always + with replacement (`replacement=False` raised `NotImplementedError` from the + API's introduction), and a parameter that can only ever take its default + misleads callers into discovering the limitation at runtime. The narrower + signature makes "with replacement only" explicit at the contract level; no + in-repo or known downstream caller passed the argument + ([#2624](https://github.com/Motphys/UniLab/issues/2624))。 + `ManagerRng.choice` 删除 `replacement` 参数:采样本就只有有放回一种 + (自该 API 引入起 `replacement=False` 即抛 `NotImplementedError`),一个 + 只能取默认值的参数会让调用方直到运行时才撞见这一限制。收窄后的签名在 + 契约层面显式表达"仅有放回";仓库内及已知下游均无调用方传入该参数 + ([#2624](https://github.com/Motphys/UniLab/issues/2624))。 ### Fixed / 修复 diff --git a/src/unilab/managers/rng.py b/src/unilab/managers/rng.py index 82160737e..1e047a1ea 100644 --- a/src/unilab/managers/rng.py +++ b/src/unilab/managers/rng.py @@ -174,21 +174,18 @@ def choice( size: int | tuple[int, ...], *, p: torch.Tensor, - replacement: bool = True, ) -> torch.Tensor: """Sample category indices from ``{0, ..., a - 1}`` with probabilities ``p``. - The method performs no host reads: it is sync-free and safe on - fixed-shape autoreset and CUDA graph hot paths. ``p`` must therefore be - trusted by the caller: finite, non-negative, with a positive sum. An - invalid ``p`` fails in ``torch.multinomial`` itself — a clean - ``RuntimeError`` on CPU, but a device-side assert with a poisoned CUDA - context on CUDA. + Sampling is always with replacement. The method performs no host + reads: it is sync-free and safe on fixed-shape autoreset and CUDA + graph hot paths. ``p`` must therefore be trusted by the caller: + finite, non-negative, with a positive sum. An invalid ``p`` fails in + ``torch.multinomial`` itself — a clean ``RuntimeError`` on CPU, but a + device-side assert with a poisoned CUDA context on CUDA. """ if isinstance(a, bool) or not isinstance(a, int) or a <= 0: raise ValueError(f"ManagerRng choice population must be a positive int, got {a}") - if not replacement: - raise NotImplementedError("ManagerRng choice supports replacement=True only") if not isinstance(p, torch.Tensor): raise TypeError("ManagerRng choice probabilities must be a torch.Tensor") if p.ndim != 1 or p.numel() != a: diff --git a/tests/managers/test_rng.py b/tests/managers/test_rng.py index c34d57a69..83d848335 100644 --- a/tests/managers/test_rng.py +++ b/tests/managers/test_rng.py @@ -70,8 +70,6 @@ def test_rng_rejects_unsupported_choice_semantics() -> None: with pytest.raises(TypeError, match="probabilities must be a torch.Tensor"): rng.choice(2, (2,), p=[0.5, 0.5]) # pyright: ignore[reportArgumentType] - with pytest.raises(NotImplementedError, match="replacement=True only"): - rng.choice(2, (2,), p=torch.ones(2), replacement=False) with pytest.raises(ValueError, match="probabilities must live on"): rng.choice(2, (2,), p=torch.ones(2, device="meta")) with pytest.raises(ValueError, match="probabilities must have shape"):