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
Expand Up @@ -7,7 +7,9 @@ RUN apt-get update && apt-get install -y git curl ca-certificates gpg && \
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && \
chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && \
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null && \
apt-get update && apt-get install -y gh && \
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg && \
Comment on lines +10 to +11

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-medium medium

Using http instead of https for package repositories can expose the system to man-in-the-middle (MITM) attacks or interception. Additionally, running curl without -fsSL can fail silently or write error pages to the keyring file, which makes debugging harder. It is safer to use https and add -fsSL to the curl command.

    echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
    curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg && \

apt-get update && apt-get install -y gh google-cloud-cli && \
rm -rf /var/lib/apt/lists/*

# Copy only requirements first to leverage Docker cache for heavy installations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 default value of sleep_seconds was updated from 30 to 60, but the docstring still mentions "a 30-second sleep". Please update the docstring to reflect the new default value or make it generic.

Suggested change
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."""
def _send_message_with_retry(chat, prompt, max_retries=5, sleep_seconds=60):
"""Sends a message to Gemini with retry and a sleep on 429 rate-limit/quota errors."""

for attempt in range(1, max_retries + 1):
try:
Expand Down
62 changes: 36 additions & 26 deletions src/maxtext/layers/mhc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -314,7 +313,7 @@ def __call__(


class DeepSeek4HyperHead(nnx.Module):
"""DeepSeek V4 Hyper Head."""
"""Implements DeepSeek4 HyperHead."""

def __init__(
self,
Expand All @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The weight matrix hc_fn is initialized using default_scalar_init, which is a constant initializer (constant(0.01)). Using a constant initializer for a projection/weight matrix prevents symmetry breaking during training, which can severely limit representation learning and model capacity. It should be initialized using a random variance scaling initializer like nd_dense_init (similar to how weight matrices are initialized in ManifoldConstrainedHyperConnections).

    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)
Loading