[JAX] SBHD reorder skip uses original shape instead of swapped tensor - #3373
Conversation
Greptile SummaryThis PR corrects the JAX distributed fused-attention test’s SBHD Striped skip guard to derive sequence length from the axis-swapped tensor.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Reviews (5): Last reviewed commit: "Merge branch 'main' into bugfix/test-dis..." | Re-trigger Greptile |
|
@KshitijLakhani, could you please help review this one? Thanks. |
17d7385 to
ecd70cd
Compare
|
@greptile-apps thanks for the review. I addressed the test-coverage gap:
The branch has been amended to a single signed-off commit (ecd70cd) and force-pushed. |
|
The approach is solid. A few observations on the updated test: What works well:
One minor subtlety to be aware of: The core fix ( |
KshitijLakhani
left a comment
There was a problem hiding this comment.
Thanks for this PR @andrewwhitecdw !
Could you please address the review comments ?
I'm happy to launch our CI on this PR after you've addressed those comments. I'll also confirm that the skipped tests for SBHD actually pass in the CI.
NOTE: A passing CI and approved review are required prior to merge
|
|
||
| if reorder_strategy == ReorderStrategy.Striped: | ||
| seq_lens = shape[seq_dim] | ||
| seq_lens = tensor.shape[seq_dim] |
There was a problem hiding this comment.
Good catch !
This change makes sense to me. I would like to think that the main branch basically just skips in the next line due to this incorrect statement (as it would be incorrectly taking the batch value as the seq value) - this explains why our CI never caught this as a failure!
I'd expect those incorrectly skipping tests to not be skipped with this change (and pass)
There was a problem hiding this comment.
I did some digging to figure how many and which tests might be skipping and this is the list. A cursory look at TE's CI from release 2.18 does show that these tests were indeed being skipped.
### L1
Shape: [3, 32, 8, 64]
For SBHD, the real sequence length is 32, but the buggy code reads batch size 3.
Incorrectly skipped:
- cp_size=4, stripe_size=1
- cp_size=8, stripe_size=1
- cp_size=2, stripe_size=4
- cp_size=4, stripe_size=4
- cp_size=8, stripe_size=4
Only cp_size=2, stripe_size=1 currently runs.
Therefore L1 has five incorrect skips.
### L2
Shape: [4, 32, 12, 32]
The buggy code reads 4 instead of sequence length 32.
Incorrectly skipped:
- cp_size=8, stripe_size=1
- cp_size=2, stripe_size=4
- cp_size=4, stripe_size=4
- cp_size=8, stripe_size=4
Shape: [1, 16, 1, 1]
The buggy code reads 1 instead of sequence length 16.
Incorrectly skipped:
- cp_size=2, stripe_size=1
- cp_size=4, stripe_size=1
- cp_size=8, stripe_size=1
- cp_size=2, stripe_size=4
- cp_size=4, stripe_size=4
I'd expect these to not be skipped and passed after @andrewwhitecdw 's changes
|
|
||
| @pytest.mark.parametrize("stripe_size", [1, 4]) | ||
| def test_sbhd_striped_uses_swapped_seq_dim(self, stripe_size): | ||
| """Regression test: SBHD Striped skip must use the swapped sequence dim.""" | ||
| cp_size = 2 | ||
| shape = (1, 16, 1, 1) # original [batch, seq, heads, dim] | ||
| tensor = random.normal(random.PRNGKey(42), shape, dtype=jnp.bfloat16) | ||
| tensor = tensor.swapaxes(0, 1) # SBHD: [seq, batch, heads, dim] | ||
|
|
||
| # Old logic read original shape[0]=1 (batch) and skipped; seq_len after swap is 16. | ||
| reorder = jax.jit(reorder_causal_load_balancing, static_argnums=[1, 2, 3, 4]) | ||
| inverse = jax.jit(inverse_reorder_causal_load_balancing, static_argnums=[1, 2, 3, 4]) | ||
|
|
||
| reordered = reorder(tensor, ReorderStrategy.Striped, cp_size, 0, stripe_size) | ||
| inversed = inverse(reordered, ReorderStrategy.Striped, cp_size, 0, stripe_size) | ||
|
|
||
| assert jnp.array_equal(inversed, tensor) |
There was a problem hiding this comment.
@andrewwhitecdw I do not think this is needed as the tests above do run SBHD.
With your change to correctly get the seq_lens we should be good.
Please remove this:
| @pytest.mark.parametrize("stripe_size", [1, 4]) | |
| def test_sbhd_striped_uses_swapped_seq_dim(self, stripe_size): | |
| """Regression test: SBHD Striped skip must use the swapped sequence dim.""" | |
| cp_size = 2 | |
| shape = (1, 16, 1, 1) # original [batch, seq, heads, dim] | |
| tensor = random.normal(random.PRNGKey(42), shape, dtype=jnp.bfloat16) | |
| tensor = tensor.swapaxes(0, 1) # SBHD: [seq, batch, heads, dim] | |
| # Old logic read original shape[0]=1 (batch) and skipped; seq_len after swap is 16. | |
| reorder = jax.jit(reorder_causal_load_balancing, static_argnums=[1, 2, 3, 4]) | |
| inverse = jax.jit(inverse_reorder_causal_load_balancing, static_argnums=[1, 2, 3, 4]) | |
| reordered = reorder(tensor, ReorderStrategy.Striped, cp_size, 0, stripe_size) | |
| inversed = inverse(reordered, ReorderStrategy.Striped, cp_size, 0, stripe_size) | |
| assert jnp.array_equal(inversed, tensor) |
| monkeypatch.setattr( | ||
| pytest, "skip", lambda reason: pytest.fail(f"unexpected pytest.skip: {reason}") | ||
| ) | ||
| self.test(cp_size, shape, QKVFormat.SBHD, ReorderStrategy.Striped, stripe_size) |
There was a problem hiding this comment.
Re-publishing my comment as it might have become stale as part of an earlier review due to recent commits pushed by @andrewwhitecdw
I do not think this is needed as the original tests in TestReorderCausalLoadBalancing test() above do run SBHD. With your change to correctly get the seq_lens we should be good.
Please remove this:
| self.test(cp_size, shape, QKVFormat.SBHD, ReorderStrategy.Striped, stripe_size) |
ecd70cd to
6c8d7e0
Compare
Use tensor.shape[seq_dim] instead of shape[seq_dim] when deciding whether a Striped SBHD case is large enough. Signed-off-by: andrewwhitecdw <andrewwhitecdw@users.noreply.github.com>
db926a2 to
1217187
Compare
|
@KshitijLakhani Thanks for the review. I removed the extra regression test as requested — the existing parametrized SBHD Striped cases now exercise the corrected skip logic directly. The branch is a single signed-off commit rebased onto main. |
|
/te-ci jax L2 |
…der-skip-uses-original-shape
|
I launched the L1 tests manually as the |
This PR fixes the SBHD Striped skip logic in
tests/jax/test_distributed_fused_attn.pyso it reads the sequence length from the swapped tensor shape rather than the original shape.Changes
tests/jax/test_distributed_fused_attn.py: usetensor.shape[seq_dim]instead ofshape[seq_dim]after the SBHD axis swap.Details
When
qkv_format == QKVFormat.SBHD, the test swaps axes 0 and 1 soseq_dimbecomes 0. The old skip guard readshape[seq_dim], which is the original (unswapped) batch size for SBHD, causing incorrect skips. The fix readstensor.shape[seq_dim], which reflects the swapped sequence length.Tests
TestReorderCausalLoadBalancing.testcases that exercise SBHD inputs.Contributor guidelines