Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions vllm_omni/diffusion/layers/activation.py
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):

Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Collaborator

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 on forward_npu) if you want the fusion to apply there too.

"""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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: self.op is resolved eagerly when the platform is cuda/rocm/musa, but the musa path never uses it (forward_musa → forward_cuda → x.device.type != "cuda" → native), and construction would crash on a musa build that doesn't register _C.silu_and_mul. Resolving the op lazily inside forward_cuda (or dropping is_musa() from the condition) avoids both.


@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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The byte-identical accuracy result surprises me: torch.ops._C.silu_and_mul computes silu in FP32 and rounds once to BF16, while the baseline F.silu(gate) * up materializes silu as BF16 before the multiply (double rounding), so I'd expect ULP-level divergence to propagate through 49 steps into the output bytes — the modulation PR (#6281) measures PSNR 39.55 dB from exactly this class of rounding change. Can you confirm how the comparison was produced (fused op active in both runs? same checkpoint/seed as the baseline)?

return out

def forward_npu(self, x: torch.Tensor) -> torch.Tensor:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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: torch_npu.npu_swiglu(gate_up, dim=-1) (wrapped at vllm_omni/platforms/npu/models/minimax_h3.py:57, covered by test_minimax_h3_qwen3vl_swiglu.py at 2e-2 tolerance). RMSNorm.forward_npu (vllm_omni/diffusion/layers/norm.py:125) sets the lazy-import precedent:

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)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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
Expand Down Expand Up @@ -586,6 +587,7 @@ def __init__(
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(
Expand All @@ -600,8 +602,7 @@ def __init__(

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

Expand Down
Loading