Skip to content

Dsv4 attention opt on lb changes - #4669

Draft
dipakg-lang wants to merge 3 commits into
AI-Hypercomputer:dsv4-nnx-supportfrom
dipakg-lang:dsv4_attention_opt_on_lb_changes
Draft

Dsv4 attention opt on lb changes#4669
dipakg-lang wants to merge 3 commits into
AI-Hypercomputer:dsv4-nnx-supportfrom
dipakg-lang:dsv4_attention_opt_on_lb_changes

Conversation

@dipakg-lang

Copy link
Copy Markdown
Collaborator

Description

Start with a short description of what the PR does and how this is a change from
the past.

The rest of the description includes relevant details and context, examples:

  • why is this change being made,
  • the problem being solved and any relevant context,
  • why this is a good solution,
  • some information about the specific implementation,
  • shortcomings of the solution and possible future improvements.

If the change fixes a bug or a Github issue, please include a link, e.g.,:
FIXES: b/123456
FIXES: #123456

You can also provide a comma-separated list. If you don't want to close a bug but
simply to reference it, use BUGS, e.g.:
BUGS: b/123456

Notice 1: Once all tests pass, the "pull ready" label will automatically be assigned.
This label is used for administrative purposes. Please do not add it manually.

Notice 2: For external contributions, our settings currently require an approval from a MaxText maintainer to trigger CI tests.

Tests

Please describe how you tested this change, and include any instructions and/or
commands to reproduce.

Checklist

Before submitting this PR, please make sure (put X in square brackets):

  • I have performed a self-review of my code. For an optional AI review, add the gemini-review label.
  • I have necessary comments in my code, particularly in hard-to-understand areas.
  • I have run end-to-end tests tests and provided workload links above if applicable.
  • I have made or will make corresponding changes to the doc if needed, including adding new documentation pages to the relevant Table of Contents (toctree directive) as explained in our documentation.

snehalv2002 and others added 3 commits July 21, 2026 16:54
…ia pure NNX MoEBiasVar

This commit migrates the DeepSeek V4 auxiliary-loss-free routing bias to a pure nnx.Variable (MoEBiasVar), automatically isolating it from the optimizer and standard sequence-wise gradients without requiring the jax.lax.stop_gradient hacks or Optax global masking.

DeepSeek V3 backward compatibility remains completely untouched and functional via the legacy nnx.Param paths.

To prove the variance reduction and model convergence, the DeepSeek-V4-Flash (284B) model was trained for 300 steps on a Ironwood cluster. The following analysis was performed:

* Run A (Baseline): Variance at Step 300 with NO load balancing active (routed_bias_update_rate=0.0, load_balance_loss_weight=0.0). Shows natural degradation and expert collapse.
* Run B (With Load Balancing): Variance at Step 300 with FULL load balancing active (routed_bias_update_rate=0.001, load_balance_loss_weight=0.0001). Shows healthy token distribution and stable loss curve comparable or better than the PR-4497 base architecture.

=== DeepSeek V4 Load Balancing Variance Analysis (Step 300) ===

| Layer Index | Routing Type | Baseline (No LB) | With Load Balancing | Improvement |
|-------------|--------------|------------------|---------------------|-------------|
|           0 | Hash Routed  |        587520.00 |           587520.00 |       0.00% |
|           1 | Hash Routed  |        587520.00 |           587520.00 |       0.00% |
|           2 | Hash Routed  |        587520.00 |           587520.00 |       0.00% |
|           3 | Top-K Routed |         14623.82 |             2447.45 |      83.26% |
|           4 | Top-K Routed |         15871.75 |             3436.97 |      78.35% |
|           5 | Top-K Routed |         15261.11 |             3110.88 |      79.62% |
|           6 | Top-K Routed |         15717.74 |             1812.94 |      88.47% |
|           7 | Top-K Routed |         15728.96 |             2615.92 |      83.37% |
|           8 | Top-K Routed |         15999.51 |             4967.23 |      68.95% |
|           9 | Top-K Routed |         15240.05 |             3919.24 |      74.28% |
|          10 | Top-K Routed |         16086.67 |             2348.59 |      85.40% |
|          11 | Top-K Routed |         16092.98 |             2607.32 |      83.80% |
|          12 | Top-K Routed |         14255.88 |             2896.68 |      79.68% |
|          13 | Top-K Routed |         14632.31 |             4420.16 |      69.79% |
|          14 | Top-K Routed |         15010.89 |             4668.00 |      68.90% |
|          15 | Top-K Routed |         16004.66 |             2190.59 |      86.31% |
|          16 | Top-K Routed |         14625.14 |             2506.02 |      82.86% |
|          17 | Top-K Routed |         13549.16 |             2880.44 |      78.74% |
|          18 | Top-K Routed |         15119.30 |             3142.03 |      79.22% |
|          19 | Top-K Routed |         14413.22 |             4134.32 |      71.32% |
|          20 | Top-K Routed |         14239.44 |             2910.45 |      79.56% |
|          21 | Top-K Routed |         13616.19 |             2687.55 |      80.26% |
|          22 | Top-K Routed |         14360.10 |             1947.84 |      86.44% |
|          23 | Top-K Routed |         14887.30 |             3880.00 |      73.94% |
|          24 | Top-K Routed |         14978.84 |             4074.12 |      72.80% |
|          25 | Top-K Routed |         14996.25 |             1401.96 |      90.65% |
|          26 | Top-K Routed |         14232.96 |             3889.90 |      72.67% |
|          27 | Top-K Routed |         14977.17 |             1858.18 |      87.59% |
|          28 | Top-K Routed |         14378.70 |             2400.12 |      83.31% |
|          29 | Top-K Routed |         13691.07 |             2181.08 |      84.07% |
|          30 | Top-K Routed |         15055.45 |             3521.05 |      76.61% |
|          31 | Top-K Routed |         14677.20 |             5095.23 |      65.28% |
|          32 | Top-K Routed |         16188.89 |             3978.03 |      75.43% |
|          33 | Top-K Routed |         14369.13 |             2921.98 |      79.66% |
|          34 | Top-K Routed |         14483.14 |             5714.06 |      60.55% |
|          35 | Top-K Routed |         15321.41 |             2911.53 |      81.00% |
|          36 | Top-K Routed |         13667.09 |             2495.93 |      81.74% |
|          37 | Top-K Routed |         14704.00 |             3494.09 |      76.24% |
|          38 | Top-K Routed |         14339.20 |             2385.22 |      83.37% |
|          39 | Top-K Routed |         14203.30 |             2454.55 |      82.72% |
|          40 | Top-K Routed |         13582.94 |             2371.51 |      82.54% |
|          41 | Top-K Routed |         16522.23 |             3767.62 |      77.20% |
|          42 | Top-K Routed |         16277.68 |             1494.26 |      90.82% |
|-------------|--------------|------------------|---------------------|-------------|
| TOTAL/AVG   | Top-K Only   |        595982.83 |           123941.04 |      79.20% |

Raw data logs and loss curve trajectories collected for this analysis:

[Run A - Baseline (No LB) Logs]: https://paste.googleplex.com/6145194301980672
[Run B - With Load Balancing Logs]: https://paste.googleplex.com/5985663009816576
[Run C - PR-4497 Legacy Baseline Logs]: https://paste.googleplex.com/4770652925067264
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@dipakg-lang
dipakg-lang changed the base branch from main to dsv4-nnx-support July 29, 2026 14:53
# -> [batch, n_windows, compress_rate, 2 * head_dim]
chunk_kv = chunk_kv.reshape((batch_size, n_windows, compress_rate, 2 * head_dim))
chunk_gate = chunk_gate.reshape((batch_size, n_windows, compress_rate, 2 * head_dim)) + position_bias
# DIPAK - OPTIMIZATION

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Remove it and you can say : Optimization technique #1 : Avoid expanding dimensions and then doing the operations like split, concat etc

Impact:

@snehalv2002
snehalv2002 force-pushed the dsv4-nnx-support branch 3 times, most recently from 29a3a5c to 3d0fc4c Compare July 31, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants