Port DeepSeek-v4 to TQ#224
Conversation
- 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
- 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.
|
Right now, it runs about 95% of the equivelent speed as on main llama.cpp I'm trying to do a bit more optimization |
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.
|
Performance changes in ds4 branch (2 new commits) 4f5cb54 — Warp-parallel HC_COMB kernel 95a7c30 — PDL + restrict fix for flash attention Additional Changes:
|
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).
|
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. |
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})LLM_ARCH_DEEPSEEK4arch 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.models/templates/deepseek-ai-DeepSeek-V4.jinjachat template.KV Cache (
src/llama-kv-cache-dsv4.{cpp,h},src/llama-kv-cache-iswa.{cpp,h})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)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.