跳到主内容
@wquguru
精选88Avi Chawla技巧与观点

MoE推理生产现实:低并发下的GEMV优化与Qwen-3.5实践

A lesser-known production reality of MoE inference:

原文
发到 X
推荐理由

LLM工程化必读,清晰拆解了MoE在低并发下的性能瓶颈及基于TPU VMEM的硬核优化方案,参数与案例详实,可直接指导推理引擎开发。

A lesser-known production reality of MoE inference:

MoE 推理中一个鲜为人知的生产现实:

(must-know for technical LLM interviews)

(技术类 LLM 面试必知)

MoE models reduce expert computation by routing each token to a small subset of experts.

MoE 模型通过将每个 token 路由到一小部分专家,从而减少专家的计算量。

But routing also divides a batch of token rows into several expert-specific batches.

但路由也会将一批 token 行分割成几个特定于专家的批次。

Their sizes depend on how many tokens enter the layer and which experts those tokens select.

这些批次的大小取决于有多少 token 进入该层,以及这些 token 选择了哪些专家。

This creates a systems problem that is not immediately evident from architecture diagrams.

这造成了一个从架构图中无法立即察觉的系统问题。

Let's walk through it.

让我们逐步了解它。

1) Dense transformer execution

1) 稠密 Transformer 执行

Assume a decode batch contains 32 token rows.

假设解码批次包含 32 个 token 行。

A dense Transformer sends all 32 rows through the same feed-forward network. For each feed-forward projection:

稠密 Transformer 将所有 32 行送入同一个前馈网络。对于每次前馈投影:

> 32 token rows x the same weight matrix

> 32 个 token 行 x 相同的权重矩阵

The accelerator performs one 32-row matrix multiplication and reuses that projection's weights across all 32 rows.

加速器执行一次 32 行的矩阵乘法,并在所有 32 行之间复用该投影的权重。

2) MoE execution

2) MoE 执行

A simplified top-1 MoE router may divide those rows like this:

简化的 top-1 MoE 路由器可能会这样划分这些行:

  • Expert 1 receives 8 rows - Expert 2 receives 3 rows - Expert 3 receives 0 rows - Expert 4 receives 11 rows - All other experts collectively receive 10 rows
  • 专家 1 接收 8 行 - 专家 2 接收 3 行 - 专家 3 接收 0 行 - 专家 4 接收 11 行 - 所有其他专家共接收 10 行

Each 32-row feed-forward projection has become several smaller expert projections with different row counts.

每个 32 行的前馈投影已变成几个行数不同的小型专家投影。

The engine must group rows by expert, run each expert, apply routing weights, and restore the original token order.

引擎必须按专家对行进行分组,运行每个专家,应用路由权重,并恢复原始的 token 顺序。

With top-k routing, each token appears in multiple expert batches.

使用 top-k 路由时,每个 token 会出现在多个专家批次中。

Sparse routing reduces expert computation, but it also fragments the original batch.

稀疏路由减少了专家的计算量,但也碎片化了原始批次。

3) How grouped GEMM helps

3) 分组 GEMM 如何提供帮助

GEMM is General Matrix Multiplication.

GEMM 是通用矩阵乘法(General Matrix Multiplication)。

Each active expert has its own weight matrices. Grouped GEMM schedules several expert matrix multiplications together:

每个激活的专家都有自己独立的权重矩阵。分组 GEMM 将多个专家矩阵乘法调度在一起执行:

  • Expert 1 rows x Expert 1 weights - Expert 2 rows x Expert 2 weights - Expert 4 rows x Expert 4 weights
  • 专家 1 的行 x 专家 1 的权重 - 专家 2 的行 x 专家 2 的权重 - 专家 4 的行 x 专家 4 的权重

This reduces launch overhead and helps the accelerator process differently sized expert batches together.

这降低了启动开销,并帮助加速器一起处理不同大小的专家批次。

It works best when each active expert receives several rows.

当每个活跃专家接收多行数据时,效果最佳。

4) During low concurrency

4) 低并发期间

During ordinary autoregressive decoding, each active request contributes one current token to a model step.

在普通的自回归解码过程中,每个活跃请求为模型步骤贡献一个当前 token。

With 32 active requests, some experts may receive several token rows.

如果有 32 个活跃请求,某些专家可能会接收多个 token 行。

With only one to three requests, the selected experts may barely overlap. Most active experts can receive a single row.

如果只有三到五个请求,所选专家的覆盖范围可能几乎不重叠。大多数活跃专家只能接收单行数据。

Each operation now resembles GEMV, or General Matrix-Vector Multiplication.

此时每个操作都类似于 GEMV(通用矩阵-向量乘法)。

The engine loads an expert's weights to process one token vector. That leaves little opportunity to reuse those weights across rows.

引擎加载一个专家的权重以处理单个 token 向量。这使得跨行复用这些权重的机会寥寥无几。

Grouped GEMM can combine the launches, but it cannot turn one-row expert batches into large matrix multiplications.

Grouped GEMM 可以合并启动次数,但无法将单行专家批次转化为大型矩阵乘法。

5) An example

5) 一个示例

Researchers encountered this while optimizing Qwen-3.5-397B-A17B-FP8 on Ironwood TPUs.

研究人员在优化 Ironwood TPU 上的 Qwen-3.5-397B-A17B-FP8 时遇到了这种情况。

Each token selected ten routed experts from a pool of 512. With three concurrent requests, they found that only 5% of the selected experts were duplicated across requests.

每个 token 从 512 个专家的池中选择了十个路由专家。在三个并发请求的情况下,他们发现只有 5% 的选中专家在不同请求间重复出现。

Most routed expert batches therefore contained one row.

因此,大多数路由专家批次只包含一行数据。

So they built a custom kernel that prefetched selected expert weights into the TPU's fast VMEM and processed these GEMV-like operations directly.

于是他们构建了一个自定义内核,将选中的专家权重预取到 TPU 的快速 VMEM 中,并直接处理这些类似 GEMV 的操作。

This resulted in a 3.6x faster MoE block at concurrency one on a TPU configuration.

这导致在 TPU 配置且并发数为 1 的情况下,MoE 块的运行速度提高了 3.6 倍。

This does not mean grouped GEMM is inefficient. Instead, it implies that the best execution strategy depends on the expert-batch shape:

这并不意味着 Grouped GEMM 效率低下。相反,它表明最佳执行策略取决于专家批次的形状:

  • Several rows per expert → grouped GEMM can work well - One row per expert → hardware-specific weight streaming may work better
  • 每个专家多行 → Grouped GEMM 可能表现良好 - 每个专家单行 → 特定硬件的权重流式传输可能效果更好

The architecture stays the same, and concurrency and routing overlap change how the work is assigned to each expert.

架构保持不变,并发数和路由重叠的变化会改变工作分配给每个专家的方式。

To dive deeper, I wrote a full article covering this and the other engineering problems behind MoE inference, including memory, cross-GPU dispatch, expert placement, load imbalance, quantization, and offloading.

为了深入探讨,我撰写了一篇完整文章,涵盖 MoE 推理背后的这一及其他工程问题,包括内存、跨 GPU 调度、专家放置、负载不平衡、量化和卸载。

Read it below.

请在下方阅读。

更进一步:量化金融体系

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

进入量化体系 →

相似阅读

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