Repository navigation
Fix flash/sage varlen prep under torch.compile with dynamic shapes #14568
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
Merged
+61
−11
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e74d38e
Fix flash/sage varlen prep under torch.compile with dynamic shapes
ShivamShrirao f3bfd56
Merge branch 'main' into varlen-compile-fix
ShivamShrirao 2628666
Merge branch 'main' into varlen-compile-fix
ShivamShrirao 669b8cc
Merge branch 'main' into varlen-compile-fix
ShivamShrirao f394ac5
Merge branch 'main' into varlen-compile-fix
ShivamShrirao 0ac4fbf
Handle zero-length sequences in varlen offsets
ShivamShrirao 20c2541
Merge branch 'main' into varlen-compile-fix
ShivamShrirao 0cb80e5
Add varlen dynamic compile test
ShivamShrirao 4642fce
Merge branch 'main' into varlen-compile-fix
ShivamShrirao 8afefba
Remove private helper test
ShivamShrirao 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
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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import contextlib | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
@@ -29,6 +30,8 @@ | |
| is_attention, | ||
| is_context_parallel, | ||
| is_kernels_available, | ||
| is_torch_compile, | ||
| require_torch_accelerator, | ||
| require_torch_multi_accelerator, | ||
| torch_device, | ||
| ) | ||
|
|
@@ -170,3 +173,50 @@ def test_attn_backend_backward_parity(self, cp_type, attention_backend): | |
| f"{attention_backend} {cp_type} gradient `{name}` rel_err={rel:.3e} " | ||
| f"exceeds tol={GRAD_RTOL:.1e}: {rel_errs}" | ||
| ) | ||
|
|
||
|
|
||
| @is_attention | ||
| class TestVarlenAttentionCompile: | ||
| """Op-level tests for the varlen backends under `torch.compile` with dynamic shapes, model independent.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "attention_backend", | ||
| [ | ||
| pytest.param( | ||
| "flash_varlen_hub", | ||
| marks=pytest.mark.skipif(not is_kernels_available(), reason="`kernels` is not available."), | ||
| ), | ||
| pytest.param( | ||
| "_flash_3_varlen_hub", | ||
| marks=pytest.mark.skipif(not is_kernels_available(), reason="`kernels` is not available."), | ||
| ), | ||
| ], | ||
| ) | ||
| @is_torch_compile | ||
| @require_torch_accelerator | ||
| def test_dynamic_shapes(self, attention_backend): | ||
| def make_qkv(seq_len): | ||
| torch.manual_seed(0) | ||
| return tuple(torch.randn(2, seq_len, 4, 64, device=torch_device, dtype=torch.bfloat16) for _ in range(3)) | ||
|
|
||
| with contextlib.ExitStack() as stack, torch.no_grad(): | ||
| try: | ||
| stack.enter_context(attention_backend_ctx(attention_backend)) | ||
| dispatch_attention_fn(*make_qkv(128)) | ||
| except Exception as e: | ||
| pytest.skip(f"Skipping test for backend '{attention_backend}': {e}") | ||
|
|
||
| torch.compiler.reset() | ||
| compiled_attention = torch.compile(dispatch_attention_fn, dynamic=True, fullgraph=True) | ||
| try: | ||
| with ( | ||
| torch._inductor.utils.fresh_inductor_cache(), | ||
| torch._dynamo.config.patch(error_on_recompile=True), | ||
| ): | ||
| for seq_len in (128, 256, 256): | ||
| q, k, v = make_qkv(seq_len) | ||
| torch.testing.assert_close( | ||
| compiled_attention(q, k, v), dispatch_attention_fn(q, k, v), atol=1e-3, rtol=1e-3 | ||
| ) | ||
|
Comment on lines
+212
to
+220
Member
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. Perfect! |
||
| finally: | ||
| torch.compiler.reset() | ||
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.
Why are we changing the return signature? The caller can reuse the
seq_len_qandseq_len_kvright?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.
The shape is unchanged, still three pairs. The third pair was
seqlens.max().item(), which for uniform lengths is the input by construction, so this just skips a device sync per call. It also matters for compile:full((b,), n, dtype=int32).max()with a symbolicnfails inductor codegen underdynamic=Trueontorch 2.12(int32/int64 loop-carried type mismatch in the reduction kernel).