Breaking the Memory Barrier of Contrastive Loss via Tile-Based Strategy

Zesen Cheng, Hang Zhang, Kehan Li, Sicong Leng, Zhiqiang Hu, Fei Wu, Deli Zhao, Xin Li, Lidong BingCVPR 2025

Introduction: The Quadratic Memory Wall

Contrastive learning has become the cornerstone of modern vision-language models like CLIP. A fundamental property of this paradigm is that performance generally improves with larger batch sizes, as more negative samples provide a harder training signal. However, researchers face a significant technical hurdle: the "memory wall." Calculating the contrastive loss requires computing a similarity matrix between all image and text embeddings in a batch. For a batch size $b$, this matrix contains $b^2$ elements, leading to quadratic memory complexity ($O(b^2)$). This scaling law effectively caps batch sizes for most researchers, even when utilizing clusters of high-end GPUs.

Inf-CL: A Multi-Level Tiling Strategy

To bypass this limitation, this work introduces Inf-CL, a framework that reduces the memory complexity of contrastive loss from quadratic to linear ($O(b)$). The core innovation is a multi-level tiling strategy that avoids materializing the full similarity matrix.

Tile-Wise Computation

Inf-CL leverages the mathematical properties of the Log-Sum-Exp (LSE) function used in contrastive loss. By breaking the global similarity matrix into smaller, manageable "tiles," the model can compute partial LSE values locally and merge them into a final global loss without ever storing the full $b \times b$ matrix.

Multi-Level Implementation

The tiling occurs at two primary levels to maximize efficiency:

  1. Cross-GPU Tiling: In a distributed setting, GPUs are organized in a ring topology. They exchange feature vectors asynchronously, allowing each device to compute its portion of the loss while overlapping communication with computation.
  2. In-GPU Tiling: Within each GPU, custom fused CUDA kernels further divide the workload. By performing calculations directly in the fast on-chip SRAM and minimizing transfers to the slower main video memory (HBM), Inf-CL maintains high throughput.
The multi-level tiling strategy of Inf-CL, showing both Cross-GPU and In-GPU tiling processes.

Key Results and Scaling

The efficiency gains of Inf-CL are substantial. At a batch size of 256k, Inf-CL achieves a 78x reduction in memory usage compared to standard implementations like OpenCLIP. This efficiency allowed the authors to train a CLIP-ViT-L/14 model with an unprecedented batch size of 4 million using only eight A800 (80GB) GPUs—a feat that would typically require hundreds of GPUs.

GPU memory usage comparison between CLIP, OpenCLIP, and Inf-CL across different batch sizes, highlighting the 78x improvement.

Importantly, Inf-CL is mathematically equivalent to the vanilla contrastive loss. It produces identical gradients, ensuring that memory savings do not come at the cost of model accuracy or precision.

Discussion and Limitations

While Inf-CL successfully breaks the memory barrier, scaling to "ultra-large" batches (above 1 million) presents new challenges. The authors noted a slight dip in accuracy at a 1024k batch size compared to 256k, suggesting that standard optimization "recipes" (e.g., learning rate schedules) may need adjustment for such extreme scales. Additionally, the reliance on custom CUDA kernels, while optimal for performance, increases implementation complexity.

Future work may extend this tiling approach to other memory-intensive domains, such as self-supervised representation learning and dense text retrieval, where large-scale contrastive objectives are prevalent.

Summary updated

BibTeX
@inproceedings{cheng2025breaking,
  author = {Zesen Cheng and Hang Zhang and Kehan Li and Sicong Leng and Zhiqiang Hu and Fei Wu and Deli Zhao and Xin Li and Lidong Bing},
  title = {{Breaking the Memory Barrier of Contrastive Loss via Tile-Based Strategy}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2025},
  doi = {10.1109/cvpr52734.2025.00938},
}
Figure