Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Traceback (most recent call last):
File "train.py", line 42, in <module>
import maxtext
File "/usr/local/google/home/fiyinbenstowe/Desktop/Project/maxtext/src/maxtext/layers/normalizations.py", line 72
mean2 = jnp.mean(lax.square(x), axis=-1, keepdims=True)
^
SyntaxError: invalid syntax
54 changes: 54 additions & 0 deletions src/maxtext/layers/mhc.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,58 @@ def __call__(
return res_out + post_out, metadata


class DeepSeek4HyperHead(nnx.Module):
"""DeepSeek V4 Hyper Head for collapsing hyper-connection streams."""

def __init__(self, config: Config, rngs: nnx.Rngs):
self.config = config
self.hc_mult = config.mhc_expansion_rate
self.eps = getattr(config, "hc_eps", 1e-6)
self.dtype = config.dtype
self.weight_dtype = config.weight_dtype
self.matmul_precision = jax.lax.Precision(config.matmul_precision)

self.input_norm = RMSNorm(
num_features=self.hc_mult * config.emb_dim,
dtype=self.dtype,
weight_dtype=self.weight_dtype,
kernel_axes=("norm",),
epsilon=config.normalization_layer_epsilon,
with_scale=False,
rngs=rngs,
)

scale_init = nd_dense_init(1.0, "fan_in", "normal")
self.hc_fn = nnx.Param(
scale_init(
rngs.params(),
(self.hc_mult * config.emb_dim, self.hc_mult),
self.weight_dtype,
in_axis=0,
out_axis=1,
),
out_sharding=(None, None),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The parameter self.hc_fn is currently initialized with out_sharding=(None, None), which replicates the entire weight matrix across all devices. Since its first dimension is self.hc_mult * config.emb_dim (which can be quite large), replicating it can lead to significant memory overhead and potential OOMs during training. Sharding the first dimension along the "activation_embed" axis is more efficient and aligns with how other projection weights (like self.res_alpha, self.pre_alpha, and self.post_alpha) are sharded in this file.

Suggested change
out_sharding=(None, None),
out_sharding=("activation_embed", None),

)

self.hc_base = nnx.Param(
default_bias_init(rngs.params(), (self.hc_mult,), self.weight_dtype),
out_sharding=(None,),
)
self.hc_scale = nnx.Param(
default_scalar_init(rngs.params(), (1,), self.weight_dtype),
out_sharding=(None,),
)

def __call__(self, x: Array) -> Array:
b, s, k, d = x.shape
flat = jnp.reshape(x, (b, s, k * d))
flat = self.input_norm(flat)

hc_fn = jnp.asarray(self.hc_fn[...], self.dtype)
hc_base = jnp.asarray(self.hc_base[...], self.dtype)
hc_scale = jnp.asarray(self.hc_scale[...], self.dtype)

mixes = jnp.einsum("bsm,mn -> bsn", flat, hc_fn, precision=self.matmul_precision)
pre = jax.nn.sigmoid(mixes * hc_scale + hc_base) + self.eps

return jnp.sum(jnp.expand_dims(pre, axis=-1) * x, axis=2).astype(self.dtype)
Loading