Skip to content

[DSV4] Run CSA through Attention Gym gather_attn - #4855

Merged
drisspg merged 1 commit into
mainfrom
drisspg/dsv4-csa-selected-attention
Sep 29, 2026
Merged

drisspg merged 1 commit into
mainfrom
drisspg/dsv4-csa-selected-attention

Conversation

@drisspg

@drisspg drisspg commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Human notes

Step 1 we port over CSA to use the gather_attn impl; supports triton and routes to FA4 cute on B2/300 machines.

full e2e speedups in top of stack

Agent Notes

CompressedSparseAttention (compress_ratio == 4) no longer concatenates
[sliding-window KV, compressed KV, sink] and builds a FlexAttention BlockMask
from the indexer top-k. It calls attn_gym.sparse.selected_attention, which
fuses the sliding-window branch, the per-query top-k branch over the
compressed KV, and the sink softmax denominator. The fused impl picks CuTe
(FA4) for supported eager SM100/SM103 shapes and Triton otherwise; CPU uses
the reference impl. The CSA branch and indexer arguments leave
DSV4FlexInnerAttention._forward_impl, which now serves sliding-window and HCA
layers only.

The GPU test keeps an inline copy of the previous BlockMask formulation and
checks outputs and gradients match it at the default and a non-default
softmax_scale. A second test runs DSV4-shaped CSA (64 heads, head_dim 512,
bf16) through the FA4 CuTe kernels against the fp32 reference implementation
and fails if CuTe is unavailable. The B200 lane runs this file, since the GPU
unit-test lane (A10G) cannot reach the CuTe path.

Based on #4452 by @floatingtrees.

Co-authored-by: Jonathan Zhou [email protected]


Stack created with GitHub Stacks CLI • Give Feedback 💬

@drisspg
drisspg added this pull request to stack #4857 September 24, 2026 00:25
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 24, 2026
@drisspg
drisspg force-pushed the drisspg/dsv4-csa-selected-attention branch 2 times, most recently from 7c34c5e to b23ee41 Compare September 24, 2026 01:05
@drisspg
drisspg force-pushed the drisspg/dsv4-csa-selected-attention branch 2 times, most recently from ad0b755 to b88792d Compare September 24, 2026 02:24
@drisspg
drisspg force-pushed the drisspg/dsv4-csa-selected-attention branch from b88792d to 2b412e6 Compare September 24, 2026 03:20
@drisspg drisspg changed the title [DSV4] Run CSA through Attention Gym selected_attention [DSV4] Run CSA through Attention Gym gather_attn Sep 24, 2026
@drisspg
drisspg force-pushed the drisspg/dsv4-csa-selected-attention branch from 2b412e6 to 67af3c9 Compare September 24, 2026 03:59
Base automatically changed from drisspg/dsv4-deps to main September 24, 2026 14:28
@drisspg
drisspg force-pushed the drisspg/dsv4-csa-selected-attention branch from 67af3c9 to 6701f5b Compare September 24, 2026 14:28
@drisspg
drisspg marked this pull request as ready for review September 24, 2026 18:07
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 24, 2026
@drisspg

drisspg commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

Im going to do 1 more bump of attn-gym before landing since we I just got to tune tile sizes for a10g: meta-pytorch/attention-gym#593

CompressedSparseAttention (compress_ratio == 4) no longer concatenates
[sliding-window KV, compressed KV, sink] and builds a FlexAttention BlockMask
from the indexer top-k. It calls attn_gym.sparse.gather_attn, which
fuses the sliding-window branch, the per-query top-k branch over the
compressed KV, and the sink softmax denominator. The fused impl picks CuTe
(FA4) for supported eager SM100/SM103 shapes and Triton otherwise; CPU uses
the reference impl.

Since CSA no longer calls FlexAttention, it moves off DSV4FlexInnerAttention
onto a new DSV4InnerAttention(InnerAttention) base that owns only the fields
gather_attn needs (window_size, compress_ratio, softmax_scale, index_topk) and
the gather_attn call itself. The CSA branch and indexer arguments leave
DSV4FlexInnerAttention._forward_impl, which now serves sliding-window and HCA
layers only.

A test checks the wiring (layouts, window, top-k indices, sink, and scale)
against dense masked attention, forward and backward, on CPU (reference impl,
fp64) and CUDA (fused impl, fp32). It lives under tests/unit_tests/gpu so the
GPU unit-test job runs both cases. Kernel numerics are Attention Gym's to
test.

Bumps attn-gym to 0.0.14, whose Triton gather_attn tiles fit SM86 shared memory
(the A10G CI lane runs the Triton backend) and Blackwell tensor memory.

Based on #4452 by @floatingtrees.

Co-authored-by: Jonathan Zhou <[email protected]>
@drisspg
drisspg force-pushed the drisspg/dsv4-csa-selected-attention branch from 6701f5b to c82a9c1 Compare September 28, 2026 22:22
@drisspg
drisspg merged commit a475ccd into main Sep 29, 2026
27 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/docker ciflow/fake-pg Run 1-GPU Fake PG integration tests CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants