-
Notifications
You must be signed in to change notification settings - Fork 591
Shard the GatedDeltaNet sequence under ici_context_parallelism #4968
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
WandLZhang
wants to merge
2
commits into
AI-Hypercomputer:main
Choose a base branch
from
WandLZhang:gdn-context-parallel
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.
+121
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Context-parallel evaluation of the GatedDeltaNet inter-chunk recurrence. | ||
|
|
||
| The recurrence h_new = A @ h + B is affine in the state, so it composes | ||
| associatively and can be split across a sharded sequence. See | ||
| apply_gdn_context_parallel.py for the derivation and the measurements. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
| from jax import lax | ||
|
|
||
| _PREC = jax.lax.Precision.HIGHEST | ||
|
|
||
|
|
||
| def compose(left, right): | ||
| """(A_r, B_r) . (A_l, B_l) = (A_r @ A_l, A_r @ B_l + B_r).""" | ||
| A_l, B_l = left | ||
| A_r, B_r = right | ||
| return ( | ||
| jnp.matmul(A_r, A_l, precision=_PREC), | ||
| jnp.matmul(A_r, B_l, precision=_PREC) + B_r, | ||
| ) | ||
|
|
||
|
|
||
| def compose_local(w, u, k, g): | ||
| """Fold this device's chunks into one affine map, in O(1) memory. | ||
|
|
||
| lax.scan rather than associative_scan on purpose: associative_scan would | ||
| materialise (A, B) and their running composition for every chunk, which is | ||
| 17 GB per device at a million tokens and defeats the point. The parallelism | ||
| that matters here is across devices, not within one. | ||
| """ | ||
| k_dim = k.shape[-1] | ||
| eye = jnp.eye(k_dim, dtype=jnp.float32) | ||
|
|
||
| # jax.checkpoint is required here. lax.scan keeps whatever | ||
| # the body computes as a backward residual, so A_i and B_i get stacked over | ||
| # every chunk even though the forward pass only ever needs one at a time. At | ||
| # sequence 262,144 with ctx=4 that was 103 GB of f32[1024,4,16,128,128] in the | ||
| # buffer dump. A_i and B_i are cheap to rebuild from w, u, k and g, which are | ||
| # already live, so recompute them in the backward pass instead of storing them. | ||
| @jax.checkpoint | ||
| def body(carry, x): | ||
| w_c, u_c, k_c, g_c = x | ||
| g_last = g_c[..., -1] | ||
| decay = jnp.exp(g_last)[..., None, None] | ||
| k_g = k_c.astype(jnp.float32) * jnp.exp(g_last[..., None] - g_c)[..., None] | ||
| k_g_T = k_g.swapaxes(-1, -2) | ||
| A_i = decay * eye - jnp.matmul(k_g_T, w_c.astype(jnp.float32), precision=_PREC) | ||
| B_i = jnp.matmul(k_g_T, u_c.astype(jnp.float32), precision=_PREC) | ||
| return compose(carry, (A_i, B_i)), None | ||
|
|
||
| lead = w.shape[1:-2] | ||
| init = ( | ||
| jnp.broadcast_to(eye, lead + (k_dim, k_dim)).astype(jnp.float32), | ||
| jnp.zeros(lead + (k_dim, u.shape[-1]), jnp.float32), | ||
| ) | ||
| (A_loc, B_loc), _ = lax.scan(body, init, (w, u, k, g)) | ||
| return A_loc, B_loc | ||
|
|
||
|
|
||
| def incoming_state(A_loc, B_loc, h_init, cp_axis): | ||
| """State entering this device, plus the final state after all devices. | ||
|
|
||
| Gathering D pairs of small matrices is the only cross-device traffic in the | ||
| scheme. Must be called inside a shard_map over `cp_axis`. | ||
| """ | ||
| A_all = lax.all_gather(A_loc, cp_axis, axis=0, tiled=False) | ||
| B_all = lax.all_gather(B_loc, cp_axis, axis=0, tiled=False) | ||
| A_cum, B_cum = lax.associative_scan(compose, (A_all, B_all), axis=0) | ||
|
|
||
| idx = lax.axis_index(cp_axis) | ||
| prev = jnp.maximum(idx - 1, 0) | ||
| carried = jnp.matmul(A_cum[prev], h_init, precision=_PREC) + B_cum[prev] | ||
| h_in = jnp.where(idx == 0, h_init, carried) | ||
| final_h = jnp.matmul(A_cum[-1], h_init, precision=_PREC) + B_cum[-1] | ||
| return h_in, final_h | ||
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
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.
In
qwen3.py,cp_axisis passed as a tuple of strings (e.g.,("context",)or("context", "context_usp_ulysses")). However,lax.axis_indexonly accepts a single string axis name, and calling it with a tuple will raise aTypeErrorat runtime. Additionally, if there are multiple context parallel axes,lax.all_gatherandlax.associative_scanneed to be handled sequentially and flattened to correctly compute the 1D associative scan across the multi-dimensional device grid.This suggestion generalizes
incoming_stateto support both a single string and a tuple of strings forcp_axis, gathers sequentially, flattens the gathered dimensions in the correct major-to-minor sequence order, and computes the correct flat device index.