Repository navigation
fix(fa4): reuse compiled masks across variable-length batches - #8
Merged
Merged
Conversation
Longxmas
force-pushed
the
fix/fa4-dynamic-mask-cache
branch
from
November 9, 2026 17:50
83499da to
f12df6c
Compare
zhouaihui
self-requested a review
November 10, 2026 02:07
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.
Outcome
Variable-length DSpark/DFlash BSHD training with FA4 repeatedly compiled custom attention kernels when the context length or anchor count changed. The same geometry already reused its kernel within one process.
There were two independent cache-key problems:
This change moves row geometry to runtime auxiliary metadata and bounds, and gives the modifier a stable hash covering the complete factory source and every remaining semantic specialization. It also forwards FA4's opt-in persistent-cache settings to Ray actors while retaining node-local cache paths for static multi-node workers. FA4's dtype/head/tile/broadcast and singleton-stride ABI distinctions remain intact.
Reproduction and before/after comparison
Environment: an SM90 machine, Python 3.12.13, PyTorch 2.13.0+cu130, CUDA 13.0, FA4 4.0.0b34, CUTLASS DSL 4.7.1; compiler and ptxas optimization level 3. Both versions used the same GPU and synthetic BF16 inputs, with persistent caching enabled. Each version started with separate empty caches, then reused its cache in a second process.
Geometry: batch 1, Q/KV heads 4/1, head dimension 128, block size 7, sparse FA4. Each process runs
(context length, anchor blocks)=(512,32), (512,32), (640,32), (640,32), (512,32), (640,40), (640,40).(512,32)(512,32)in the same process(640,32)(640,40)(512,32)The seven-call sequences invoked
cute.compile9 / 6 times in baseline processes one/two, versus 5 / 0 times after the fix. Modifier hashes now match across lengths and processes.These are operator-level compilation/cache diagnostics. Wall time covers synchronized forward and backward; compilation counts and CPU time are measured by wrapping
cute.compile. All observed outputs and gradients were finite. End-to-end training speedup is not measured here.Reproduce the regression
From the PR branch, run the same tests against the unchanged baseline production code and the fix:
Verification
row geometry recompiled an existing FA4 dispatch family.git diff --check: passed.Runtime validation is on SM90 with the versions above. Other GPU architectures and end-to-end training/serving performance are not measured by this PR.
Compatibility and provenance
Model parameters, attention masks, losses, data selection, checkpoint formats, and default persistent-cache enablement are unchanged. The per-forward context-boundary tensor uses the existing metadata-cache lifetime. Persistent reuse remains explicitly enabled with
FLASH_ATTENTION_CUTE_DSL_CACHE_ENABLED=1andFLASH_ATTENTION_CUTE_DSL_CACHE_DIR.This modifies ASpec's retained NVIDIA/Megatron-derived adapter; existing source headers and license attribution are preserved. No dependency changes or additional upstream implementation copies are introduced.