跳到主内容
@wquguru
精选88SemiAnalysis(RSS)技巧与观点

SemiAnalysis:推理计算与数据移动的工程解析

Computation and Data Movement for Inference

原文
发到 X
推荐理由

深入拆解了MoE推理中不同阶段的硬件资源需求与调度权衡,是优化推理集群性能与成本必读的工程参考。

Mixture of Experts, now widely used in frontier models, has changed both the structure of serving and the economics of useful inference. It did more than increase parameter count. It changed which tensors are active for each token, what must remain close together, which transfers need strong local bandwidth, which can tolerate a weaker network link, and how memory movement, storage, and scheduling contribute to useful throughput.

专家混合(Mixture of Experts)如今在前端模型中广泛应用,既改变了服务架构,也改变了有效推理的经济性。它的作用不止于增加参数量。它改变了每个 token 激活哪些张量、哪些内容必须保持紧密相邻、哪些传输需要强大的本地带宽、哪些可以容忍较弱的网络链路,以及内存移动、存储和调度如何贡献于有效吞吐量。

The best place to begin is the service as a whole. Inference runs inside a cluster coordinated by an orchestration layer such as NVIDIA Dynamo, Mooncake, or a custom scheduler. These work closely with inference servers like vLLM or SQlang, and those also have their own orchestration features. This article will not go into the details of how you work with the orchestration software or which you should choose. The article aims to provide an overview of the process and reasons for various features you may use.

最好的起点是整体服务。推理运行在一个由编排层协调的集群内部,例如 NVIDIA Dynamo、Mooncake 或自定义调度器。这些组件与 vLLM 或 SQlang 等推理服务器紧密协作,而这些服务器自身也具备编排功能。本文不会深入探讨如何使用编排软件或应如何选择它们。本文旨在概述您可能使用的各种功能的流程及其原因。

A user (or their agent) starts a conversation with a request (query), and the conversation may continue after answers with more requests. The request- answer is a “turn”. In modern AI systems the user is often running a in a client application (whether GUI or command-line oriented) and some of the answers coming back from the AI are intercepted by that application as instructions for it to run, things like editing your code or searching your company guidelines on an HR question.

用户(或其智能体)通过请求(查询)发起对话,并在获得回答后可能通过更多请求继续对话。请求-回答构成一个“轮次”(turn)。在现代 AI 系统中,用户通常正在客户端应用程序(无论是图形界面还是命令行导向)中运行,部分从 AI 返回的回答会被该应用程序拦截为执行指令,例如编辑代码或在人力资源问题上搜索公司指南。

The AI system can also do some of those things itself from its data center, for example it may search the web. The results from these intercept tool actions are also returned to the AI as requests, creating more turns. The server keeps a context (often referred to as KV cache or just cache) which is the distillation of the session, allowing each new request, from user or from tool, to be properly interpreted for overall forward progress. There can be thousands of turns per hour when the user launches an agent on a long-running task. The conversation can also pause and resume hours or even days later.

AI 系统也可以从其数据中心自行执行其中一些操作,例如搜索网页。这些工具拦截操作的返回结果也会作为请求返回给 AI,从而产生更多的轮次。服务器会维护一个上下文(通常称为 KV 缓存或简称缓存),这是会话的浓缩体现,使得来自用户或工具的每个新请求都能被正确解释,以推动整体向前进展。当用户在长期运行的任务上启动智能体时,每小时可能有数千个轮次。对话也可能暂停,并在数小时甚至数天后恢复。

When a request arrives at the inference servers the orchestration puts it into a queue, while will feed into an input-fill worker, along with any context from system prompts and prior turns in the conversation. The query is then converted into new context appended to the conversation. That new context state is then moved to a decode worker. Decode repeatedly reads the accumulated state, generates answer tokens, and those answer tokens are also appended to the conversation state. The process may loop through tools, agents, users, and further input-fill work. A model worker running on GPUs is only one part of this token factory; storage, networking, and orchestration connect its stages.

当请求到达推理服务器时,编排系统会将其放入队列,随后与系统提示词中的上下文以及对话历史中的先前轮次一起输入到输入填充(input-fill)工作器中。查询内容随后被转换为新的上下文并追加到对话中。该新上下文状态接着被传递至解码(decode)工作器。解码工作器反复读取累积的状态,生成答案令牌,这些答案令牌也会被追加到对话状态中。此过程可能会在工具、智能体、用户以及进一步的输入填充工作之间循环。运行在 GPU 上的模型工作器仅是这个令牌工厂的一部分;存储、网络和编排系统将各个阶段连接起来。

Inside the workers, four operating regimes are worth distinguishing from the beginning:

在工作器内部,从一开始就有必要区分四种运行模式:

  • Prefill, where the initial block of new tokens is processed together.
  • Midfill, where a continuation request is appended to an existing cached context.
  • Decode attention, where the context is used to generate the basics of newly generated tokens.
  • Decode experts, where each basic new token is refined by a selection of experts a large total set of possible experts.
  • 预填充(Prefill),在此模式下,初始的新令牌块被一起处理。
  • 中填充(Midfill),在此模式下,延续请求被追加到现有的缓存上下文中。
  • 解码注意力(Decode attention),在此模式下,上下文用于生成新生成令牌的基础部分。
  • 解码专家(Decode experts),在此模式下,每个基础新令牌由大量可能专家中的一个子集进行精炼。

These regimes place different demands on compute, memory, and networking. Prefill and midfill are closely related, with Prefill being a Midfill with zero prior context. However, Prefill is a common special case, corresponding to one-shot queries like classic “”chatbot” use, and can be optimized a bit differently than Midfill. Prefill reaches high arithmetic intensity (ratio of computation to data movement) and does not need to wait for context to be located and read. Midfill begins from an existing KV cache for prior state and appends a new request input sequence. This generally has a more moderate arithmetic intensity than prefill because there are fewer new tokens and more existing data to move per token.

这些模式对计算、内存和网络提出了不同的要求。预填充和中填充密切相关,其中预填充可视为零先验上下文的特例。然而,预填充是一种常见情况,对应于类似经典“聊天机器人”的单次查询用例,其优化方式可与中填充略有不同。预填充具有较高的算术强度(计算量与数据移动量的比率),且无需等待上下文定位和读取。中填充则从现有 KV 缓存(用于先前状态)开始,并追加新的请求输入序列。由于每令牌涉及的新令牌较少而需移动的数据较多,中填充的算术强度通常低于预填充。

Decode attention and decode expert work are generally low arithmetic intensity since there are few new tokens compared to the prior context or expert weights, which at decode state are data that needs to be moved. Meanwhile, across all of them, the tensors for attention and for experts are the same regardless of token value, so there is benefit to sharing a single reading of a tensor with as many tokens as can be queued to use it, even tokens coming from unrelated requests by other users which simply happen to be running at the same time and which are networked closely enough to share. Treating all four mode as the same workload gives away much of the structural advantages that MoE models offer in terms of different intensity and different sharing patterns.

解码注意力(decode attention)和解码专家工作(decode expert work)通常具有较低的算术强度,因为与之前的上下文或专家权重相比,新增的 token 数量很少,而在解码状态下,这些数据是需要移动的数据。同时,在所有这些情况下,无论 token 值如何,注意力和专家的张量都是相同的,因此让尽可能多的排队使用同一张量的 token 共享一次读取是有利的,即使这些 token 来自其他用户的不相关请求,它们只是恰好同时运行并且网络足够接近以共享资源。将四种模式视为相同的工作负载,会丧失 MoE 模型在强度差异和共享模式差异方面提供的许多结构优势。

In this document we will treat the 4 stages separately, and sometimes assume they are disaggregated. There are tradeoffs between aggregated (where the models stay on one server, which reconfigures as the request progresses between the stages) and disaggregated where the orchestrator may find a free slot on another machine already configured appropriately. Generally we will talk about the stages as disaggregated, with aggregation being a special case of always making the current node available when a previous stage finishes. There will be a section on the trade-offs at the end, after the work to be done has been more completely introduced.

在本文档中,我们将分别处理这 4 个阶段,有时假设它们是解耦的。聚合式(即模型保留在一台服务器上,随着请求在各个阶段之间推进而重新配置)和解耦式(编排器可以在另一台已适当配置的机器上找到空闲槽位)之间存在权衡。通常情况下,我们将把各个阶段作为解耦的来讨论,而聚合被视为一种特殊情况,即在上一阶段完成时始终使当前节点可用。在完成对需要执行的工作更全面的介绍之后,最后会有一个关于权衡的部分。

A transformer model is a deep stack of repeating layer groups. A group may contain one kind of layer, or it may contain a full-attention layer together with several linear, local, selective, or otherwise optimized-attention layers (this is a hybrid group of layers). Within each layer, attention, shared transformations, routing, selected experts, and recombination occur in sequence. Those steps are known in advance and repeat continuously, allowing the same hardware resources to serve different parts of the flow at different moments.

Transformer 模型是由重复的层组堆叠而成的深层结构。一个组可能包含一种类型的层,也可能包含一个全注意力层以及几个线性、局部、选择性或其他优化过的注意力层(这是一种混合层组)。在每个层内,注意力、共享变换、路由、选定的专家和重组依次发生。这些步骤是预先知道的并持续重复,使得相同的硬件资源能够在不同时刻服务于流程的不同部分。

The machines can also be described equally simple recurring units. A node is a group of accelerators, typically a tray in a rack, each accelerator pairs compute with local fast memory. A node is coordinated by a CPU and connected to other nodes through NICs (network interface controller). Nodes stack into racks. A rack may act as one scale-up domain when very large tensors are , several smaller scale-up islands, or a set of pipeline stages. Datacenter networking then joins bounded workers to storage and orchestration rather than participating in every inner tensor operation.

这些机器也可以被描述为同样简单的重复单元。一个节点是一组加速器,通常是一个机架中的托盘,每个加速器将计算与本地高速内存配对。节点由 CPU 协调,并通过 NIC(网络接口控制器)与其他节点连接。节点堆叠成机架。当处理非常大的张量时,一个机架可以作为一个扩展域;或者作为几个较小的扩展岛;或一组流水线阶段。数据中心网络随后将受限的工作者与存储和编排连接起来,而不是参与每一个内部张量操作。

This essay follows that progression. It begins with the global service and the operating regimes, then opens the model into wide layer-group slices and maps them onto accelerator nodes and racks. From there it develops pipeline, tensor, and expert parallelism; the limit imposed by KV-cache memory; the different value of batching in prefill, midfill, and decode; and the role of scheduling in a many-rack token factory.

本文遵循这一进展。它从全局服务和运行模式开始,然后将模型展开为宽层组切片,并将它们映射到加速器节点和机架上。在此基础上,它开发了流水线、张量和专家并行性;KV 缓存内存施加的限制;预填充、中填充和解码中批处理的不同价值;以及在多机架令牌工厂中调度的作用。

Figure 1. Overview of model stages in an inference service.

图 1. 推理服务中模型阶段的概述。

1. Orchestration delivers requests via queues into pools of workers

1. 编排通过队列将请求交付给工作者池

An inference service is a cluster where prior requests and immutable state are stored when idle and then queued for delivery to specialized workers when a new request pulls in context or a prior request reawakens for continuation. The incoming queue is not yet a batch. Each request carries prompt tokens, references to reusable context, service objectives, and often an existing conversation or agent state. An orchestrator may place it into a prefill, midfill, or decode batch, and the request can join or leave that batch independently as it completes, making that capacity available for new work. Continuous batching works best when the scheduler knows which workers have slots that match the request’s context length and service objective.

推理服务是一个集群,其中在空闲时存储先前的请求和不可变状态,然后排队等待交付给专门的工作者,以便在新请求拉入上下文或先前请求重新唤醒以继续进行时进行处理。传入的队列还不是一个批次。每个请求都携带提示令牌、对可重用上下文的引用、服务目标,以及通常已有的对话或代理状态。编排器可以将其放入预填充、中填充或解码批次中,并且随着请求完成,它可以独立地加入或离开该批次,从而使其容量可用于新工作。当调度器知道哪些工作者拥有与请求上下文长度和服务目标匹配的槽位时,连续批处理效果最佳。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

关联讨论

同一事件的更多信源

相似阅读

关联信息,但可能不是同一事件