# 连续批处理

连续批处理是一种面向大语言模型推理服务器的调度方法，其核心在于每个生成步骤都会重新组建批次中的请求集合：已完成的序列被立即逐出，新请求即刻插入，而不是等待整个静态批次全部完成。该技术最初以“迭代级调度”之名在 Orca 系统中提出（Yu 等，OSDI 2022），如今已成为现代服务引擎的核心吞吐机制，包括 vLLM、TGI、[TensorRT-LLM](https://www.edgepedia.org/zh/tensorrt-llm) 和 SGLang。

| 关键事实 | 详情 |
|---|---|
| 机制 | 批次成员在每个解码迭代中决定；请求每一步都可以加入和离开在途批次<sup>[1](https://huggingface.co/docs/transformers/en/continuous_batching)</sup> |
| 起源 | Orca，OSDI 2022，名称为“迭代级调度”<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup><sup> • </sup><sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> |
| 标志性收益 | Anyscale 基准测试中相较朴素静态批处理最高提升 23 倍；聊天类工作负载通常为 5–15 倍<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup><sup> • </sup><sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> |
| 采用情况 | vLLM、SGLang、TGI 和 TensorRT-LLM 的默认方案（后者称为“在途批处理”）<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup><sup> • </sup><sup>[4](https://blog.dailydoseofds.com/p/continuous-batching-in-llms)</sup> |
| 利用率 | 将有效 GPU 利用率从静态批处理下的约 20–40% 提升至典型工作负载下的 80–95%（从业者估计）<sup>[5](https://iamulya.one/posts/llm-serving-in-depth/)</sup> |
| 关键扩展 | 分块预填充（源自 Sarathi-Serve），在 vLLM 的 V1 引擎中默认启用<sup>[6](https://packet.ai/blog/continuous-batching-explained)</sup> |
| 2024–2026 趋势 | 在独立 GPU 池上进行预填充-解码分离（DistServe、Splitwise）<sup>[7](https://vinayj.com/sizing/06-batching-strategy)</sup><sup> • </sup><sup>[8](https://tianpan.co/blog/2026/04/09/continuous-batching-llm-inference)</sup> |

## 连续批处理是什么

在 Transformer 服务系统中，生成过程一次一个解码步骤推进：每次迭代为每个活跃序列产生一个令牌。静态批处理将一组固定请求编入一批，运行至所有序列全部完成，因此单个长生成任务会迫使其他所有槽位空转，直到整批退役。连续批处理则相反，在每个生成步骤重新调度批次：请求一完成，新请求立即加入，GPU 始终保持满载，吞吐量维持高位。<sup>[1](https://huggingface.co/docs/transformers/en/continuous_batching)</sup>

两项要素结合使这一机制得以运作：不规则批处理（不同长度的序列同时占据批次）和动态调度（成员每轮迭代变化）。该技术还允许通过注意力掩码在同一批次中混合预填充和解码阶段，这正是大型服务高效处理大量并发请求的方式。<sup>[9](https://huggingface.co/blog/continuous%5Fbatching)</sup>

## 起源：Orca 与迭代级调度

该技术可追溯至《Orca：面向基于 Transformer 的生成模型的分布式服务系统》，发表于 OSDI 2022。据 [Anyscale](https://www.edgepedia.org/zh/anyscale) 作者所知，Orca 是首个解决静态批处理浪费问题的系统，实现了迭代级调度——批大小按迭代确定，完成的序列被立即替换。<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup> Orca 报告称，在同等延迟下，这一改动相较 NVIDIA [FasterTransformer](https://www.edgepedia.org/zh/fastertransformer) 带来了 36.9 倍的吞吐量提升。<sup>[6](https://packet.ai/blog/continuous-batching-explained)</sup>

该技术在开源社区由 vLLM（加州大学伯克利分校，2023 年 6 月）以“连续批处理”之名推广开来；NVIDIA TensorRT-LLM 以“在途批处理”之名提供同一技术。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> 一个二手来源的提醒：本文的证据基础中未收录一手来源（即 Orca 论文本身）；36.9 倍的数据来自二手报道。

## 实际运作方式

每步调度器在每次迭代中决定哪些请求解码、哪些预填充、哪些等待，并在序列进出时跟踪 [KV 缓存](https://www.edgepedia.org/zh/kv-cache)（每个活跃序列存储的注意力键和值）。vLLM 2023 年 6 月的发布是采用的拐点：分页注意力加连续批处理作为单一开源运行时一同发布，六个月内其他所有服务栈（TGI、TensorRT-LLM、SGLang、MLC-LLM、MAX）都采用了相同模式。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> 分页注意力是互补的内存管理技术：它对 KV 缓存进行分页，使准入/逐出驱动的碎片化不再成为问题，从而让调度器能够安全地准入更多序列。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup>

如今 vLLM、SGLang、TGI 和 TensorRT-LLM 均默认运行连续批处理。<sup>[4](https://blog.dailydoseofds.com/p/continuous-batching-in-llms)</sup> 证据基础中不包含关于各调度器除命名之外具体差异的系统性比较。

## 数据说话

实测结果及其来源：

- **Anyscale 独立基准测试**（Ray Serve 与 Hugging Face TGI）测得：使用连续批处理配合 vLLM 针对批处理的专用内存优化，吞吐量最高提升 23 倍；仅连续批处理本身相较朴素批处理提升 8 倍；优化模型实现（NVIDIA FasterTransformer）贡献 4 倍——均对比朴素静态批处理。<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup> 在生成长度方差较大的情况下，朴素静态批处理的吞吐量骤降至每秒 81 个令牌，而 Ray Serve 和 TGI 上的连续批处理器表现几乎相同。<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup> 据一则轶事，在这些基准测试中 vLLM 在 QPS=8 附近趋于饱和，吞吐量接近每秒 1900 个令牌。<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup>
- **从业者给出的范围**：相较静态批处理，聊天类工作负载通常提升 5–15 倍，输出长度方差极大时偶尔可达 20 倍以上。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> 另一篇报道给出 5–20 倍；来源之间未统一为单一区间。<sup>[10](https://zeroentropy.dev/concepts/continuous-batching/)</sup>
- **利用率**：一位从业者估计，有效 GPU 利用率从静态批处理下的 20–40% 升至典型工作负载下的 80–95%；Anyscale 的基准测试仅以吞吐量倍数表达收益，因此该估计未获独立佐证。<sup>[5](https://iamulya.one/posts/llm-serving-in-depth/)</sup>
- **规模化分离式服务**：SGLang 在 96 块 H100 GPU 上，配合分离式服务加专家并行，在 [DeepSeek-R1](https://www.edgepedia.org/zh/deepseek-r1) 上每节点达到每秒 5.23 万个输入令牌和 2.23 万个输出令牌，相较同硬件上的原生张量并行提升 5 倍。<sup>[7](https://vinayj.com/sizing/06-batching-strategy)</sup>

延迟同样改善，不仅是吞吐量：Anyscale 发现连续批处理在所有百分位上均改善延迟，因为新请求每次迭代都加入批次，而非等待批次槽位。系统饱和时收益缩小（在其测试中约为 QPS=4），而 vLLM 的延迟曲线在 QPS=1 到 QPS=4 之间基本不变，这归因于其更高的最大批大小。<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup>

**归因争议。** vLLM 的优势中有多少来自调度、多少来自内存管理，尚无定论。Anyscale 的基准测试发现，vLLM 在每个测试数据集上相较朴素连续批处理性能提升超过一倍，归因于动态（即时）KV 空间预留而非提前预留，从而支持更大的批大小——也就是说，归因于内存管理而非仅调度。<sup>[2](https://www.anyscale.com/blog/continuous-batching-llm-inference)</sup> 其他报道将连续批处理与分页注意力呈现为联合的、结构上配对的胜利，而不分配功劳。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> 两种解读都同意两项技术一同发布且相互增强。

## 近期扩展

**分块预填充**将连续批处理扩展至预填充阶段。源自 Sarathi-Serve，它在同一迭代中将预填充块与解码令牌交错，消除了单次预填充在长提示词中途到达时造成的队头阻塞。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> 它已被直接采纳进 vLLM 自身的调度器，而非作为外部附加组件：vLLM 的 V1 引擎在可能的情况下默认启用分块预填充，由 `max_num_batched_tokens` 调优参数控制。<sup>[6](https://packet.ai/blog/continuous-batching-explained)</sup> 一种常见策略是给予待处理解码优先权，并将剩余预填充预算分块。<sup>[4](https://blog.dailydoseofds.com/p/continuous-batching-in-llms)</sup>

**预填充-解码分离**更进一步，将两个阶段分离到不同硬件上。DistServe（OSDI 2024）使用排队论将其形式化，将预填充和解码建模为两个独立队列，并证明分别独立优化每个阶段所产生的有效吞吐量远优于任何同置配置。<sup>[7](https://vinayj.com/sizing/06-batching-strategy)</sup> [Splitwise](https://www.edgepedia.org/zh/splitwise)（2024）和 vLLM 的 PD-Disaggregation 将预填充分离到计算优化硬件、解码分离到内存带宽优化硬件，各集群独立伸缩。<sup>[5](https://iamulya.one/posts/llm-serving-in-depth/)</sup> 到 2025 年底，每个主要生产级服务框架都已原生支持预填充-解码分离：SGLang 和 [NVIDIA Dynamo](https://www.edgepedia.org/zh/nvidia-dynamo) 将其视为核心服务模式，Ray Serve LLM 和 llm-d 将其作为有文档的部署模式发布，vLLM 持续加固以趋向稳定版本。<sup>[7](https://vinayj.com/sizing/06-batching-strategy)</sup> 截至 2026 年 4 月，该模式被描述为正在成为大规模部署的标准。<sup>[8](https://tianpan.co/blog/2026/04/09/continuous-batching-llm-inference)</sup>

**公平性与准入控制。** 朴素的先来先服务调度会让一个请求繁多的租户垄断运行批次；生产级多租户栈在运行时调度器之上叠加自己的准入控制、[加权公平队列](https://www.edgepedia.org/zh/weighted-fair-queueing)或按租户限流。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup>

## 局限与代价

- **调度器开销。** 在小型模型（1B–7B）极高吞吐量场景下，每步调度器开销可能与内核时间相当。运行时通过多步调度缓解（vLLM 的 `--num-scheduler-steps`，默认 1，生产典型值 8–16），将 Python 开销分摊到多个解码步骤。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup>
- **内存压力下的抢占。** 当突发负载下 KV 缓存内存耗尽时，运行时必须逐出一个仍在运行的序列。重算模式浪费该序列已完成的工作；交换模式在恢复时增加 PCIe 往返延迟，使受影响请求的延迟飙升。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup>
- **准入控制的权衡。** 无准入控制时，连续批处理在容量允许时调度新请求进入批次，因此请求可能在队列中等待，系统可能在内存压力下卸载或抢占活跃请求；有准入控制时，新请求在 KV 缓存预算耗尽时排队。<sup>[11](https://huggingface.co/docs/transformers/continuous_batching_architecture)</sup> 排队以新到达者的首令牌时间增加为代价保护 P99 延迟，而抢占以被逐出请求的延迟峰值为代价保护 TTFT。<sup>[7](https://vinayj.com/sizing/06-batching-strategy)</sup>
- **尾延迟与队头阻塞。** 尾延迟比静态批处理下更具波动性，因为新准入请求的预填充可能对现有解码器造成一轮迭代的延迟抖动；分块预填充平滑了但未消除这一问题。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup> 底层权衡没有免费选项：在批次中途运行新请求的预填充会将每个当前解码序列延迟一个预填充时间——在长提示词上可能相当可观——而推迟预填充则会恶化新请求的 TTFT。<sup>[6](https://packet.ai/blog/continuous-batching-explained)</sup>
- **超长上下文。** 对于上下文超过 12.8 万个令牌的预填充密集型工作负载，预填充块本身过大，即使分块预填充也会产生可见的迭代抖动，预填充必须通过张量或流水线并行跨 GPU 拆分。<sup>[3](https://yobitel.com/knowledge-base/continuous-batching)</sup>

证据基础仅提供了小模型调度器开销这一量化支撑，来说明连续批处理何时是错误选择；关于边缘设备或单用户延迟敏感型工作负载的说法，现有来源尚未给出定论。

## References

1. [Continuous batching · Hugging Face Transformers documentation](https://huggingface.co/docs/transformers/en/continuous_batching)
2. [Achieve 23x LLM Inference Throughput & Reduce p50 Latency](https://www.anyscale.com/blog/continuous-batching-llm-inference)
3. [Continuous Batching — Knowledge Base](https://yobitel.com/knowledge-base/continuous-batching)
4. [Continuous Batching in LLMs - by Avi Chawla](https://blog.dailydoseofds.com/p/continuous-batching-in-llms)
5. [LLM Serving in Depth: Batching, Scheduling, and Parallelism](https://iamulya.one/posts/llm-serving-in-depth/)
6. [Continuous Batching Explained 2026](https://packet.ai/blog/continuous-batching-explained)
7. [Batching Strategy: From Static to Continuous to Disaggregated](https://vinayj.com/sizing/06-batching-strategy)
8. [Continuous Batching: The Single Biggest GPU Utilization Unlock for LLM Serving](https://tianpan.co/blog/2026/04/09/continuous-batching-llm-inference)
9. [Continuous batching from first principles](https://huggingface.co/blog/continuous%5Fbatching)
10. [Continuous batching: vLLM's in-flight scheduling trick](https://zeroentropy.dev/concepts/continuous-batching/)
11. [Continuous batching architecture · Hugging Face](https://huggingface.co/docs/transformers/continuous_batching_architecture)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026 · Edited: Oct 11, 2026; Oct 15, 2026 · Last review: Sep 30, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › Foundation-model methods and training › Inference, serving, and efficiency of foundation models

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/continuous-batching

English original: https://www.edgepedia.org/continuous-batching
