OpenFlowLM-Next icon indicating copy to clipboard operation
OpenFlowLM-Next copied to clipboard

add gguf support

Open Cyronius opened this issue 2 months ago • 14 comments

we should support gguf and treat q4nx as a legacy format

Cyronius avatar Sep 06 '26 13:09 Cyronius

100% Agree

Atomic-Germ avatar Sep 06 '26 18:09 Atomic-Germ

I will look into this.

vegah avatar Sep 07 '26 21:09 vegah

@vegah I've given it a few very different tries, but it's definitely doable for one of you.

Atomic-Germ avatar Sep 07 '26 22:09 Atomic-Germ

This is an extremely high value change. I'm excited to see it! I have some rough stuff in my phlegm codebase but you will probably find that starting over is easier.

Cyronius avatar Sep 07 '26 23:09 Cyronius

@vegah Would this be a starting point? #24

Atomic-Germ avatar Sep 08 '26 02:09 Atomic-Germ

still in progress? anything I can do to help push this along this weekend? it's kind of funny that we all have separate gguf implementations. We need one amazing one, but for now I would settle for one proof of concept that can only handle Q4_0 or Q4_1.

Cyronius avatar Sep 18 '26 16:09 Cyronius

still in progress? anything I can do to help push this along this weekend? it's kind of funny that we all have separate gguf implementations. We need one amazing one, but for now I would settle for one proof of concept that can only handle Q4_0 or Q4_1.

Same. Actually I've now tried three alternate ways. And then there's the one amd devs are working on privately. And then there's the llama.cpp effort for npu inference.

This all has to converge, I'm sure it will. I'm definitely struggling to get anything that isn't painfully hacky and the last thing we need is to be self-hampered by needing to support specific models.

I've made most of my progress toward Q4_K and a bit toward mxfp4, but I'm having a hard time not ending up with a near useless translation layer, or some kind of repack.

Still trying though. It'll happen. It's just hard.

Atomic-Germ avatar Sep 18 '26 17:09 Atomic-Germ

Looks like @chizamd made progress on this: https://github.com/ROCm/FastFlowLM/pull/724/changes#diff-9fa36bc5938fcb1c1dd2dc60fcabbcbc8df1022a718c4089f5c6d9f86f19e266

EDIT: He didn't get very far in actually, it has some huge flaws.

Atomic-Germ avatar Sep 18 '26 18:09 Atomic-Germ

Same. Actually I've now tried three alternate ways. And then there's the one amd devs are working on privately. And then there's the llama.cpp effort for npu inference.

oh geez, I have this in another project as well. Turns out you don't even need to fork llama.cpp, you can just provide a backend. We need to have a discussion on how this project should enable that, probably. It feels like that is one effort, openflowllm-next is another project, and then the open kernels are almost a 3rd project -- I keep having to pull the open kernels into other things, so maybe other people will want to as well.

I've made most of my progress toward Q4_K and a bit toward mxfp4, but I'm having a hard time not ending up with a near useless translation layer, or some kind of repack.

Your runtime translation of the weights to f32 was deemed better than my bf16 attempt by claude, fwiw: He solved the scale problem a third way, and it's cleverer than either option I gave you. The pool stores scales as f32, widened from fp16 exactly, so the file loses nothing. The kernel then narrows f32 to bf16 with a single rounding instruction at the moment of use. His stated reason is that this makes a GGUF model bit-identical to its converted twin, which it does.

Cyronius avatar Sep 18 '26 18:09 Cyronius

I'm thinking we should see what we can learn from https://github.com/fencerJP/llama-apu -- it's doing an interesting thing or two.

Atomic-Germ avatar Sep 19 '26 16:09 Atomic-Germ

I spent a day going through all of these properly — the two branches here, my own two repos, chizamd's PR, and llama-apu — to work out what's actually duplicated and what's worth keeping. Short answer: there's one real GGUF implementation, not five, and the pieces worth salvaging from the others are smaller and more specific than I expected.

Filed as #104 against proposed-gguf-support (not main).


Findings, by repo

Atomic-Germ/OpenFlowLM-Next #24 (proposed-gguf-support) — this is the real one and it's further along than the thread suggests. 2d8a16e loads a Gemma 3 4B GGUF and executes it on the NPU. CI green, zero commits behind main, blocked only on a review approval. The Q4_0 off-by-8*d I reported on 2026-09-13 is fixed (pools.cpp:514).

OpenFlowLM-Next main — two things are already here that the thread talks about as missing:

  • utilities/q4nx-build/convert.py converts GGUF → .q4nx today, for 12+ families. That's the "proof of concept that only handles Q4_0 or Q4_1" — it shipped, so nobody is blocked while #24 is reviewed.

  • open_kernels/q4_1_pack.py packs GGUF Q4_1 blocks into the 5120-byte bf16 pool chunk, with a self-test asserting the pool dequantizes exactly equal to the GGUF blocks with d/m narrowed to bf16. PROVENANCE.md calls it "the packer seed", and gemv_q4, gemm_q4_prefill and moe_batch already build their fixtures from it.

    That second one matters: #24's gguf_pool.py + std_perm_gguf is a second packer, in f32, alongside a bf16 one that already existed in the same repo and already feeds the kernel tests. That duplication is the thing worth resolving, and it's what #104 is about.

vegah — not a separate implementation. He folded his branch into #24 on 2026-09-08 ("let's keep it one branch") and his fork carries no GGUF branch. What he actually contributed is f3b9cda, the six-file merge resolution that set the rule everything else follows — take main's side, then route it through the weights view, 18 call sites — because a .q4nx manifest and a GGUF-derived manifest are both valid manifests and reading the wrong one fails silently. Treating this as a three-way convergence problem is what makes it look harder than it is.

Cyronius/ggml-xdna (the llama.cpp backend) — this is where the missing evidence turned out to be. hybrid/q4_pack.{h,cpp} is q4_1_pack.py ported to C++, both Q4_0 and Q4_1, same 5120-byte bf16 chunk, same nibble law. hybrid/test-q4-pack.cpp holds the two byte-identical on real GGUF tensors, and Qwen3-1.7B packed that way ran through the stock kernels correctly at 1.44 TOPS. So the bf16 path isn't theoretical — it's byte-checked and it's been on hardware.

Its specs/xdna-backend/spec.md also has two findings worth having here regardless of GGUF: bit-equality against ggml's CPU backend is the wrong correctness bar (ggml quantizes the activation to int8 and we don't — the CPU is the less accurate side by ~7e-3 NRMSE on q4_K), and per-op weight copies measured 40% slower than plain CPU prefill (94 vs 157 tok/s), which is why offload_op is deliberately unimplemented there.

Cyronius/phlegm — mostly already upstreamed, which is the good outcome. open_kernels is a strict superset of phlegm's designs; the bisect findings are already written up at open_kernels/README.md:301-318; q4nx-convert shipped as utilities/q4nx-build; the Rust npu-engine and Python server are superseded. The one thing still only there is tools/seq-capture, the XRT proxy-DLL rig we used to capture the closed engine's dispatches — and its purpose is finished now that the kernels are open. Archive value.

chizamd / ROCm/FastFlowLM#724 — closed on 2026-09-12, title now [withdrawn] experimental prototype, body Withdrawn. Description removed. Dead as a source. The part worth having, per-file revision pinning and hash-verified downloads, is already in #24 as f4a38da.

fencerJP/llama-apu — worth being precise about, since it's been floated twice. It's a fresh fork of llama.cpp (upstream commits through 2026-09-18) with six APU-specific commits, all from the last five days. Its xclbins/ tree is FastFlowLM's src/xclbins/ copied verbatim — the Gemma3-4B-NPU2 byte sizes match exactly (attn 317148, layer 437868, lm_head 556812, mm 512220, …). FFLM's runtime licence is MIT so that's permitted, but it means the "37-profile XDNA 2 XCLBIN bank" in its README is AMD's bank, not an independent kernel effort. backend/physical.rs is 1.6 KB against backend/mock.rs at 19 KB, and its own published numbers are 6,033 ms TTFT on a 180-token prompt with Qwen2.5-0.5B — worse than CPU.

The idea is still good, though, and it's the one worth taking: a phase-partitioned pipeline (compute-bound prefill on the iGPU, memory-bound decode on the NPU, sampling on AVX-512) with dma-buf zero-copy handoff, and a CLI that routes stages rather than layers. That's the answer to why ggml-xdna is parked — per-matmul granularity caps below the iGPU, and routing whole phases sidesteps it. Worth an afternoon in zero-copy_model_runner/src/engine/{rocm_prefill,xrt_decode}.rs. Nothing to take below that level.


The one technical finding

#24's structural cost is the f32 scale plane — 6144-byte q4 chunks and 9216-byte q8 chunks, which is why every design exports twice (dx_f32, lm_head_q4_f32, the gemv_q4s32_* TUs, GEMV_Q4_PREFIX, the nested gguf manifest, the Gemma 3 12B stack workaround, and the open lm_head_q8 numerics bug that open-gguf-direct/SKILL.md calls the MoE blocker).

The kernel narrows those scales to bf16 before the first MAC. gemv_q4.h:136-143 says so itself: "the same result a q4nx chunk's pre-narrowed bf16 scales give." fp16 → f32 is exact, so that's one rounding either way — the f32 plane defers it, it doesn't avoid it, and "bit-identical to its converted twin" survives narrowing at pack time.

#104 measures it rather than asserting it. All 63488 finite fp16 patterns, both packers against each other on the same tensors, and the q8 lm_head chunk too. No hardware needed.


Parts worth keeping

  1. ggml-xdna/hybrid/q4_pack.{h,cpp} — ~200 lines, this is std_perm_gguf done against the 5120-byte chunk, already byte-verified and already on hardware. Yours to take.
  2. Q4_0 in q4_1_pack.py — the Python seed was Q4_1-only while the C++ port handled both; they're meant to be a byte-identical pair. In #104.
  3. The two correctness findings from ggml-xdna's spec above — the CPU-backend bar and the offload_op measurement.
  4. tools/bench-dispatch.cpp and a discrepancy worth resolving: it measured ~0.1 ms per dispatch and 0.02–0.04 ms in a runlist (2026-09-13, through a vendored XRT shim), against open_kernels/README.md:326's 0.13–0.25 ms and 0.07–0.16 ms (2026-09-02, through the engine driver). Two to five times apart. Related: the open engine never uses a runlist at all — Core::run is a bare start()/wait() per dispatch, and block prefill fires T sequential single-token dxB dispatches on one kernel. Not a free win (each iteration patches the instruction stream per position and shuttles buffers, so it'd need T patched instruction BOs), but worth measuring.

And one thing for the thread: phlegm's prior-art survey names xdna-engine and OllamaAMDNPU. With llama-apu that's six XDNA2 inference efforts, not three. The convergence problem is bigger than us.


@Atomic-Germ — #104 is evidence and a plan, not a rewrite. I deliberately didn't touch std_perm_gguf, the recipes or any kernel: I can't build the xclbins here, and deleting 938 lines of your kernel work on an argument you haven't agreed with yet isn't reviewable. If the argument holds I'll do that half as a follow-up, or you take it since you know the export path. If there's a reason for the f32 plane I've missed, I'd rather hear it than keep guessing at it.

🤖 Generated with Claude Code

Cyronius avatar Sep 20 '26 15:09 Cyronius

a couple of corrections to the opus speak. I did not spend a day, this was just a quick review of the gguf implementations and efforts we have available. And I definitely can build the xclbins. Opus is insane. But i think we have a path forward. @Atomic-Germ will you look at my PR and give your thoughts? it merges into your branch, not main.

Cyronius avatar Sep 20 '26 15:09 Cyronius

@Cyronius It definitely clears some things up for me, because I've been trying to solve things I didn't need to. Thanks!

Atomic-Germ avatar Sep 20 '26 18:09 Atomic-Germ

@Cyronius If we think this is a convergence problem, take a loop at the llama.cpp MoE experts streaming issue!! A new fork at least once a week repeating the same work since I first did Atomic-Germ/Guanaco because I have 48GB of UMA and wanted to run Qwen3-Coder-Next. Which worked. Not sure what they're waiting on for their own implementation there.

Anyway, it is actually further along than I thought.

Atomic-Germ avatar Oct 03 '26 14:10 Atomic-Germ