Skip to content

Use Flash Attention for RoFormer separation on Vulkan - #1152

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
ruihe774:vulkan-roformer-fa
Oct 31, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
ruihe774:vulkan-roformer-fa

Conversation

@ruihe774

Copy link
Copy Markdown
Contributor

BS-RoFormer and Mel-Band RoFormer now use the same F32-accumulating, view-preserving Flash Attention lowering on Vulkan that CUDA already uses. CPU and other backends keep the explicit attention path.

On Vulkan the explicit path stores the full matrix of attention scores for every axial attention block. ggml-vulkan's FLASH_ATTN_EXT accepts the RoFormer shapes (head dim 64, F32 Q/K/V), and using it cuts both time and memory.

attention_from_heads in src/models/roformer/runtime.cpp now takes the existing ScaledDotProductAttentionModule / FlashPreserveViews / GGML_PREC_F32 branch for BackendType::Vulkan as well as CUDA. No other CUDA-only RoFormer paths (strided head views, packed QK RoPE) are extended. The docs are updated to match.

Results: AMD RX 9070 (RDNA4, RADV), Release, Q8_0 GGUFs. Inputs: a 4.8 min dialogue-over-music clip (test1) and a 5.4 min pop song (test2), 44.1 kHz stereo. Wall clock including load, mean of two warm runs; cold runs were within 0.2 s.

Model Input Overlap Explicit Flash Speedup Peak VRAM
Mel-Band test1 1 11.3 s 8.8 s 1.28× 2806 → 1837 MiB
Mel-Band test1 2 22.6 s 17.3 s 1.31× 2824 → 1854 MiB
Mel-Band test2 1 12.9 s 10.1 s 1.28× 2824 → 1854 MiB
Mel-Band test2 2 25.6 s 19.8 s 1.29× 2822 → 1852 MiB
BS-RoFormer ep368 test1 1 23.5 s 18.2 s 1.29× 3104 → 2217 MiB
BS-RoFormer ep368 test1 4 96.5 s 74.3 s 1.30× 3173 → 2237 MiB
BS-RoFormer ep368 test2 1 26.9 s 21.1 s 1.27× 3109 → 2229 MiB
BS-RoFormer ep368 test2 4 108.7 s 84.5 s 1.29× 3110 → 2233 MiB

VRAM is for the whole card; the desktop uses about 0.8–0.9 GiB at idle.

Output equivalence against the explicit Vulkan path (PCM16): the largest difference is 9–153 steps; the difference sits 54.6–74.1 dB below the signal. ggml-vulkan's flash kernel rounds Q, K, V and the attention weights to F16 and accumulates in F32, as the CUDA kernel does.

Against a CPU explicit-F32 reference (test1, overlap 1), both Vulkan paths are equally close:

  • Mel-Band: explicit 57.9 / 48.1 dB, flash 57.9 / 48.0 dB (vocals / instrumental).
  • BS-RoFormer: explicit 61.4 / 51.6 dB, flash 61.4 / 51.6 dB.

Repeat runs of the flash path are bit-identical.

This PR was implemented with assistance from AI, and I have reviewed the implementation.

BS-RoFormer and Mel-Band RoFormer take the same F32-accumulating,
view-preserving Flash Attention lowering on Vulkan that CUDA already uses;
CPU and other backends keep the explicit attention path.

On an RX 9070 (RADV, Release, Q8_0) both models run about 1.27-1.31x
faster and peak VRAM drops by about 0.9 GiB (BS-RoFormer, 5.4 min song,
overlap 4: 108.7 s -> 84.5 s, 3110 -> 2233 MiB). Output differs from the
explicit path by 55-74 dB below the signal, and both are equally close to
a CPU F32 reference. Intel Vulkan is untested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UYvC1gKw9CahuP7gXNqo5D
@0xShug0
0xShug0 merged commit 7583cdd into 0xShug0:main Oct 31, 2026
6 of 8 checks passed
@0xShug0

0xShug0 commented Oct 31, 2026

Copy link
Copy Markdown
Owner

@ruihe774 Thanks! PR merged.

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.

2 participants