Skip to content

Commit 790ab3a

Browse files
committed
[Perf][MoE] Share FlashInfer B12x MoE workspaces across layers
Every MoE layer allocated its own B12xMoEWrapper workspaces, which is a large per-layer GPU memory cost. The workspaces are identical for every layer and layers execute sequentially, so all layers in a worker process can share the buffers allocated by the first wrapper. Depends on flashinfer-ai/flashinfer#4603, which adds the shared_static_workspace / shared_dynamic_workspace / shared_output parameters to B12xMoEWrapper. Signed-off-by: Zihua Wu <13583761+lucifer1004@users.noreply.github.com>
1 parent f1178f3 commit 790ab3a

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

vllm/model_executor/layers/fused_moe/experts/flashinfer_b12x_moe.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class FlashInferB12xExperts(mk.FusedMoEExpertsModular):
4949
MoEActivation.RELU2_NO_MUL: "relu2",
5050
}
5151

52+
# Per-process (per-worker) buffers shared across layers; see
53+
# _ensure_wrapper.
54+
_shared_buffers: dict[tuple, tuple] = {}
55+
5256
def __init__(
5357
self,
5458
moe_config: FusedMoEConfig,
@@ -239,6 +243,21 @@ def _ensure_wrapper(self) -> None:
239243

240244
from flashinfer.fused_moe import B12xMoEWrapper
241245

246+
# Workspaces are identical for every MoE layer and layers execute
247+
# sequentially, so all layers share the buffers allocated by the
248+
# first wrapper instead of paying the (large) per-layer cost.
249+
key = (
250+
self.global_num_experts,
251+
self.topk,
252+
self.hidden_dim,
253+
self.intermediate_size_per_partition,
254+
self.max_num_tokens,
255+
self._activation_str,
256+
torch.accelerator.current_device_index(),
257+
)
258+
shared = FlashInferB12xExperts._shared_buffers.get(key)
259+
static_ws, dynamic_ws, output = shared if shared else (None, None, None)
260+
242261
self._wrapper = B12xMoEWrapper(
243262
num_experts=self.global_num_experts,
244263
top_k=self.topk,
@@ -248,7 +267,16 @@ def _ensure_wrapper(self) -> None:
248267
max_num_tokens=self.max_num_tokens,
249268
num_local_experts=self.num_local_experts,
250269
activation=self._activation_str,
270+
shared_static_workspace=static_ws,
271+
shared_dynamic_workspace=dynamic_ws,
272+
shared_output=output,
251273
)
274+
if shared is None:
275+
FlashInferB12xExperts._shared_buffers[key] = (
276+
self._wrapper._static_workspace,
277+
self._wrapper._dynamic_workspace,
278+
self._wrapper._moe_output,
279+
)
252280

253281
def apply(
254282
self,

0 commit comments

Comments
 (0)