# RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

- **ID:** `pytorch/grad-accumulation-required-grad-false`
- **Domain:** pytorch
- **Category:** autograd_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Calling backward() twice on the same graph without retain_graph=True, which is common in gradient accumulation loops where the graph is re-used.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.10 | active | — | — |
| 1.11 | active | — | — |
| 1.12 | active | — | — |
| 1.13 | active | — | — |
| 2.0 | active | — | — |
| 2.1 | active | — | — |
| 2.2 | active | — | — |
| 2.3 | active | — | — |

## Workarounds

1. **Set retain_graph=True only on the first backward call when accumulating gradients over multiple steps: loss.backward(retain_graph=True) for all but the last step, then loss.backward() on the final step.** (80% success)
   ```
   Set retain_graph=True only on the first backward call when accumulating gradients over multiple steps: loss.backward(retain_graph=True) for all but the last step, then loss.backward() on the final step.
   ```
2. **Use gradient accumulation with model.zero_grad() and loss.backward() only once per accumulation cycle, ensuring the graph is freed after each step.** (90% success)
   ```
   Use gradient accumulation with model.zero_grad() and loss.backward() only once per accumulation cycle, ensuring the graph is freed after each step.
   ```

## Dead Ends

- **** — Adding retain_graph=True every time hides the real issue of memory growth and can cause OOM. (40% fail)
- **** — This only works if the graph is re-used; if not, it just defers the error. (50% fail)
