Skip to content

cuda + llama : zero-copy host-mapped sparse main KV offloading - #33774

Open
MacarPeng wants to merge 3 commits into
ggml-org:masterfrom
MacarPeng:feat/sparse-kv-mapped-offload
Open

MacarPeng wants to merge 3 commits into
ggml-org:masterfrom
MacarPeng:feat/sparse-kv-mapped-offload

Conversation

@MacarPeng

Copy link
Copy Markdown

Overview

This PR adds --kv-offload-sparse (llama_context_params::kv_offload_sparse) and -cti / --cache-type-idx (llama_context_params::type_idx) to keep sparse-attention indexer keys and recurrent/DeltaNet state on GPU VRAM while placing only the large sparse-attention main KV cache in per-layer zero-copy host-mapped pinned memory (CUDA<dev>_Mapped). On 8-16 GiB consumer GPUs, this removes the main KV VRAM bottleneck for hybrid sparse-attention models like qwen4exp (QSA) and unlocks 256K-512K contexts with full CUDA Graph decode acceleration instead of OOMing or falling back to slow CPU attention via -nkvo.

During single-token decode, qwen4exp emits a compact kv_rows list (<= 2,080 rows per sparse layer, padded to FATTN_KQ_STRIDE = 256) and ggml-cuda performs a single 1x coalesced PCIe gather into a small (~2 MiB) VRAM scratch buffer before running flash_attn_ext_vec. This avoids the 7.6x slowdown caused by GQA Q-heads repeatedly reading zero-copy mapped host memory across PCIe (which bypasses GPU L2 on Windows WDDM), while keeping decode 100% CUDA Graph compatible.

For mapped prefill, ggml_cuda_flash_attn_ext_get_alloc_size skips the permanent full-context F16 scratch reservation (saving 512-1024 MiB VRAM at 256K-512K), and ggml_cuda_flash_attn_ext_mapped_prefill compacts and dequantizes only the live sparse KV rows per query group into a transient VRAM F16 tile (supporting F32, F16, BF16, F8_E4M3, Q4_0, Q4_1, Q5_0, Q5_1, Q8_0). Backends or devices without zero-copy mapped buffer support fall back to regular device VRAM with a single warning, and HIP/MUSA builds are guarded cleanly.

Additional information

This work is complementary to open PRs #33682 (CUDA sparse flash-attention decode for q8_0) and #31802 (fused KV dequant in MMA kernels), which optimize VRAM-resident attention throughput rather than VRAM capacity offload.

Hardware microbenchmark: direct 8x GQA mapped read vs 1x coalesced gather (RTX 5070 Laptop 8 GB)

10 sparse layers x 2,080 selected q8_0/q8_0 rows/layer (22.6 MiB/tok unique rows) on Windows 11 WDDM:

Read strategy Latency / token Effective PCIe BW
Direct 8x GQA read from CUDA_Mapped (L2 bypass) 8.688 ms/tok - (7.59x slower)
1x coalesced gather into 2.16 MiB VRAM scratch 1.144 ms/tok 19.78 GB/s
Perplexity verification (llama-perplexity on official wikitext-2-raw/wiki.test.raw)

Hardware and build: Windows 11, RTX 5070 Laptop (8,151 MiB VRAM), 32 GB RAM, CUDA 13.5 (sm_120a), MSVC 19.44, GGML_CUDA_FA_QUANTS="f16-f16;q8_0-q8_0;q8_0-q4_0;q4_0-q4_0".
Model: Qwen4-35B-A3B (UD-Q2_K_XL), -ngl 99 -lm none -cmoe --moe-cache-mib 3000 --moe-cache-cpu-share 0.0 -fa on -b 512.

Controlled dense and single-token kv_rows exactness checks (f16/f16, --chunks 1):

Context / ub Mode Path exercised PPL Delta vs OFF
-c 2048 -ub 8 --no-kv-offload-sparse Dense baseline 5.6852 +/- 0.43428 -
-c 2048 -ub 8 --kv-offload-sparse CUDA0_Mapped + mapped_prefill 5.6852 +/- 0.43428 0.0000 (exact)
-c 4096 -ub 128 --no-kv-offload-sparse Sparse QSA prefill 7.5083 +/- 0.41555 -
-c 4096 -ub 128 --kv-offload-sparse Sparse mapped_prefill 7.5116 +/- 0.41612 +0.0033
-c 4096 -ub 1 --no-kv-offload-sparse Sparse single-token decode 7.5192 +/- 0.41657 -
-c 4096 -ub 1 --kv-offload-sparse Compact kv_rows + 1x gather 7.5267 +/- 0.41808 +0.0075

At -c 2048 -ub 8 (all pools selected, matching MMA tile shape), OFF and ON are bit-identical. In sparse regimes (>2,080 tokens), small ~0.003-0.016 differences are smaller than stock master's own -ub 128 vs -ub 8 spread (7.5083 vs 7.5238, +0.0155) due to borderline QSA top-K / MoE router tie-flips from tile reduction order.

Multi-KV-type and -cti matrix (-c 8192 -ub 128 --chunks 2, 16,384 tokens):

KV types (-ctk / -ctv / -cti) Mode Main KV buffer Indexer buffer (VRAM) PPL (chunks=2) Delta vs OFF
f16 / f16 / f16 OFF 160.00 MiB VRAM 40.00 MiB 5.1378 +/- 0.13266 -
f16 / f16 / f16 ON 160.00 MiB Mapped (0 VRAM) 40.00 MiB 5.1226 +/- 0.13206 -0.0152
q8_0 / q8_0 / q8_0 OFF 85.00 MiB VRAM 21.25 MiB 5.1373 +/- 0.13269 -
q8_0 / q8_0 / q8_0 ON 85.00 MiB Mapped (0 VRAM) 21.25 MiB 5.1361 +/- 0.13251 -0.0012
q8_0 / q4_0 / q8_0 OFF 65.00 MiB VRAM 21.25 MiB 5.1399 +/- 0.13254 -
q8_0 / q4_0 / q8_0 ON 65.00 MiB Mapped (0 VRAM) 21.25 MiB 5.1230 +/- 0.13194 -0.0169
q8_0 / q4_0 / q4_0 (-cti) ON 65.00 MiB Mapped (0 VRAM) 11.25 MiB 5.1194 +/- 0.13178 -0.0205
Controlled VRAM, throughput, CUDA Graph reuse, and 32K Needle scaling (llama-server)

All runs use -b 512 -ub 128 -fa on -ngl 99 -lm none -cmoe --moe-cache-cpu-share 0.0 -t 12 --reasoning-budget 0. The 64K OFF vs ON pair fixes --moe-cache-mib 3600 identically.

Config Context KV (K/V/Idx) MoE cache VRAM used / free Free RAM Short decode (n_g=128) Long context pp / tg Needle Graphs reused
64k_q8q8_OFF 64K q8_0/q8_0/q8_0 3600 MiB 7,052 / 840 MiB 11.74 GB 55.95 t/s 16.5K: 320.09 / 55.07 t/s Pass 219
64k_q8q8_ON 64K q8_0/q8_0/q8_0 3600 MiB 6,364 / 1,528 MiB (-688 MiB) 11.06 GB 56.17 t/s 16.5K: 223.85 / 48.16 t/s Pass 219
256k_q8q8_ON 256K q8_0/q8_0/q8_0 3800 MiB 7,090 / 802 MiB 9.00 GB 54.68 t/s 32.2K: 217.89 / 45.45 t/s Pass 219
512k_q8q4_cti4_ON 512K q8_0/q4_0/q4_0 3800 MiB 7,274 / 618 MiB 7.57 GB 53.33 t/s 32.2K: 224.24 / 46.69 t/s Pass 219
512k_q4q4_cti4_ON 512K q4_0/q4_0/q4_0 3800 MiB 7,274 / 618 MiB 8.82 GB 53.61 t/s 32.2K: 231.11 / 48.18 t/s Pass 219
Open questions for maintainers (RFC)
  1. CLI and API naming: are --kv-offload-sparse / --no-kv-offload-sparse (llama_context_params::kv_offload_sparse) and -cti / --cache-type-idx (llama_context_params::type_idx) acceptable, or would you prefer unifying under a buffer-override / memory-placement option?
  2. Scope of compact kv_rows: this PR wires llama_memory_hybrid_idx for both qwen4exp and glm5-next, and adds the compact decode kv_rows path to qwen4exp. Should compact kv_rows for glm5-next / DeepSeek DSA be added here or as a follow-up?
  3. Other backends: HIP (hipHostMallocMapped) is guarded off for now until tested on AMD hardware.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES - used Gemini (Antigravity) to assist with design iteration, CUDA/C++ implementation, benchmark scripting, and drafting this PR description; all changes were compiled, verified with llama-perplexity and llama-server on RTX 5070 Laptop hardware, and reviewed by the author.

Written with Gemini

@ggml-gh-bot

ggml-gh-bot Bot commented Dec 5, 2026

Copy link
Copy Markdown

Hi @MacarPeng, thanks for your contribution!

Important

Your AI agent does not seem to follow AGENTS.md

Many agents do not load AGENTS.md on their own. Please tell your agent explicitly, e.g.:
Read AGENTS.md and follow it strictly. Then review all changes in this branch against it and fix any violation.

Then update this PR accordingly.

What was found

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • Large PR: Large changes require prior discussion (e.g. an issue or RFC) and maintainers may not be able to review this PR as-is. Consider splitting it into smaller, focused PRs.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@github-actions github-actions Bot added model Model specific ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Dec 5, 2026
@MacarPeng
MacarPeng marked this pull request as ready for review December 5, 2026 17:00
Copilot AI balanced review requested due to automatic review settings December 5, 2026 17:00
@MacarPeng
MacarPeng requested review from a team and CISC as code owners December 5, 2026 17:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning model Model specific

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants