Memory-Efficient Backpropagation through Large Linear Layers

Daniel Bershatsky, Aleksandr Mikhalev, Aleksandr Katrutsa, Julia Gusak, Daniil Merkulov, Ivan Oseledets

TL;DR

This paper introduces a memory-efficient method for backpropagation in large linear layers of neural networks by employing randomized matrix multiplication. This approach significantly reduces memory footprint during training with only a moderate impact on test accuracy, making it particularly useful for large models.

What Problem Does This Paper Address?

Modern deep learning models, especially Transformers, rely heavily on large linear layers. During the training process, specifically during the backward pass, these layers require substantial memory to store intermediate activations for gradient computation. This memory bottleneck can limit the size of models that can be trained or the batch sizes that can be used, especially on hardware with limited memory. While attention mechanisms have seen advancements in memory efficiency, linear layers have remained a significant memory consumer. This paper aims to alleviate this by proposing a memory-efficient backpropagation technique for these large linear layers.

Methods

The core of the proposed method lies in approximating the matrix multiplication involved in computing gradients for linear layers using randomized matrix multiplication (RMM). Instead of storing the full input tensor $X$ for the backward pass, the authors propose storing a compressed version, $X_{proj} = S \cdot X$, where $S$ is a randomly generated matrix. This compressed representation significantly reduces the memory required for storing activations. The random matrix $S$ is not stored explicitly but is rematerialized from a random seed, further saving memory.

The paper also delves into the theoretical implications of this randomization, analyzing the variance of the gradient estimate induced by RMM. They compare this variance to the inherent noise in stochastic gradient descent (SGD) due to mini-batch sampling. The authors provide theoretical bounds and empirical estimates for these variances, suggesting that the added noise from RMM might not necessarily harm convergence and could even act as a regularizer.

Datasets

The experiments were conducted using the GLUE benchmark, a collection of diverse natural language understanding tasks. The authors fine-tuned the pre-trained RoBERTa-base model on these tasks. Additionally, an auxiliary experiment was performed on a graph neural network (GCN2) on the OBGN-ARXIV dataset to demonstrate the universality of the RMM approach.

Key Insights and Ideas

  • Memory Savings are Significant: The proposed RMM approach can reduce memory requirements by a factor of 5-10 times, leading to a 10-20% reduction in overall runtime memory for models like RoBERTa.
  • Accuracy Trade-off is Manageable: While there is a decrease in test accuracy, it is generally moderate, especially with compression rates of 5-10 times. In some cases, the added noise from RMM might even be beneficial for generalization.
  • Theoretical Justification for Noise: The paper provides a theoretical framework to understand the variance introduced by RMM and its relationship to the inherent noise in SGD. This analysis suggests that the added noise is not always detrimental and can be comparable to or even smaller than the noise from mini-batch sampling.
  • Practical Implementation is Feasible: The method is implemented as a drop-in replacement for standard linear layers, requiring minimal changes to existing deep learning pipelines.

Key Results

  • GLUE Benchmark Performance: Fine-tuning RoBERTa on GLUE tasks with 5-10x compression rates resulted in an insignificant drop in performance across most tasks. For example, on the CoLA task, accuracy dropped from 60.51% (No RMM) to 57.53% (10% compression).
  • Memory Reduction: Table 3 shows that for tasks like MRPC with a batch size of 128, memory usage was reduced from 11.3 GiB (No RMM) to 8.7 GiB (10% compression), a saving of 23.3%. Figure 2 (left) visually demonstrates this memory reduction with increasing batch sizes.
  • Throughput: Figure 2 (right) shows that while some compression rates might slightly decrease throughput, others can even improve it, suggesting that RMM can sometimes be computationally more efficient.
  • Graph Neural Networks: On the GCN2 dataset, RMM (Gaussian) achieved a test accuracy of 70.99% compared to 72.87% for the baseline, a drop of 2.58% with a compression rate of 0.1.

Limitations and Future Work

  • Implementation Details: The authors note that naive high-level implementations of RMM in PyTorch might not be optimal, and low-level optimizations are needed for better performance.
  • Random Matrix Choice: While Gaussian matrices were used, other variants like Subsampled Orthogonal with Random Signs were tested but showed higher variance, requiring larger projection dimensions.
  • Future Directions:
    • Developing stable and robust implementations of RMM with fast matrix-by-vector products.
  • Exploring other random matrix distributions and tensor-product sampling matrices for further complexity reduction, especially for Transformer models where the input dimension is large.
  • Deeper study of the variance estimator and its connection to learning rate schedules, potentially leading to theoretically justified schedules that leverage the estimated variance.

Summary updated

BibTeX
@inproceedings{bershatsky2024memory,
  author = {Daniel Bershatsky and Aleksandr Mikhalev and Aleksandr Katrutsa and Julia Gusak and Daniil Merkulov and Ivan Oseledets},
  title = {{Memory-Efficient Backpropagation through Large Linear Layers}},
  booktitle = {ICOMP Publication},
  year = {2024},
  url = {https://openreview.net/forum?id=OJU6mq4Iyg},
}
Figure