Efficient Scaling of LLM Training with Flexible Context Parallelism
Authors: Yifan Niu, Han Xiao, Dongyi Liu, Wei Zhou, Jia Li
Organizations: The Hong Kong University of Science and Technology (Guangzhou) · Huawei Technologies Co., Ltd. · The Hong Kong University of Science and Technology
Scaling long-context capabilities is crucial for Large Language Models (LLMs). However, real-world data contain a large number of sequences with heterogeneous lengths. Existing training libraries for LLMs rely on static parallelism strategies, which suffer from severe load imbalance, redundant communication, and suboptimal hardware utilization under data heterogeneity. In this work, we propose Flexible Context Parallelism (FCP), an efficient parallelism strategy that adaptively reconfigures communication groups and context parallelism degrees during LLM training. We generalize more flexible non-power-of-two parallelism degrees and develop a polynomial-time algorithm to generate near-optimal parallelism strategies with only millisecond-level overhead per training batch. FCP is able to maintain high hardware efficiency even under extreme data heterogeneity. Experimental results demonstrate that FCP significantly outperforms Megatron-LM and DeepSpeed in both LLM and MLLM training, achieving up to 1.46x speedup in average throughput while maintaining near-linear scaling efficiency across large-scale clusters. For extremely unbalanced batches, FCP even achieves 2.24x speedup.
Figures & tables
Figure 1: (a) Length distribution of text and multimodal datasets. (b) Compared to static parallelism, dynamic parallelism facilitates load balancing against heterogeneous data.
Figure 2: Overall workflow of FCP.
Figure 3: Average iteration time (in seconds) comparison on text datasets for Qwen3-1.7B/4B/8B, Llama3.2-1B/3B, and Llama3-8B. We compare Megatron-LM (diagonal stripes), DeepSpeed (dots), and FCP (vertical stripes) on GitHub, CommonCrawl, and Wikipedia.
Figure 4: Average iteration time (in seconds) comparison for unfrozen MLLM training across MSRVTT, InternVid, and OpenVid datasets. The evaluated models include InternVL3-2B/8B, InternVL2.5-4B and Qwen3VL-2B/4B/8B.
Figure 5: Token throughput (in k tokens/s) comparison across different cluster size (8, 16, 32, and 64) for FCP, DeepSpeed, and Megatron-LM.
Model
Model Parameters
2B
4B
8B
Qwen3VL
7.93
6.71
4.27
InternVL2.5/3
7.48
6.54
4.12
Table 3: Time cost estimation error (%).
Case Study
Case 1
Case 2
DeepSpeed
⟨8⟩×4
⟨4⟩×8
Megatron-LM
⟨8⟩×4
⟨4⟩×8
FCP
⟨8⟩×1⟨6⟩×2⟨4⟩×1⟨2⟩×2⟨1⟩×4
⟨4⟩×2⟨3⟩×4⟨2⟩×6
Table 6: CP groups assigned by FCP.
Appendix figures & tables3 assets
Supplementary material from the paper’s appendix.
Appendix
Model
#Layers
#Heads
#Groups
Hidden Dim
FFN Hidden Dim
Qwen3-1.7B
28
16
8
2048
6144
Qwen3-4B
36
32
8
2560
9728
Qwen3-8B
36
32
8
4096
12288
Llama3.2-1B
16
32
8
2048
8192
Llama3.2-3B
28
24
8
3072
8192
Llama3-8B
32
32
8
4096
14336
Appendix
Table 7: LLM models for evaluation.
Model
#Layers
#Heads
#Groups
Hidden Dim
Vision Hidden Dim
InternVL3-2B
28
12
2
1536
1024
InternVL2.5-4B
36
16
2
2048
1024
InternVL3-8B
28
28
4
3584
1024
Qwen3-VL-2B
28
16
8
2048
1024
Qwen3-VL-4B
36
32
8
2560
1024
Qwen3-VL-8B
36
32
8
4096
1152
Appendix
Table 8: MLLM models for evaluation.
Figure 6: Average iteration time (in seconds) comparison for frozen-vision MLLM training across MSRVTT, InternVid, and OpenVid datasets. FCP keeps the same scheduling mechanism while the vision encoder is frozen, yielding shorter wall time than the unfrozen setting.
Context parallelism (CP) is essential for training large-scale, long-context language models, as it partitions sequences to reduce memory overhead. However, existing CP methods suffer from workload imbalance, inefficient kernels, and redundant communication due to static sequence sharding and key-value (KV) tensor communication. We present FlashCP, a load-balanced and communication-efficient framework for CP training. FlashCP introduces a sharding-aware communication mechanism to eliminate redundant KV communication and proposes a novel Whole-Doc sharding strategy that maximizes communication savings while maintaining balanced workloads. To efficiently combine Whole-Doc and Per-Doc sharding, FlashCP further designs a heuristic algorithm to search for near-optimal sharding plans. Extensive experiments show that FlashCP achieves up to 1.63x speedup over state-of-the-art CP frameworks across diverse datasets.
Zheng Wang, Eric Liu, Linan Jiang +5
University of California San Diego, La Jolla, USA · University of Southern California, Los Angeles, USA · Rice University, Houston, USA.
Long context training is crucial for extending LLM context windows. Existing schemes, such as sequence parallelism, incur substantial communication overhead. Pipeline parallelism (PP) reduces this cost, but its effectiveness hinges on partitioning granularity. Batch-level PP employing sequence packing exhibits high memory consumption in long-context scenarios, whereas token-level PP splitting sequences into slices alleviates memory overhead but may introduce performance degradation. Moreover, the skewed sequence-length distribution in real-world datasets defeats any monolithic, static choice of PP granularity. In this paper, we propose Dynamic Pipeline Parallelism (DPP), which transforms PP granularity from a static design choice into a workload-adaptive optimization space over packed, split, and hybrid chunks. DPP further introduces a new coupling between heterogeneous pipeline scheduling and gradient checkpointing. To solve this coupling, \name co-optimizes dynamic chunk scheduling with Stage-Aware Chunk-Level Adaptive Checkpointing. Comprehensive experiments demonstrate that \name achieves up to 1.69\texttimes\ speedup over FlexSP and up to 1.40\texttimes\ over MEPipe. The source code is available at https://github.com/wsjdsg/InfiniPipe-code.
Shiju Wang, Yujie Wang, Fangcheng Fu +6
Tsinghua University · Peking University · Shanghai Jiao Tong University +1
Large-language-models (LLMs) demonstrate enormous utility in long-context tasks which require processing prompts that consist of tens to hundreds of thousands of tokens. However, existing LLM training libraries do not provide easy to use abstractions to optimize for long-context training, instead focusing on optimizations for models with large parameter counts through ZeRO-3/FSDP, Tensor and Pipeline parallelism. This forces users to rewrite LLM training libraries to incorporate compositions of various complex long-context optimizations, such as sequence-parallelism, to training pipelines; a process that requires in-depth expertise, reducing developer productivity. To tackle these challenges, we introduce AutoSP: the first automated solution to automatically optimize LLM training for longer-contexts. AutoSP compiles models and applies a targeted set of optimizations: automated sequence parallelism, and long-context aware activation-checkpointing, to drastically enhance LLM trainability at negligible cost to throughput. Our evaluation demonstrates AutoSP's capability on both NVIDIA and AMD hardware, increasing training contexts by upto 2.7× and 2.5× respectively over competitive hand-written baseline at negligible cost to runtime performance.
Ahan Gupta, Zhihao Wang, Neel Dani +3
SSAIL Lab, University of Illinois Urbana-Champaign Champaign, IL, USA · Anyscale San Francisco, CA, USA · Snowflake San Mateo, CA, USA