Abstract
The online Gradient Balancing (GraB) algorithm greedily choosing the examples ordering by solving the herding problem using per-sample gradients is proved to be the theoretically optimal solution that guarantees to outperform Random Reshuffling. However, there is currently no efficient implementation of GraB for the community to easily use it. This work presents an efficient Python library, $\textit{GraB-sampler}$, that allows the community to easily use GraB algorithms and proposes 5 variants of the GraB algorithm. The best performance result of the GraB-sampler reproduces the training loss and test accuracy results while only in the cost of 8.7% training time overhead and 0.85% peak GPU memory usage overhead.
Introduction
In modern deep learning, Stochastic Gradient Descent (SGD) is typically performed using Random Reshuffling (RR)—shuffling the dataset at the start of every epoch. While standard, recent theoretical work suggests RR is sub-optimal. Gradient Balancing (GraB) offers a superior alternative by treating data ordering as a "herding problem." By calculating per-sample gradients, GraB selects a data sequence that ensures the running average of gradients remains close to the true mean, thereby accelerating convergence.
Historically, implementing GraB in PyTorch has been challenging because the C++ kernel averages gradients within a batch before they reach the user. This makes extracting per-sample gradients difficult without slow workarounds or heavy external dependencies. The GraB-sampler library addresses this by providing an efficient, native PyTorch implementation that requires only a three-line code modification to existing training scripts.
Methodology
GraB-sampler integrates directly with PyTorch’s DataLoader and leverages functorch (introduced in PyTorch 2.0) to compute per-sample gradients efficiently. The library supports five distinct balancing variants:
- Mean Balance (Vanilla GraB): The standard herding-based approach.
- Pair Balance (CD-GraB): An existing memory-efficient variant.
- Batch Balance: A new variant that delays updates to the accumulator until a full batch is processed, improving parallelism.
- Recursive Balance: Uses a binary-tree structure to balance gradients, inspired by kernel thinning techniques.
- Recursive Pair Balance: Combines the memory efficiency of Pair Balance with the rapid convergence of recursive structures.
Experimental Setup
The library was evaluated using the CIFAR-10 dataset (50,000 training images) and the LeNet architecture. The experiments focused on benchmarking convergence rates (training loss and test accuracy) against computational overhead (wall-clock time and GPU memory).
Key Results
All GraB variants successfully outperformed Random Reshuffling in terms of training loss and test accuracy per epoch. As shown in the performance metrics, the GraB algorithms achieve lower loss and higher accuracy more rapidly than the baseline.
While all variants improve convergence, they introduce varying levels of computational overhead. The "Recursive" variants show the most aggressive convergence in the initial epochs but come with a significant "tax" in training time and memory. In contrast, the Batch Balance and Pair Balance variants represent a practical "sweet spot," providing substantial convergence gains with only 8.7% training time overhead and a negligible 0.85% peak GPU memory overhead.
Discussion and Limitations
Despite its advantages, GraB-sampler has specific constraints. Because it relies on functorch, models must be written in a functional style. This currently limits compatibility with layers that store inter-batch statistics, such as BatchNorm, or those with stochastic behavior like Dropout.
Furthermore, scaling to Large Language Models (LLMs) remains a challenge. The memory overhead for recursive algorithms scales exponentially with tree depth ($O(2^D d)$), making them computationally expensive for models with billions of parameters. Future research will focus on adapting these tree-based balancing techniques for massive-scale architectures.
Conclusion
GraB-sampler provides a powerful, easy-to-use tool for researchers looking to move beyond the sub-optimality of Random Reshuffling. By optimizing data ordering, the library enables faster convergence with minimal overhead, particularly when using the Batch or Pair Balance variants.
Summary updated