A Gradient Accumulation Method for Dense Retriever under Memory Constraint

Jaehee Kim, Yukyung Lee, Pilsung Kang

TL;DR

Training high-performance dense retrievers usually requires massive batch sizes to provide enough negative samples, creating a steep hardware bottleneck for researchers with limited resources. This paper introduces CONTACCUM (Contrastive Accumulation), a novel method that uses a dual memory bank to cache both query and passage representations from previous steps. This simple addition stabilizes training, requires negligible extra memory, and allows a modest 11GB GPU to outperform traditional models trained on high-end 80GB hardware.

What Problem Does This Paper Address?

Modern dense text retrieval relies on dual-encoder architectures (one encoder for queries, another for passages) trained using the InfoNCE loss function. To train these models effectively, researchers use "in-batch negative sampling," where passages paired with other queries in the same batch serve as negative examples. Mathematically, more negatives lead to better performance, which is why state-of-the-art models are trained with massive batch sizes (ranging from 128 to over 8,000), requiring clusters of expensive GPUs.

For researchers with limited hardware, standard workarounds fall short:

  • Gradient Accumulation (GradAccum) saves memory by splitting a large batch into smaller local batches, but it drastically reduces the number of negative samples available per step, degrading model quality.
  • Gradient Cache (GradCache) retains the full negative count by decomposing the backpropagation process, but it requires extra forward passes, making training painfully slow.
  • Single Memory Banks (caching only passage representations) introduce a "gradient norm imbalance problem." Because the passage encoder sees many more representations than the query encoder, their gradients become highly imbalanced, leading to severe training instability.

Methods

The authors propose CONTACCUM (Contrastive Accumulation) to achieve the best of both worlds: the speed of gradient accumulation and the large negative sample size of memory banks, without the training instability.

Figure 1: Illustrations of CONTACCUM and Comparative Methods. The illustrations show a total batch size (Ntotal) of 4, a local batch size (Nlocal) of 2, and a memory bank size (Nmemory) of 4. (a) GradCache uses Ntotal −1 negative passages. (b) GradAccum uses Nlocal −1 negative passages. (c) CONTACCUM leverages Nlocal + Nmemory −1 negative samples, more than Ntotal −1.

The core innovation is a dual memory bank structure ($M_q$ for queries and $M_p$ for passages) implemented as simple First-In-First-Out (FIFO) queues.

As shown in the training process below, at each accumulation step, newly generated query and passage representations are enqueued, and the oldest are dequeued.

Figure 2: Training process of CONTACCUM at each accumulation step. The illustration shows a total batch size (Ntotal) of 4, an accumulation step (K) of 2, and a memory bank size (Nmemory) of 4. The dual memory bank caches both query and passage representations. New representations are enqueued, and the oldest are dequeued at each step, maintaining the similarity matrix (Sk) size at (Nlocal + Nmemory, Nlocal + Nmemory).

To keep memory usage low, CONTACCUM applies a stop-gradient operation (sg(·)) to all cached representations in the memory bank. This means the model does not store activations or backpropagate gradients through the historical cache—it simply reuses them as extra negative samples to compute the similarity matrix. Crucially, maintaining equal-sized memory banks for both queries and passages ensures that the gradient calculations for both encoders remain perfectly balanced.

Datasets

The methodology was validated across five widely used information retrieval benchmarks:

  • Natural Questions (NQ)
  • TriviaQA
  • Curated TREC (TREC)
  • Web Questions (WebQ)
  • MS Marco

Key Insights and Ideas

  • The Gradient Norm Imbalance Problem: The paper mathematically demonstrates why previous single-memory-bank methods failed. Caching only passages causes the passage encoder's gradient norm to skyrocket (up to 30 times larger than the query encoder's), destabilizing training. A dual memory bank completely resolves this imbalance.
  • Stale Representations are Useful Early On: Conventional wisdom suggested that using memory banks early in training is bad because the encoders change too rapidly, making cached representations "stale." CONTACCUM proves otherwise: the similarity mass of past representations closely tracks that of in-batch representations, providing a valuable training signal right from the first epoch.

Key Results

  • Outperforming High-Resource Setups: Running on a low-resource 11GB VRAM GPU, CONTACCUM outperformed standard Dense Passage Retrieval (DPR) trained on an 80GB GPU across 18 out of 24 evaluation metrics.
  • Substantial Speedups: CONTACCUM avoids the redundant forward/backward passes of GradCache. At a total batch size of 512, CONTACCUM completed training iterations 34% faster than GradCache, adding only a minor 26% time overhead compared to standard GradAccum.
  • Minimal Memory Footprint: Because it only stores raw embedding vectors without their activation graphs, the dual memory bank requires virtually no extra VRAM—adding a maximum of just 12MB of memory even at a massive bank size of 5,096.

Limitations and Future Work

While CONTACCUM represents a massive win for accessible deep learning, the authors note a few limitations:

  • Supervised Focus: The study focused entirely on the supervised fine-tuning stage of dense retrievers. It remains to be seen how the gradient norm imbalance problem behaves during large-scale unsupervised pre-training.
  • Softmax Bottleneck: CONTACCUM still relies on the standard softmax operation over the enlarged similarity matrix, which can become a computational bottleneck as the memory bank scales to extreme sizes. Future work will explore more efficient alternative training strategies to mitigate this.

Summary updated

BibTeX
@inproceedings{kim2024gradient,
  author = {Jaehee Kim and Yukyung Lee and Pilsung Kang},
  title = {{A Gradient Accumulation Method for Dense Retriever under Memory Constraint}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=QDG2q5MYHV},
}
Figure