Skip to content

CUDAGraph.reset() frees the workspace shared by other graphs captured on the same stream #395

Open

Description

@mark-yong

Summary

torch.cuda.CUDAGraph.reset() frees the cached cuBLAS/cuBLASLt workspace for the entire capture stream, not just the graph being reset. Graphs captured on one stream share that stream's single cached workspace, so resetting a subset of a stream's graphs frees a block the remaining graphs still reference — the same dangling-pointer class as #266.

Mechanism

In PyTorch 2.14.1, CUDAGraph::reset() calls clearCublasWorkspacesForStream(capture_stream_.stream()) (aten/src/ATen/cuda/CUDAGraph.cpp). The workspace cache is keyed by (handle, stream) and owns the allocation; clearing it releases the block back to the allocator while sibling graphs retain the recorded address in their captured kernel arguments.

Consequences are silent: the freed block is only observed corrupting behavior when it is reallocated and a surviving graph replays — the same presentation family as #266 (device-side fault or silent aliasing, depending on reuse).

Reproduction

On an RTX PRO 6000 (SM120, torch 2.14.1+cu132), capturing two cuBLASLt mm graphs into one graph_pool_handle() on one stream, then resetting only the first:

  • torch.cuda.memory_snapshot(pool) before reset: 1 active_allocated block (the shared workspace)
  • After reset(graph_a): 0 active blocks — the workspace was freed while graph_b is alive and still references the address
  • graph_b.replay() still returns correct output only because the freed block had not yet been reused

The same sequence with a second MemPool allocation in between reuses the freed block, at which point graph_b's workspace contents are corrupted.

Production exposure

Preparation code already shares one stream and one graph pool across capture groups with different lifetimes. For example, ignition/recipes/qwen38_dense/bootstrap.py passes the same stream and pool to the serving capture_forwards call and to study.capture(), while ignition/bench/study.py close() later resets only the study's graphs. If that reset runs while any same-stream graph remains live and is later replayed or exported, its workspace pointer dangles. Whether a current caller actually hits this depends on the ordering of subset resets relative to serialization and replay — severity needs a lifecycle audit, not just the mechanism.

Other reset sites to audit: ignition/preparation/model.py:159, ignition/bench/observations.py:60, ignition/recipes/{glm53,qwen38}/bootstrap.py, ignition/recipes/deepseek41/contextual_selection.py:275,301, ignition/recipes/kimi31/selection_tuning.py:173, ignition/direct/executor.py:62.

Invariant

All graphs captured on one CUDA stream share that stream's workspace and therefore constitute a single lifetime group: callers must reset all of a stream's graphs together, or give separately reset groups separate streams. (PR #331 documents this constraint in capture_forwards and pins the reset behavior in tests/preparation/test_capture_cublas_workspace_cuda.py, but does not change callers.)

Fix direction

Audit the reset sites above; give each independently reset capture group its own stream (e.g. the study capture in bootstrap.py gets a dedicated stream instead of sharing the serving stream). Same-stream groups that always reset together need no change. Per-graph streams are not a viable fix — each stream's workspace is ~32 MiB on these GPUs, which does not scale across the serving catalog.

Refs #266, #331.

Discord: myong_acct

Activity

  1. yatesdr commented on Nov 19, 2026

    @yatesdr
    Collaborator

    The mechanism is in torch 2.14.1: CUDAGraph::reset() calls clearCublasWorkspacesForStream(capture_stream_.stream()) (aten/src/ATen/cuda/CUDAGraph.cpp:388). We audited every graph.reset() in ignition/ on main 1a53275 for a graph on the same stream that is replayed, captured into, timed or exported after the reset, and found none:

    Reset site Why no surviving same-stream graph is used afterward
    ignition/preparation/model.py:159 (close) Resets serving, startup-tuning, observer and study graphs together at close; export runs before close (model_core.py:44 before :68; host_ranks.py:470 before :496-501).
    ignition/bench/study.py:60 StudyModel.close resets the studies and then closes the model, with no replay between (study.py:81-86); benchmark only.
    ignition/bench/observations.py:60 Runs only from the model close; observers exist only under ignition bench.
    ignition/recipes/glm53/bootstrap.py:624 Resets all measurement trials before the final capture_forwards at :628 on the same stream.
    ignition/recipes/qwen38/bootstrap.py:732 Error path; re-raises.
    ignition/recipes/deepseek41/contextual_selection.py:275, :301 Each _capture uses a new stream (:209); all graphs are reset together after the race, during tuning, before the serving stream exists.
    ignition/recipes/kimi31/selection_tuning.py:173 Trial graphs on a one-off stream, reset in finally during tuning, before the serving stream exists.
    ignition/direct/executor.py:62 Eager diagnostic mode, after the last graph replay.

    So current callers are not exposed; the constraint you state (graphs on one stream share one lifetime) holds by the order of these calls rather than by anything that enforces it. Not covered: graph.reset() calls in qualify/, scripts/ and tests/, and the internals of b12x's race_contexts beyond its finally block. Nothing here was run on a GPU.

  2. mark-yong commented on Nov 19, 2026

    @mark-yong
    ContributorAuthor

    Thank you for the audit — that settles the exposure question: no current caller resets a same-stream subset while siblings stay live, so the constraint holds today by call ordering rather than by enforcement.

    The remaining value of this issue is that ordering. Nothing prevents a future caller from capturing a second group on a shared stream and resetting it independently — the failure would be silent (the block only corrupts on reuse). If documenting the invariant in capture_forwards is sufficient, this can close; if the project wants the constraint enforced (for example a dedicated stream per independently reset capture group, or an assertion at reset sites), the audit table is a good starting point for where that would go.

  3. added
    area:kernels
    area:memory
    area:preparation
    bug
    performance
    triage:investigate
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:kernelsarea:memoryarea:preparationbugperformancetriage:investigate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions