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
18 changes: 18 additions & 0 deletions docs/sphinx/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ PyPI 版本变更与未发布变更;发布日期采用 PyPI 上传日期。完
status → start → train → stop 完整生命周期,包括 orphan daemon 的手动恢复
路径。
([#2610](https://github.com/Motphys/UniLab/issues/2610)).
- `ManagerRng.choice` no longer validates the probability table with eager
host reads on every call: the per-call `torch.isfinite` / `p < 0` check
forced a device-to-host synchronization that broke sync-free hot paths
(fixed-shape autoreset with sync-debug mode, CUDA graph capture). The check
was redundant — `torch.multinomial` itself raises a clean `RuntimeError` on
CPU and a device-side assert on CUDA for invalid probabilities — and
contradicted the repository's deferred finite-validation boundary pattern.
The method is now sync-free; callers are responsible for passing finite,
non-negative probabilities with a positive sum, as documented on the method
([#2624](https://github.com/Motphys/UniLab/issues/2624)).
`ManagerRng.choice` 不再在每次调用时用 eager host 读校验概率表:逐调用的
`torch.isfinite` / `p < 0` 检查强制 device→host 同步,破坏了免同步热路径
(sync-debug 模式下的 fixed-shape autoreset、CUDA graph 捕获)。该检查本就
冗余——非法概率下 `torch.multinomial` 自身会在 CPU 抛出清晰的
`RuntimeError`、在 CUDA 触发 device-side assert——且与仓库"延迟有限值校验边界"
的模式相矛盾。该方法现在免同步;调用方需自行保证概率有限、非负且和为正,
方法 docstring 已注明
([#2624](https://github.com/Motphys/UniLab/issues/2624))。

### Removed / 移除

Expand Down
11 changes: 9 additions & 2 deletions src/unilab/managers/rng.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ def choice(
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.
"""
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:
Expand All @@ -190,8 +199,6 @@ def choice(
raise ValueError(
f"ManagerRng choice probabilities must live on {self.device}, got {p.device}"
)
if p.numel() == 0 or not bool(torch.isfinite(p).all()) or bool((p < 0).any()):
raise ValueError("ManagerRng choice probabilities must be finite and non-negative")
count = _shape(size)
if len(count) != 1:
raise TypeError("ManagerRng choice size must be one-dimensional")
Expand Down
Loading