Repository navigation
Isolate cuBLAS warmup workspaces from CUDA graph capture - #331
Conversation
yatesdr
left a comment
There was a problem hiding this comment.
The clear is the right fix for #266: in torch 2.14.1, clearCublasWorkspaces() empties both the cuBLAS and the cuBLASLt workspace maps (aten/src/ATen/cuda/CublasHandlePool.cpp:134-145), and every capture_forwards path runs it. It can't merge until the evidence matches the claim.
Blocking
- Show the test result. Post the command and output of the new test on f950d32 and with the
capture.pyhunk reverted:FLARE_RUN_CUDA_TESTS=1 .venv/bin/python -m pytest tests/preparation/test_capture_cublas_workspace_cuda.py. Expect a pass, then a failure. - "Fixes #266" claims more than the test shows. The test uses a plain
torch.cuda.MemPool; an export keeps warmup memory inCoreStorageretention, which the test never touches. Either post one export with the out-of-allocation pointer check from #266 showing no hits, or change it to "Refs #266". @calvarado2004 reported that the same clear removed the fault on four GB10 ranks with a local patch; a run of this branch would settle it. - CONTRIBUTING.md requires
Signed-off-byon every commit; f950d32 has none.
Should fix
- The test restores
preferred_blas_library("cublas")unconditionally (test_capture_cublas_workspace_cuda.py:62). Save and restore the value it found. - The
>= (1 << 20)block filter (line 55) equals the default workspace size of 1024 KiB (CublasHandlePool.cpp:233), so the check sits exactly on its edge. Scope the snapshot to the graph pool, asignition/preparation/core_storage.py:579does, instead of filtering by size. - The description's "15 tests with one ImportError that reproduces on the unmodified tree" names no test, command or error. Name them or drop the sentence. The commit message should state the symptom and #266 itself.
|
GPU result on a DGX Spark (GB10, SM121), with this PR (f950d32) applied to main eb8a062,
So on SM121 the test fails without the workspace clear and passes with it. This does not cover an export with the pointer check, or SM120; the other points in the review above still apply. |
|
I exported DeepSeek V4.1 Flash from Across the 4,096-, 7,168-, and 8,192-row graphs, all 120 cuBLAS split-K workspace arguments identified in #266 resolved to the same recorded allocation on each rank. With The broader scan found 864 unaligned byte-window matches and one aligned match per rank in a CUTLASS parameter of the |
b7963f9 to
eea68ee
Compare
|
@yatesdr Addressed the remaining test detail in eea68ee: the assertion now uses torch.cuda.memory_snapshot(pool, include_traces=False) directly, matching the core_storage pattern with no global snapshot traversal. Re-ran the A/B on SM120: fails on the unmodified tree (no cuBLASLt workspace was recorded in the captured graph pool), passes with the fix. Combined with your GB10 validation and the posted four-GB10 DeepSeek export, all review items are addressed. Ready for re-review when you have a chance. Thanks @FujitsuPolycom |
yatesdr
left a comment
There was a problem hiding this comment.
The six points from the last round are addressed, and the revised test behaves as intended on SM121. One new defect remains, found by reading the callers and confirmed on a GB10.
Our run on eea68ee (DGX Spark GB10, SM121, this PR applied to main eb8a062, FLARE_RUN_CUDA_TESTS=1 pytest tests/preparation/test_capture_cublas_workspace_cuda.py): 1 passed with the change; with only the capture.py hunk reverted, 1 failed with AssertionError: no cuBLASLt workspace was recorded in the captured graph pool.
Blocking
-
A second
capture_forwardscall frees the workspace that the first call's graphs captured. The clear atcapture.py:141runs on every call. Once the first call has allocated its workspace inside the graph pool, a later call into the same pool drops that cache entry, and the block is freed while the first call's graphs still hold its address. A probe on the same GB10 captured one cuBLASLttorch.mmwithcapture_forwards, then a second forward with another call into the same pool and stream:Graph pool block After the first call After the second call first workspace, 32 MiB active_allocatedinactivenew workspace, 32 MiB active_allocatedFreed blocks are left out of retained preparation pools (
core_storage.py:303-313on main). Second calls into the same pool exist:ignition benchbenchmark selection captures speculation studies and observers after the serving graphs it keeps (qwen38_dense/bootstrap.py:351and:360; nemotron_h and kimi3 follow the same pattern), and DeepSeek V4.1 joint tuning captures tuning and observation graphs back to back and runs both (deepseek41/contextual_selection.py:242-256). In both cases graphs that are still used point at a freed block: it is no longer retained, and the next allocation in the pool can take it, the same dangling-workspace failure as #266. Required: graphs captured by an earlier call keep their workspace allocated for as long as they exist, and a test that captures twice into one pool and asserts the first call's workspace block is still active. -
Contact line. The description has no
Discord:line (AGENTS.md, "Contact on PRs and issues": "Never omit the line"). Add your username, orDiscord: no username provided.
Should fix
- The commit message's evidence refers to the conversation ("a reviewer run on a GB10", "reported on the pull request"). State the conditions and results instead: the test fails without the clear and passes with it on SM120 and SM121, and a four-rank GB10 TP4 export resolved all 120 split-K workspace arguments to recorded allocations.
- The description should say that cores exported before this change keep the transient workspace address and need a new export, and it should keep the export's stated limits: text prefill only, and one aligned match per rank in a CUTLASS parameter of the
vision1152graph whose type was not verified.
Not run by us: an export on SM120, and an ignition bench export with the pointer scan from #266.
eea68ee to
e7e3ff9
Compare
8cbe507 to
86d679d
Compare
|
Head is now 86d679d on fix/capture-cublas-workspace, replacing the clear-based revision you tested. The design is now warmup-stream isolation rather than cache clearing: ForwardCache.prepare_many, the explicit serving warmups, and the before_run callback all run on a dedicated per-device stream, so no transient workspace can enter the capture stream's (handle, stream) cache: the first capture allocates its workspace inside the recording pool, and nothing ever needs clearing. Your dangling-workspace failure mode is structurally impossible: no clear exists to free the in-pool block an earlier graph references. The warmup stream's own workspaces are seeded once per capture_forwards call — on the calling thread, under the ambient backend — inside a dedicated module-owned MemPool, so their backing can't come from transient storage, CoreStorage's pool, or the exported graph pool. Validation on this head (2x RTX PRO 6000, SM120, torch 2.14.1+cu132, flashinfer 0.7.3 @ 2cce3e67): the regression test covers same-stream and different-stream repeated captures into one pool, a before_run doing torch.mm, transient-pool cleanliness, dedicated-pool residency, and first-graph replay; passes here, fails on the unmodified tree. A TP2 Qwen3.8-Flash-Next export through prepare_many captured and serialized all 112 graphs and wrote a complete core (via --no-weights; the older cached layouts fail the current weight-layout contract). This revision has no SM121 coverage and no serving replay yet. If you can run the regression on GB10 that would be the most useful check: |
yatesdr
left a comment
There was a problem hiding this comment.
The redesign removes the clear, and on our GB10 the problem from the last round is gone, but the warmup stream can be the capture stream.
Our run on 86d679d (DGX Spark GB10, SM121, torch 2.14.1, this PR applied to main eb8a062):
FLARE_RUN_CUDA_TESTS=1 pytest tests/preparation/test_capture_cublas_workspace_cuda.py: 1 passed; with onlycapture.pyreverted, 1 failed withAssertionError: no cuBLASLt workspace was recorded in the captured graph pool.- Two
capture_forwardscalls into one graph pool on one stream: the first call's workspace block staysactive_allocatedafter the second call, and the first graph's output is correct. - The six capture test files you edited: 30 passed and 2 failed (
test_compiled_warmup_widens_the_roce_wait_and_capture_keeps_the_serving_waitandtest_capture_drains_after_prepare_and_sync_before_capture); the same two also fail on main alone in our CPU environment, so they are not from this PR.
Blocking
- The warmup stream can alias a capture stream.
_warmup_stream(ignition/preparation/capture.py:38) usestorch.cuda.Stream(device), which PyTorch serves round-robin from a pool of 32 streams per device. On the same GB10, of 40torch.cuda.Stream(0)objects created in a row, only 32 were distinct and the 33rd had the samecuda_streamas the first. The workspace cache is keyed by the CUDA stream, so when a recipe's serving or tuning stream (alsotorch.cuda.Stream(device)in most recipes, and a new one per call indeepseek41/contextual_selection.py:209) lands on the warmup stream, the capture records the seeded workspace from the module pool and the #266 failure returns, silently.ignition/preparation/scratch.py:90-97creates its overlap stream outside the pool for exactly this reason ("PyTorch's round-robin stream pool can alias a later capture stream"). The warmup and capture streams must be distinct by construction, with a test that would alias them under the pool.
Should fix
- Retained memory is not stated. No workspace is released now: each capture stream keeps its workspace (32 MiB on these GPUs) for the life of the process in the pool that first captured on it, and the module-level
_warmup_streamsand_warmup_pools(capture.py:21-22) hold the warmup pool and its seeded workspace for the life of the process. The description says the seed "touches no process-global state"; state the retained memory per device instead (AGENTS.md: material resource-lifetime changes). - Ordering against the caller's stream.
warm.wait_stream(torch.cuda.current_stream(device))(lines 132 and 170) orders warmups after the current stream, not after thestreamargument the warmups ran on before. No caller we found queues work onstreamfirst, but the contract changed; waiting onstreamkeeps it. - Description. The second paragraph of "Why" describes the clear-based revision, which main never had; state the invariant instead (prose skill: no attempts or pivots). "Fixes the workspace part of the transient-storage defect tracked in #266" needs either an export of this head with the out-of-allocation pointer check from #266 or "Refs #266"; the earlier four-GB10 export covered the clear-based head. "Written with AI assistance; reviewed before opening." is a process statement, not evidence (AGENTS.md, "Remove process assurances").
86d679d to
7a9094b
Compare
7a9094b to
10d28a7
Compare
|
Head 10d28a7 addresses all four points:
One related finding verified while testing: CUDAGraph::reset() clears the stream's whole workspace cache, so resetting a subset of a stream's graphs frees the workspace the rest still reference (confirmed on SM120, pool blocks 1 -> 0). That is a caller-side lifetime constraint — e.g. qwen38_dense/bootstrap.py shares a stream between serving and study captures while bench/study.py resets only the study's — documented in capture_forwards, pinned in a second test, and filed as #395 since fixing it requires caller-side grouping outside this PR. |
yatesdr
left a comment
There was a problem hiding this comment.
The round-three points hold at 10d28a7: the warmup stream comes from cuStreamCreate as in scratch.py, the warmups wait on both the caller's stream and the capture stream, and no workspace is cleared, so a second capture_forwards call keeps the first call's workspace. No blocking findings from reading this head. We could not rerun it on GPU; our GB10 results above are for 86d679d. These should still be fixed:
- The A/B claim does not match the test as written.
tests/preparation/test_capture_cublas_workspace_cuda.py:42calls_capture._warmup_stream(0)before any capture, so on unmodified main the test stops withAttributeError, not with "no cuBLASLt workspace was recorded". State which revert produced the quoted failure, or check the workspace before touching the new helpers. - Nothing distinguishes the rerouting. Every CPU test stubs
_warmup_streamto return the capture stream (for exampletest_capture_selection.py:34and:73,test_capture_rank_readiness.py:55), and the GPU test runs withcompile_decoder=False, soForwardCache.prepare_manyis never exercised on the warmup stream. Movingprepare_manyorbefore_runback onto the capture stream would pass every test. A CPU test with distinct warmup and capture streams, and a GPU check thatbefore_run's workspace stays out of the capture stream's cache, would cover it. - Retained memory. "Roughly 32 MiB per device" is one workspace. Each capture stream keeps its own until its graphs are reset, plus the seeded warmup workspace, and the tuning paths create a stream per call (
deepseek41/contextual_selection.py:209,kimi31/selection_tuning.py:135). State it per workspace and name what bounds the count. - Reset test and contract.
test_graph_reset_frees_the_stream_workspace_shared_by_other_graphsasserts PyTorch's behavior rather than Flare's, so it fails if PyTorch changesresetand passes or fails independently of this change. The constraint itself belongs with #395; everygraph.reset()inignition/on main currently runs before the remaining captures on its stream or after their last use (audit in #395), so "a pre-existing exposure on main" overstates it. - Description. "Why" says
capture_forwards"broke both halves of that contract", while the commit message says the first half; main never released a recorded block. "Workspaces are never cleared: a clear would free..." still describes the earlier revision; state the invariant only. Say which head each result in Validation ran on. - Nit. The
finalize(..., cuStreamDestroy, ...)atcapture.py:49never runs, because_warmup_streamsholds the stream for the process; drop it or say the stream lives for the process.
Refs NVIDIA#266. PyTorch caches one cuBLAS/cuBLASLt workspace per (handle, CUDA stream). For exported graphs to keep valid workspace pointers, every workspace-allocating call that runs inside transient storage must stay off the stream that captures graphs: the capture stream's cache then allocates its workspace inside the recording pool during capture, and nothing may release a recorded block while a graph still references it. capture_forwards broke the first half of that contract. Its explicit warmup loop, compiled-forward preparation in ForwardCache.prepare_many, and the before_run callback all ran on the capture stream inside transient storage, so the first captured graph could record a workspace address whose transient backing is not part of the exported package. That left the graph with a workspace pointer serving cannot map. All warmup execution now runs on a dedicated per-device stream that never captures. The stream is created with cuStreamCreate outside torch.cuda.Stream's round-robin pool, because a caller's later pool stream can alias an earlier one -- a capture stream aliasing the warmup stream would restore the defect. The warmup stream's own workspaces are seeded once per call on the calling thread, under the ambient BLAS backend, inside a dedicated module-owned MemPool whose backing outlives transient storage, caller-owned pools such as CoreStorage's, and the exported graph pool. Nothing releases a pool-recorded workspace while a graph references it, and warmup-to-capture ordering keeps the upstream dependencies on the caller's current stream and on the capture stream. Graphs captured on one stream share that stream's cached workspace, so callers must reset all of a capture stream's graphs together or give separately reset groups separate streams; CUDAGraph.reset() frees the stream's whole workspace cache. That shared-lifetime constraint is documented for callers and tracked in NVIDIA#395. The design retains GPU memory by construction: one workspace per distinct capture stream plus the per-device seeded warmup workspace, roughly 32 MiB each on RTX PRO 6000, held for the life of the process. The CUDA regression test captures the same cuBLASLt mm into one pool through capture_forwards on same-stream and different-stream repeated calls, each with its own transient MemPool, exercises a before_run that invokes torch.mm, requires the first call's workspace blocks to stay allocated, no transient pool to hold a live workspace, the seed to be resident in the dedicated pool, and the warmup stream to alias no torch.cuda.Stream pool entry, and replays the first graph checking its output. A new CPU test with distinct warmup and capture streams verifies every warmup stream context -- compiled preparation, serving warmups, before_run -- enters the warmup stream while capture uses the capture stream. The existing CPU orchestration tests stub the two new helpers alongside their other CUDA mocks. Validation on two RTX PRO 6000 (SM120, torch 2.14.1+cu132, flashinfer 0.7.3 at 2cce3e67): with FLARE_RUN_CUDA_TESTS=1 the test passes on this tree and fails on the unmodified tree because the pool records no workspace. The two stream-isolation tests and thirty of thirty-two mocked CPU orchestration tests pass; the other two fail on unmodified nvidia/main as well, because their Cache stubs return functions without capture_allocation_sizes. A TP2 Qwen3.8-Flash-Next export on a revision of this design prepared 76 forwards through the rerouted compiled-forward path, captured and serialized all 112 graphs, scanned 1.08 GiB of reachable storage, and wrote a complete core; it used --no-weights because the older cached layouts no longer satisfy the current weight-layout contract. Limitations: the rerouted compiled-forward path is covered only by the CPU stream-context test and that export, not a GPU regression; the seed covers only the ambient backend's workspace and the non-unified TORCH_CUBLASLT_UNIFIED_WORKSPACE=0 configuration is outside the tested contract; same-device preparation is assumed serialized; the change has run only on SM120; no serving replay of an exported core has run. Design-Signed-Off-By: Devin (SWE-2); design-review=1af4 Signed-off-by: mark-yong <mark-yong@users.noreply.github.com>
10d28a7 to
d835b28
Compare
|
d835b28 addresses the six points: the A/B order is fixed so the unmodified tree fails on the workspace assertion; a new CPU test (test_capture_stream_isolation.py) with distinct warm and capture streams verifies every warmup stream context enters the warmup stream while capture uses the capture stream; retained memory is stated per workspace; the reset test is removed and the constraint moved to the docstring and #395; the description states the invariant only and scopes each validation result to the head it ran on; the unreachable finalizer is dropped. |
|
Checked d835b28 against the last round. On x86-64 Linux, CPU only: |
|
The CUDA regression passes on this head (d835b28) on 2x RTX PRO 6000 (SM120, torch 2.14.1+cu132) — the GB10 coverage gap on SM121 remains as you note; the 86d679d runs covered the same stream-isolation design, and the deltas since were test/prose-only on the capture path plus the ordering and stream-creation changes. |
… and polish docs Why: Review of PR NVIDIA#606 identified that the initial CUDA test ran in-process alongside test_first_capture_binds_workspace_from_the_recording_pool (sharing module-level _warmup_streams[0] and cuBLASLt workspace cache state) and stubbed CoreStorage with SimpleNamespace instead of exercising real CoreStorage mappings and native RuntimeCore export. In addition, the updated _seed_warmup_workspaces docstring needed reconciliation with NVIDIA#331, self._warmup_pool was not declared in CoreStorage.__init__, and docs/preparation-development.md did not mention CoreStorage.warmup_pool(). What exactly changed: - tests/preparation/test_capture_cublas_workspace_cuda.py: replaced the in-process stub test with test_export_warmup_pool_seeds_workspace_inside_core_storage, which runs in a fresh subprocess (matching test_recorded_export_graph_cuda.py) against a real CoreStorage and ModuleImages instance, verifies _warmup_pools remains empty, asserts warmup segments lie inside CoreStorage.mappings, verifies the captured graph binds its workspace in storage.graph_pool, and writes a native RuntimeCore package. - ignition/preparation/capture.py: reconciled the _seed_warmup_workspaces docstring for both pool=None and CoreStorage.warmup_pool() paths and added a short comment explaining the 2-argument call preservation. - ignition/preparation/core_storage.py: declared self._warmup_pool = None in CoreStorage.__init__. - docs/preparation-development.md: documented CoreStorage.warmup_pool() beside transient warmup pool usage. Claimed benefit: Eliminates cross-test order dependence on _warmup_streams[0], verifies real CoreStorage mappings and native export acceptance end-to-end on CUDA, and keeps docstrings, initialization fields, and preparation docs consistent. Supporting evidence and limitations: Ran FLARE_RUN_CUDA_TESTS=1 pytest on GB10 (spark-b23d) across tests/preparation/test_core_storage.py, tests/preparation/test_capture_cublas_workspace_cuda.py, tests/preparation/test_recorded_export_graph_cuda.py, tests/preparation/test_capture_graph_metadata.py, tests/preparation/test_capture_rank_readiness.py, and tests/preparation/test_preparation_reclamation.py (36 passed, 1 skipped on Linux for Windows-only address check), plus an explicit reversed-order run of test_capture_cublas_workspace_cuda.py (2 passed). Single-GPU GB10 run only. Signed-off-by: Avi Fenesh <aviarchi1994@gmail.com> Design-Signed-Off-By: Antigravity (Gemini); design-review=2e5b
Refs #266.
Why
PyTorch caches one cuBLAS/cuBLASLt workspace per
(handle, CUDA stream). For exported graphs to keep valid workspace pointers, every workspace-allocating call that runs inside transient storage must stay off the stream that captures graphs: the capture stream's cache then allocates its workspace inside the recording pool during capture, and nothing may release a recorded block while a graph still references it.capture_forwardsbroke the first half of that contract. Its explicit warmup loop, compiled-forward preparation inForwardCache.prepare_many, and thebefore_runcallback all ran on the capture stream inside transient storage, so the first captured graph could record a workspace address whose transient backing is not part of the exported package. In #266 this presented as Xid 31 atchunk_size=8192and silent aliasing atchunk_size=4096.What changed
All warmup execution now runs on a dedicated per-device stream that never captures.
cuStreamCreateoutsidetorch.cuda.Stream's round-robin pool — a caller's later pool stream can alias an earlier one, and a capture stream aliasing the warmup stream would restore the defect.prepare_many, the explicit warmup loop, andbefore_runall run on the warmup stream. Warmup-to-capture ordering keeps the upstream dependencies on the caller's current stream and on the capture stream.MemPoolwhose backing outlives transient storage, caller-owned pools such asCoreStorage's, and the exported graph pool.Graphs captured on one stream share that stream's cached workspace, so callers must reset all of a capture stream's graphs together or give separately reset groups separate streams —
CUDAGraph.reset()frees the stream's whole workspace cache. That shared-lifetime constraint is documented incapture_forwardsand tracked in #395, which audits the caller-side reset sites. The design also retains GPU memory by construction: one workspace per distinct capture stream plus the per-device seeded warmup workspace, roughly 32 MiB each on RTX PRO 6000, held for the life of the process.Tests
tests/preparation/test_capture_cublas_workspace_cuda.py: captures the same cuBLASLtmminto one pool throughcapture_forwardson same-stream and different-stream repeated calls, each with its own transientMemPool, exercisesbefore_runinvokingtorch.mm, asserts the warmup stream aliases notorch.cuda.Streampool entry, requires the first call's workspace blocks to stay allocated, no transient pool to hold a live workspace, and the seed to be resident in the dedicated pool, and asserts the warmup stream aliases notorch.cuda.Streampool entry, and replays the first graph checking its output.tests/preparation/test_capture_stream_isolation.py(new, CPU): with distinct warmup and capture streams, verifies every warmup stream context — compiled preparation, serving warmups,before_run— enters the warmup stream whiletorch.cuda.graphuses the capture stream.Validation
On two RTX PRO 6000 (SM120, torch 2.14.1+cu132, flashinfer 0.7.3 at
2cce3e67):FLARE_RUN_CUDA_TESTS=1, the regression test fails on the unmodified tree — no cuBLASLt workspace is recorded in the captured graph pool — and passes with the change.test_capture_roce_waitand onetest_transient_retentioncase fail identically on unmodifiednvidia/mainbecause theirCachestubs return functions withoutcapture_allocation_sizes— pre-existing breakage, verified against upstream sources.qwen3.8-flashexport on a revision of this design, through the rerouted compiled-forward path, prepared 76 forwards, captured and serialized all 112 graphs, scanned 1.08 GiB of reachable storage, and wrote a complete core. It used--no-weightsbecause the older cached weight layouts no longer satisfy the current weight-layout contract.Independent validation on GB10 (SM121, torch 2.14.1) covered
86d679d5, the revision before the out-of-pool stream change: it reproduced the A/B result, kept the first call's workspace active through a second capture, and reproduced the first graph's correct output.Limitations: the rerouted compiled-forward path is covered only by the CPU stream-context test and the export above, not a GPU regression; the seed covers only the ambient backend's workspace, and the non-unified
TORCH_CUBLASLT_UNIFIED_WORKSPACE=0configuration is outside the tested contract; same-device preparation is assumed serialized; no serving replay of an exported core has run. Six adjacenttests/preparationmodules fail collection withNo module named 'helios'in this environment, on both trees — a missing native build, unrelated to the change. Cores exported before this change may retain a transient workspace address and need a new export.Discord: myong_acct
Thanks to @calvarado2004 for the issue investigation and proposal, and to @yatesdr and @FujitsuPolycom for review and independent validation.