[JAX] Add sqrtsoftplus router score function - #3448
Conversation
Signed-off-by: Jeremy Berchtold <jberchtold@nvidia.com>
|
/te-ci jax |
Greptile SummaryThe PR exposes the native sqrtsoftplus router score function through the JAX and Flax MoE APIs.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[JAX or Flax MoE caller] --> B[Validate sqrtsoftplus score function]
B --> C[JAX FFI router primitive]
C --> D[Native sqrtsoftplus scoring]
D --> E[Top-k routing and weights]
E --> F[Expert dispatch]
D -. dense scores .-> G[Optional auxiliary loss]
Reviews (2): Last reviewed commit: "Merge branch 'main' into jberchtold/jax-..." | Re-trigger Greptile |
| 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) |
There was a problem hiding this comment.
not sure why we are adding this?
| score_function=score_function, | ||
| ) | ||
| return jnp.sum(p) | ||
| return jnp.sum(p * grad_weights) |
| @pytest_parametrize_wrapper("group_topk", GROUP_TOPK_OPTIONS) | ||
| @pytest_parametrize_wrapper("scaling_factor", SCALING_FACTOR_OPTIONS) | ||
| @pytest_parametrize_wrapper("enable_bias", ENABLE_BIAS_OPTIONS) | ||
| @pytest.mark.triton |
There was a problem hiding this comment.
This is not just in your PR but I saw a lot of mark.triton on this file and it actually does not use triton here (fused router is a CUDA kernel not triton kernel so I can make anoother PR to remove all the wrong marks)
| def test_sqrtsoftplus_score_function_enum(): | ||
| from transformer_engine.jax.router import ScoreFunction | ||
|
|
||
| logits = make_logits(128, 32, "sqrtsoftplus") | ||
| string_fn = jax.jit( | ||
| partial(fused_topk_with_score_function, topk=4, score_function="sqrtsoftplus") | ||
| ) | ||
| enum_fn = jax.jit( | ||
| partial( | ||
| fused_topk_with_score_function, | ||
| topk=4, | ||
| score_function=ScoreFunction.SQRTSOFTPLUS, | ||
| ) | ||
| ) | ||
|
|
||
| string_probs, string_routing_map = string_fn(logits) | ||
| enum_probs, enum_routing_map = enum_fn(logits) | ||
| assert jnp.array_equal(string_probs, enum_probs) | ||
| assert jnp.array_equal(string_routing_map, enum_routing_map) |
There was a problem hiding this comment.
This does not seem to be needed to test between enum and string to be passed into score_function
| num_groups=num_groups, | ||
| group_topk=group_topk, | ||
| scaling_factor=scaling_factor, | ||
| score_function="sqrtsoftplus", |
There was a problem hiding this comment.
we can also just pytest parameterize the score_function, what do you think?
Description
Expose sqrtsoftplus routing to TE/JAX
Type of change
Changes
Checklist: