Repository navigation
Add opt-in memory-efficient CSR aggregation for graded synapses - #27
TorgemanTarak wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
The custom autograd and CUDA sparse operations warrant final human review despite comprehensive parity testing.
Review effort: Balanced
Findings: None
What changed in this PR
Adds an opt-in CSR backend for memory-efficient graded-synapse training while retaining scatter as the default.
Changes:
- Implements custom sparse forward and first-order backward aggregation.
- Integrates backend selection, validation, configuration, and parity tests.
- Documents limitations and provides reproducible RTX 5090 benchmarks.
| File | Description |
|---|---|
flyvis/network/sparse.py |
Implements CSR aggregation and gradients. |
flyvis/network/network.py |
Integrates backend selection and sparse weights. |
flyvis/network/dynamics.py |
Supports node-level current aggregation. |
tests/test_sparse_current.py |
Tests parity, gradients, edge cases, and checkpoints. |
docs/docs/reference/network.md |
Documents usage and limitations. |
benchmarks/sparse_current.py |
Adds the benchmark runner. |
benchmarks/rtx5090-sparse-training.md |
Summarizes benchmark methodology and results. |
benchmarks/rtx5090-sparse-training.jsonl |
Records raw benchmark measurements. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
This is very useful for recurent training otherwise the memory usage is too high |
lappalainenj
left a comment
There was a problem hiding this comment.
Thanks for the careful PR. Recap of my review:
Verified
- On CPU (torch 2.10) and an H200 (torch 2.5.1, CUDA 12.1), the new tests plus the network, dynamics and initialization suites give 198 passed and 12 skipped. That matches your count, and it also covers an older torch than the one you used.
- On the extent-15 graph, outputs and all parameter and input gradients match scatter to about 1e-15 in float64 and at most 5e-6 in float32.
simulatefrom a grey steady state also matches. - A fuzz test on 300 random graphs (duplicate edges, int32 indices, isolated nodes, empty graphs) gives a max error of 1e-13 against
scatter_add. Double backward raises an error, as intended. - Peak memory on H200 matches your 5090 numbers exactly. Neither backend is bitwise reproducible on CUDA, so determinism is unchanged.
Speed relative to scatter (H200)
- Batch 4, 32–40 steps: 1.4× slower.
- Batch 16: 1.3–1.5× faster.
- Batch 32: 1.8–2.1× faster, with 4.6× less peak memory.
Since batch 4 is the default training setup, keeping scatter as the default is right.
Requested changes
- Keep the backend out of the model config. Please drop
self.config.backend = backendand take the Hydra option out ofnetwork:. Use a solver-level key that the solver passes toNetwork(...). The backend changes how the model runs, not what it computes. In the model config, it would also decide the backend used for all later analysis, make ensembles that mix backends fail the config match, and change cache keys. - Rename
"sparse_trainable"to"csr". Scatter is also trainable, and the sparse path also runs inference. Renaming now avoids breaking saved configs later. - Docs: replace "benchmark your graph" with the crossover: use it at batch ≥16 or when memory is the limit.
- Cleanup:
- Drop the redundant activation-type check; it always passes.
- Revert the unrelated
torch\n.stack(formatting change, which the pinned ruff v0.5.5 reformats back. - Keep
benchmarks/sparse_current.py, but move the single-machine.jsonland.mdresults into the PR description.
Keep the strict check that allows only PPNeuronIGRSynapses and rejects in-place activations. The in-place rejection is required for correctness.
Could you also open a separate datamate issue for the Windows HDF5 handle bug you hit? It's currently only mentioned in the benchmark notes.
With these changes I'm happy to approve.
— Claude (review draft), reviewed by @lappalainenj
Thanks for the review i applied your changes hope its useful |
FlyVis's scatter aggregation retains edge-sized activity tensors during recurrent training. This adds an optional
Network(backend="csr")backend for the default graded-synapse dynamics, reducing activation memory while preserving the existing outputs, first-order gradients, parameter sharing, and checkpoint keys. Hydra training accepts the solver overridebackend=csr. The model config stays identical across backends, and reconstruction for analysis defaults to scatter.The implementation coalesces parallel edges, caches both CSR layouts, and computes sparse weight and activity gradients without retaining batch-by-edge tensors. It supports CPU/CUDA float32 and float64 and the built-in pointwise activations. Custom dynamics, mixed precision, higher-order gradients, and compilation are outside this patch's supported scope.
On the supplied visual-system graph (45,669 nodes, 1,513,231 edges), an RTX 5090 benchmark at batch 16 and 32 steps reduced peak allocated memory from 3.641 to 0.886 GiB (75.7%) and median forward/backward time from 48.02 to 30.95 ms (1.55x). Batch 4 saved memory but was slower. These are synthetic forward/backward measurements, excluding optimizer updates; they do not establish task accuracy or time to convergence. Reproduction instructions and raw measurements are below.
Original validation: 198 tests passed and 12 skipped across new sparse tests and existing network, dynamics, and initialization suites. Coverage includes output/gradient parity, finite-difference checks, shared parameters, duplicate edges, initial-state gradients, optimizer-step parity, checkpoint compatibility, and CPU/CUDA execution. Ruff and whitespace checks passed. The full pretrained-asset suite was not run.
Review follow-up on CPU (PyTorch 2.5.1) covered the sparse, network, dynamics, initialization and solver suites (186 passed, 12 skipped). The solver configuration tests also passed through the training CLI’s config preparation. Ruff 0.5.5 lint and format checks passed.
RTX 5090 benchmark
Measured 2026-09-26 on an RTX 5090, Windows 11, Python 3.12.11, PyTorch
2.14.0+cu130 (CUDA 13.0), float32, four CPU threads. Source commit:
7a76dc3cf98c8c1d380d0d86275b094c22b081c8(clean at benchmark start).The supplied
fib25-fib19_v2.2.jsongraph at extent 15 has 45,669 nodes,1,513,231 edges and 734 trainable shared parameters. This measures the
actual FlyVis
Networkforward/backward with its default dynamics, syntheticinput,
dt=0.02, and a squared-activity loss. It excludes graph compilation,data loading, a task decoder, and optimizer updates. It does not measure
optical-flow accuracy or learning speed to a target score.
Times are medians of 10 synchronized wall-clock repetitions after 3 warmups.
Peak is
torch.cuda.max_memory_allocated, including live model tensors,input, activations and gradients. It is not allocator-reserved or total
process VRAM. The JSONL includes every timing, the baseline allocation,
incremental peak, loss, and environment. Initialization uses seed 20260926;
input uses seed 20260927 for both backends.
scattercsrscattercsrscattercsrscattercsrAt batch 16 and 32 steps, the sparse backend used 4.11x less peak allocated memory
(75.7% reduction) and was 1.55x faster in this run.
At batch 4, it saved memory but was slower. This supports an opt-in
backend, not a universal speedup claim. Timing varies across runs;
the individual repetitions are retained for inspection. All gradients
were finite and paired final losses matched at the recorded precision.
Reproduce:
The benchmark used datamate 1.0.0 with a local workaround for a Windows file lock during connectome cache creation. Datamate now removes the previous file before opening a new HDF5 handle. The fix was merged after the 1.0.0 release. Additional benchmark environment versions were NumPy 2.5.3, pandas 3.0.6, SciPy 1.18.1 and torchvision 0.29.0+cu130. Datamate issue #22 · Merged fix #23
Raw RTX 5090 measurements (JSONL)
The original runner recorded CSR under its earlier name,
sparse_trainable.{"backend": "scatter", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 4, "steps": 8, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 6.658950005657971, "repetition_ms": [12.325900024734437, 6.494899978861213, 7.438400061801076, 6.689100060611963, 6.978000048547983, 7.02719995751977, 6.510999985039234, 6.6287999507039785, 6.534100044518709, 6.385100074112415], "baseline_bytes": 119839232, "peak_bytes": 440835584, "incremental_peak_bytes": 320996352, "loss": 0.2890213429927826, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "sparse_trainable", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 4, "steps": 8, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 9.959999995771796, "repetition_ms": [27.376099955290556, 9.837899939157069, 10.078800027258694, 10.149299982003868, 9.966399986296892, 10.204300051555037, 9.953600005246699, 9.730400051921606, 9.147300035692751, 9.25999996252358], "baseline_bytes": 170425344, "peak_bytes": 260221440, "incremental_peak_bytes": 89796096, "loss": 0.2890213429927826, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "scatter", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 4, "steps": 32, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 20.696200022939593, "repetition_ms": [54.0492000291124, 22.04800001345575, 21.971500013023615, 19.167799968272448, 20.561299985274673, 19.632500014267862, 18.653900013305247, 20.831100060604513, 18.73799995519221, 28.39949994813651], "baseline_bytes": 137375744, "peak_bytes": 1118399488, "incremental_peak_bytes": 981023744, "loss": 0.2865479290485382, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "sparse_trainable", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 4, "steps": 32, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 29.006949975155294, "repetition_ms": [34.638999961316586, 35.13699991162866, 26.536599965766072, 27.772999950684607, 29.86629994120449, 25.52969998214394, 28.1476000091061, 30.274000018835068, 25.780600029975176, 31.48280002642423], "baseline_bytes": 188438528, "peak_bytes": 382202880, "incremental_peak_bytes": 193764352, "loss": 0.2865479290485382, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "scatter", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 16, "steps": 8, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 13.377799943555146, "repetition_ms": [29.85140006057918, 13.411999912932515, 13.18919996265322, 12.390799936838448, 13.343599974177778, 15.434899949468672, 14.01330006774515, 13.886100030504167, 12.91369996033609, 12.86340004298836], "baseline_bytes": 137375744, "peak_bytes": 1294208512, "incremental_peak_bytes": 1156832768, "loss": 0.288987934589386, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "sparse_trainable", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 16, "steps": 8, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 8.459850039798766, "repetition_ms": [12.31670007109642, 8.641700027510524, 8.04660003632307, 8.791400003246963, 8.373800083063543, 8.452400099486113, 9.07190004363656, 8.081800071522593, 7.650499930605292, 8.46729998011142], "baseline_bytes": 188438528, "peak_bytes": 373998592, "incremental_peak_bytes": 185560064, "loss": 0.288987934589386, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "scatter", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 16, "steps": 32, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 48.0207500513643, "repetition_ms": [96.65060008410364, 48.018300090916455, 47.19750001095235, 47.827200032770634, 48.02320001181215, 49.035799922421575, 48.76949999015778, 49.54509995877743, 46.662600012496114, 46.610499965026975], "baseline_bytes": 208365056, "peak_bytes": 3909596672, "incremental_peak_bytes": 3701231616, "loss": 0.2865470051765442, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}} {"backend": "sparse_trainable", "extent": 15, "nodes": 45669, "edges": 1513231, "trainable_parameters": 734, "batch": 16, "steps": 32, "dtype": "torch.float32", "dt": 0.02, "repeats": 10, "warmup": 3, "forward_backward_ms": 30.952000059187412, "repetition_ms": [38.178599905222654, 33.12720009125769, 27.798900031484663, 27.643300010822713, 30.70800006389618, 27.18470001127571, 31.554799992591143, 28.717399924062192, 31.196000054478645, 32.88720001000911], "baseline_bytes": 257521152, "peak_bytes": 951452672, "incremental_peak_bytes": 693931520, "loss": 0.2865470051765442, "gradients_finite": true, "environment": {"torch": "2.14.0+cu130", "cuda": "13.0", "device": "NVIDIA GeForce RTX 5090", "platform": "Windows-11-10.0.26200-SP0", "python": "3.12.11", "threads": 4, "source_commit": "7a76dc3cf98c8c1d380d0d86275b094c22b081c8", "source_dirty": false}}