Eliminate redundant sorts in BWD pass by caching indexes between embedding chunks #4583
Open
Shuwen-Fang wants to merge 1 commit into
Open
Eliminate redundant sorts in BWD pass by caching indexes between embedding chunks #4583Shuwen-Fang wants to merge 1 commit into
Shuwen-Fang wants to merge 1 commit into
Conversation
Shuwen-Fang
requested review from
A9isha,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
michelle-yooh,
richjames0,
shralex,
suexu1025,
vipannalla and
xibinliu
as code owners
July 23, 2026 21:06
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Shuwen-Fang
force-pushed
the
sort_index_once
branch
from
July 23, 2026 21:45
79d1448 to
28c35d4
Compare
Shuwen-Fang
force-pushed
the
sort_index_once
branch
from
July 23, 2026 21:51
28c35d4 to
39bf166
Compare
NuojCheng
reviewed
Jul 24, 2026
| mask = jnp.any(valid_rows_mask.reshape(-1, reduce_group_size), axis=-1) | ||
| return ( | ||
| sorted_by_validity.astype(jnp.int32), | ||
| sorted_by_validity, |
Collaborator
There was a problem hiding this comment.
we need jnp.int32 for accumulation, otherwise got regression issue
NuojCheng
reviewed
Jul 24, 2026
| inputs, | ||
| gate_logits, | ||
| pre_bias_logits, | ||
| pre_bias_logits=None, |
Collaborator
There was a problem hiding this comment.
emmm not clear why make this change
NuojCheng
reviewed
Jul 24, 2026
NuojCheng
left a comment
Collaborator
There was a problem hiding this comment.
- You could add a test protecting your feature.
- Could you add a regression test using flags in here? I would like to make sure our performance is not impacted by this change. Feel free to reduce layers and chip sizes for easier repro.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Optimizes token routing in
ring_ragged_sortduring embedding chunking by eliminating redundantjnp.argsortcalls in both the forward and backward passes.Context & Impact
Performance Impact
How & Why Sort Is Removed
All embedding chunks within a layer share identical token routing (topk_indices_2d and valid_rows_mask), but every embedding chunk repeatedly recomputed the SparseCore valid-row index permutation via jnp.argsort in _preprocess. This PR caches the sorted by validity array by computing it once during the forward pass, then passing it into future bwd passes via custom VJP residuals _ring_ragged_sort_bwd, so that jnp.argsort runs once per layer across all forward and backward chunk ops.
Tests
Unit Test Verification:
test_moe_emb_chunking_random_routingintests/unit/moe_test.pyon TPU (tpu7x-128) on VM:Loss & Perplexity Correctness Run (
num_moe_emb_chunks=4):tmp/run_correctness.sh) comparingmainbranch vssort_index_oncebranch with 4 embedding chunks active:12.258(main) vs12.258(sort_index_once)11.985(main) vs11.985(sort_index_once)11.919(main) vs11.919(sort_index_once)150053.750(main) vs150053.750(sort_index_once)Sort Elimination Verification via Profile:
deepseekv3-fsdp512-ep8-chunked-2) to verify via profile trace thatargsortoverhead in the backward pass was eliminated.Checklist