llama.cpp Hexagon flash_attn 改按 KV head 分核,PP 最高提升 58%
llama.cpp 的 Hexagon 後端(b11430)把 flash_attn 從按 Q token 切分改為按 KV head 分核:當 n_kv_heads 能被核心數整除時,每個核心只讀自己那份 KV cache,不再重複讀全量 KV。新行為由 GGML_HEXAGON_FA_HEAD_SPLIT 控制、預設開啟;無法整除時(如 4 核跑 2 個 KV head 的 Gemma-4)回退到原 token 分塊。
首次把 Hexagon 多核 flash_attn 從 token 分塊改為 KV head 分片,給出 4 核 PP 實測增幅與回退條件,對在 Qualcomm NPU 上跑本地推理的開發者有直接參考價值。
原標題:b11430: hexagon: matmul and flash-atten scalability updates (#29974)
閱讀原文
| 評分 | 70 / 70(平均 70,門檻 60) |
| 狀態 | 精選 |
|---|
全文翻譯
hexagon:面向行分割多核的按頭並行 flash_attn 分割槽
在行分割模式下,每個核心計算每個 MUL_MAT 的輸出行分片,但 flash_attn 之前是按 Q token 進行分割槽(扁平 qrow 分割),而不是按頭進行分割槽。這迫使每個核心都要讀取完整的 KV cache(所有 n_kv_heads),抵消了多核在 flash_attn 上的記憶體頻寬優勢。
當 n_kv_heads 能被 n_cores 整除時,將 HMX 和 HVX flash_attn 核心都改為按 KV 頭進行分割槽:核心 i 獨佔處理頭 [i*n_kv_heads/N, (i+1)*n_kv_heads/N),只讀取其對應的 KV cache 頭分片。當 n_kv_heads % n_cores != 0 時(例如 Gemma-4 在 4 核上有 2 個 KV 頭),回退到原始的 token 塊分割。
由 GGML_HEXAGON_FA_HEAD_SPLIT 控制(預設 1 = 開啟)。該標誌被打包進現有 is_dst_fp32 kparams 位元組的第 1 位,以保持在 128 位元組的 kernel_params blob 限制內。
在 4c 行分割下測得的增益(PP t/s,ubatch=1024):
- Qwen3-0.6B:6977 -> 11026(+58%)
- llama-3.2-3B:3717 -> 5522(+49%)
- Qwen3.5-4B:2739 -> 2855(+4%)
- Gemma-4 MoE:無變化(MoE FFN 佔主導,回退路徑)
TG 保持不變(相對於每層的 matmul+barrier 成本,flash_attn 只佔解碼時間的一小部分)。
- hex-fa:清理 kern_params 和 head-split 選擇
- hex-fa:在 run.py 中新增 -fa-head-split 選項
- hex-mdev:更新 matmul 求解器以考慮行分割場景中減少的工作量
- hex-mmid:在多裝置場景中按專家更好地拆分工作
- hex-fa:基於模型/n-hvx/ctx-len 掃描更新 HMX 門控
- hex-fa:在主機上預計算 softcap/scale
- hexagon:將 matmul 展平為 2d 以在多序列中使用 HMX
- hex-mm:清理 kparams 並使用 collapse 進行 3/4D -> 2D 對映
- hex-mm:修復 collapse 回退中的拼寫錯誤
- hex-mm:再次統一 act 張量的命名
- hex-mm:在融合 matmul 中新增對摺疊維度的支援
- hex-build:修復 WoS 建置錯誤
- hex-mm:確保在 can_collapse 中強制 dst stride
- hex-fa:為 head-split 檢查新增一行式提交
- hex-fa:移除未使用的區域性 head_split 變數
- hex-fa:收緊 can_split 檢查
- hex-mm:更新非融合路徑以使用 act 而非 src1
- hex-mm:確保檢查所有 dst 以進行拆分
- hexagon:修復批次 HMX matmul 序言中的第二個權重塊地址
- hexagon:HMX matmul 中的 F16 啟用和 ragged N
- hex-mm:收緊 mdev 情況下的 ragged/split 檢查
- hex-mm:為 F16 啟用啟用 MM 融合
- hex-mm:在融合路徑中將平鋪尺寸傳遞給求解器
- hex-mmid:從專家對映迴圈中移除標量除法
- hex-mmid:為 mdev 拆分實施適當的 cacheline 安全強制
- hex-mm:改進針對 mdev 拆分場景和尾部處理的求解器
- hex-mm:移除冗餘檢查
- hex-mm:修復融合 HMX MUL_MAT_NX 對量化權重丟棄最後部分瓦片的問題
- hex-mm:更好地處理 ragged 形狀(移除 vtcm 的標量 memset)
Co-authored-by: ebateni ebateni@qti.qualcomm.com
Co-authored-by: Jhen-Jie Hong iainst0409@gmail.com
Co-authored-by: Yiwei Shao yiwei@aizip.ai
由 AI 翻譯,以原文為準。
原文
hexagon: head-parallel flash_attn partitioning for row-split multicore
In row-split mode each core computes its output row shard of every
MUL_MAT, but flash_attn was previously partitioning by Q tokens
(flat qrow split) instead of by heads. This forced every core to
read the full KV cache (all n_kv_heads), negating the memory
bandwidth benefit of multicore on flash_attn.
Change both HMX and HVX flash_attn kernels to partition by KV heads
when n_kv_heads is divisible by n_cores: core i processes heads
[i*n_kv_heads/N, (i+1)*n_kv_heads/N) exclusively, reading only its
head shard of the KV cache. Falls back to the original token-block
split when n_kv_heads % n_cores != 0 (e.g. Gemma-4 with 2 KV heads
on 4 cores).
Controlled by GGML_HEXAGON_FA_HEAD_SPLIT (default 1 = on).
The flag is packed into bit 1 of the existing is_dst_fp32 kparams
byte to stay within the 128-byte kernel_params blob limit.
Measured gains at 4c row-split (PP t/s, ubatch=1024):
Qwen3-0.6B: 6977 -> 11026 (+58%)
llama-3.2-3B: 3717 -> 5522 (+49%)
Qwen3.5-4B: 2739 -> 2855 (+4%)
Gemma-4 MoE: no change (MoE FFN dominates, fallback path)
TG is unchanged (flash_attn is a small fraction of decode time
relative to the matmul+barrier cost per layer).
hex-fa: cleanup kern_params and head-split selection
hex-fa: add -fa-head-split option to run.py
hex-mdev: update matmul solver to account for reduced work in row-split scenarios
hex-mmid: better work splitting by expers in multi-dev scenarios
hex-fa: update HMX gating based on the model/n-hvx/ctx-len sweep
hex-fa: precompute softcap/scale on the host
hexagon: flatten matmul into 2d to use HMX in multi-sequence
hex-mm: cleanup kparams and use collapse to 3/4D -> 2D mapping
hex-mm: fix typo in collapse fallback
hex-mm: another pass at consistent naming for act tensors
hex-mm: add support for colapsing dims in fused matmuls
hex-build: fix WoS build errors
hex-mm: make sure to enforce dst stride in can_collapse
hex-fa: add a onliner commit for head-split check
hex-fa: remove unused local head_split var
hex-fa: tighten up can_split checks
hex-mm: update unfused paths to use act instead src1
hex-mm: make sure to check all dsts for splitting
hexagon: fix the second weight chunk address in the batched HMX matmul prologue
hexagon: F16 activation and ragged N in the HMX matmul
hex-mm: tighten the ragged/split checks in mdev cases
hex-mm: enable MM fusion for F16 activations
hex-mm: pass tiled sizes to the solver in fused paths
hex-mmid: remove scalar divs from expert mapping loops
hex-mmid: proper cacheline safety enforcement for mdev splits
hex-mm: improve solver for mdev split scanarios and tail handling
hex-mm: remove redundant checks
hex-mm: fix fused HMX MUL_MAT_NX drops the final partial tile for quantized weights
hex-mm: better handling of ragged shapes (removes scalar memset of vtcm)
Co-authored-by: ebateni ebateni@qti.qualcomm.com
Co-authored-by: Jhen-Jie Hong iainst0409@gmail.com
Co-authored-by: Yiwei Shao yiwei@aizip.ai
相關報導
llama.cpp releases10/5 05:35AI 評分37
llama.cpp 發布 b11402 版本,CUDA 後端改為優先採用整塊(whole-tile)FlashAttention 排程,以提升兩階段核心效率。
llama.cpp releases● 精選10/3 01:28AI 評分68
llama.cpp 合併 PR #29570,在 Metal 後端為 F16 KV 快取加入基於張量 API 的 flash attention 核心。該核心覆蓋 DK=DV=512、DK=576/DV=512、DK=192/DV=128 等配置,並支援 attention sinks、ALiBi 與 logit softcap。改動隨 b11362 建置發布,覆蓋 macOS Apple Silicon、iOS 及 Linux、Windows、Android 等多平台建置。
llama.cpp releases10/2 14:11AI 評分43
llama.cpp 合併 PR #29717,為 Hexagon 後端新增 q2_k 和 q3_k 兩種量化型別支援,並統一了 src1_row_size 的分配方式。該改動由高通工程師 Max Krasnyansky 參與提交,面向在驍龍 Hexagon NPU 上執行本地推理的使用者,相關建置覆蓋 Linux arm64 與 Android arm64 的 Snapdragon CPU、Adreno GPU、Hexagon NPU 組合。
llama.cpp releases10/5 15:50AI 評分42
llama.cpp 發布 b11424 建置版本,修復 Vulkan 後端 Flash Attention 的共享記憶體越界寫問題(#29988)。該版本照例提供 macOS/iOS、Linux、Windows、Android 的預編譯包,涵蓋 Vulkan、CUDA 12/13、ROCm 10.0、OpenVINO、SYCL、OpenCL 等後端,並附驍龍 CPU/Adreno GPU/Hexagon NPU 的安裝指引。
llama.cpp releases10/5 12:46AI 評分36
llama.cpp 發布建置 b11414,本版列出的程式碼改動是 Vulkan 後端修復 prealloc_y 在 flash attention 與 soft_max 之間複用時殘留舊資料的問題(PR #29591),提交標註 Assisted-by: Claude。