Repository navigation
Conversation
…fill Assisted-by: Gemini
…tion models Assisted-by: Gemini
|
Hi @MacarPeng, thanks for your contribution! Important Your AI agent does not seem to follow AGENTS.md Many agents do not load 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:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
Assisted-by: Gemini
There was a problem hiding this comment.
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.
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 likeqwen4exp(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,
qwen4expemits a compactkv_rowslist (<= 2,080rows per sparse layer, padded toFATTN_KQ_STRIDE = 256) andggml-cudaperforms a single 1x coalesced PCIe gather into a small (~2 MiB) VRAM scratch buffer before runningflash_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_sizeskips the permanent full-context F16 scratch reservation (saving 512-1024 MiB VRAM at 256K-512K), andggml_cuda_flash_attn_ext_mapped_prefillcompacts 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_0rows/layer (22.6 MiB/tok unique rows) on Windows 11 WDDM:CUDA_Mapped(L2 bypass)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_rowsexactness checks (f16/f16,--chunks 1):-c 2048 -ub 8--no-kv-offload-sparse-c 2048 -ub 8--kv-offload-sparseCUDA0_Mapped+mapped_prefill-c 4096 -ub 128--no-kv-offload-sparse-c 4096 -ub 128--kv-offload-sparsemapped_prefill-c 4096 -ub 1--no-kv-offload-sparse-c 4096 -ub 1--kv-offload-sparsekv_rows+ 1x gatherAt
-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 128vs-ub 8spread (7.5083vs7.5238, +0.0155) due to borderline QSA top-K / MoE router tie-flips from tile reduction order.Multi-KV-type and
-ctimatrix (-c 8192 -ub 128 --chunks 2, 16,384 tokens):-ctk / -ctv / -cti)chunks=2)f16 / f16 / f16f16 / f16 / f16q8_0 / q8_0 / q8_0q8_0 / q8_0 / q8_0q8_0 / q4_0 / q8_0q8_0 / q4_0 / q8_0q8_0 / q4_0 / q4_0(-cti)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 3600identically.K/V/Idx)n_g=128)pp/tg64k_q8q8_OFFq8_0/q8_0/q8_064k_q8q8_ONq8_0/q8_0/q8_0256k_q8q8_ONq8_0/q8_0/q8_0512k_q8q4_cti4_ONq8_0/q4_0/q4_0512k_q4q4_cti4_ONq4_0/q4_0/q4_0Open questions for maintainers (RFC)
--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?kv_rows: this PR wiresllama_memory_hybrid_idxfor bothqwen4expandglm5-next, and adds the compact decodekv_rowspath toqwen4exp. Should compactkv_rowsforglm5-next/ DeepSeek DSA be added here or as a follow-up?hipHostMallocMapped) is guarded off for now until tested on AMD hardware.Requirements
llama-perplexityandllama-serveron RTX 5070 Laptop hardware, and reviewed by the author.Written with Gemini