Skip to content

Update fake tensor wrapper for flash_attn and falash_attn_varlen_func…#163

Open
sahirema wants to merge 1 commit into
ck_tile/fa3from
torch_compile_fix
Open

Update fake tensor wrapper for flash_attn and falash_attn_varlen_func…#163
sahirema wants to merge 1 commit into
ck_tile/fa3from
torch_compile_fix

Conversation

@sahirema

Copy link
Copy Markdown

Updates the fake tensor wrapper function to make flash_attn_func and flash_attn_varlen_func compatible with torch compile. Without the update torch.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_output and test_flash_attn_varlen_output are extended to test both the eager and compiled versions of the function with the help of a Boolean parameter called compiled. Here are the results of the test

  • Reference results in Eager mode
    • flash_attn_func : 42240 passed, 1 warning
    • flash_attn_varlen_func: 46464 passed, 1 warning
  • Compiled FA results without the patch
    • flash_attn_func : 10 failed, 42230 passed, 1 warning
    • flash_attn_varlen_func: 10 failed, 46454 passed, 1 warning
  • Compiled FA results with patch
    • flash_attn_func : 42240 passed, 1 warning
    • flash_attn_varlen_func: 46464 passed, 1 warning

Motivation

Technical Details

Test Plan

Test Result

Submission Checklist

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (notably softmax_d shape 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 compiled boolean 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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants