Repository navigation
Conversation
Assisted-by: ChatGPT
Assisted-by: ChatGPT
Assisted-by: ChatGPT
Assisted-by: ChatGPT
…decode on vec Assisted-by: Claude
|
@am17an could you check perf on cuda, the changes to flash_attn_mask_to_sparse_indices should be positive there too but i cant test. |
8652d5d to
dc3cf0e
Compare
|
Force push was for: rebase on latest master. |
| } | ||
|
|
||
| #ifdef GGML_USE_HIP | ||
| bool ggml_cuda_flash_attn_ext_tile_shall_use_sparse(const int cc, const ggml_tensor * dst, const int DKQ, const int DV) { |
There was a problem hiding this comment.
The function ggml_cuda_flash_attn_ext_tile_shall_use_sparse() decides whether sparse Flash Attention should be used, but it does not check logit_softcap but ggml_cuda_flash_attn_ext_tile_may_use_sparse() explicitly disables sparse execution when logit_softcap is enabled.
This creates an inconsistency:
- The kernel selector chooses
BEST_FATTN_KERNEL_TILE, expecting to use sparse attention. - The tile launcher rejects the sparse variant because logit_softcap != 0.
- The dense tile kernel runs instead, potentially bypassing a faster MFMA/WMMA implementation.
Which lead to possible performance regression for models using bothlogit_softcapandn_kv_max > 0.
Maybe we should add something like this:
float max_bias = 0.0f;
+ float logit_softcap = 0.0f;
memcpy(&max_bias, (const float *) dst->op_params + 1, sizeof(float));
+ memcpy(&logit_softcap, (const float *) dst->op_params + 2, sizeof(float));
if (!mask || mask->ne[0] != n_kv || mask->ne[1] < Q->ne[1] ||
- mask->ne[2] != 1 || dst->src[5] || n_kv_max <= 0 || max_bias != 0.0f) {
+ mask->ne[2] != 1 || dst->src[5] || n_kv_max <= 0 || max_bias != 0.0f || logit_softcap != 0.0f) {
return false;
}Also we do not have test-backend-ops test with logit_softcap != 0 (they are all logit_softcap = 0), we should add one maybe
| } | ||
| warp_item_offset += ggml_cuda_popc(selected_warp[item]); | ||
| const int i_t = i0 + tid*cpt; | ||
| uint64_t sel = ((const sel_t *) sel_lds)[tid]; |
There was a problem hiding this comment.
sel_lds is defined as a uint8_t array but later used as uint64_t or uint16_t
should do something like
- uint64_t sel = ((const sel_t *) sel_lds)[tid];
+ sel_t sel_bits;
+ memcpy(&sel_bits, sel_lds + tid*nload, sizeof(sel_bits));
+ uint64_t sel = sel_bits;|
Measured this PR's sparse tile kernel on NVIDIA Pascal (Tesla P40, sm_61) by ungating the sparse path for pre-Volta CUDA. Patch (~150 lines on top of this branch): pascal-sparse.patch What the patch changes:
Correctness: all sparse Perf on Tesla P40, fp32 tile kernel, 256/256 gqa 12 f16 KV, n_kv_max=2048 — speedup vs dense at the same shape:
No crossover below the 25% selection cap even at nb=64. Fraction sweep at kv=32768 confirms the low end: 58x at 0.8% selection, 27x at 3.1%, 10.9x at 12.5%, 6.1x at 25% for nb=1; at nb=8 still 18.7x / 6.8x / 2.2x / 1.14x — a win even at n_gather = 2*n_kv, so no gather-count rule is needed beyond the 25% cap. Quantized K/V (q8_0, n_kv_max=1024): the existing DSA/MLA shapes with the gqa gates dropped (f16 KV, nb=1, kv=49152):
The GCN gqa>=16/32 gates for 512/512 and 576/512 were tuned for GCN's LDS-heavy gather; on Pascal the gathered rows stream at full memory bandwidth and low-gqa sparse wins big, so the patch drops those gates for NVIDIA. The kernels are arch-neutral (index gather + ballot compaction only), so this is a straight ungate + thresholds. Happy to fold it into this PR or send it as a follow-up — whichever you prefer. |
|
Two additions to the Pascal enablement posted above: 1. Interaction with #30792 (native q8_0 KV tile FA): both PRs rewrite 2. Reusable break-even test cases: my Pascal tuning was driven by a set of dense/sparse paired perf cases added to |
|
I'm trying this (#32741 with #30792 merged with the "merge reference" gist) on 2x RX6800 on Qwen 3.8 Flash-Next (Linux, ROCm v7.2.3, layer split mode, no pipeline parallelism, no MTP - I can provide more details if something in particular is important).
It does not look related to running out of memory because I've made sure to have more than enough headroom, and each one works on its own but not both of them. I don't think I could have messed up the merge since the patch applied properly, but just to make sure - and for anyone else who wants to try this but has trouble figuring out how to apply the "merge reference" - here is what I got: https://github.com/dark-penguin/llama.cpp/tree/cherrypick/q8_0_x_sparse_fattn Qwen 3.8-27B seems to be unaffected. Logs |
Overview
optimizes the masking kernel and enables and tunes the sparse path in the tile kernel for RDNA2, RDNA3, RDNA4
masking kernel now dose 16 byte loads, keeps more loads in flight, row_count is kept in registers and the data for compaction is first staged trough lds.
This pr is based on and must be merged after #30370
Additional information
perf cdna mi100.log
perf rdna3 rx7900xtx.log
perf rdna4 r9700.log
test-backend-ops perf patch used to generate the attached perf logs fattn-sparse-baseline mentioned in the logs is master with this patch applied
Requirements