Repository navigation
[Perf] Fuse MiniMax H3 SwiGLU activation #6283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
|
||
| @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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The byte-identical accuracy result surprises me: |
||
| return out | ||
|
|
||
| def forward_npu(self, x: torch.Tensor) -> torch.Tensor: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NPU is left on the two-kernel native path even though a fused operator with this exact packed contract already exists in-repo: def forward_npu(self, x):
import torch_npu
return torch_npu.npu_swiglu(x, dim=-1)With that the DiT MLP gets the same one-kernel activation on Ascend and the PR goal ("avoid the native two-kernel fallback") holds on NPU too. |
||
| return self.forward_native(x) | ||
|
|
||
| def forward_xpu(self, x: torch.Tensor) -> torch.Tensor: | ||
| return self.forward_native(x) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fan2956 does it apply to npu as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified from the code: as written, NPU keeps the native two-kernel path (
forward_npu→forward_native), so Ascend behavior is unchanged — but a fused op already exists in-repo (torch_npu.npu_swiglu; see my comment onforward_npu) if you want the fusion to apply there too.