Skip to content

Commit 4d0324b

Browse files
committed
Merge deepseek_v4_core_primitives into dsv4-moe-routing-primitives
2 parents 0e3615a + 4e5725c commit 4d0324b

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/maxtext/layers/linears.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ def __init__(
603603
# Grouped block-diagonal projection kernel parameters
604604
# Kernels are stored as a 3D tensor: [n_groups, in_features_per_group, out_features_per_group]
605605
kernel_shape = (n_groups, in_features_per_group, self.out_features_per_group)
606-
self.weight = nnx.Param(
606+
self.kernel = nnx.Param(
607607
kernel_init(
608608
rngs.params(),
609609
kernel_shape,
@@ -623,10 +623,10 @@ def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
623623
Projected tensor of shape [..., n_groups, out_features_per_group]
624624
"""
625625
x = jnp.asarray(x, self.dtype)
626-
weight = jnp.asarray(self.weight[...], self.dtype)
626+
kernel = jnp.asarray(self.kernel[...], self.dtype)
627627

628628
# Execute parallel group projection via optimized einsum broadcasting.
629629
# x: [..., g, i]
630-
# weight: [g, i, o]
630+
# kernel: [g, i, o]
631631
# output: [..., g, o]
632-
return jnp.einsum("...gi,gio->...go", x, weight)
632+
return jnp.einsum("...gi,gio->...go", x, kernel)

src/maxtext/layers/normalizations.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def l2norm(x: Array, dim: int = -1, eps: float = 1e-6) -> Array:
243243

244244

245245
class 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

Comments
 (0)