Skip to content

Port DeepSeek-v4 to TQ#224

Open
giveen wants to merge 10 commits into
TheTom:feature/turboquant-kv-cachefrom
giveen:ds4
Open

Port DeepSeek-v4 to TQ#224
giveen wants to merge 10 commits into
TheTom:feature/turboquant-kv-cachefrom
giveen:ds4

Conversation

@giveen

@giveen giveen commented Jul 18, 2026

Copy link
Copy Markdown

Overview

Adds DeepSeek V4 model support to the turboquant fork. V4 introduces a new architecture with lightweight indexer-based sparse attention, hyper-connection (HC) layers for cross-block communication, and compressed-state KV cache storage.

Key Changes

Model Architecture (src/models/deepseek4.cpp, src/llama-arch.{cpp,h})

  • New LLM_ARCH_DEEPSEEK4 arch with V4-specific hparams: indexer head count/size/top-k, output group count, output LoRA rank, compress RoPE frequency base, hyper-connection multiplier, sinkhorn iterations, SWIGLU clamp per expert/shared-expert arrays.
  • New models/templates/deepseek-ai-DeepSeek-V4.jinja chat template.

KV Cache (src/llama-kv-cache-dsv4.{cpp,h}, src/llama-kv-cache-iswa.{cpp,h})

  • DSV4 compressed-state KV cache with configurable compression ratio (CSA-4 for K, HCA-128 for V) and stream-based tensor ops.
  • ISWA (Importance-based Sliding Window Attention) cache improvements — variable window sizes, per-head importance tracking.

CUDA Kernels (ggml/src/ggml-cuda/)

  • dsv4-hc: Hyper-connection layer fused kernel (sinkhorn normalization, attention-biased routing).
  • lightning-indexer: Sparse top-k indexer — computes per-head relevance scores, selects top-k positions, gathers scatter KV blocks.
  • set-rows.cu: Extended for V4's compressed-state scatter/gather.

CPU Ops (ggml/src/ggml-cpu/)

  • dsv4-ops.cpp: Reference implementations for DSV4 tensor operations (compressed-state packing, hyper-connection).
  • ops.{cpp,h}: New CPU op entries.

GGML Infra (ggml/src/ggml.c, ggml/include/ggml.h)

  • New tensor ops for indexer and hyper-connection primitives.
  • RPC header updates for V4 op forwarding.

Conversion Scripts (conversion/)

  • deepseek.py: V4 parameter mapping, hyper-connection weight layout, indexer config export.
  • constants.py / gguf_writer.py: New V4 keys — attention.indexer.head-count, attention.indexer.top-k, hyper-connection.count, attention.output.group-count, attention.output.lora-rank, attention.compress-rope.freq-base, etc.

Removed

  • build-xcframework.sh: Stale Apple framework script, not used in this fork.

Additional Information

Requires a DeepSeek V4 GGUF checkpoint — not compatible with V2/V3 or other architectures. The indexer CUDA kernel is optimized for NVIDIA GPUs with sm_90+; CPU fallback is available for all ops but significantly slower for inference.

- DeepSeek V4 conversion support
- DSV4 CPU ops and CUDA kernels (dsv4-hc, lightning-indexer)
- GGML RPC updates
- KV cache ISWA improvements
- Model architecture updates for V4 support
- Chat template for DeepSeek V4
Merge remote-tracking branch 'origin/feature/turboquant-kv-cache' into ds4
@giveen giveen changed the title ds4 : turboquant feature work Port DeepSeek-v4 to TQ Jul 18, 2026
- Rewrite DSV4_HC_COMB CUDA kernel: 16 threads per 4x4 sinkhorn matrix
  (2 tokens per warp, 8 per block) instead of 1 thread per token.
  Replaces serial per-thread 4x4 ops with warp-shuffle reductions
  (__shfl_xor_sync with XOR 1,2 for rows and XOR 4,8 for columns).
- Fix cmake CUDA arch targets: add 120-real for Blackwell RTX 5090.
- Add test_dsv4_hc_comb test case (15 configs: n_tokens=1,2,4,8,64 x
  n_iter=1,3,5) verified on CUDA0 vs CPU reference.
@giveen

giveen commented Jul 18, 2026

Copy link
Copy Markdown
Author

Right now, it runs about 95% of the equivelent speed as on main llama.cpp
Main - 230pp/12tks gen
TQ Build - 180pp/10tk gen

I'm trying to do a bit more optimization
I have tested using q8_0/turbo4 , and it works, but being VRAM poor makes the model slow anyways.

./llama-server \
   --host 0.0.0.0 \
   --port 8080 \
   --alias deepseek \
   --parallel 1 \
   --cont-batching \
   -m /mnt/storage/models/deepseek-v4-flash/Q3/UD-Q3_K_XL/DeepSeek-V4-Flash-UD-Q3_K_XL-00001-of-00004.gguf \
   --ctx-size 262000 \
   -fa 1 \
   --fit off \
   --cache-type-k q8_0 \
   --cache-type-v q8_0 \
   --cache-reuse 4096 \
   -ctxcp 32 \
   -cms 8192 \
   --no-mmap \
   --mlock \
   --numa isolate \
   --n-cpu-moe 46 \
   --threads 12 \
   --threads-batch 8 \
   --batch-size 2048 \
   --ubatch-size 256 \
   --jinja \
   --no-warmup \
   --chat-template-kwargs '{"reasoning_effort":"max"}' \
   --presence-penalty 0.0 \
   --repeat-penalty 1.05

   ```

   


Port upstream commit b820cc8 (CUDA: consistent use of __restrict__
+ PDL for FA ggml-org#25185). Avoids compiler race condition between PDL and
__restrict__ on Hopper+ GPUs by using GGML_CUDA_RESTRICT (which is a
no-op when PDL is active) instead of raw __restrict__ on kernel params.
Also switches the kernel launch to ggml_cuda_kernel_launch() for PDL
enrollment.
@giveen

giveen commented Jul 18, 2026

Copy link
Copy Markdown
Author

Performance changes in ds4 branch (2 new commits)
Because it touched things outside of the DS4 direct port , I also tested on Qwen3.6-27B and Gemma4-26

4f5cb54 — Warp-parallel HC_COMB kernel
Rewrote the DSV4 hyper-connection comb kernel:
- 16 threads per 4×4 sinkhorn matrix (one per element) instead of 1 thread per token.
- Uses __shfl_xor_sync for row/column reductions, eliminating the serial softmax loop.
- Improves occupancy from ~4/256 to ~32/128 threads on small batches (BS 1–4).
- Added 15 test configurations (n_tokens=1..64 × n_iter=1..5) verified against CPU reference on RTX 5090.

95a7c30 — PDL + restrict fix for flash attention
- Port of upstream b820cc8: Consistent use of restrict + PDL for FA.
- Fixes a compiler race condition between PDL and restrict on Hopper/Blackwell GPUs
that causes incorrect codegen in flash_attn_mask_to_KV_max.
- Enables PDL enrollment for this kernel via ggml_cuda_kernel_launch().
- Scope: All models using flash attention on sm_90+ GPUs.

Additional Changes:

  • Build fix for RTX 5090: Added -DCMAKE_CUDA_ARCHITECTURES="90-real;100-real;120-real".
  • Without this, the 5090 (sm_120) would silently fallback to CPU compute—a classic
    case of "working, but not actually working."

giveen and others added 6 commits July 18, 2026 15:51
Port upstream commit 9f364c7 (llama : dsv4 graph fixes):
- Rename layer output from 'l_out' to 'l_last' so graph_get_cb can
  force it onto the correct GPU backend, preventing cross-backend
  data transfers that slow decode with CPU MoE offloading.
- Add ggml_build_forward_expand for residual/post/comb tensors to
  ensure they are computed before the FFN norm.
Port upstream commit 4937ca8. The i32 token-id -> expert-id
routing table (DeepSeek-V4) cannot be quantized like float weights.
Missing this exclusion causes llama-quantize to fail.
… dimensions (ggml-org#25650)

Co-authored-by: Stanisław Szymczyk <sszymczy@gmail.com>
Left over from merge conflict resolution: GGML_ASSERT(!ggml_is_quantized)
was outside the conflict zone and wasn't removed by the fix patch.
Also removed GGML_ASSERT(ggml_blck_size == 1) from the top-level
check since it's now only relevant for non-quantized types (preserved
in the else branch).
@giveen

giveen commented Jul 18, 2026

Copy link
Copy Markdown
Author

Okay speeds are the same as main llama.cpp and it holds steady even across long context, just like main, I'm still working on a few ideas.

@giveen
giveen marked this pull request as ready for review July 18, 2026 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants