torch-cudagraph-debug
Focused debugging tools for PyTorch CUDA Graphs:
tensor_debug inserts native tensor probes and builds persisted eager or
CUDA Graph runs for offline differential analysis.
memory_debug records allocator states and analyzes default and
non-default pools, including CUDA Graph private pools.
The package requires Linux, Python 3.10+, and CUDA-enabled PyTorch 2.6 or
newer. Memory Debug reads private PyTorch allocator interfaces whose schemas
can change between PyTorch minors; validate a new PyTorch/container combination
with the GPU compatibility gate before relying on it in production.
Architecture at a Glance
Tensor Debug and Memory Debug share two collection workflows:
- A
Probe supports immediate local inspection; snapshot() returns a
standalone ProbeSnapshot without run identity or recording-session
lifecycle.
- A
Recorder owns a complete experiment; finish() returns a Run with
named Point objects, metadata, optional persistence, and structured
multi-point or cross-run analysis.
Both paths use the same ownerless Observation model within each domain.
Choose between them based on the scope of the investigation and whether the
result needs named points or persistence.
flowchart LR
P["<b>Probe</b><br/>for a local question"] -->|produces| PS["<b>ProbeSnapshot</b>"]
PS -->|contains| O["<b>Observation</b><br/>(domain-specific leaf)"]
R["<b>Recorder</b><br/>for a complete experiment"] -->|produces| RUN["<b>Run</b>"]
RUN -->|contains| PT["<b>Point</b>"]
PT -->|contains| O
O -->|used by| T["<b>Tensor Debug</b><br/>values / gradients / online checks<br/>snapshot / point / run / series / run-group comparison<br/>bundles / reports / TensorBoard"]
O -->|used by| M["<b>Memory Debug</b><br/>pools / streams / attribution<br/>timeline / lifetime / phase / run-group analysis<br/>bundles / reports"]
The diagram uses shared API roles; tensor and memory provide separate concrete
types such as TensorObservation and MemoryObservation. See the
detailed architecture for Collector boundaries,
ownership, and lifecycle rules.
Install
Source installation requires CUDA-enabled PyTorch, a compatible CUDA toolkit,
and a C++17 compiler:
python -m pip install --upgrade "setuptools>=77.0.3" wheel
python -m pip install --no-cache-dir --no-build-isolation .
To install directly from the repository, run the same setuptools/wheel
upgrade first (with --no-build-isolation, pip does not install the build
requirements for you):
python -m pip install --no-cache-dir --no-build-isolation \
"git+https://github.com/buptzyb/torch-cudagraph-debug.git@main"
Tensor Debug uses the native extension built during source installation.
On machines without a CUDA toolchain — a laptop used for offline bundle
analysis, or a container that only needs memory diagnostics — install the
offline Tensor Debug mode:
TCGD_TENSOR_DEBUG_MODE=offline \
python -m pip install --no-cache-dir .
Offline mode needs no compiler and no CUDA-enabled PyTorch at build time, and
default build isolation works — no setuptools/wheel preparation and no
--no-build-isolation flag are required. It disables every live Tensor Debug
workflow: enabled TensorProbe objects and both eager and CUDA Graph
TensorRecorder modes raise LiveTensorDebugUnavailableError. Tensor bundle
loading and comparison remain available, and Memory Debug collection and
analysis are unchanged (memory collection still requires CUDA-enabled PyTorch
at runtime).
The mode is fixed at installation time and available through
torch_cudagraph_debug.tensor_debug_mode(). On the first package import in
each Python process, an offline install writes one concise capability and
full-mode reinstall notice to standard error. Always pass --no-cache-dir for
both full and offline source installs because pip's wheel cache does not encode
the selected mode or the build-time PyTorch/CUDA ABI.
Tensor Quick Start
Insert a probe at intermediate tensors inside a captured dataflow. This example
uses the recommended RecordAction to inspect two hidden states without
changing the graph's output:
import torch
from torch_cudagraph_debug.tensor_debug import RecordAction, TensorProbe
static_x = torch.arange(8, device="cuda", dtype=torch.float32)
probe = TensorProbe("hidden", [RecordAction()])
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph):
# Each probe call records one intermediate tensor in this dataflow.
first_hidden = probe(static_x * 2, name="after_scale")
second_hidden = probe(torch.relu(first_hidden - 5), name="after_relu")
output = second_hidden.square()
replay_stream = torch.cuda.current_stream()
graph.replay()