@@ -76,7 +76,7 @@ def forward(self, x, cache_x=None):
7676class RMSNorm (nn .Module ):
7777 """Root mean square normalization supporting channel-first tensors."""
7878
79- def __init__ (self , dim , channel_first = True , images = True ):
79+ def __init__ (self , dim , channel_first = True , images = True , bias = False ):
8080 """Initialize learnable RMS normalization parameters."""
8181 super ().__init__ ()
8282 broadcastable_dims = (1 , 1 , 1 ) if not images else (1 , 1 )
@@ -85,15 +85,18 @@ def __init__(self, dim, channel_first=True, images=True):
8585 self .channel_first = channel_first
8686 self .eps = 1e-12
8787 self .gamma = nn .Parameter (torch .ones (shape ))
88+ self .bias = nn .Parameter (torch .zeros (shape )) if bias else 0.
8889
8990 def forward (self , x ):
90- """Normalize input tensor and apply scale parameters."""
91+ """Normalize input tensor and apply scale and bias parameters."""
9192 weight = self .gamma .reshape (- 1 )
9293 if self .channel_first :
9394 # Move channels to the last axis so F.rms_norm can use the fused kernel.
9495 x = F .rms_norm (x .movedim (1 , - 1 ), weight .shape , weight = weight , eps = self .eps )
95- return x .movedim (- 1 , 1 )
96- return F .rms_norm (x , 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
97100
98101
99102class Upsample (nn .Upsample ):
0 commit comments