[CuTe,Sm100] Varlen Dynamic Persistent scheduler and metadata#2559
Open
reubenconducts wants to merge 18 commits into
Open
[CuTe,Sm100] Varlen Dynamic Persistent scheduler and metadata#2559reubenconducts wants to merge 18 commits into
reubenconducts wants to merge 18 commits into
Conversation
…sm100, update tests to use scheduler metadata
…for non-varlen blocksparse batch sorting
…namicPersistent schedulers
28faa92 to
5a69571
Compare
horakka5
added a commit
to horakka5/flash-attention
that referenced
this pull request
May 28, 2026
…d, use shared utility Addresses @reubenconducts's May 22 review comments on Dao-AILab#2520: 1. Rename mTileCumsum -> mCuTotalMBlocks across all 9 kernels + scheduler + interface for consistency with the convention introduced in Dao-AILab#2224 (already in main; used by blocksparse, and by Dao-AILab#2559). 2. Drop num_head (and the related pack_gqa arch-conditional remap) from the host cumsum. Per-batch cumsum is now pure m_blocks; the scheduler handles num_head separately. Removes the SM80/SM120 vs SM90/100/110/MLA branching that previously mirrored the pack_gqa_layout reshape behavior. 3. Replace the inline binary search in _varlen_coord_map's cumsum-on branch with a call to utils.get_batch_from_cu_tensor (the shared utility from Dao-AILab#2556). The existing snap-to-group-boundary + warp-scan structure is preserved — the cumsum serves as a hint to skip ahead, and the warp-scan refines to the exact batch using _get_num_m_blocks (which already handles pack_gqa, q_stage, cluster, etc.). This matches the scheduler-side approach in Dao-AILab#2559. The pack_gqa seqlen multiplier stays in _compute_cu_total_m_blocks so that per-batch m_block counts match the kernel's _get_num_m_blocks formula — the snap is forward-only, so under-estimating per-batch counts is safe but over-estimating (which dropping the multiplier would cause when pack_gqa is on) would land the snap past the correct batch and the warp-scan couldn't recover. Verified on SM100: - 72 new tests (test_varlen_scheduler_binary_search_correctness{,_bwd}): pass - existing test_varlen (B=20 slice, 576 cases): pass - existing test_flash_attn_mla_absorbed_varlen (480 cases): pass
…onfig method; remove cluster_size==1 restriction; guard architectures against unused scheduler metadata args
2e55d75 to
3416514
Compare
drisspg
reviewed
Jun 2, 2026
| learnable_sink: Optional[cute.Tensor] = None, | ||
| blocksparse_tensors: Optional[BlockSparseTensors] = None, | ||
| aux_tensors=None, | ||
| mCuTotalMBlocks: Optional[cute.Tensor] = None, |
Collaborator
There was a problem hiding this comment.
nit: can we turn this into a namedTuple for keepign them colocated?
Contributor
Author
There was a problem hiding this comment.
It's a bit tricky. This is what SchedulerMetadataTensorsTorch is for, but this PR doesn't add general scheduler metadata to the non-sm100 kernels.
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
| return out, lse | ||
|
|
||
|
|
||
| def _get_scheduler_metadata( |
Collaborator
There was a problem hiding this comment.
you think some of this should live inteh scheduler file?
Contributor
Author
There was a problem hiding this comment.
I think it's best left in interface, for example to hint that tile size selection needs to agree.
drisspg
reviewed
Jun 2, 2026
drisspg
reviewed
Jun 2, 2026
| ) | ||
| ) | ||
| nheads_in_l2 = min(nheads_in_l2, self.num_head) | ||
| mh_in_l2 = nheads_in_l2 * num_m_blocks |
Collaborator
There was a problem hiding this comment.
how different is this swizziling (havent read) should it be shared between LPT or at least parts?
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.
This PR adds the
VarlenDynamicPersistentScheduler, seen in FA3, to FA4.prepare_scheduler_metadata.pyFlashPrepareSchedulerkernel:num_m_blocks_ptrto precompute number of m blocks for the lpt sort (unused on SM100)num_splits_dynamic_ptr: holdsnum_splitsfor each sequence in a batch, which can vary in mixed workloadsvirtual_batch_idx_ptr: unused currently; used to permute sequences according to "virtual" batch indices, e.g. when sorting a batch for load balancing (to be utilized in a subsequent PR)num_nheads_in_l2_ptr: used for head swizzle computation in the tile schedulertile_count_semaphore: zeroed out here, used in main kernel for dynamic persistenceFlashPrepareKernel, or pass in pre-computed cached metadata to amortize cost across layers.tile_scheduler.pyso that dynamic persistent schedulers (CLC and "traditional", like that in this PR) share methods and so that varlen schedulers (SingleTileandDynamicPersistent) reuse common methods.Additionally, adds in a
mCuTotalMBlockstensor to be used by theSingleTileVarlenSchedulerto perform a binary search for the current batch. This is due to @horakka5 in #2520, with an additionalmCuTotalSplitsMBlockspointer used jointly to deduce dynamic num splits, when appropriate.Comprehensive performance numbers are attached. The regressions seen (e.g. with large batch, small seqlen) are attributable mainly to the fact that the combine kernel does not early-exit for 1-split sequences. We see that "traditional" dynamic persistent outperforms CLC (to which I've also wired up the appropriate metadata tensors) almost always. It's worth noting that in the few tests where prepare kernel latency appears extreme, a large proportion of that latency comes from torch.empty (known issue on Grace CPUs; ostensibly fixed with cuda 13.x, but I ran on 12.x).
varlen_dynamic_scheduler_perf.txt
To-do in follow-up PRs:
FlashPrepareKernel. This is proven to have an enormous impact on mixed prefill/decode workloads (including with CLC, from my preliminary testing). (In addition to this varlen case, batch sort will be helpful for load balancing with blocksparsity.)