Skip to content

Commit 1bebd34

Browse files
committed
Adding improved profiles for qwen3 models
The current profiles don't provide enough information for optimization / profiling runs.
1 parent 87e6bdd commit 1bebd34

1 file changed

Lines changed: 39 additions & 28 deletions

File tree

src/maxtext/models/qwen3.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def scan_body(prev_state, x):
180180
return core_attn_out, final_state if output_final_state else None
181181

182182

183+
@functools.partial(jax.named_call, name="jax_chunked_delta_rule")
183184
def jax_chunk_gated_delta_rule(
184185
query: Array,
185186
key: Array,
@@ -565,6 +566,7 @@ def a_log_init(key, shape, dtype=jnp.float32):
565566
rngs=rngs,
566567
)
567568

569+
@functools.partial(jax.named_call, name="qwen3_next_gated_delta_net")
568570
def __call__(
569571
self,
570572
hidden_states: Array,
@@ -1031,6 +1033,7 @@ def __init__(
10311033
rngs=rngs,
10321034
)
10331035

1036+
@functools.partial(jax.named_call, name="qwen3_next_full_attention")
10341037
def __call__(
10351038
self,
10361039
inputs: jnp.ndarray,
@@ -1437,6 +1440,7 @@ def __init__(
14371440
rngs=rngs,
14381441
)
14391442

1443+
@functools.partial(jax.named_call, name="apply_attention_with_norm")
14401444
def apply_attention_with_norm(
14411445
self,
14421446
inputs: jnp.ndarray,
@@ -1451,25 +1455,28 @@ def apply_attention_with_norm(
14511455
inputs = nn.with_logical_constraint(inputs, self.activation_axis_names)
14521456
inputs = checkpoint_name(inputs, "decoder_layer_input")
14531457
# Pre attention norm
1454-
lnx = self.pre_self_attention_layer_norm(inputs)
1455-
lnx = nn.with_logical_constraint(lnx, self.activation_axis_names)
1458+
with jax.named_scope("pre_self_attention"):
1459+
lnx = self.pre_self_attention_layer_norm(inputs)
1460+
lnx = nn.with_logical_constraint(lnx, self.activation_axis_names)
14561461
# Self attention
1457-
attention_lnx, kv_cache = self.self_attention(
1458-
lnx,
1459-
lnx,
1460-
decoder_positions,
1461-
decoder_segment_ids=decoder_segment_ids,
1462-
deterministic=deterministic,
1463-
model_mode=model_mode,
1464-
kv_cache=kv_cache,
1465-
attention_metadata=attention_metadata,
1466-
)
1462+
with jax.named_scope("self_attention"):
1463+
attention_lnx, kv_cache = self.self_attention(
1464+
lnx,
1465+
lnx,
1466+
decoder_positions,
1467+
decoder_segment_ids=decoder_segment_ids,
1468+
deterministic=deterministic,
1469+
model_mode=model_mode,
1470+
kv_cache=kv_cache,
1471+
attention_metadata=attention_metadata,
1472+
)
14671473
attention_lnx = nn.with_logical_constraint(attention_lnx, self.activation_axis_names)
14681474
# Residual connection after attention
14691475
intermediate_inputs = inputs + attention_lnx
14701476
# Post attention norm
1471-
hidden_states = self.post_self_attention_layer_norm(intermediate_inputs)
1472-
hidden_states = nn.with_logical_constraint(hidden_states, self.activation_axis_names)
1477+
with jax.named_scope("post_self_attention_layer"):
1478+
hidden_states = self.post_self_attention_layer_norm(intermediate_inputs)
1479+
hidden_states = nn.with_logical_constraint(hidden_states, self.activation_axis_names)
14731480
return hidden_states, intermediate_inputs, kv_cache
14741481

14751482

@@ -1502,6 +1509,7 @@ def __init__(
15021509
rngs=rngs,
15031510
)
15041511

1512+
@functools.partial(jax.named_call, name="qwen3_decoder_layer")
15051513
def __call__(
15061514
self,
15071515
inputs: jnp.ndarray,
@@ -1517,18 +1525,19 @@ def __call__(
15171525
# Unpack inputs if it's a tuple (e.g. from a previous layer returning (hidden_states, kv_cache))
15181526
if isinstance(inputs, tuple):
15191527
inputs = inputs[0]
1520-
hidden_states, intermediate_inputs, kv_cache = self.apply_attention_with_norm(
1521-
inputs,
1522-
decoder_segment_ids,
1523-
decoder_positions,
1524-
deterministic,
1525-
model_mode,
1526-
kv_cache=kv_cache,
1527-
attention_metadata=attention_metadata,
1528-
)
1529-
1530-
mlp_lnx = self.mlp(hidden_states, deterministic=deterministic)
1531-
mlp_lnx = nn.with_logical_constraint(mlp_lnx, self.activation_axis_names)
1528+
with jax.named_scope("apply_attention_with_norm"):
1529+
hidden_states, intermediate_inputs, kv_cache = self.apply_attention_with_norm(
1530+
inputs,
1531+
decoder_segment_ids,
1532+
decoder_positions,
1533+
deterministic,
1534+
model_mode,
1535+
kv_cache=kv_cache,
1536+
attention_metadata=attention_metadata,
1537+
)
1538+
with jax.named_scope("apply_mlp"):
1539+
mlp_lnx = self.mlp(hidden_states, deterministic=deterministic)
1540+
mlp_lnx = nn.with_logical_constraint(mlp_lnx, self.activation_axis_names)
15321541

15331542
layer_output = intermediate_inputs + mlp_lnx
15341543
layer_output = nn.with_logical_constraint(layer_output, self.activation_axis_names)
@@ -1565,6 +1574,7 @@ def __init__(
15651574
rngs=rngs,
15661575
)
15671576

1577+
@functools.partial(jax.named_call, name="qwen3_moe_decoder_layer")
15681578
def __call__(
15691579
self,
15701580
inputs: jnp.ndarray,
@@ -1598,8 +1608,9 @@ def __call__(
15981608
attention_metadata=attention_metadata,
15991609
)
16001610

1601-
mlp_lnx, load_balance_loss, _ = self.moe_block(hidden_states)
1602-
mlp_lnx = nn.with_logical_constraint(mlp_lnx, self.activation_axis_names)
1611+
with jax.named_scope("moe_block"):
1612+
mlp_lnx, load_balance_loss, _ = self.moe_block(hidden_states)
1613+
mlp_lnx = nn.with_logical_constraint(mlp_lnx, self.activation_axis_names)
16031614
if self.config.load_balance_loss_weight > 0.0 and load_balance_loss is not None:
16041615
self.moe_lb_loss = nnx.Intermediate(load_balance_loss)
16051616

0 commit comments

Comments
 (0)