[JAX]: SBHD reorder skip uses original shape instead of swapped tensor - #3373
Conversation
Greptile SummaryThis PR corrects the SBHD Striped test guard to inspect the reordered tensor’s sequence dimension and adds regression coverage for the corrected branch.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains. Important Files Changed
Reviews (2): Last reviewed commit: "fix: SBHD reorder skip uses original sha..." | Re-trigger Greptile |
|
@KshitijLakhani, could you please help review this one? Thanks. |
- Use tensor.shape[seq_dim] instead of shape[seq_dim] when deciding whether a Striped SBHD case is large enough. - Update the regression test to invoke the parametrized test method directly so the skip branch is actually exercised. Signed-off-by: Andrew White <andrewwhitecdw@users.noreply.github.com>
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) |
This PR addresses the following issue in
tests/jax/test_distributed_fused_attn.py: SBHD reorder skip uses original shape instead of swapped tensor.Changes
tests/jax/test_distributed_fused_attn.py: SBHD reorder skip uses original shape instead of swapped tensor.Details
Tests
tests/jax/test_distributed_fused_attn.pyGreptile feedback addressed
test_sbhd_striped_uses_swapped_seq_dimto invoke the parametrizedtest(...)method directly with SBHD format, so the corrected skip logic is actually exercised. A monkeypatchedpytest.skipturns an unexpected skip into a test failure.Local verification:
python3 -m py_compile tests/jax/test_distributed_fused_attn.pypassed. Full pytest execution was not feasible because JAX is not installed in this environment.