Skip to content

Commit 1695eb1

Browse files
Merge pull request baidu-baige#129 from XueSongTap/master
[vla] perf: use fused F.rms_norm in FastWAM VAE RMSNorm
2 parents b7ebd54 + a506bcb commit 1695eb1

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

  • loongforge/embodied/model/fastwam/wan

loongforge/embodied/model/fastwam/wan/vae.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,20 @@ def __init__(self, dim, channel_first=True, images=True, bias=False):
8383
shape = (dim, *broadcastable_dims) if channel_first else (dim,)
8484

8585
self.channel_first = channel_first
86-
self.scale = dim**0.5
86+
self.eps = 1e-12
8787
self.gamma = nn.Parameter(torch.ones(shape))
8888
self.bias = nn.Parameter(torch.zeros(shape)) if bias else 0.
8989

9090
def forward(self, x):
9191
"""Normalize input tensor and apply scale and bias parameters."""
92-
return F.normalize(
93-
x, dim=(1 if self.channel_first else
94-
-1)) * self.scale * self.gamma + self.bias
92+
weight = self.gamma.reshape(-1)
93+
if self.channel_first:
94+
# Move channels to the last axis so F.rms_norm can use the fused kernel.
95+
x = F.rms_norm(x.movedim(1, -1), weight.shape, weight=weight, eps=self.eps)
96+
x = x.movedim(-1, 1)
97+
else:
98+
x = F.rms_norm(x, weight.shape, weight=weight, eps=self.eps)
99+
return x + self.bias
95100

96101

97102
class Upsample(nn.Upsample):

0 commit comments

Comments
 (0)