From 9683b7f4b44997c20c4e865ec89b9775bb60a49b Mon Sep 17 00:00:00 2001 From: Canlin Guo Date: Wed, 19 Aug 2026 11:12:10 +0800 Subject: [PATCH] [Perf] Fuse MiniMax H3 SwiGLU activation (#6283) Signed-off-by: Canlin Guo --- vllm_omni/diffusion/layers/activation.py | 37 +++++++++++++++++++ .../minimax_h3/minimax_h3_transformer.py | 5 ++- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 vllm_omni/diffusion/layers/activation.py diff --git a/vllm_omni/diffusion/layers/activation.py b/vllm_omni/diffusion/layers/activation.py new file mode 100644 index 000000000..c9658cf6e --- /dev/null +++ b/vllm_omni/diffusion/layers/activation.py @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: Apache-2.0 +"""Custom activation functions for diffusion models.""" + +import torch +import torch.nn.functional as F + +from vllm_omni.diffusion.layers.custom_op import CustomOp +from vllm_omni.platforms import current_omni_platform + + +class SiluAndMul(CustomOp): + """SwiGLU activation dispatched by the current vLLM-Omni platform.""" + + def __init__(self) -> None: + super().__init__() + if current_omni_platform.is_cuda() or current_omni_platform.is_rocm() or current_omni_platform.is_musa(): + self.op = torch.ops._C.silu_and_mul + + @staticmethod + def forward_native(x: torch.Tensor) -> torch.Tensor: + d = x.shape[-1] // 2 + return F.silu(x[..., :d]) * x[..., d:] + + def forward_cuda(self, x: torch.Tensor) -> torch.Tensor: + if x.device.type != "cuda": + return self.forward_native(x) + d = x.shape[-1] // 2 + output_shape = x.shape[:-1] + (d,) + out = torch.empty(output_shape, dtype=x.dtype, device=x.device) + self.op(out, x) + return out + + def forward_npu(self, x: torch.Tensor) -> torch.Tensor: + return self.forward_native(x) + + def forward_xpu(self, x: torch.Tensor) -> torch.Tensor: + return self.forward_native(x) diff --git a/vllm_omni/diffusion/models/minimax_h3/minimax_h3_transformer.py b/vllm_omni/diffusion/models/minimax_h3/minimax_h3_transformer.py index 52f9ab589..2116182ee 100644 --- a/vllm_omni/diffusion/models/minimax_h3/minimax_h3_transformer.py +++ b/vllm_omni/diffusion/models/minimax_h3/minimax_h3_transformer.py @@ -33,6 +33,7 @@ from vllm_omni.diffusion.distributed.sp_plan import ( SequenceParallelInput, SequenceParallelOutput, ) +from vllm_omni.diffusion.layers.activation import SiluAndMul from vllm_omni.diffusion.layers.fused_qk_norm_rope import fused_qk_norm_rope from vllm_omni.diffusion.layers.norm import RMSNorm from vllm_omni.diffusion.layers.rope import RotaryEmbedding @@ -586,6 +587,7 @@ class MiniMaxH3MLP(nn.Module): quant_config=quant_config, prefix=f"{prefix}.fc1", ) + self.act_fn = SiluAndMul() # Chunk the fused fc1 output as [gate, up], then compute # silu(gate) * up. self.fc2 = RowParallelLinear( @@ -600,8 +602,7 @@ class MiniMaxH3MLP(nn.Module): def forward(self, x: torch.Tensor) -> torch.Tensor: hidden, _ = self.fc1(x) - gate, up = hidden.chunk(2, dim=-1) - hidden = nn.functional.silu(gate) * up + hidden = self.act_fn(hidden) out, _ = self.fc2(hidden) return out