This release is a pre-release and may not be stable for production use.
TorchSpace
Spatial debugging for PyTorch — architecture, activations and gradients in one interactive 3D scene
▲ Live Demo — the full interactive viewer with two demo models, running entirely in your browser (nothing to install, no server)
TorchSpace renders a PyTorch model as one interactive Three.js scene: X·Y is the architecture (layers, blocks, skip connections), +Z is forward activation magnitude and −Z is backward gradient magnitude, both on a log scale. Vanishing gradients, activation explosions, dead regions and forward/backward asymmetry become visible shapes instead of numbers you have to hunt for. Built as a non-invasive extension of torchview — no code copied, full torchview feature set preserved.
| axis | meaning |
|---|---|
| X · Y | architecture — layers, containers, skip connections |
| +Z | forward |activation| per layer (log scale) |
| −Z | backward |gradient| per layer (log scale) |
Everything is captured as summary statistics only (never raw tensors) and exported as a single self-contained HTML file: three.js is inlined and the statistics are embedded, so it works in Colab, Jupyter, VS Code, as an email attachment, and offline.
Install
pip install --pre torchspace # pre-release: --pre is required
Quick start
import torchspace
run = torchspace.trace(model, input_data=x) # structure + instrumentation
out = model(x) # captured forward
run.capture_backward(criterion(out, y)) # captured backward
run.show(height=620) # interactive scene, inline in a notebook
run.export_html("model.torchspace.html") # ...or one shareable file
run.detach() # de-instrument the model
torchspace.view(model, input_size=(1, 3, 224, 224), device="meta")
# structure-only mode, no activation capture (works on meta device)
Tutorial: TorchSpace_Tutorial.ipynb walks
through the full API — quick start, diagnosing a pathological network, the IR,
meta-device inspection, torchview-parity graphviz export, module attributes,
training-loop instrumentation, and export/sharing. Open it with the Colab
badge at the top of this page.
What you get
- One scene for the whole story — architecture, per-layer activation and gradient magnitude, execution order and timing, all aligned in space.
- Rule-based diagnostics with evidence — vanishing/exploding gradients, dead regions, NaN/Inf, forward/backward asymmetry. Click a finding to jump to the layer; select a layer to filter the panel to its findings.
- Timeline replay — animate the actual forward (green) / backward (purple) execution order with flow pulses.
- Real training loops — statistics are captured by ordinary hooks on your
actual training steps, on-device, detached immediately. No separate
profiling pass, no
RecorderTensornear your hot path. - Notebook-native —
run.show()renders the scene in a sandboxedsrcdociframe that works in Colab's network-restricted output frames.
Viewer controls
Drag = rotate · wheel = zoom · right-drag = pan · hover = exact stats ·
click = inspect (+ diagnostics filter) · double-click = expand/collapse ·
2D Ortho = torchview-style top-down view · ▶ Replay = forward/backward
replay · toggles for tensor geometry, ±Z overlays, ops, rolling, labels.
The log-scale ruler beside the model gives the quantitative 1e^x scale.
torchview parity
The trace captures at full detail (torchview's information is a strict subset
of the IR), module/function attributes are collected by default
(collect_attributes=False to disable), and torchview's native graphviz
output stays available at any granularity:
run.export_dot("model.gv", depth=3, graph_dir="LR") # DOT source
run.draw_graph(depth=2, roll=True, save_graph=True,
filename="model", format="png") # torchview pipeline
draw_graph/export_dot accept every torchview.draw_graph option
(depth, hide_inner_tensors, hide_module_functions, roll, graph_dir,
strict, ...) and reuse the exact inputs of the original trace; runtime
capture hooks are suspended for the auxiliary pass.
What ends up in an export (privacy)
Exports contain statistics only — never raw tensors. Two edges to know
before sharing: collect_attributes=True (default) embeds a repr-style
summary of each module's public attributes, which for custom modules can
include checkpoint paths or config values — pass collect_attributes=False
for sensitive models; and for a 1-element tensor the stats (min=max=mean)
necessarily reveal its exact value.
Repository layout
torchspace/
_tv_compat.py # single audited import surface over torchview
tracer.py # non-invasive structural tracer (extension points A & B)
runtime.py # forward/backward hook capture on plain tensors
stats.py # compact tensor statistics (never raw tensors)
ir.py # IR builder: stable IDs, empty-pass contraction, edges
diagnostics.py # rule-based anomaly detection
api.py # public view()/trace()/SpaceRun (+ draw_graph/export_dot)
export.py # embeds IR into the self-contained viewer
assets/viewer.html # packaged viewer (generated — do not edit)
viewer/
src.html # viewer source (edit this)
three_r128_inline.html # vendored three.js r128 (script-wrapped)
scripts/build_viewer.py # src.html + three -> torchspace/assets/viewer.html
docs/ # GitHub Pages live demo + README assets
build_demo.py # end-to-end driver -> out/torchspace_viewer.html
tests/test_smoke.py
Developing the viewer: edit viewer/src.html, then run
python scripts/build_viewer.py to regenerate the packaged asset, and
python build_demo.py to rebuild the demo scene.
Known limitations (PoC)
- Layout is a simple deterministic layered algorithm; production uses ELK
compound layout. The 3D viewer has no
graph_direquivalent (useexport_dot(graph_dir=...)for that). - Tensor dtype/bytes are captured for op outputs and inputs, not for tensors
created by
torch.*creation ops insideforward. requires_gradin the IR reflects the (no-grad) structural pass.- Single step / single device shown in the viewer; earlier steps stay in the IR. Static HTML export only (no live server).
Creator
License
MIT — torchview (MIT) is imported as a dependency, none of its code is copied; three.js r128 (MIT, © Three.js Authors) is bundled inside the packaged viewer with its license header retained.
Metadata
Release files for torchspace 0.1.0a2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| torchspace-0.1.0a2.tar.gz | 357.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| torchspace-0.1.0a2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 550.2 kB
Release files / torchspace-0.1.0a2.tar.gz
| Download URL | torchspace-0.1.0a2.tar.gz |
|---|---|
| Size | 357.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb71d8fb2f8d7a203e1e5b75687aeb221285ccbc91806347ac1fa90c734cf79e
|
|
BLAKE2b-256 checksum How to use checksums |
0821114bcc8bd3bca948f94d8d50a2e9937a57b01f788cdf4ddeb6366cb04659
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.2
|
Release files / torchspace-0.1.0a2-py3-none-any.whl
| Download URL | torchspace-0.1.0a2-py3-none-any.whl |
|---|---|
| Size | 192.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
746f9c69883af6c777158b593b6ef093951ccffdc00389c4630745cb9777f37b
|
|
BLAKE2b-256 checksum How to use checksums |
b8fad9148b0556baf9e125e9d06c81d402ced12a4d66f7f1bdded8b10bab22ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.2
|