PyTorch commit slashes test memory usage by 66% with explicit deallocation
A single PR drops peak memory from 14.2GB to 4.8GB in a critical serialization test.
Edward Z. Yang (ezyang) at Meta submitted PR #189451 to the PyTorch repository to fix an out‑of‑memory (OOM) condition in a serialization test. The test `test_serialization_2gb_file` was crashing in CI because its peak memory usage reached ~14.2 GiB, well above the memory budget for a single test on a `linux.4xlarge` instance that runs multiple processes. The root cause was that the test kept large buffers alive across phases and allocated a 3.13 GiB float32 tensor while holding both it and a reloaded copy for an equality check.
The fix introduces three optimizations: first, explicit `del` statements free large buffers (the model, state, BytesIO, and source tensor) immediately before the next large allocation – verified that no garbage collection was needed. Second, the phase‑2 tensor is changed from a 3.13 GiB float32 to a >2 GiB uint8 tensor, which is still large enough to exercise the zip64 code path the test is designed to protect. Third, instead of comparing the entire tensor, the test now writes a known 8‑byte pattern at the start, the 2 GiB boundary, and the end, then verifies only those slices. This avoids holding two multi‑GiB tensors simultaneously. The result is a peak memory drop from ~14.2 GiB to ~4.8 GiB, with the remaining 4.8 GiB from the unavoidable model‑plus‑state_dict step.
- Peak memory reduced from ~14.2 GiB to ~4.8 GiB, a 66% reduction.
- Explicit `del` statements free buffers before next allocation, eliminating need for `gc.collect()`.
- Equality check changed from full tensor comparison to slice comparison at key boundaries.
Why It Matters
PyTorch CI tests now run reliably without OOMs, reducing flaky failures and improving developer productivity.