Efficient Scaling of LLM Training with Flexible Context Parallelism

Yifan Niu, Han Xiao, Dongyi Liu, Wei zhou, Jia LiNeurIPS 2026

TL;DR

Training Large Language Models on real-world data is like running a grocery store where most shoppers buy a carton of milk, a few bring overflowing carts, and your checkout lanes are welded into rigid, unchangeable configurations. Because attention computation scales quadratically with sequence length, standard training frameworks suffer from massive hardware idle times when mixing short snippets with ultra-long documents or videos. Flexible Context Parallelism (FCP) solves this by dynamically re-grouping devices and adjusting parallelism on the fly for every micro-batch—boosting training speeds by up to 1.39× (and 2.24× on extreme batches) with virtually zero scheduling lag.

What Problem Does This Paper Address?

Real-world datasets—whether code repositories, web scrapes, or video clips—follow heavily skewed length distributions: mountains of short sequences and a long tail of massive ones.

When training LLMs or Multimodal LLMs (MLLMs), standard distributed frameworks (like Megatron-LM and DeepSpeed) rely on static parallelism. To prevent out-of-memory errors on long sequences, they split sequences across multiple devices using Sequence Parallelism (SP) or Context Parallelism (CP). To handle short sequences efficiently, they often "pack" several together to equalize token counts per device.

Here is the catch: Transformer self-attention scales quadratically, $\mathcal{O}(L^2)$, with sequence length $L$. A device handed one 64k-token document does vastly more math than a device handed sixty-four 1k-token documents ($L^2 \gg \sum l_i^2$), even though both hold 64k tokens! Under static layouts, devices crunching single massive sequences become stragglers, while devices with packed short sequences incur unnecessary inter-device communication and then sit idle at synchronization barriers.

Methods

Instead of locking hardware into fixed teams before training starts, FCP reconfigures Ring-style Context Parallelism (Ring-CP) groups for every micro-batch. Ring-CP passes Key/Value blocks around a virtual ring of devices, overlapping communication with attention math. Unlike head-sharding methods (e.g., DeepSpeed-Ulysses) where device counts must cleanly divide attention heads (usually powers of two like 2, 4, 8), Ring-CP allows arbitrary integer team sizes—say, 3 or 5 devices.

Finding the optimal way to group sequences and assign devices to minimize the slowest group's runtime (the makespan) is NP-hard. FCP cracks this in milliseconds using a clever two-stage pipeline built seamlessly atop Megatron-LM:

Figure 2: Overall workflow of FCP, including micro- batch planning within the scheduler.
  1. Offline Profiling & Cost Modeling: Before training, a lightweight profiler measures execution and communication times across sample lengths and CP degrees, fitting a cost model that even accounts for causal vs. full multimodal attention masks.
  2. Stage 1 – Memory-Aware Grouping (Heuristic): Sequences are sorted from longest to shortest. Long sequences establish "bins" sized to their minimum memory-safe CP degree, and shorter sequences are greedily packed into leftover memory headroom via Best-Fit Decreasing.
  3. Stage 2 – 2D Dynamic Programming (Exact Subproblem): With sequence groups fixed, a polynomial-time DP algorithm—running in $\mathcal{O}(K'N^2)$ time for $K'$ groups and $N$ model replicas—allocates the exact integer number of devices to each group to minimize maximum execution time.

Crucially, this CPU scheduler runs asynchronously, planning batch $t+1$ while the accelerators crunch batch $t$.

Datasets

No new benchmarks are introduced; the authors evaluate FCP on six established datasets (up to 128k context length) chosen for their natural length skewness:

  • Text (LLMs): GitHub, CommonCrawl, and Wikipedia.
  • Video-Language (MLLMs): MSRVTT, InternVid, and OpenVid.
Length and duration distributions of real-world text and video datasets

Key Insights and Ideas

  • Equal tokens $\neq$ equal work: Packing short sequences to match a long sequence's token count creates a mirage of load balance while hiding massive $\mathcal{O}(L^2)$ compute disparities.
  • Breaking the power-of-two straitjacket: Allowing 3-device or 5-device Ring-CP groups prevents forcing awkward sequences into over-provisioned 8-device rings (wasting bandwidth) or under-provisioned 4-device rings (running out of memory). However, ablations show FCP still beats static baselines by ~1.10× even when restricted to powers of two—proving the DP scheduler itself carries serious weight.
  • Relative order beats absolute perfection: FCP’s cost profiler has up to ~8% prediction error, yet injecting up to 20% artificial noise didn't change its scheduling choices at all. To balance loads, the solver just needs to know which group is slower, not exact microsecond latencies.

Key Results

Tested on clusters of up to 64 Ascend 910C NPUs (64GB each) across Qwen3, Llama3, and InternVL families (1B to 8B parameters):

  • End-to-End Speedups: FCP outperforms the faster static baseline (Megatron-LM or DeepSpeed) by 1.06×–1.39× on LLMs and 1.07×–1.36× on unfrozen MLLM training. On extremely unbalanced batches, speedups reach 2.24×.
  • Dynamic Baselines: Against in-framework reproductions of dynamic schedulers (ByteScale-style and FlexSP-style), FCP achieves 1.10× and 1.12× geometric-mean speedups.
  • Negligible Overhead: At the largest scale (64 devices, global batch size 512), total scheduling takes 905 ms (the DP solver itself takes at most 86 ms), and updating communication groups takes ~1 ms—completely hidden behind the 11.65-second device compute step.

Limitations and Future Work

  • Static Model Parallelism: Tensor and Pipeline Parallelism (TP/PP) remain fixed during training because reshuffling model weights across devices mid-run is prohibitively expensive.
  • Hardware & FSDP Validation: All experiments ran on Huawei Ascend 910C NPUs using Ring-CP. Practical validation on NVIDIA GPUs and complex hybrid meshes involving Sharded Data Parallelism (FSDP × CP × TP)—which require strict collective ordering across shifting groups—remains future work.
  • Diminishing Returns on Uniform Data: As expected, if your dataset has uniform sequence lengths, FCP's dynamic machinery offers little advantage over static setups.

Summary updated

Abstract

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 support non-power-of-two Ring-CP degrees and combine memory-aware grouping with polynomial-time degree allocation, whose solve takes tens of milliseconds in the evaluated configurations. 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, improving throughput across heterogeneous workloads on clusters of up to 64 devices. For extremely unbalanced batches, FCP even achieves 2.24 $\times$ speedup. The code is available at https://github.com/ivanniu/FCP.
BibTeX
@inproceedings{niu2026efficient,
  author = {Yifan Niu and Han Xiao and Dongyi Liu and Wei zhou and Jia Li},
  title = {{Efficient Scaling of LLM Training with Flexible Context Parallelism}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=RHltKicIIC},
}
Figure