Skip to content

Commit ff8bd43

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

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,
@@ -1029,6 +1031,7 @@ def __init__(
10291031
rngs=rngs,
10301032
)
10311033

1034+
@functools.partial(jax.named_call, name="qwen3_next_full_attention")
10321035
def __call__(
10331036
self,
10341037
inputs: jnp.ndarray,
@@ -1435,6 +1438,7 @@ def __init__(
14351438
rngs=rngs,
14361439
)
14371440

1441+
@functools.partial(jax.named_call, name="apply_attention_with_norm")
14381442
def apply_attention_with_norm(
14391443
self,
14401444
inputs: jnp.ndarray,
@@ -1449,25 +1453,28 @@ def apply_attention_with_norm(
14491453
inputs = nn.with_logical_constraint(inputs, self.activation_axis_names)
14501454
inputs = checkpoint_name(inputs, "decoder_layer_input")
14511455
# Pre attention norm
1452-
lnx = self.pre_self_attention_layer_norm(inputs)
1453-
lnx = nn.with_logical_constraint(lnx, self.activation_axis_names)
1456+
with jax.named_scope("pre_self_attention"):
1457+
lnx = self.pre_self_attention_layer_norm(inputs)
1458+
lnx = nn.with_logical_constraint(lnx, self.activation_axis_names)
14541459
# Self attention
1455-
attention_lnx, kv_cache = self.self_attention(
1456-
lnx,
1457-
lnx,
1458-
decoder_positions,
1459-
decoder_segment_ids=decoder_segment_ids,
1460-
deterministic=deterministic,
1461-
model_mode=model_mode,
1462-
kv_cache=kv_cache,
1463-
attention_metadata=attention_metadata,
1464-
)
1460+
with jax.named_scope("self_attention"):
1461+
attention_lnx, kv_cache = self.self_attention(
1462+
lnx,
1463+
lnx,
1464+
decoder_positions,
1465+
decoder_segment_ids=decoder_segment_ids,
1466+
deterministic=deterministic,
1467+
model_mode=model_mode,
1468+
kv_cache=kv_cache,
1469+
attention_metadata=attention_metadata,
1470+
)
14651471
attention_lnx = nn.with_logical_constraint(attention_lnx, self.activation_axis_names)
14661472
# Residual connection after attention
14671473
intermediate_inputs = inputs + attention_lnx
14681474
# Post attention norm
1469-
hidden_states = self.post_self_attention_layer_norm(intermediate_inputs)
1470-
hidden_states = nn.with_logical_constraint(hidden_states, self.activation_axis_names)
1475+
with jax.named_scope("post_self_attention_layer"):
1476+
hidden_states = self.post_self_attention_layer_norm(intermediate_inputs)
1477+
hidden_states = nn.with_logical_constraint(hidden_states, self.activation_axis_names)
14711478
return hidden_states, intermediate_inputs, kv_cache
14721479

14731480

@@ -1500,6 +1507,7 @@ def __init__(
15001507
rngs=rngs,
15011508
)
15021509

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

15311540
layer_output = intermediate_inputs + mlp_lnx
15321541
layer_output = nn.with_logical_constraint(layer_output, self.activation_axis_names)
@@ -1563,6 +1572,7 @@ def __init__(
15631572
rngs=rngs,
15641573
)
15651574

1575+
@functools.partial(jax.named_call, name="qwen3_moe_decoder_layer")
15661576
def __call__(
15671577
self,
15681578
inputs: jnp.ndarray,
@@ -1596,8 +1606,9 @@ def __call__(
15961606
attention_metadata=attention_metadata,
15971607
)
15981608

1599-
mlp_lnx, load_balance_loss, _ = self.moe_block(hidden_states)
1600-
mlp_lnx = nn.with_logical_constraint(mlp_lnx, self.activation_axis_names)
1609+
with jax.named_scope("moe_block"):
1610+
mlp_lnx, load_balance_loss, _ = self.moe_block(hidden_states)
1611+
mlp_lnx = nn.with_logical_constraint(mlp_lnx, self.activation_axis_names)
16011612
if self.config.load_balance_loss_weight > 0.0 and load_balance_loss is not None:
16021613
self.moe_lb_loss = nnx.Intermediate(load_balance_loss)
16031614

0 commit comments

Comments
 (0)