Skip to content
Merged
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
12 changes: 12 additions & 0 deletions docs/sphinx/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 / 修复

Expand Down
15 changes: 6 additions & 9 deletions src/unilab/managers/rng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 0 additions & 2 deletions tests/managers/test_rng.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
Loading