Lowering PyTorch's Memory Consumption for Selective Differentiation
Samarth Bhatia, Felix Dangel
TL;DR
The paper tackles the memory bottleneck of PyTorch's automatic differentiation in the context of selective differentiation, where gradients are requested for only a subset of graph leaves. It introduces differentiability-aware, drop-in memory-saving layers that discard layer inputs when weights are non-differentiable, preserving forward/backward behavior while reducing AD graph storage. Empirical evaluations on CNNs and Transformers show memory reductions without runtime penalties, though gains depend on layer interactions and activation choices; a mask-based ReLU and evaluation-mode BN can substantially amplify savings. The approach is a practical, easy-to-use converter that enables scalable selective fine-tuning and highlights avenues for further AD-optimization research.
Abstract
Memory is a limiting resource for many deep learning tasks. Beside the neural network weights, one main memory consumer is the computation graph built up by automatic differentiation (AD) for backpropagation. We observe that PyTorch's current AD implementation neglects information about parameter differentiability when storing the computation graph. This information is useful though to reduce memory whenever gradients are requested for a parameter subset, as is the case in many modern fine-tuning tasks. Specifically, inputs to layers that act linearly in their parameters (dense, convolution, or normalization layers) can be discarded whenever the parameters are marked as non-differentiable. We provide a drop-in, differentiability-agnostic implementation of such layers and demonstrate its ability to reduce memory without affecting run time.
