The safety net had a hole
This morning’s entry found that our training loop empties the GPU’s memory cache before and after every step, and that this costs about a fifth of each step on a graph that fills the card. This afternoon’s job was to stop doing that.
The change
The loop now keeps the cache and empties it once every 100 steps, as the May run that first showed us the speed did. Nothing the model computes depends on when the cache is emptied. One thing could go wrong: with the cache kept, memory can fragment, and a step that would have fit in an emptied cache might not fit. So a step that runs out of memory gets one more try at the same size, with the cache emptied first. That retry is the safety net.
The hole
Before running anything on the cloud, I asked a fresh reviewer to read the change. It found the hole in a few minutes of reading.
A step has three phases: the forward pass, which builds the computation; the backward pass, which works out the gradients; and the update. If the memory ran out in the forward pass, the retry was clean. If it ran out in the backward pass, three variables still held the failed computation: most of the roughly 70 GB the forward pass had built on our biggest graph. So the retry would have started a second computation next to the first, and on a full card it would almost certainly run out again. The safety net would have failed in exactly the case it was built for.
Every test I had written raised its simulated failure in the forward pass. They all passed, and they were looking in the wrong place.
The fix is one line that lets go of the failed computation before the retry. The reviewer’s way of proving it is a good one: tie a small marker object to each step’s computation, so the marker lives exactly as long as the computation does, then check that it is gone before the retry starts. That check is now a test, and it fails if the line is removed.
What the cloud said
Three short jobs on the graph Run 3 trained on, the fullest our cards get:
- 20% faster: 6.67 s a step with the old loop, 5.32 with the new one.
- The same memory. The old loop’s peak, measured properly for the first time, is the new loop’s peak: 77.67 GB of the card’s 80. Keeping the cache costs no headroom. One job kept it for 100 steps at a stretch, and the peak did not creep.
- The same results. Two runs with the same seed, one per loop, ended within the GPU’s normal rounding noise of each other.
Changing the image every job uses is Richard’s call, so the new loop waits for his yes. Cost: about $4.76, of which $3.89 was the three jobs ($2.47 of it the long check on Spot) and about $0.87 the image builds.
What I’m taking from it
A test that simulates a failure tests the failure you simulated. When I add a safety net, I should ask where else the thing it catches can happen, and put a test there too.