A researcher is training a transformer model in PyTorch and notices that GPU memory usage grows steadily with each batch, eventually causing an out-of-memory error. The training loop accumulates predictions for later logging but does not detach them from the computation graph. Which practice directly addresses this memory growth?
- AEnable mixed precision training so that activations are stored in half precision, halving the memory footprint across the entire computation graph.
- BConvert accumulated prediction tensors to plain Python scalars or detach them from the computation graph before storing them, so the autograd graph for each batch is freed after the backward pass. Correct
- CReduce the global learning rate so that fewer gradient updates are performed per epoch, limiting the number of backward passes that accumulate memory across the graph.
- DSwitch the data loader to use multiple worker processes so that CPU preprocessing is offloaded, reducing the volume of data resident on the GPU at any one time.
Why A is wrong: Mixed precision reduces the memory used by parameters and activations but does not release references that are intentionally held in Python variables. A tensor kept alive in a list still retains its full computation graph regardless of dtype, so the memory leak continues.
Why B is correct: Retaining tensors that are still attached to the computation graph prevents PyTorch from releasing the intermediate activations built during the forward pass. Detaching or converting to a scalar breaks that reference, allowing the graph to be garbage-collected after each iteration.
Why C is wrong: The learning rate controls the magnitude of parameter updates, not memory retention. Fewer backward passes would reduce training progress without fixing the root cause, which is holding live tensor references outside the training step.
Why D is wrong: Multi-worker data loading improves throughput by parallelising CPU preprocessing but does not affect memory held inside the GPU computation graph. The accumulation problem is caused by retained autograd references, not by the data-loading pipeline.