Skip to content

[Gemma4][Attention] Enable Gemma4 FA4 on SM90 with head-dim 512 support - #42019

Merged
BBuf merged 9 commits into
sgl-project:mainfrom
cognaiger9:feat/fa4-gemma4-sm90
Oct 10, 2026
Merged

BBuf merged 9 commits into
sgl-project:mainfrom
cognaiger9:feat/fa4-gemma4-sm90

Conversation

@cognaiger9

@cognaiger9 cognaiger9 commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Enable opt-in FlashAttention 4 serving for Gemma4 on Hopper GPUs.
Gemma4 combines sliding-window attention with head dimension 256 and global attention with head dimension 512. The existing SM90 FA4 implementation does not support the latter, preventing Gemma4 from using this backend.

Modifications

  • Add SM90 support for equal Q/K and V head dimensions of 512, including tile selection, shared-memory layout, pipeline stages, and paged KV handling.
  • Add a generalized SM90 decode split heuristic that avoids splitting short KV spans when sufficient query/head blocks are available.
  • Allow Gemma4 to select --attention-backend fa4; retain Triton as the default.
  • Add compact image-range metadata and an FA4 mask callback for image-token bidirectional attention. Image spans crossing prefill chunks retain the existing Triton causal fallback.
  • Extend the existing test_flash_attention_4.py with SM90 regression coverage.

Accuracy Tests

Kernel correctness

Passed 2 test methods covering 14 subcases on H200

GSM8K

Backend Correct / Total Accuracy Request errors Truncated outputs
FA4 1,282 / 1,319 97.19% 0 0
Triton 1,282 / 1,319 97.19% 0 0

Evaluation configuration:

  • Model: RedHatAI/gemma-4-31B-it-FP8-block
  • 2 × H200 per backend, TP2 / DP1
  • FP8 block-quantized weights; BF16 activations and KV cache
  • sgl-eval==0.1.2, full GSM8K test set, one pass
  • Temperature 0, top-p 1, seed 42, thinking disabled
  • Maximum output length 2,048; concurrency 32

Speed Tests and Profiling

Measured using sglang.bench_serving with the same model and hardware configuration as the accuracy evaluation.

Common serving settings:

--tp-size 2
--dp-size 1
--dtype bfloat16
--kv-cache-dtype auto
--context-length 16384
--chunked-prefill-size 8192
--max-running-requests 64
--cuda-graph-max-bs-decode 64
--mem-fraction-static 0.85
--disable-radix-cache

Benchmark settings:

  • Random dataset with tokenized prompts and fixed input/output lengths.
  • Concurrency: 1, 8, 32, 64.
  • Requests per cell: max(32, 8 × concurrency).
  • Eight warmup requests per cell.
  • Request rate: unlimited.
  • Random-length ratio: 1; seed: 42.
  • Cache flushed before each cell.

Output throughput

Input tokens Output tokens Concurrency FA4 tokens/s Triton tokens/s FA4 difference
1,024 1,024 1 103.98 105.90 −1.8%
1,024 1,024 8 766.58 796.51 −3.8%
1,024 1,024 32 2,055.35 2,128.23 −3.4%
1,024 1,024 64 3,016.26 3,004.31 +0.4%
8,192 128 1 80.70 63.30 +27.5%
8,192 128 8 239.46 155.32 +54.2%
8,192 128 32 296.35 179.95 +64.7%
8,192 128 64 321.47 183.79 +74.9%
8,192 1,024 1 98.93 86.92 +13.8%
8,192 1,024 8 541.64 493.13 +9.8%
8,192 1,024 32 973.71 865.93 +12.4%
8,192 1,024 64 1,368.74 978.21 +39.9%

Median TTFT

Time to first token, in milliseconds.

Input / Output tokens Concurrency FA4 (ms) Triton (ms) Change
1024 / 1024 1 79.69 75.54 +5.5%
1024 / 1024 8 305.95 374.94 -18.4%
1024 / 1024 32 759.00 945.21 -19.7%
1024 / 1024 64 1,388.40 1,686.95 -17.7%
8192 / 128 1 342.67 656.01 -47.8%
8192 / 128 8 1,590.93 3,119.63 -49.0%
8192 / 128 32 4,182.48 7,765.26 -46.1%
8192 / 128 64 3,217.28 5,934.14 -45.8%
8192 / 1024 1 343.91 657.20 -47.7%
8192 / 1024 8 1,593.54 3,038.94 -47.6%
8192 / 1024 32 4,195.92 7,891.17 -46.8%
8192 / 1024 64 3,240.89 5,892.62 -45.0%

Median TPOT

Time per output token, excluding the first token, in milliseconds.

Input / Output tokens Concurrency FA4 (ms) Triton (ms) Change
1024 / 1024 1 9.54 9.36 +2.0%
1024 / 1024 8 10.16 9.68 +4.9%
1024 / 1024 32 14.76 14.11 +4.6%
1024 / 1024 64 19.88 19.63 +1.3%
8192 / 128 1 9.76 10.75 -9.2%
8192 / 128 8 21.07 27.84 -24.3%
8192 / 128 32 75.95 117.86 -35.6%
8192 / 128 64 174.47 303.29 -42.5%
8192 / 1024 1 9.77 10.88 -10.2%
8192 / 1024 8 13.22 13.27 -0.4%
8192 / 1024 32 28.80 29.27 -1.6%
8192 / 1024 64 43.56 59.63 -27.0%

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): 🚫 Run #37921815088
Latest PR Test (Extra): ❌ Run #37921814430
Latest PR Test (AMD ROCm 10): ❌ Run #37921814854

@cognaiger9 cognaiger9 changed the title [Gemma4] Support FA4 attention backend [Gemma4][Attention] Enable Gemma4 FA4 on SM90 with head-dim 512 support Oct 1, 2026
@cognaiger9
cognaiger9 marked this pull request as ready for review October 1, 2026 10:03
or self.use_mla
or self.fa_skip_kv_cache
or metadata.swa_spec_metadata is not None
or metadata is self.full_cg_prefill_metadata

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.

Could we check this before graph replay? Full prefill graph replay skips forward_extend(), so this guard never runs. A graph captured without images would still use causal attention for image requests, even after image_token_ranges is set. Falling back to eager for image batches would avoid this.

@cognaiger9 cognaiger9 Oct 3, 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.

I’ve modified the code so that prefill batches containing images fall back to eager mode in full-graph backend

@SamMausberg

SamMausberg commented Oct 3, 2026 •

Copy link
Copy Markdown

One thing on the paged_kv.py line: at tile_n 144, max(1, n_block_size // num_threads) leaves each thread one page-table entry short. A ceil-divide, as in Dao-AILab/flash-attention#2745 and #35757, covers that and still gives 1 at this PR's tile_n 64.

head_dim 80 and 96 get tile_n 144 when there is no causal or local masking. There the formula gives each thread 1 page-table entry, as the floor did before this PR. But the loader's last pass reads a second: the one load_page_table would fill for rows 128-143.

I evaluated both forms on CPU at every SM90 tile this PR's _tile_size_fwd_sm90 picks. That covers head_dim 16-256 in steps of 16, and 512, each non-causal, causal and local. max(1, ...) is short only at head_dim 80 and 96, and the ceil-divide is never short.

That part is code reading plus the CPU check. My GPU runs of this form at tile_n 144 give wrong output, but they also hit a separate bug in the loader's partial last pass (Dao-AILab/flash-attention#2957, fix in Dao-AILab/flash-attention#2958). So they don't isolate the missing entry. That bug also means the ceil-divide alone doesn't make tile_n 144 correct. On SGLang main, head_dim 80 and 96 are still wrong with it and correct with both changes. My follow-up on #35757 has the runs and the vendored-copy diff.

@cognaiger9
cognaiger9 force-pushed the feat/fa4-gemma4-sm90 branch from 1508bfe to 05e7c12 Compare October 3, 2026 14:06
@cognaiger9

Copy link
Copy Markdown
Contributor Author

One thing on the paged_kv.py line: at tile_n 144, max(1, n_block_size // num_threads) leaves each thread one page-table entry short. A ceil-divide, as in Dao-AILab/flash-attention#2745 and #35757, covers that and still gives 1 at this PR's tile_n 64.

head_dim 80 and 96 get tile_n 144 when there is no causal or local masking. There the formula gives each thread 1 page-table entry, as the floor did before this PR. But the loader's last pass reads a second: the one load_page_table would fill for rows 128-143.

I evaluated both forms on CPU at every SM90 tile this PR's _tile_size_fwd_sm90 picks. That covers head_dim 16-256 in steps of 16, and 512, each non-causal, causal and local. max(1, ...) is short only at head_dim 80 and 96, and the ceil-divide is never short.

That part is code reading plus the CPU check. My GPU runs of this form at tile_n 144 give wrong output, but they also hit a separate bug in the loader's partial last pass (Dao-AILab/flash-attention#2957, fix in Dao-AILab/flash-attention#2958). So they don't isolate the missing entry. That bug also means the ceil-divide alone doesn't make tile_n 144 correct. On SGLang main, head_dim 80 and 96 are still wrong with it and correct with both changes. My follow-up on #35757 has the runs and the vendored-copy diff.

@SamMausberg thanks for the detailed analysis, I’ve changed the allocation to ceiling division and confirmed on an H200 that it alone does not fix tile_n 144. Are you working on fixing the loader's partial last pass in SGLang?

@SamMausberg

SamMausberg commented Oct 3, 2026 •

Copy link
Copy Markdown

@cognaiger9 No, not yet. Would you like me to, or should someone else handle it? The fix is in Dao-AILab/flash-attention#2958, which is waiting for review. If you want tile_n 144 correct, the diff is in #35757 (comment). It's a few lines in paged_kv.py. If it's easiest, you're welcome to use it.

@cognaiger9
cognaiger9 force-pushed the feat/fa4-gemma4-sm90 branch from 7a0f3c8 to aaa04e1 Compare October 4, 2026 02:26
@cognaiger9

Copy link
Copy Markdown
Contributor Author

@SamMausberg got it, i just folded the diff in my PR and it has passed tile_n 144

@BBuf BBuf added run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) labels Oct 6, 2026
@BBuf

BBuf commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

@cognaiger9 Please fix the conflict

@cognaiger9

Copy link
Copy Markdown
Contributor Author

@BBuf i just resolved

@BBuf

BBuf commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4.py test/registered/attention/unittests/dense/test_fa4.py test/registered/attention/test_flash_attention_4.py test/registered/unit/model_executor/test_prefill_cuda_graph_runner.py

Targeted coverage for this PR:

  • FA4 kernel correctness on Hopper and Blackwell, including the new SM90 head-dim 256/512 paged-attention and image-mask cases.
  • FA4 attention-backend regression coverage, including CUDA Graph and speculative-decoding paths.
  • Existing FA4 end-to-end GSM8K and EAGLE3 regression tests on B200.
  • Prefill CUDA Graph CPU tests, including the new multimodal fallback / DP-vote regression.

The existing B200 end-to-end tests use Qwen models; they do not replace Gemma4 FA4 end-to-end multimodal validation.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-h100 (2 tests): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4.py
cd test/ && python3 registered/attention/unittests/dense/test_fa4.py

🚀 4-gpu-b200 (3 tests): ❌ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4.py
cd test/ && python3 registered/attention/unittests/dense/test_fa4.py
cd test/ && python3 registered/attention/test_flash_attention_4.py

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/model_executor/test_prefill_cuda_graph_runner.py

# Conflicts:
#	test/registered/unit/model_executor/test_prefill_cuda_graph_runner.py
@cognaiger9

Copy link
Copy Markdown
Contributor Author

Hi @BBuf , the failure in run 37569883960 isn’t from this PR. That rerun-test was pinned to the old head 0ce316a, which had main’s qv mValue arg mismatch from sgl-project/sglang#42239. It was fixed in sgl-project/sglang#42926, and the current head 2ef8ad8 includes it

@BBuf

BBuf commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4.py test/registered/unit/model_executor/test_prefill_cuda_graph_runner.py

Merged current main and resolved the FA4 wrapper/test conflicts in 3fb0f12. Both image mask_mod and output-buffer out support are retained, together with the main-branch workspace regressions. Pre-commit and four CPU wrapper-forwarding checks passed; GPU numerical validation is requested above.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4.py

🚀 4-gpu-b200 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4.py

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/model_executor/test_prefill_cuda_graph_runner.py

@BBuf
BBuf merged commit 3767878 into sgl-project:main Oct 10, 2026
163 of 217 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants