-
Notifications
You must be signed in to change notification settings - Fork 582
Fix shape mismatch and add DeepSeek4HyperHead in mhc.py #4780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |||||||||
| logger = logging.getLogger(__name__) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _send_message_with_retry(chat, prompt, max_retries=3, sleep_seconds=30): | ||||||||||
| def _send_message_with_retry(chat, prompt, max_retries=5, sleep_seconds=60): | ||||||||||
| """Sends a message to Gemini with retry and a 30-second sleep on 429 rate-limit/quota errors.""" | ||||||||||
|
Comment on lines
+14
to
15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value of
Suggested change
|
||||||||||
| for attempt in range(1, max_retries + 1): | ||||||||||
| try: | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,6 @@ | |
| from maxtext.common.common_types import Array, Config | ||
| from maxtext.common.common_types import HyperConnectionType | ||
| from maxtext.layers.initializers import default_bias_init, default_scalar_init, nd_dense_init | ||
| from maxtext.layers import linears | ||
| from maxtext.layers.normalizations import RMSNorm | ||
|
|
||
|
|
||
|
|
@@ -314,7 +313,7 @@ def __call__( | |
|
|
||
|
|
||
| class DeepSeek4HyperHead(nnx.Module): | ||
| """DeepSeek V4 Hyper Head.""" | ||
| """Implements DeepSeek4 HyperHead.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -323,33 +322,44 @@ def __init__( | |
| rngs: nnx.Rngs, | ||
| ): | ||
| self.config = config | ||
| self.mesh = mesh | ||
| self.hc_mult = config.mhc_expansion_rate | ||
| self.rngs = rngs | ||
| self.k = config.mhc_expansion_rate | ||
| self.dim = config.emb_dim | ||
| self.dtype = config.dtype | ||
| self.weight_dtype = config.weight_dtype | ||
|
|
||
| # tid2eid layers | ||
| self.tid2eid = nnx.Sequential( | ||
| *[ | ||
| linears.DenseGeneral( | ||
| in_features_shape=self.dim, | ||
| out_features_shape=self.dim, | ||
| dtype=self.dtype, | ||
| weight_dtype=self.weight_dtype, | ||
| rngs=self.rngs, | ||
| ) | ||
| for _ in range(config.first_num_hash_layers) | ||
| ] | ||
| self.mesh = mesh | ||
| self.dtype = self.config.dtype | ||
| self.weight_dtype = self.config.weight_dtype | ||
| self.eps = 1e-6 | ||
|
|
||
| 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, | ||
| rngs=self.rngs, | ||
| ) | ||
|
|
||
| self.hc_fn = nnx.Param( | ||
| default_scalar_init(self.rngs.params(), (self.hc_mult, self.hc_mult * config.emb_dim), self.weight_dtype), | ||
| out_sharding=(None, None), | ||
| ) | ||
|
Comment on lines
+341
to
+344
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The weight matrix scale_init = nd_dense_init(1.0, "fan_in", "normal")
self.hc_fn = nnx.Param(
scale_init(
self.rngs.params(),
(self.hc_mult, self.hc_mult * config.emb_dim),
self.weight_dtype,
in_axis=1,
out_axis=0,
),
out_sharding=(None, None),
) |
||
| self.hc_base = nnx.Param( | ||
| default_scalar_init(self.rngs.params(), (self.hc_mult,), self.weight_dtype), | ||
| out_sharding=(None,), | ||
| ) | ||
| self.hc_scale = nnx.Param( | ||
| default_scalar_init(self.rngs.params(), (1,), self.weight_dtype), | ||
| out_sharding=(None,), | ||
| ) | ||
|
|
||
| def __call__(self, x: Array) -> Array: | ||
| # x shape: [batch, seq, expansion_rate, emb] | ||
| # Reduce expansion_rate dimension | ||
| x = jnp.sum(x, axis=2, dtype=x.dtype) | ||
| b, s, k, d = x.shape | ||
| flat = jnp.reshape(x, (b, s, k * d)) | ||
| flat = self.input_norm(flat) | ||
|
|
||
| # Apply tid2eid layers | ||
| x = self.tid2eid(x) | ||
| mixes = jnp.einsum("bsm,nm->bsn", flat, jnp.asarray(self.hc_fn[...], self.dtype)) | ||
| pre = ( | ||
| jax.nn.sigmoid(mixes * jnp.asarray(self.hc_scale[...], self.dtype) + jnp.asarray(self.hc_base[...], self.dtype)) | ||
| + self.eps | ||
| ) | ||
|
|
||
| return x | ||
| return jnp.sum(x * jnp.expand_dims(pre, axis=3), axis=2) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
httpinstead ofhttpsfor package repositories can expose the system to man-in-the-middle (MITM) attacks or interception. Additionally, runningcurlwithout-fsSLcan fail silently or write error pages to the keyring file, which makes debugging harder. It is safer to usehttpsand add-fsSLto thecurlcommand.