Repository navigation
llama: use f16 mask for FA to save VRAM - #23764
Conversation
|
This stacked along with reserving only |
JohannesGaessler
left a comment
There was a problem hiding this comment.
This seems like the right approach to me. For non-FA configurations the mask is still FP32 but since the mask size scales linearly with context depth while the size of the KQ matrix scales quadratically it should not make a difference. For most models we would not even need FP16 since the mask values will just be either 0 or -inf. Or we could even calculate the mask values from indices (but which gets complicated a lot for multiple concurrent contexts). But these further optimizations would be a lot more invasive, require a lot of effort to implement properly, and only make sense if with this PR the mask would still be the largest tensor in the compute graph.
|
@am17an since you wrote:
Does that mean there will be more changes or is this PR ready for review? |
|
You can review, I just didn't like the extra llama_mask function |
JohannesGaessler
left a comment
There was a problem hiding this comment.
Looking at the code for creating the mask again, I think it can be simplified a bit more with this patch:
diff --git a/src/llama-graph.cpp b/src/llama-graph.cpp
index 92e8d0d29..1b976eb8e 100644
--- a/src/llama-graph.cpp
+++ b/src/llama-graph.cpp
@@ -396,8 +396,9 @@ void llm_graph_input_attn_no_cache::set_input(const llama_ubatch * ubatch) {
const int64_t n_kv = ubatch->n_tokens;
const int64_t n_tokens = ubatch->n_tokens;
- const auto fill_mask_inner = [&](auto * data, int n_swa, llama_swa_type swa_type) {
+ const auto fill_mask = [&](auto * data, int64_t ne, int n_swa, llama_swa_type swa_type) {
using T = std::remove_reference_t<decltype(*data)>;
+ std::fill(data, data + ne, llama_cast<T>(-INFINITY));
for (int i1 = 0; i1 < n_tokens; ++i1) {
const llama_seq_id s1 = ubatch->seq_id[i1][0];
const llama_pos p1 = ubatch->pos[i1];
@@ -426,39 +427,27 @@ void llm_graph_input_attn_no_cache::set_input(const llama_ubatch * ubatch) {
data[idst + i0] = llama_cast<T>(hparams.use_alibi ? -std::abs(p0 - p1) : 0.0f);
}
}
- };
-
- const auto fill_mask = [&](ggml_tensor * mask, int n_swa, llama_swa_type swa_type) {
- GGML_ASSERT(mask);
- GGML_ASSERT(ggml_backend_buffer_is_host(mask->buffer));
-
- if (mask->type == GGML_TYPE_F16) {
- ggml_fp16_t * data = (ggml_fp16_t *) mask->data;
-
- std::fill(data, data + ggml_nelements(mask), llama_cast<ggml_fp16_t>(-INFINITY));
-
- fill_mask_inner(data, n_swa, swa_type);
-
- if (debug) {
- print_mask(data, n_tokens, n_kv, n_swa, swa_type);
- }
- } else {
- float * data = (float *) mask->data;
-
- std::fill(data, data + ggml_nelements(mask), -INFINITY);
-
- fill_mask_inner(data, n_swa, swa_type);
-
- if (debug) {
- print_mask(data, n_tokens, n_kv, n_swa, swa_type);
- }
+ if (debug) {
+ print_mask(data, n_tokens, n_kv, n_swa, swa_type);
}
};
- fill_mask(self_kq_mask, 0, LLAMA_SWA_TYPE_NONE);
+ GGML_ASSERT(self_kq_mask);
+ GGML_ASSERT(ggml_backend_buffer_is_host(self_kq_mask->buffer));
+ if (self_kq_mask->type == GGML_TYPE_F16) {
+ fill_mask((ggml_fp16_t *) self_kq_mask->data, ggml_nelements(self_kq_mask), 0, LLAMA_SWA_TYPE_NONE);
+ } else {
+ fill_mask((float *) self_kq_mask->data, ggml_nelements(self_kq_mask), 0, LLAMA_SWA_TYPE_NONE);
+ }
if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
- fill_mask(self_kq_mask_swa, hparams.n_swa, hparams.swa_type);
+ GGML_ASSERT(self_kq_mask_swa);
+ GGML_ASSERT(ggml_backend_buffer_is_host(self_kq_mask_swa->buffer));
+ if (self_kq_mask_swa->type == GGML_TYPE_F16) {
+ fill_mask((ggml_fp16_t *) self_kq_mask_swa->data, ggml_nelements(self_kq_mask_swa), 0, LLAMA_SWA_TYPE_NONE);
+ } else {
+ fill_mask((float *) self_kq_mask_swa->data, ggml_nelements(self_kq_mask_swa), 0, LLAMA_SWA_TYPE_NONE);
+ }
}
}From upstream PR #23764. Avoids ggml_cast in graph builder. Our kernel already reads masks as half — no changes needed.
* origin/master: vocab : support tokenizer for LFM2.5-8B-A1B (ggml-org#23826) graph : ensure DS32 kq_mask_lid is F32 (ggml-org#23864) server: remove obsolete scripts (ggml-org#23870) ci : update macos release to use macos-26 runner (ggml-org#23878) download: add option to skip_download (ggml-org#23059) mtmd: Add DeepSeekOCR 2 Support (ggml-org#20975) CUDA: Check PTX version on host side to guard PDL dispatch (ggml-org#23530) server: bump timeout to 3600s (ggml-org#23842) model : support for DeepseekV32ForCausalLM with generic DeepSeek Sparse Attention (DSA) implementation (ggml-org#23346) llama: use f16 mask for FA to save VRAM (ggml-org#23764) sync : ggml ggml : bump version to 0.13.1 (ggml/1523) ngram-mod : Add missing include (ggml-org#23857) llama: add llm_graph_input_mtp (ggml-org#23643) app : move licences to llama-app (ggml-org#23824) cuda : disables launch_fattn PDL enrollment due to compiler bug (ggml-org#23825) meta : Add missing `buffer` set in allreduce fallback !COMPUTE clear (ggml-org#23480)
* llama: use f16 mask for FA * review: add llama_cast + formatting * simplify
…026-07-12) 4 parallele Subagents (Vulkan/AMD, CUDA/MoE, arXiv, Multi-GPU/Batching) konsolidiert zu 25 neuen Items in ROADMAP: Tier 1 Quick Wins (3 neu): - AtomicBot-ai#31 K-Quant A-Matrix Transpose CM1 (PR ggml-org#22970, +5-15% PP auf Mars) - AtomicBot-ai#32 Pascal L1 Cache Tuning (-Xptxas -dlcm=ca, Styx) - AtomicBot-ai#33 Per-Quant MMVQ/MMQ Batch Threshold (AMD MFMA, Mars/Venus) Tier 2 (7 neu): - AtomicBot-ai#34 UBBoost, AtomicBot-ai#35 Row-Packing DMMV, AtomicBot-ai#36 Auto Param Fitting TP - AtomicBot-ai#37 LFRU Expert Caching, AtomicBot-ai#38 Conf-KV, AtomicBot-ai#39 Talon, AtomicBot-ai#40 MoE Load Balancing Tier 3 (5 neu): - AtomicBot-ai#41 GRKV, AtomicBot-ai#42 CapKV, AtomicBot-ai#43 SliderQuant, AtomicBot-ai#44 Alloc-MoE, AtomicBot-ai#45 CUDA Streams QKV Tier 4 (10 neu): - AtomicBot-ai#46-55: SpecMD, QUICK, FluxMoE, STAR-KV, VQKV, CompilerKV, SliceMoE, MoBiE, DASH-Q, GOOSE 7 PRs als bereits im Fork identifiziert (nicht erneut vorschlagen): ggml-org#21472, ggml-org#23764, ggml-org#22299, ggml-org#21611, ggml-org#22423, ggml-org#18749, Warp Shuffle, Constant Memory
* llama: use f16 mask for FA * review: add llama_cast + formatting * simplify
* llama: use f16 mask for FA * review: add llama_cast + formatting * simplify
With flash attention on, the attention mask was filled as f32 and cast to f16 inside the graph. This ports upstream ggml-org/llama.cpp#23764 (031ddb2e08): the KV cache fills an f16 mask directly, and the f32 mask and its cast are gone. The mask takes half the memory, and the graph makes no extra pass over it. This matters most when a long prompt runs in one ubatch, because the mask holds keys times queries: at 27k tokens the f32 mask was 3 GB, plus a 1.5 GB f16 copy. On a Vulkan device whose bindings stop at 1 GiB (Intel GPUs), the cast of a larger mask could not run on the GPU, so it ran on the CPU from about 16k tokens. - `llama_kv_cache::set_input_kq_mask` fills an f32 or an f16 mask; the fill is templated on the element type. - Every mask is allocated in the type the attention reads, and every cast to f16 is dropped: the KV cache masks (the sliding-window and hybrid ones included), the no-cache and cross-attention masks, which also fill f16 now, and the DeepSeek-V4.1 raw mask, which upstream also allocates as f16 (2ed3c1abbb). Validation: - The attention reads the same mask as before. The mask holds only 0, -inf or an ALiBi distance; f16 represents 0 and -inf exactly, and the fill rounds an ALiBi distance the way the cast rounded it. - `ctest -L ci` passes on an Apple M5 (Metal). - Qwen3.6-35B-A3B on an Apple M5 (16 GB), measured on a branch with this change and #347: 2k- and 6k-token prompts answer with the same word as main's, and their prompt eval takes as long as main's (2073 tokens: 4.04 s, main 4.03 s; 6168 tokens: 17.98 s, main 18.42 s). - DeepSeek-V4.1-Flash Q8_0 on an RTX 5090 Laptop GPU (CUDA), one cold request per launch, 523 prompt tokens and 256 output tokens, with this change and #347. Both runs of each build fall in the same spread: first token 36.6 and 38.4 s with main, 37.2 and 38.2 s with the changes; decode 1.05 and 0.84 tok/s with main, 0.91 and 0.88 tok/s with the changes.
Overview
Currently we reserve the KQ mask in f32 even if FA is used, which is then is converted to f16 while passing to backends. The f32 mask still uses the compute buffer even though is not used, taking up extra VRAM. This PR reserves the kq-mask in f16. This provides 1.2GB of VRAM saving at
-ub 2048and ~300Mb at-ub 512when using MTPAdditional information
Requirements