"A forward-only path that should have used 2 GB used 75. The model wasn't too big. The framework was quietly saving its homework."
The Crash That Didn't Make Sense
A modest image model, generating a single image, took the machine down — twice. The numbers were absurd: a path that should fit in about 2 GB of memory had ballooned to roughly 75 GB before the display server itself fell over. Nothing about the model's size explained it. The model was small. The image was small. Something invisible was eating memory by the tens of gigabytes.
What Autograd Does When You Aren't Looking
The culprit was the automatic differentiation engine. By default, a deep-learning framework assumes you might want to train — so on every forward pass, it quietly retains the intermediate activations of every layer, because it'll need them to compute gradients during the backward pass. For training, that retention is essential. For inference, where you never call backward, it is pure dead weight — and on a deep diffusion model with many timesteps, that dead weight stacks up into tens of gigabytes of activations nobody will ever use.
The One-Line Fix
The fix is to tell the framework, explicitly, that this path will never need gradients: wrap the forward pass in an inference/no-grad context. With that, the engine stops retaining activations, and the 75 GB collapses back to the ~2 GB the work actually requires. One context manager, an order-of-magnitude memory difference. The bug wasn't in the model or the math — it was a missing declaration of intent.
Why It Took Down the Whole Machine
On a unified-memory Apple Silicon system, GPU and system memory share one pool. So a runaway inference allocation doesn't just crash the Python process — it starves the operating system, and the display server (the thing drawing your screen) goes down with it. That's why the failure was so dramatic: there's no separate VRAM to absorb the mistake. Unified memory is wonderful for big models and unforgiving when something leaks.