训练阶段的流水线气泡:随着模型规模增大,需要更高的流水线并行度(PP)来扩展训练。然而,流水线气泡的比例随PP规模增加而增加,显著降低训练效率。在常用的1F1B流水线调度(如图3上)中,气泡比例为 (N-1)/(N-1+M),其中N是PP阶段数,M是微批次数。当N接近M时(这在训练数百亿参数模型时很常见),气泡比例接近50%,意味着一半的GPU处于空闲状态。尽管Interleaved 1F1B调度【索引16,Megatron-lm: Training multi-billion parameter language models using model parallelism,2020】能缓解此问题(如图3下),但它引入了额外的通信开销且加剧了PP阶段间的不平衡,因此实践中通常将交错因子K设为2,优化空间有限。其他旨在完全消除气泡的工作要么破坏了同步训练语义【索引25,Pipedream: Generalized pipeline parallelism for dnn training,2019,ACM SOSP】,要么依赖不切实际的假设【索引21,Zero bubble pipeline parallelism,2023,arXiv】。
确定实例数量(m):假设总共有 n 个生成实例,我们需要选择 m 个实例来处理剩余样本。m 的确定基于两个约束:
性能约束:在内存密集型的解码阶段,只要批次大小不超过使GPU饱和的阈值 BSmax,每次解码迭代的延迟几乎不变【索引26,Distserve: Disaggregating prefill and decoding for goodput-optimized large language model serving,2024,arXiv】【索引27,Fast distributed inference serving for large language models,2023,arXiv】。因此,我们设置第一个约束 m >= Rt / BSmax,确保迁移后长尾样本的生成时间不变。
内存约束:LLM生成过程中,KV缓存会消耗大量内存,尤其是在序列较长时【索引28,Efficient memory management for large language model serving with pagedattention,2023,SOSP】。因此,我们需确保目标实例有足够内存容纳剩余的长尾样本,避免OOM。第二个约束为 m >= Rt * M / C,其中 M 是最大输出长度样本的KV缓存消耗,C 是目标实例为KV缓存分配的可用GPU内存。m 的最终值取这两个约束计算结果的最大值。
选择具体实例:为最小化迁移开销,我们选择剩余样本数最多的前 m 个实例作为迁移目的地,这样需要迁移的样本总数最少。
机遇:在RLHF训练阶段,Actor和Critic模型是独立训练的。受Chimera的双向流水线调度【索引20,Chimera: efficiently training largescale neural networks with bidirectional pipelines,2021,SC】启发,我们可以将这两个训练任务分解为微批次的子任务,并将它们共同部署以相互填充流水线气泡。Chimera通过复制模型并以相反的流水线方向训练副本,来减少气泡并平衡激活内存(如图6(a))。RLHF训练阶段天然存在两个不同模型,使我们无需额外复制模型即可应用融合流水线调度。
模拟退火(Simulated Annealing):我们采用模拟退火算法【索引30,Optimization by simulated annealing,1983,science】来搜索更优的解。模拟退火能在一个巨大的搜索空间中,在合理时间内找到可接受的局部最优解。我们的方法能够扩展,并且可以轻松扩展到多模型场景。
阶段内融合:使用MPI【索引35,Open mpi: Goals, concept, and design of a next generation mpi implementation,2004,Recent Advances in Parallel Virtual Machine and Message Passing Interface】并行化模拟退火搜索过程。找到最优解后,会为每个设备生成NCCL【索引36,Optimized primitives for collective multi-gpu communication,2024】操作序列,运行时按照此指令流执行。
系统优化:
生成阶段:
使用内部开发的、带有优化CUDA核的推理引擎。
支持连续批处理(continuous batching)【索引37,Orca: A distributed serving system for {Transformer-Based} generative models,2022,USENIX OSDI】,允许短样本提早离开批次。
支持前缀共享(prefix sharing)【索引38,Sglang: Efficient execution of structured language model programs,2024】,对共享相同提示的样本避免KV缓存的重复计算。
推理阶段:
优化了广义优势估计(Generalized Advantage Estimation, GAE)【索引39,High-dimensional continuous control using generalized advantage estimation,2018】的计算。原始的递归计算在输出序列长时会导致大量核函数启动。我们通过展开递归公式并预计算系数矩阵,将计算转化为单次矩阵乘法,将延迟从数十秒降至几十毫秒。
【13】Realhf: Optimized rlhf training for large language models through parameter reallocation (Z. Mei et al., 2024, arXiv)
引用段落:§3 RLHFuse Overview
原文描述:"We formalize the training iteration of RLHF workflow as a dataflow graph G(V , E) following previous work [13]."
【16】Megatron-lm: Training multi-billion parameter language models using model parallelism (M. Shoeybi et al., 2020)
引用段落:§3 RLHFuse Overview, §6 System Implementation and Optimizations
原文描述 (1):"For each vi, RLHFuse will assign it a tailored parallel strategy Pi = (d p, pp,t p) based on the model and cluster configurations to maximize the GPU utilization." (隐式引用,该工作是3D并行的基础)
原文描述 (2):"Optimizing the parallel strategy for LLM training is a well-studied problem [13, 16, 33, 34]."
原文描述 (3):"We implement RLHFuse based on Megatron-LM [16] with 7K lines of code in Python, C++, and CUDA."
原文描述 (4):"Then we follow the guidelines in the Megatron-LM [16] paper to prune the design space and brute-force search the optimal strategy."
【20】Chimera: efficiently training largescale neural networks with bidirectional pipelines (S. Li and T. Hoefler, 2021, SC)
引用段落:§5.1 Opportunities and Challenges
原文描述:"Inspired by the bi-directional pipeline schedule [20] from Chimera, we can break the two training tasks into subtasks of micro-batches and co-locate these subtasks to mutually fill the pipeline bubbles of each other."
【25】Pipedream: Generalized pipeline parallelism for dnn training (D. Narayanan et al., 2019, ACM SOSP)
引用段落:§6 System Implementation and Optimizations
原文描述:"An optimal solution can then be found through optimizations [25, 33, 34]."
【26】Distserve: Disaggregating prefill and decoding for goodput-optimized large language model serving (Y. Zhong et al., 2024, arXiv)
引用段落:§4.2 Fused Execution Plan
原文描述:"During the memorybound decoding phase of generation, as long as the batch size does not exceed a certain threshold that saturates the GPU (denoted as BSmax), the latency of each decoding iteration remains almost constant [26,27]."
【27】Fast distributed inference serving for large language models (B. Wu et al., 2023, arXiv)
引用段落:§4.2 Fused Execution Plan
原文描述:"During the memorybound decoding phase of generation, as long as the batch size does not exceed a certain threshold that saturates the GPU (denoted as BSmax), the latency of each decoding iteration remains almost constant [26,27]."
【28】Efficient memory management for large language model serving with pagedattention (W. Kwon et al., 2023, SOSP)
引用段落:§4.2 Fused Execution Plan
原文描述:"During LLM generation, the key-value cache is maintained for each token position for autoregressive generation. When the sequence length is long, this memory consumption becomes non-negligible [28]."
【29】Flow shop scheduling: theoretical results, algorithms, and applications (H. Emmons and G. Vairaktarakis, 2012)
引用段落:§5.2 Fused Pipeline Schedule
原文描述:"It can be reduced to a special case of flow job scheduling problem which proves to be NP-hard [29]."
【30】Optimization by simulated annealing (S. Kirkpatrick et al., 1983, science)
引用段落:§5.2 Fused Pipeline Schedule
原文描述:"Instead, we use simulated annealing [30] to search for a better solution."
【33】Alpa: Automating inter- and Intra-Operator parallelism for distributed deep learning (L. Zheng et al., 2022, USENIX OSDI)
引用段落:§6 System Implementation and Optimizations
原文描述 (1):"Optimizing the parallel strategy for LLM training is a well-studied problem [13, 16, 33, 34]."
原文描述 (2):"An optimal solution can then be found through optimizations [25, 33, 34]."
【34】Galvatron: Efficient transformer training over multiple gpus using automatic parallelism (X. Miao et al., 2022, VLDB Endowment)
引用段落:§6 System Implementation and Optimizations
原文描述 (1):"Optimizing the parallel strategy for LLM training is a well-studied problem [13, 16, 33, 34]."
原文描述 (2):"An optimal solution can then be found through optimizations [25, 33, 34]."
【35】Open mpi: Goals, concept, and design of a next generation mpi implementation (E. Gabriel et al., 2004, Recent Advances in Parallel Virtual Machine and Message Passing Interface)
引用段落:§6 System Implementation and Optimizations
原文描述:"For intra-stage fusion, we use MPI [35] to parallelize the simulated annealing with different random seeds."
【36】Optimized primitives for collective multi-gpu communication (NVIDIA, 2024)
引用段落:§6 System Implementation and Optimizations
原文描述:"After finding the optimal solution, it will generate the sequences of NCCL [36] operations on each device following the schedule."
【37】Orca: A distributed serving system for {Transformer-Based} generative models (G.-I. Yu et al., 2022, USENIX OSDI)
引用段落:§6 System Implementation and Optimizations
原文描述:"It supports continuous batching [37] to allow the short samples to leave the batch early."
【38】Sglang: Efficient execution of structured language model programs (L. Zheng et al., 2024)
引用段落:§6 System Implementation and Optimizations
原文描述:"For samples that share the same prompt, it supports prefix sharing [38] to avoid key-value cache duplication."
【39】High-dimensional continuous control using generalized advantage estimation (J. Schulman et al., 2018)
引用段落:§6 System Implementation and Optimizations
原文描述:"During the post-processing of the inference stage, one metric called Generalized Advantage Estimation [39] is needed which requires recursive computation along the output length dimension for each sample."
【40】On optimizing the communication of model parallelism (Y. Zhuang et al., 2024)
引用段落:§6 System Implementation and Optimizations
原文描述:"This communication pattern is well-studied [40] and we adopt a similar allto-all approach to minimize cross-node communication."