From f9e7af6e723cc2e1e156ad754e75845e186f09cd Mon Sep 17 00:00:00 2001 From: Lumpiasty Date: Mon, 23 Nov 2026 10:39:12 +0100 Subject: [PATCH] ggml-vulkan: enable flash-attn mask_opt for GCN head size 256, note why shmem staging stays off (F2) Squashed from 2 fork commits (original subjects and messages below). ---- ggml-vulkan: enable flash-attn mask_opt for GCN large head sizes (was 2b5e1962d) mask_opt was disabled on AMD GCN for head sizes <= 256, but it is beneficial there in high-context prefill: on fully-visible causal blocks it skips the per-block mask load+add, and it skips fully-masked blocks entirely. The attention op on GCN is compute-bound on the softmax path (no matrix cores), so this cuts real work. Verified lossless (perplexity bit-identical with it on vs off) and a measured prefill win on Qwen3.5-35B-A3B (head_dim 256) on an RX 580: pp2048 unchanged at short context, +8% @ 16k, +12% @ 32k, growing with depth. Enable for GCN when HSK/HSV >= 256; the existing large-mask conditions keep it off for decode. ---- vulkan: record why flash-attn shmem staging stays off for GCN (was 5c5af7b39) Staging K/V through shared memory looks like an obvious win on GCN: without it each rowgroup re-reads the whole K/V block from global memory, and with row_split 4 that pulls one 16KB block through a 16KB vector L1 four times. Measured, it loses: -6.7% pp2048 at depth 16k and -7.4% at 32k on Polaris at head size 128. The kvsh stride of D/4+1 dwords is 4 mod 32, which costs an 8-way LDS bank conflict on wave64 - the +1 padding is tuned for warp32 - and the extra shared memory eats occupancy this shader is already short of. Comment only, no behaviour change. Leaving a note so the next person does not spend a GPU on it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ --- ggml/src/ggml-vulkan/ggml-vulkan.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ggml/src/ggml-vulkan/ggml-vulkan.cpp b/ggml/src/ggml-vulkan/ggml-vulkan.cpp index 60cebe494..8ff376a3a 100644 --- a/ggml/src/ggml-vulkan/ggml-vulkan.cpp +++ b/ggml/src/ggml-vulkan/ggml-vulkan.cpp @@ -1256,6 +1256,10 @@ static vk_fa_tuning_params get_fa_tuning_params_scalar(const vk_device& device, result.d_split = std::min(std::min(result.subgroup_size, 8u), D_lsb / 4); + // Staging K/V through shared memory is a loss on AMD GCN: measured -6.7% at depth 16k and + // -7.4% at 32k on Polaris (head size 128). The kvsh stride of D/4+1 dwords is 4 mod 32, which + // costs an 8-way LDS bank conflict on wave64, and the extra shared memory cuts into occupancy + // that this shader is already short of. Keep it to NVIDIA. result.shmem_staging = (device->vendor_id == VK_VENDOR_ID_NVIDIA && hsk < 256 && hsv < 256) ? 1 : 0; if (!reduce_block_rows && result.block_rows > 1 && !ggml_vk_flash_attn_scalar_shmem_support(device, result, hsk, hsv, f32acc, k_type, v_type)) { @@ -9200,8 +9204,10 @@ void ggml_vk_flash_attn(ggml_backend_vk_context * ctx, vk_context& subctx, const } // Only use mask opt when the mask is fairly large. This hasn't been tuned extensively. + // GCN with large head size (>= 256) benefits in high-context prefill (skipping the + // per-block mask add on fully-visible blocks dominates), so enable it there too. bool use_mask_opt = mask && !use_sparse && nem1 >= 32 && nem0 * nem1 > 32768 && nem0 >= tuning_params.block_cols * 16 - && (ctx->device->architecture != vk_device_architecture::AMD_GCN || HSK > 256 || HSV > 256); + && (ctx->device->architecture != vk_device_architecture::AMD_GCN || HSK >= 256 || HSV >= 256); vk_fa_pipeline_state fa_pipeline_state = get_fa_pipeline_state(ctx->device, tuning_params, HSK, HSV, aligned, f32acc, mask != nullptr, use_mask_opt, logit_softcap != 0, use_sparse, k_type_eff, v_type_eff);