[Perf] Optimize RMSNorm in Z-Image (#3304)
Signed-off-by: Lancer <maruixiang6688@gmail.com>
This commit is contained in:
1 parent
33586d845c
commit
bb69cbc9f1
3 files changed
+114
-3
No files matched your search
@@ -70,17 +70,33 @@ class RMSNorm(CustomOp):
|
||||
self.weight = nn.Parameter(torch.ones(hidden_size))
|
||||
self.variance_epsilon = eps
|
||||
|
||||
def _forward_fused(self, x: torch.Tensor) -> torch.Tensor:
|
||||
from vllm._custom_ops import rms_norm as fused_rms_norm
|
||||
|
||||
orig_shape = x.shape
|
||||
hidden_size = orig_shape[-1]
|
||||
x_2d = x.reshape(-1, hidden_size)
|
||||
out = torch.empty_like(x_2d)
|
||||
fused_rms_norm(out, x_2d, self.weight.data, self.variance_epsilon)
|
||||
return out.reshape(orig_shape)
|
||||
|
||||
def forward_cuda(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
return self.forward_native(x)
|
||||
try:
|
||||
return self._forward_fused(x)
|
||||
except Exception:
|
||||
return self.forward_native(x)
|
||||
|
||||
def forward_hip(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
return self.forward_native(x)
|
||||
try:
|
||||
return self._forward_fused(x)
|
||||
except Exception:
|
||||
return self.forward_native(x)
|
||||
|
||||
def forward_npu(
|
||||
self,
|
||||
|
||||
Reference in new issue
Block a user