Skip to content

[CuTe, Flex] Allow score mod use in varlen backward#2547

Open
reubenconducts wants to merge 12 commits into
Dao-AILab:mainfrom
reubenconducts:rstern/varlen-score-mod-bwd
Open

[CuTe, Flex] Allow score mod use in varlen backward#2547
reubenconducts wants to merge 12 commits into
Dao-AILab:mainfrom
reubenconducts:rstern/varlen-score-mod-bwd

Conversation

@reubenconducts

@reubenconducts reubenconducts commented May 7, 2026

Copy link
Copy Markdown
Contributor

Lifts the current restriction on score_mod being used in FA-4 backward with variable sequence length. Supersedes PR #2544.

I added some examples of backwards score mods that use globally-indexed aux tensors to showcase the API.

cc @drisspg @v0i0

@reubenconducts reubenconducts marked this pull request as draft May 8, 2026 17:00
@reubenconducts reubenconducts marked this pull request as ready for review May 8, 2026 18:35
score_mod_bwd = utils.create_softcap_scoremod_bwd(softcap)
if score_mod is not None:
assert score_mod_bwd is not None, "score_mod_bwd is required when score_mod is provided"
assert cu_seqlens_q is None and cu_seqlens_k is None, (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

we have full varlen bwd coverage right? I havnt thought about deterministic for instance

@reubenconducts reubenconducts Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We don't have blocksparse varlen bwd yet (i.e. it isn't plumbed up at all), but I didn't guard against this in the interface. I'll add an assertion.

Comment thread tests/cute/test_score_mod_varlen.py
Comment thread tests/cute/test_flash_attn.py
),
mCuBlockIdxOffsets=(
blocksparse_tensors.cu_block_idx_offsets if blocksparse_tensors is not None else None
blocksparse_tensors.cu_block_idx_offsets

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the SM90 bwd score_mod partials should be rebuilt after the per-batch fastdiv_mods recompute. Right now score_mod_fn / score_mod_bwd_fn capture (via partial) the original fastdiv_mods before the work-tile loop, so reassigning fastdiv_mods inside the loop only reaches mask_fn, not the score_mod paths. SM90 fwd constructs its score_mod partial after the recompute; can we mirror that here?

The code section is above but we shoudl probs test on hopper before land

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, and tested on Hopper. Two failures due to small numerical error one seqlen x score mod config.

FAILED tests/cute/test_score_mod_varlen.py::test_varlen_score_mod_backward_with_global[True-score_mod_tuple0-seqlens_q23-seqlens_k23-128-dtype0] - AssertionError: dK CuTE err 9.96e-02 exceeds 3*PT err 3.12e-02 + 1.00e-03
FAILED tests/cute/test_score_mod_varlen.py::test_varlen_score_mod_backward_with_global[False-score_mod_tuple0-seqlens_q23-seqlens_k23-128-dtype0] - AssertionError: dK CuTE err 9.96e-02 exceeds 3*PT err 3.12e-02 + 1.00e-03
============================================ 2 failed, 11746 passed, 9372 skipped, 8712 xfailed, 1540 warnings in 289.50s (0:04:49)

@reubenconducts reubenconducts force-pushed the rstern/varlen-score-mod-bwd branch from 342d620 to b311c04 Compare June 5, 2026 00:03
@jasonkrone

jasonkrone commented Jul 2, 2026

Copy link
Copy Markdown

Any sense for prio of full varlen bwd coverage ? This may be critical for one of my use cases. If it's not a prioritized follow-up, I may work on a PR to add that functionality once this lands.

@reubenconducts reubenconducts force-pushed the rstern/varlen-score-mod-bwd branch from b311c04 to cafa779 Compare July 7, 2026 19:20
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.

3 participants