From fd72462032aee1f1b23cdd89884f68b525ab01bc Mon Sep 17 00:00:00 2001 From: TATP-233 Date: Thu, 19 Nov 2026 22:02:23 +0800 Subject: [PATCH] fix(rng): drop eager host validation in ManagerRng.choice to unblock sync-free hot paths The per-call torch.isfinite / p < 0 check in ManagerRng.choice forced a device-to-host read on every invocation, breaking sync-free step paths (fixed-shape autoreset with sync-debug mode, CUDA graph capture). The check was redundant: torch.multinomial itself fails fast on invalid probabilities (clean RuntimeError on CPU, device-side assert on CUDA), and the eager per-call pattern contradicted the repository's deferred finite-validation boundary. Existing tests only assert metadata checks (type/device/shape), so no coverage is lost. choice is now sync-free; the method docstring documents the caller contract (finite, non-negative probabilities with a positive sum). Closes #2624 --- docs/sphinx/source/changelog.md | 18 ++++++++++++++++++ src/unilab/managers/rng.py | 11 +++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/sphinx/source/changelog.md b/docs/sphinx/source/changelog.md index 9851d7048..152c9a3c3 100644 --- a/docs/sphinx/source/changelog.md +++ b/docs/sphinx/source/changelog.md @@ -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 / 移除 diff --git a/src/unilab/managers/rng.py b/src/unilab/managers/rng.py index ba02da4bf..82160737e 100644 --- a/src/unilab/managers/rng.py +++ b/src/unilab/managers/rng.py @@ -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: @@ -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")