Update fake tensor wrapper for flash_attn and falash_attn_varlen_func…#163
Open
sahirema wants to merge 1 commit into
Open
Update fake tensor wrapper for flash_attn and falash_attn_varlen_func…#163sahirema wants to merge 1 commit into
sahirema wants to merge 1 commit into
Conversation
… to make them compatible with torch.compile
There was a problem hiding this comment.
Pull request overview
This PR updates the FlashAttention custom-op fake tensor wrappers (and related tests) to make flash_attn_func / flash_attn_varlen_func compatible with torch.compile, addressing a backward-pass shape mismatch seen during tracing/compilation.
Changes:
- Adjust
_flash_attn_backward*and_flash_attn_varlen_backward*fake wrappers to return shapes compatible with compiled execution (notablysoftmax_dshape handling). - Update backward wrapper return signatures to return
(dq, dk, dv, softmax_d)to better align with the underlying kernel signature. - Extend CK test coverage to run both eager and compiled variants via a new
compiledboolean parameter.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.
| File | Description |
|---|---|
tests/test_flash_attn_ck.py |
Adds compiled parameterization and uses torch.compile(...) when requested. |
flash_attn/flash_attn_interface.py |
Updates backward + fake backward wrappers’ return signatures and fake shape logic for compile compatibility. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+347
to
+348
| flash_func = torch.compile(flash_attn_func) if compiled else flash_attn_func | ||
| out, lse, S_dmask = flash_func( |
Comment on lines
+604
to
+605
| flash_varlen_func = torch.compile(flash_attn_varlen_func) if compiled else flash_attn_varlen_func | ||
| out_unpad, sm_lse, S_dmask = flash_varlen_func( |
Comment on lines
+308
to
+311
| if torch.cuda.is_available() and torch.version.hip: | ||
| softmax_d = torch.empty((batch_size, num_heads, seqlen_q), device=q.device, dtype=torch.float32) | ||
| else: | ||
| softmax_d = torch.empty((batch_size, num_heads, round_multiple(seqlen_q, 128)), device=q.device, dtype=torch.float32) |
Comment on lines
+416
to
+419
| if torch.cuda.is_available() and torch.version.hip: | ||
| softmax_d = torch.empty((batch_size, num_heads, max_seqlen_q), device=q.device, dtype=torch.float32) | ||
| else: | ||
| softmax_d = torch.empty((batch_size, num_heads, round_multiple(max_seqlen_q, 128)), device=q.device, dtype=torch.float32) |
Comment on lines
+312
to
+314
| # dq, dk, dv are already allocated in the fwd pass | ||
| # we are passing them here to match the cpp signature and help torch.compile in infering shape during tracing | ||
| # without this torch.compile will struggels infer the shape of softmax_d |
Comment on lines
+421
to
+423
| # dq, dk, dv are already allocated in the fwd pass | ||
| # we are passing them here to match the cpp signature and help torch.compile in infering shape during tracing | ||
| # without this torch.compile will struggels infer the shape of softmax_d |
| rng_state, | ||
| ) | ||
| return softmax_d | ||
| return dq.clone(), dk.clone(), dv.clone(), softmax_d |
Comment on lines
+381
to
+382
| # return clones else torch.compile will about mutated tensors being returned | ||
| return dq.clone(), dk.clone(), dv.clone(), softmax_d |
| ], | ||
| ) | ||
| @pytest.mark.parametrize("dropout_p", [0.0, 0.17]) | ||
| @pytest.mark.parametrize("compiled", [False, True]) |
| ], | ||
| ) | ||
| @pytest.mark.parametrize("dropout_p", [0.0, 0.17]) | ||
| @pytest.mark.parametrize("compiled", [False, True]) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Updates the fake tensor wrapper function to make
flash_attn_funcandflash_attn_varlen_funccompatible with torch compile. Without the updatetorch.compile(flash_attn_func)throws "wrong number of dimensions" error because the bwd pass returns a 3d tensor when the fake tensor wrapper expects a 2d tensor. The details of the errors are reported in SWDEV-559708, SWDEV-546369, SWDEV-559718.test_flash_attn_outputandtest_flash_attn_varlen_outputare extended to test both the eager and compiled versions of the function with the help of a Boolean parameter calledcompiled. Here are the results of the testMotivation
Technical Details
Test Plan
Test Result
Submission Checklist