llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

ggml: add DeepSeek V4 hyperconnection + KV ops (CPU)

Open cchuter opened this issue 6 months ago • 20 comments

This is the first of at 4 or more PRs to support Deepseek V4 https://github.com/ggml-org/llama.cpp/issues/22319 - the full branch with complete support for DeepSeek V4 is here: https://github.com/cchuter/llama.cpp/tree/feat/v4-port-cuda (this branch is where the PRs will be carved from and will undoubtedly change with reviewer responses to this and following PRs)

Overview

I used guidance from @CISC in the issue to break up the PR. This is the first to add the basic ggml support. It adds the five DeepSeek-V4-Flash-specific ggml ops with CPU reference implementations and test-backend-ops coverage:

  • GGML_OP_DSV4_HC_SPLIT_SINKHORN hyperconnection mix split + Sinkhorn
  • GGML_OP_DSV4_HC_WEIGHTED_SUM hyperconnection weighted residual sum
  • GGML_OP_DSV4_HC_EXPAND hyperconnection stream expand
  • GGML_OP_DSV4_FP8_KV_QUANTIZE e4m3 FP8 KV-cache quantize/dequantize
  • GGML_OP_DSV4_ROPE_TAIL V4 partial-RoPE tail rotation

THis is CPU only

GGML_OP_COUNT goes 96 -> 101 (5 new ops).

The CPU implementations are the numerical reference; test-backend-ops compares backend ops against the CPU backend, so on a CPU-only build the new cases register but are inert (CPU is the reference). THere will be follow up GPU PRs.

Additional information

Attribution

Derived from antirez/llama.cpp-deepseek-v4-flash (https://github.com/antirez/llama.cpp-deepseek-v4-flash), which builds on fairydreaming's work and the ggml/llama.cpp lineage. The V4 ops and model implementation originate there. Used with antirez's permission

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes, I used a combination of models through Claude code. I am the architect and I have built a custom agent dev team for coding purposes

cchuter avatar May 15 '26 21:05 cchuter

Hi @cchuter, thanks for your contribution!

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

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.

  • 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.

ggml-gh-bot[bot] avatar May 15 '26 21:05 ggml-gh-bot[bot]

@cchuter is this complete for CPU inference? Where can I download gguf to test?

segmond avatar May 16 '26 11:05 segmond

@segmond No, these are just the backend ops which are needed for the model support in CPU version.

@cchuter actually when adding new ops for models, we tend to prefer PRs with the model support added in as well, as otherwise there's no simple way to test whether the operation implementation is correct.

pwilkin avatar May 16 '26 12:05 pwilkin

@segmond, ggufs are here: https://huggingface.co/teamblobfish/DeepSeek-V4-Flash-GGUF

@pwilkin, I've update the PR to add a full cpu end to end test. THe model loads and answers the test questions.

I was a little over aggressive in splitting up the PRs. Here's the plan:

This PR 1: base ggml and cpu support PR 2: Metal Kernel PR 3: CUDA (including multi-gpu support, that was fun) PR 4: MTP (yay, nice timing on the merge) PR 5: DSA (this is the PR that was recently closed by @fairydreaming)

PR's 4 and 5 are not done yet and might prove challenging. They will also require new gguf's (which I will make).

THe full working code base of PR 1-3 is here: https://github.com/cchuter/llama.cpp/tree/v4-clean-base

Before all those PRs are merged I don't expect a performant DeepSeek4, but I do expect a working one at all stages. Right now you should peak around 20-30 t/s generation on GPUs

cchuter avatar May 16 '26 17:05 cchuter

Did you just copied straight from antirez's version? https://github.com/antirez/llama.cpp-deepseek-v4-flash/blob/main/ggml/include/ggml.h

Please note that dishonest about code's origin will resulting in being banned from the project.

ngxson avatar May 17 '26 11:05 ngxson

@ngxson you're right, thanks for pushing on it. This is derived from antirez's llama.cpp-deepseek-v4-flash (which builds on fairydreaming's and the ggml/llama.cpp lineage). The V4 ops and model are his; my part is CUDA/multi-GPU, a DSA-free rebase onto current master, and GGUF packaging. I should have credited that in the PR from the start and didn't. That's on me.

I have contacted @antirez and @fairydreaming to let them know I'm building on top of their work and crediting. They've both moved on to other things.

cchuter avatar May 17 '26 14:05 cchuter

Happy to restructure, or close, if you'd rather. I'm not aware of the history of this work

cchuter avatar May 17 '26 14:05 cchuter

Since your code contains many lines that are directly copied from antirez's work, we require explicit agreement from the original author to proceed.

ngxson avatar May 17 '26 14:05 ngxson

Hi, I agree with taking the code I developed and making it part of llama.cpp or any other compatibly-licensed project. I don't ask for any credit. Have fun hacking LLMs! :)

antirez avatar May 17 '26 14:05 antirez

I checked out your v4-clean-base branch and gave it a go, still needs more work. I couldn't build it because GGML_OP_COUNT wasn't updated in ggml-rpc.h. had to bump it to 101 from 96 to build. My guess is you didn't build with RPC.

On multi CUDA GPU setup it fails, abords fro ggml_backend_sched_split_graph(), I had to bump up GGML_SCHED_MAX_BACKEND and GGML_SCHED_MAX_SPLIT_INPUTS to stop the crash.

Once I go past 1 GPU I get the infamous <<<<<<<<<<< for output. With 1 GPU it works. 3090s btw. Nevermind the bad output, with the model completely loaded in memory vs 1 24gb GPU, performance isn't that much improved, all in memory TG 14.81tk/s, 1 GPU 11.70tk/s for about 4500 tokens generated. PP with multiGPU is 3x 177 vs 65. Performance leaves lots to be desired, but for now let's focus on correctness. To give an example, I get 800 tk/s on PP and 52 tk/s for TG for Qwen3.5-122 which is larger loaded all in memory. From reading the deepseek paper, it's suppose to require less compute....

I have tried every fork I could find, and these 2 in these order have worked best for me on multi cuda setup. mix of 3090s/3080s.

https://github.com/nonzod/llama.cpp-deepseek-v4-flash-spark https://github.com/Fringe210/llama.cpp-deepseek-v4-flash-cuda

segmond avatar May 17 '26 18:05 segmond

Since your code contains many lines that are directly copied from antirez's work, we require explicit agreement from the original author to proceed.

https://github.com/ggml-org/llama.cpp/pull/23201 consent

Pushing #metoo'ish propaganda into coding where licensing+copyright already provides 100% coverage is actively malicious, and I'd like you to stop doing that.

whoreson avatar May 18 '26 10:05 whoreson

@segmond thanks, that's very helpful. I'll hold off on the other GPU PRs until I've confirmed perf and correctness.

cchuter avatar May 18 '26 19:05 cchuter

any chance this would also support https://github.com/ggml-org/llama.cpp/issues/22436

vanmilleru avatar May 20 '26 16:05 vanmilleru

No news in 2 weeks?

narikm avatar Jun 01 '26 15:06 narikm

Things are happening.

pwilkin avatar Jun 01 '26 16:06 pwilkin

Sorry, I've refocused and I've been working on adding multi-gpu support to @antirez 's project. Anyone is free to use my fork and previous work to further this along- no credit needed. If I get good results I'll see if I can get it working in llama.cpp as well.

cchuter avatar Jun 01 '26 17:06 cchuter

can ds4 run on multiple gpu on different machines?

vanmilleru avatar Jun 02 '26 17:06 vanmilleru

@vanmilleru I think antirez ds4 project just added such feature, but it seems to be only for the DS4 PRO Q4 GGUF to run across two 512 GB M3 Ultra Mac Studios.

fairydreaming avatar Jun 02 '26 17:06 fairydreaming

@fairydreaming any chance nvidia gpu can also get this

vanmilleru avatar Jun 03 '26 03:06 vanmilleru

@fairydreaming any chance nvidia gpu can also get this

@vanmilleru No idea, but there's #24162, so maybe you can check if it works on multiple machines by using llama.cpp RPC.

fairydreaming avatar Jun 06 '26 07:06 fairydreaming