TL;DR
KnapFormer is an online load-balancing framework designed to eliminate severe GPU workload imbalances during the distributed training of Diffusion Transformers (DiTs). By accurately modeling token workloads and solving a global multi-knapsack problem, it dynamically redistributes variable-length data chunks across GPUs, delivering a 2x to 3x training speedup for state-of-the-art models like FLUX.
What Problem Does This Paper Address?
Modern generative vision-language models, particularly Diffusion Transformers (DiTs), are trained on highly diverse multimodal datasets. A single training batch might contain short text prompts paired with low-resolution images alongside long prompts paired with high-resolution videos.
When this data is tokenized, it creates massive heterogeneity in sequence lengths. In standard data-parallel distributed training, this causes a severe workload imbalance: some GPUs are overloaded with massive sequences (risking out-of-memory errors), while others finish early and sit idle. This is known as the straggler effect, where the slowest GPU dictates the speed of the entire cluster. Traditional fixes like padding waste compute on useless tokens, while static bucketing requires tedious manual planning and fails when data dynamics change mid-training.
Methods
KnapFormer solves this by introducing a dynamic, sequence-chunk-level online load balancer that integrates seamlessly with sequence parallelism. Its core components include:
-
Gamma-Corrected Workload Modeling: Standard theoretical FLOP counting fails to predict real-world GPU latency because attention layers are memory-bandwidth bound rather than compute-bound. The authors introduce a hardware-specific correction factor ($\gamma$) to accurately estimate execution time based on sequence length.
Figure 2: Latency prediction based on the FLOPs (Eq. 1) is a bit off from the empirical observations, while our gamma-corrected formula (Eq. 2) provides more accurate latency estimates. The estimated γ = is 0.385 for FLUX model on H100 GPUs. -
Compute Topology Specification: GPUs are logically partitioned into "compute bags" of varying sizes (e.g., a mix of 1-GPU, 2-GPU, or 4-GPU groups) to handle different sequence lengths flexibly.
- Greedy Load Balancer: A fast, three-pass algorithm executed on the CPU at each iteration. It treats sequence assignment as a multi-knapsack problem, sorting and distributing sequences to compute bags to minimize per-GPU workload variance.
-
Single All-to-All Collective: Once the optimal mapping is calculated, data chunks are redistributed across the cluster using a single, highly efficient PyTorch
all-to-allcommunication primitive before the forward pass, and reversed after the backward pass.Figure 1: In KnapFormer, GPUs are logically grouped into compute bags, each responsible for processing a subset of sequences. A global multi-knapsack problem is solved on the CPU to assign sequences to bags in a way that minimizes workload imbalance. The resulting mapping is then executed on the GPU via a single all-to-all collective. For bags containing multiple GPUs, assigned sequences are evenly chunked across devices to enable parallel processing within each bag. -
Ulysses Attention Integration: For compute bags containing multiple GPUs, KnapFormer leverages DeepSpeed-Ulysses sequence parallelism. This splits sequences across GPUs during attention computation, ensuring equal workload distribution while allowing the use of highly optimized kernels like FlashAttention.
Datasets
The authors evaluated KnapFormer using a custom-built training simulator that generates synthetic data streams mimicking real-world large-scale text-to-image and text-to-video training setups (modeled after recipes like FLUX). They tested three practical scenarios:
- Low-resolution image pretraining (homogeneous baseline)
- Mixed-resolution image pretraining (heterogeneous)
- Joint image and video pretraining (highly heterogeneous)
Text prompt lengths varied randomly up to 392 tokens, and visual aspect ratios ranged from 1/4 to 4 to simulate real-world aspect ratio bucketing.
Key Insights and Ideas
- Coarse-Grained Routing Wins: Unlike Mixture-of-Experts (MoE) architectures that route data at a fine-grained, per-token level during every transformer block (causing massive communication overhead), KnapFormer routes at a coarse, sequence-chunk level only twice per training iteration.
- Exploiting Intra-Node Bandwidth: Grouping all 8 GPUs within a single server node into a single compute bag (
g8n4topology) yields near-perfect load balancing. It leverages high-bandwidth NVIDIA NVLink for sequence-parallel attention, bypassing slower cross-node bottlenecks. - Data-Agnostic Usability: KnapFormer acts as a plug-and-play module. It provides automatic, data-driven token redistribution without requiring engineers to manually reconfigure data loaders or alter core model internals when changing datasets.
Key Results
- 2x to 3x Speedups: In highly heterogeneous settings (mixed-resolution and joint image-video training), KnapFormer achieved a 2x to 3x speedup when training a 12-billion parameter FLUX DiT model on 32 H100 GPUs.
- Under 1% Workload Discrepancy: The greedy knapsack solver successfully reduced the Workload Imbalance Ratio (WIR) to a near-perfect 1.00, meaning the difference in total workload between the most and least utilized GPUs was less than 1%.
- Doubled Throughput: For joint image-video pretraining, the Hardware-FLOPs Utilization (HFU) and Tokens Per Second (TPS) more than doubled compared to the baseline training setup without a balancer.
Limitations and Future Work
- Static Topology: Currently, the logical GPU compute bag configuration must be manually pre-specified before training begins. A promising future direction is automatically inferring optimal, non-uniform bag configurations dynamically based on real-time data distributions.
- Homogeneous Mask Assumption: KnapFormer currently assumes uniform attention masks (either entirely bidirectional or entirely causal) within a batch. Further optimization is required to handle irregular attention masking patterns often found in unified multimodal generative models.
Summary updated