Skip to main content
QUICK REVIEW

[Paper Review] Scaling Deep Contrastive Learning Batch Size under Memory Limited Setup

Luyu Gao, Yunyi Zhang|arXiv (Cornell University)|Jan 18, 2021
Domain Adaptation and Few-Shot LearningComputer Science16 references5 citations
TL;DR

This paper introduces Gradient Cache, a technique that decouples encoder backpropagation from batch-wise contrastive loss, enabling training with arbitrarily large batch sizes on memory-limited GPUs by caching representation gradients. The method achieves state-of-the-art performance on dense passage retrieval using only a single consumer-grade GPU, matching models trained with multiple high-end GPUs while using 20% more runtime but constant memory usage regardless of batch size.

ABSTRACT

Contrastive learning has been applied successfully to learn vector representations of text. Previous research demonstrated that learning high-quality representations benefits from batch-wise contrastive loss with a large number of negatives. In practice, the technique of in-batch negative is used, where for each example in a batch, other batch examples' positives will be taken as its negatives, avoiding encoding extra negatives. This, however, still conditions each example's loss on all batch examples and requires fitting the entire large batch into GPU memory. This paper introduces a gradient caching technique that decouples backpropagation between contrastive loss and the encoder, removing encoder backward pass data dependency along the batch dimension. As a result, gradients can be computed for one subset of the batch at a time, leading to almost constant memory usage.

Motivation & Objective

  • To overcome GPU memory limitations that restrict batch size in contrastive learning, especially for researchers with limited hardware.
  • To enable training with large batches—critical for high-quality representation learning—on a single consumer-grade GPU.
  • To preserve the exact gradient update of large-batch training without approximation, ensuring model performance parity with multi-GPU setups.
  • To democratize access to large-batch contrastive learning by making it feasible for academic and resource-constrained settings.

Proposed method

  • Introduces a two-stage backpropagation process: first compute gradients from loss to representations, then from representations to model parameters.
  • Caches the gradient of the loss with respect to representations (representation gradient cache) to decouple batch dependency in encoder updates.
  • Performs encoder optimization in sub-updates over mini-batches of representations, enabling large effective batch sizes within fixed GPU memory.
  • Applies the same principle to deep distance functions by introducing a separate distance gradient cache for parameterized similarity functions.
  • Uses the cached gradients to compute exact full-batch gradients in a memory-efficient manner, avoiding the need to store all activations from the full batch.
  • Extends the method to support deep similarity functions Φ(·,·) by caching both distance gradients and representation gradients for joint optimization.

Experimental results

Research questions

  • RQ1Can we train contrastive models with large batch sizes on a single GPU without exceeding memory limits?
  • RQ2Does decoupling encoder backpropagation from the contrastive loss enable exact gradient updates equivalent to full-batch training?
  • RQ3Can the proposed method reproduce state-of-the-art performance on dense retrieval benchmarks using only a single consumer GPU?
  • RQ4How does the method scale in terms of training speed and memory usage compared to gradient accumulation and sequential training?
  • RQ5Can the technique be generalized to deep distance functions beyond simple dot product similarity?

Key findings

  • The Gradient Cache method enables training with a batch size of 512 on a single RTX 2080ti, matching the performance of models trained with 8 V100 GPUs.
  • With 20% more runtime, the method achieves state-of-the-art results on the MS MARCO passage ranking benchmark, outperforming both standard and accumulated training baselines.
  • The method maintains constant peak memory usage regardless of batch size, making it feasible to scale batch size beyond hardware limits.
  • Gradient accumulation fails to match performance due to fewer in-batch negatives per update, while sequential training suffers from high variance and lower accuracy.
  • The approach successfully generalizes to deep distance functions via a dual-cache mechanism, maintaining exact gradient computation for both similarity and representation parameters.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.