@@ -243,7 +243,7 @@ def l2norm(x: Array, dim: int = -1, eps: float = 1e-6) -> Array:
243243
244244
245245class DeepSeekV4RMSNorm (nnx .Module ):
246- """RMS normalization for DeepSeek-V4 (equivalent to T5LayerNorm) ."""
246+ """RMS normalization for DeepSeek-V4."""
247247
248248 def __init__ (
249249 self ,
@@ -257,8 +257,9 @@ def __init__(
257257 self .dtype = dtype
258258 self .weight_dtype = weight_dtype
259259
260- # Initialize learnable scale weight to ones matching T5LayerNorm behavior
260+ # Initialize learnable scale weight to ones
261261 self .weight = nnx .Param (jnp .ones ((hidden_size ,), dtype = weight_dtype ))
262+ self .scale = self .weight
262263
263264 def __call__ (self , x : jnp .ndarray ) -> jnp .ndarray :
264265 # [B, S, D] where D = hidden_size
@@ -296,6 +297,7 @@ def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
296297 # Calculate variance across features axis
297298 variance = jnp .mean (lax .square (x_f32 ), axis = - 1 , keepdims = True ) # [..., 1]
298299
299- # Apply reciprocal square root and cast back to active precision
300- normalized = x_f32 * lax .rsqrt (variance + self .eps ) # [..., D]
301- return jnp .asarray (normalized , self .dtype ) # [..., D]
300+ # Apply reciprocal square root, cast to active precision, and multiply
301+ inv_norm = jnp .asarray (lax .rsqrt (variance + self .eps ), self .dtype ) # [..., 1]
302+ x_active = jnp .asarray (x , self .dtype ) # [..., D]
303+ return x_active * inv_norm # [..., D]
0 commit comments