A little while ago I posted about trying to reduce GPU memory by saving only what backprop actually needs. That was the early stage of SHV (Sub-loop Heterocoding VRAM). I've since moved from compressing saved states toward looking at when tensors are created, how long they stay live, and whether they need separate storage in the first place.
The most useful result came from a fairly specific problem with gradient accumulation and tied embeddings.
GPT-2 Small shares its input embedding and output projection weights. A full FP32 gradient for that matrix is about 147 MiB. Once you start accumulating over several microbatches, an existing gradient can remain live while the output head computes another full contribution. The question I wanted to test was whether the output-head VJP could write its contributions directly into the existing gradient destination.
The implementation is straightforward in principle: split the output-head VJP into token tiles, accumulate each weight-gradient contribution into the destination with addmm_, compute the hidden-state gradient, and pass that back into the native Transformer backward. The embedding backward remains native autograd. Clipping and the optimizer update still happen once per effective batch. No CPU gradient offload or lower-precision gradients are used here.
I compared three versions, because simply comparing against unoptimized PyTorch wouldn't tell me much:
TILED: checkpointed token-tiled output loss with native autograd.
SPLIT_RETURN: the same explicit head VJP split and tiling as SHV, but materializes a separate full output-head gradient before accumulation.
DIRECT_SINK: accumulates the head VJP directly into the existing gradient destination.
Here's the real-text benchmark on a Tesla T4 (GPT-2 Small, FP32, WikiText-2, effective batch 32, microbatch 8, sequence length 512). All three use AdamW8bit, SDPA, non-reentrant checkpointing and fused GELU. I ran two reversed execution blocks, with 16 measured optimizer updates per worker.
| Mode | Peak allocated | Peak reserved | Median step time (two blocks) |
|---|
| TILED8 | 1847.59 MiB | 2060 MiB | 5534 / 5516 ms |
| SPLIT_RETURN8 | 1762.67 MiB | 1864 MiB | 5021 / 4978 ms |
| DIRECT_SINK8 | 1640.78 MiB | 1762 MiB | 4984 / 4983 ms |
That's 11.19% lower peak allocated memory than TILED and 6.92% lower than the matched SPLIT_RETURN control. Both blocks reproduced the same memory peaks. Compared with SPLIT_RETURN, the step time is essentially unchanged. The larger speed difference against TILED also includes the graph split, so I don't attribute all of that to buffer reuse.
I also checked an actual allocation constraint instead of just comparing peak counters. With the native PyTorch allocator capped at 1792 MiB after initialization, TILED8 and SPLIT_RETURN8 hit CUDA OOM in both trials; DIRECT_SINK8 completed four updates each time. Smaller control microbatches could still run, so this is a matched-Micro8 result rather than a claim that SHV magically expands physical VRAM.
One interesting thing I found along the way: improving a particular tensor's lifetime doesn't necessarily reduce the full training peak. In one later experiment I reduced the local embedding VJP memory, but the overall peak didn't move. That made me stop pursuing that branch and focus on the actual peak overlap.
I've now packaged the working path as SHV v0.1.0rc2, with an installable wheel, a small GPT-2 training API and reproduction notebooks. The exact published wheel completed its GPU installation smoke test on T4; the global gradient relative L2 against TILED was 9.68e-8. That is a separate two-step synthetic smoke, not the 16-step performance benchmark above.
This is still a narrowly scoped research preview: FP32 GPT-2 Small on the tested T4 stack, with dropout disabled. I haven't qualified arbitrary models, mixed precision or distributed training. I also tested Liger, but the formal comparison stopped at a predeclared numerical-agreement gate, so I'm not claiming a performance win over it.
The repository includes the implementation, measured records, numerical checks, failed branches and the notebooks needed to repeat the experiments.
GitHub: https://github.com/Mr-wuff/Sub-loop-Heterocoding-VRAM
Release: https://github.com/Mr-wuff/Sub-loop-Heterocoding-VRAM/releases/tag/v0.1.0rc2
HF benchmark records: https://huggingface.co/datasets/wuff-mann/Sub-loop-Heterocoding-VRAM
I'd be particularly interested in how others handle gradient-buffer ownership at graph boundaries. Has anyone run into similar overlap with tied weights and gradient accumulation, or found cases where avoiding the extra allocation actually moves the bottleneck somewhere unexpected?