llama : flash_attn_ext_rows - more efficient unified kv for multi-seq (#29510)

* llama : use llm_attn_mask and kv_rows for unified attention

Assisted-by: dsh:llama.cpp/ggml-org/DeepSeek-V4-Flash-Vision-Exp-GGUF (Mac Studio, M2 Ultra, 192GB)

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>

* cont : optimize `get_seq_cells`

* llama : build the kv_rows lists from per-sequence cell bitmaps

Keep a bitmap over the cells for each sequence (the transpose of `seq`), updated in seq_pos_inc/dec, and build the cell lists by walking its set bits. The lists stay in cell order, cells shared by several sequences go to every list, and nothing has to be rebuilt when cells are removed.

Also leave out of the lists the cells that the SWA hides from every token of the slice (standard and chunked SWA).

* cont : make get_kv_rows_p_min private

* cont : minor metal

* llama : use kv_rows for every ubatch of a unified cache

A ubatch made of equal-length runs with a single sequence each (f.ex. the drafts of several slots) now splits into per-sequence slices.

Any other ubatch (interleaved, uneven or coupled sequences) becomes a single slice over the cells of all its sequences instead of falling back to the dense KQ mask, so the graph keeps the topology of the reserved graph.

* llama : merge get_kv_rows_seq_ids and get_kv_rows_p_min into get_kv_rows_slices

Assisted-by: dsh:llama.cpp/ggml-org/GLM-5.3-Flash-GGUF (Mac Studio, M5 Ultra, 256GB)

---------

Co-authored-by: Georgi Gerganov <ggerganov@gmail.com>
This commit is contained in:
Aman GuptaandGeorgi Gerganov authored and GitHub committed 2026-11-02 18:13:33 +02:00
1 parent 3f8af15a76
commit 1b6935b227
49 files changed
+1207 -333

No files matched your search

+26
View File
@@ -129,6 +129,12 @@ static const llm_fused_op_probe llm_fused_op_flash_attn_probe = {
/*.n_tokens_per_seq =*/ 1,
};
static const llm_fused_op_probe llm_fused_op_flash_attn_kv_rows_probe = {
/*.op =*/ LLM_FUSED_OP_FLASH_ATTN_KV_ROWS,
/*.name =*/ "Flash Attention over K/V rows",
/*.n_tokens_per_seq =*/ 1,
};
static const llm_fused_op_probe llm_fused_op_gdn_ar_probe = {
/*.op =*/ LLM_FUSED_OP_GDN_AR,
/*.name =*/ "fused Gated Delta Net (autoregressive)",
@@ -370,6 +376,19 @@ llama_context::llama_context(
cparams.kv_unified = params.kv_unified;
cparams.moe_cache_size = params.moe_cache_size;
// resolved after flash attention, see resolve_fused_ops()
{
cparams.fused_kv_rows = false;
const char * LLAMA_KV_ROWS_DISABLE = getenv("LLAMA_KV_ROWS_DISABLE");
if (!LLAMA_KV_ROWS_DISABLE) {
cparams.auto_fkvr = cparams.kv_unified;
} else {
LLAMA_LOG_INFO("%s: LLAMA_KV_ROWS_DISABLE set, disabling kv_rows\n", __func__);
cparams.auto_fkvr = false;
}
}
// initialized later
cparams.pipeline_parallel = false;
cparams.training = false;
@@ -662,6 +681,13 @@ void llama_context::resolve_fused_ops(const llama_memory_context_i * mctx, uint3
cparams.auto_fa = false;
}
// the fallback attends to the whole unified cache with a dense mask
if (cparams.auto_fkvr) {
cparams.fused_kv_rows = cparams.flash_attn;
resolve(llm_fused_op_flash_attn_kv_rows_probe, cparams.fused_kv_rows);
cparams.auto_fkvr = false;
}
if (cparams.auto_fgdn) {
LLAMA_LOG_INFO("%s: resolving fused Gated Delta Net support:\n", func);
resolve(llm_fused_op_gdn_ar_probe, cparams.fused_gdn_ar);