-
Notifications
You must be signed in to change notification settings - Fork 816
[JAX] Add sqrtsoftplus router score function #3448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jberchtold-nvidia
wants to merge
4
commits into
NVIDIA:main
Choose a base branch
from
jberchtold-nvidia:jberchtold/jax-sqrtsoftplus-router
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eacb4b5
[JAX] Add sqrtsoftplus router score function
jberchtold-nvidia 5b51cf6
Address comments
jberchtold-nvidia e1b1c7b
Merge branch 'main' into jberchtold/jax-sqrtsoftplus-router
jberchtold-nvidia 0bd2ef0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ def _inject_router(request): | |
| jax.config.update("jax_use_shardy_partitioner", True) | ||
|
|
||
| from test_fused_router import ( | ||
| reference_topk_softmax_sigmoid, | ||
| reference_topk_with_score_function, | ||
| reference_compute_scores_for_aux_loss, | ||
| reference_aux_loss, | ||
| make_logits, | ||
|
|
@@ -86,7 +86,6 @@ def _inject_router(request): | |
| } | ||
|
|
||
|
|
||
| @pytest.mark.triton | ||
| class TestDistributedFusedTopk: | ||
| """Test distributed execution of fused_topk_with_score_function. | ||
|
|
||
|
|
@@ -133,7 +132,7 @@ def target_fwd(x): | |
|
|
||
| logits_shards = jnp.reshape(logits, (num_dp_devices, local_num_tokens, num_experts)) | ||
| ref_fwd_fn = jax.jit( | ||
| lambda x: reference_topk_softmax_sigmoid( | ||
| lambda x: reference_topk_with_score_function( | ||
| x, | ||
| topk=topk, | ||
| score_function=score_function, | ||
|
|
@@ -160,21 +159,23 @@ def target_fwd(x): | |
| ), "Routing map mismatch in distributed fused_topk" | ||
|
|
||
| # === Backward === | ||
| grad_weights = jnp.linspace(0.5, 1.5, num_experts, dtype=jnp.float32)[None, :] | ||
|
|
||
| def target_loss(x): | ||
| p, _ = fused_topk_with_score_function( | ||
| x, | ||
| topk=topk, | ||
| score_function=score_function, | ||
| ) | ||
| return jnp.sum(p) | ||
| return jnp.sum(p * grad_weights) | ||
|
|
||
| def ref_chunk_loss(x_chunk): | ||
| p, _ = reference_topk_softmax_sigmoid( | ||
| p, _ = reference_topk_with_score_function( | ||
| x_chunk, | ||
| topk=topk, | ||
| score_function=score_function, | ||
| ) | ||
| return jnp.sum(p) | ||
| return jnp.sum(p * grad_weights) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
|
||
| target_grad = jax.jit(jax.grad(target_loss))(logits_sharded) | ||
|
|
||
|
|
@@ -195,7 +196,7 @@ def ref_chunk_loss(x_chunk): | |
| "num_tokens,num_experts,topk", | ||
| TOPK_CASES, | ||
| ) | ||
| @pytest.mark.parametrize("score_function", ["softmax", "sigmoid"]) | ||
| @pytest.mark.parametrize("score_function", ["softmax", "sigmoid", "sqrtsoftplus"]) | ||
| def test_distributed_topk( | ||
| self, | ||
| device_count, | ||
|
|
@@ -219,7 +220,6 @@ def test_distributed_topk( | |
| ) | ||
|
|
||
|
|
||
| @pytest.mark.triton | ||
| class TestDistributedScoreForAuxLoss: | ||
| """Test distributed execution of fused_topk_with_score_function with compute_aux_scores=True. | ||
|
|
||
|
|
@@ -293,22 +293,24 @@ def target_fwd(x): | |
| ), "Routing map mismatch in distributed score_for_aux_loss" | ||
|
|
||
| # === Backward === | ||
| grad_weights = jnp.linspace(0.5, 1.5, num_experts, dtype=jnp.float32)[None, :] | ||
|
|
||
| def target_loss(x): | ||
| s, _ = fused_topk_with_score_function( | ||
| x, | ||
| topk=topk, | ||
| score_function=score_function, | ||
| compute_aux_scores=True, | ||
| ) | ||
| return jnp.sum(s) | ||
| return jnp.sum(s * grad_weights) | ||
|
|
||
| def ref_chunk_loss(x_chunk): | ||
| _, s = reference_compute_scores_for_aux_loss( | ||
| x_chunk, | ||
| topk=topk, | ||
| score_function=score_function, | ||
| ) | ||
| return jnp.sum(s) | ||
| return jnp.sum(s * grad_weights) | ||
|
|
||
| target_grad = jax.jit(jax.grad(target_loss))(logits_sharded) | ||
|
|
||
|
|
@@ -329,7 +331,7 @@ def ref_chunk_loss(x_chunk): | |
| "num_tokens,num_experts,topk", | ||
| TOPK_CASES, | ||
| ) | ||
| @pytest.mark.parametrize("score_function", ["softmax", "sigmoid"]) | ||
| @pytest.mark.parametrize("score_function", ["softmax", "sigmoid", "sqrtsoftplus"]) | ||
| def test_distributed_score_for_aux_loss( | ||
| self, | ||
| device_count, | ||
|
|
@@ -353,7 +355,6 @@ def test_distributed_score_for_aux_loss( | |
| ) | ||
|
|
||
|
|
||
| @pytest.mark.triton | ||
| class TestDistributedMoEAuxLoss: | ||
| """Test distributed execution of fused_moe_aux_loss. | ||
|
|
||
|
|
||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why we are adding this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added to ensure we're testing the backward pass with varied inputs. Before this change, we had varied forward
logitsinput. But we were just callingjax.gradon this function for the backwardSince the output is an unweighted sum, the incoming gradient will be all 1s. So we're only testing the backward with constant 1 input grad
This weighting is mocking a more realistic scenario where the following layer has non-constant incoming gradients in the backward pass. I'd be okay with using randomly initialized values here too instead of linspace
This is an issue in our other tests that use
meanorsumtoo, but for this PR I'm fixing only these tests.