mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3457] [Engine] Metal: opt-in --dflash DFlash2 drafter in place of the MTP layer #4644
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4644
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3457
Author: @hotocoo
Created: 10/20/2026
Status: 🔄 Open
Base:
main← Head:metal-dflash2📝 Commits (10+)
c6f7585Metal: opt-in --dflash, a DFlash2 drafter in place of the MTP layer (native Metal kernels or llama.cpp's DFLASH graph)9b1167dMetal DFlash2: benchmark against MTP on an M4 Max, and the --dflash note in docs/MAC.mdeee8e7dbench: DFlash2 results, say where the MTP baseline comes from; fix the drafter repo namef0debc5Merge remote-tracking branch 'origin/main' into metal-dflash2-perf2aa30f0Metal --dflash: the native drafter is the default and needs no llama.cpp build (-DSTRATA_DFLASH=ON)8377413Metal --dflash native drafter: any token_embd type the host dequantizer knows; refusals say how to make the draftee5e11bMetal --dflash native drafter: ffn_down on the default gemv tile, the inject's fc on 32,2,4,2 (draft 11% faster)11a9addMerge branch 'main' into metal-dflash273f2625bench: Metal DFlash2 on the native-only build (ee5e11b9) against MTP, same binary, same day69e04f2Merge remote-tracking branch 'fork/metal-dflash2' into metal-dflash2-perf📊 Changes
35 files changed (+2664 additions, -2 deletions)
View changed files
➕
bench/results/2026-10-20-metal-dflash2/README.md(+95 -0)➕
bench/results/2026-10-20-metal-dflash2/greedy-mtp.json(+32 -0)➕
bench/results/2026-10-20-metal-dflash2/greedy-tuned.json(+32 -0)➕
bench/results/2026-10-20-metal-dflash2/long-102k-engine-lines.txt(+6 -0)➕
bench/results/2026-10-20-metal-dflash2/matrix.tsv(+59 -0)➕
bench/results/2026-10-20-metal-dflash2/scripts/long120.py(+16 -0)➕
bench/results/2026-10-20-metal-dflash2/scripts/matrix.py(+52 -0)➕
bench/results/2026-10-20-metal-dflash2/scripts/matrix2.py(+51 -0)➕
bench/results/2026-10-20-metal-dflash2/scripts/matrix3.py(+50 -0)➕
bench/results/2026-10-20-metal-dflash2/scripts/run.py(+24 -0)➕
bench/results/2026-10-20-metal-dflash2/scripts/swap.sh(+7 -0)➕
bench/results/2026-10-22-metal-dflash2-native/README.md(+62 -0)➕
bench/results/2026-10-22-metal-dflash2-native/greedy-dflash2-v2.json(+32 -0)➕
bench/results/2026-10-22-metal-dflash2-native/greedy-mtp.json(+32 -0)➕
bench/results/2026-10-22-metal-dflash2-native/matrix.tsv(+7 -0)➕
bench/results/2026-10-22-metal-dflash2-native/scripts/matrix4.py(+58 -0)➕
bench/results/2026-10-22-metal-dflash2-native/scripts/run.py(+24 -0)➕
bench/results/2026-10-22-metal-dflash2-native/scripts/run4.sh(+22 -0)➕
bench/results/2026-10-22-metal-dflash2-native/scripts/swap4.sh(+11 -0)📝
docs/MAC.md(+10 -0)...and 15 more files
📄 Description
Problem and result
On a Mac the only drafter for verify windows is the MTP layer (plus the suffix drafter). The engine cannot use a DFlash2 draft model; #3322 adds DFlash2 to the CUDA engine, and the Metal engine has nothing.
With this change,
--dflash <draft.gguf>(opt-in, Metal--serve) drafts in place of--mtp: up to--spec - 1tokens a window from the outputs of target layers 5/15/25/35/45 (the mean of the four hyper-connection streams), and--spec-min-pstops a draft where the selector's confidence falls below it. Without--dflashthe engine runs the same dispatches as before: every new dispatch and buffer is behind a flag only--dflashsets.On an M4 Max (64 GB) with an abliterated IQ3_S target, greedy, at a 42 GB expert arena:
The stock
incoai/Qwen3.8-Flash-Next-DFlash2drafter was slower than MTP on this target (41.1-42.5 vs 42.1-43.7 tok/s short, arena 33). The gain comes from a drafter fine-tuned on the target's own features, not from DFlash2 alone. Greedy output is identical to MTP.The MTP layer here is not the target's: the abliterated GGUF has no MTP tensors, so (as setup does for it) the MTP arm uses the
mtp.*block of the officialQwen/Qwen3.8-Flash-NextBF16 checkpoint (revision de4b8e4d), packed asmtp-q2_0.gguf(experts Q2_0). It was trained with the official model, not this fine-tune, and its experts are Q2_0 against the drafter's Q8_0; both favor DFlash2. This compares what Strata runs for this GGUF today with the fine-tuned drafter, not two drafters made the same way.Update 2026-10-22, on this revision (native-only build,
ee5e11b9drafter tiles, main merged; same binary for botharms, same day, M4 Max 64 GB, arena 42 GB, greedy; bench/results/2026-10-22-metal-dflash2-native):
Greedy output identical to MTP on 6 of 6 prompts (reasoning and answer). The drafter is the incoai checkpoint fine-tuned
on this target (v2, Q8_0); the stock incoai drafter is still not faster than MTP on this abliterated target.
Related discussion
src/program/generate.cpp, where everything is behind#if defined(STRATA_GPU_METAL) && defined(STRATA_DFLASH).Approach
metal/kernels/gr.metal:hc_mean, one dispatch per captured layer, writes the HC-stream mean ofRinto a feature buffer (5 x n_embd per row).window.cppandprompt_chain.incencode it after the hyper-connection read of layers 5/15/25/35/45 in the decode/verify window, the resident prompt chain and the streaming prompt chunks, only whendf_on_is set.src/spec/dflash_drafter.cpp,include/strata/spec/dflash_*.hpp: the drafter. Two backends: Strata's own Metal kernels (metal/kernels/dflash.metal,metal/src/engine/dflash.cpp), the default; and, only in a build withSTRATA_DFLASH_LLAMA, llama.cpp's DFLASH graph (libllamafrom the pinned llama.cpp 3cf03257, built shared with Metal;STRATA_DFLASH_NATIVE=0), the parity reference. The native drafter takes anytoken_embdtype the host dequantizer knows and needs the draft-vocabulary head (tools/dflash_head.py --draft-vocab, at most 40960 rows); otherwise it refuses with that command. Each prompt chunk and committed window injects its rows' features; a draft runs the block and the selector.src/program/generate.cpp:--dflashparsing and the serve loop's draft source; it refuses--dflashwith--mtpand with--spec< 2.STRATA_DFLASH_DUMP=<prefix>(opt-in, with--dflash): appends each committed row's position, token and features to<prefix>.idx/<prefix>.f16. That is how the training data for the fine-tuned drafter was made; the training script is not in this PR.tools/dflash_head.py: makes the draft GGUF: a converted DFlash2 draft plus the target'stoken_embdand output head (Q8_0), optionally only the rows of the token ids in the MTP folder'sdraft_vocab.bin(a token-id list,data/draft_vocab_en.binin these runs; no MTP weights) with ad2tmap.metal/src/ref_quant.cpplearns GGUF type 27 (I64) for that map.metal/CMakeLists.txt:-DSTRATA_DFLASH=ONbuilds--dflashwith the native drafter only (no llama.cpp build needed).-DSTRATA_DFLASH_LLAMA=<llama.cpp shared Metal build>adds the llama.cpp backend and the two test programs. Neither set: nothing new is built and--dflashis refused.docs/MAC.md: a short--dflashparagraph.bench/results/2026-10-20-metal-dflash2/: the runs.Validation
Revision:
metal-dflash2(main9ae93f7b+ this change). The engine runs were measured on maine88ee39d+ this change before two edits made for review: a per-window debug trace (STRATA_DFLASH_TRACE, off unless set) was removed, and the prompt path's token copy for the drafter now runs only with--dflash. Neither changes what runs with--dflash; the engine runs were not repeated on the final revision. The change applied to9ae93f7bwithout conflicts. Apple M4 Max, 64 GB, macOS 26.5. llama.cpp 3cf03257 (the pinned commit), metal-cpp from setup.Update (main
9de04cd4merged; commits2aa30f03and83774137: native drafter by default, no llama.cpp build needed):-DSTRATA_DFLASH=ON(otool -L strata: nolibllama),-DSTRATA_DFLASH_LLAMA=…and neither. All link.DflashDrafter) gives the same FNV-1a hash over every lattice and draft on the native-only build and on the llama.cpp build: the native drafter is unchanged by the refactor.dflash_native_parity_test t3000-q8-dv.gguf: ok. The stockdflash2-q8-dv.ggufloads and drafts on the native drafter.--dflashgets by default changed, and the matrix's DFlash2 arms already ran with the native one (STRATA_DFLASH_NATIVE=1).Update 2 (commit
ee5e11b9: drafter gemv tiles, measured on an idle GPU): ffn_down (7680 x 2560) moves to the16,2,4,1 default tile and the inject's fc (12800 x 2560) to 32,2,4,2. A 300-cycle inject(3) + draft(3) loop, interleaved
with the previous build, three rounds: draft(3) p50 3.97-3.99 ms -> 3.51-3.53 ms, inject(3) + draft(3) p50 4.47-4.50 ms
-> 3.98-4.00 ms (M4 Max). fc's tile keeps the sums' order (bitwise equal); ffn_down's does not, so it was checked against
the f64 reference:
dflash_native_parity_testpasses with the same lattice error as before (max 0.0041, mean 0.000685)and 300 / 300 chains equal llama.cpp's. Engine runs on this revision: not run yet (the GPU is busy with another job).
Update 3 (
82a83d97): main merged into the branch on GitHub left a duplicated member line inwindow.cpp(PromptBufs) and the Metal build failed; fixed. All three CMake configurations build; the drafter's output is bitwise
equal to ee5e11b9's;
dflash_native_parity_testpasses. The 2026-10-22 runs above usedee5e11b9before this merge. After it, the same build (82a83d97,-DSTRATA_DFLASH=ON) servedthree more fresh-server runs of the same workload (DFlash2 v2, spec 4, min-p 0.75, arena 42): 49.8, 49.3, 49.8 tok/s
short decode, 51.7, 53.0, 52.4 tok/s on the 29K cold prompt, 2.12 tokens per window, in line with ee5e11b9's.
Passed (first revision):
9ae93f7b+ this change:cmake -S metal -B build-df -G Ninja -DCMAKE_BUILD_TYPE=Release -DMETAL_CPP_DIR=… -DSTRATA_GGML_DIR=<llama.cpp> -DSTRATA_DFLASH_LLAMA=<llama.cpp shared Metal build>andninja -C build-df strata dflash_drafter_test dflash_native_parity_test: links; warnings are existing ones plus a duplicate-rpathfrom the linker.-DSTRATA_DFLASH_LLAMA(ninja -C build-off strata): links.build-df/dflash_drafter_test t3000-q8-dv.gguf: ok (steady draft 3.55 ms for 1 token, 9.73 ms for 7; another model server was running, so these are upper bounds).build-df/dflash_native_parity_test t3000-q8-dv.gguf: ok (native Metal drafter against an f64 host reference, 2200 context rows, 300 trials).bench/results/…/scripts/run.py): MTP on the unpatched binary against the fine-tuned DFlash2 drafter on the patched one: 6 of 6 identical, reasoning and answer, character for character (greedy-mtp.json,greedy-tuned.json).scripts/matrix*.py,matrix.tsv, 59 rows): arenas 33/38/40/42/45/47 GB, spec 3/4/5, severalspec_min_p, suffix drafter on/off, stock and fine-tuned drafters. 102K engine lines inlong-102k-engine-lines.txt.Failed:
matrix.tsv).Not run:
--dflashagainst unpatched main, output by output. The MTP arms ran on an unpatched build. The no-change claim for the default path rests on reading the code: every new dispatch is behinddf_on_, set only by--dflash.src/program/generate.cpp, whose additions are all inside#if defined(STRATA_GPU_METAL) && defined(STRATA_DFLASH), apart from the--dflashoption string, its usage text and its parsing.strata launchpaths.Limits
serve/server.py; setup does not offer it.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.