Skip to content

feat(gpu,arch,spec,quant): DFlash/DFlash2 Q4_0, FlashCascade, SenseNova-Loop mHC, external draft models, q4/q2 KV, RDNA3.5 WMMA & AVX-512 - #56

Merged
Vasyl-Trefilov merged 2 commits into
mainfrom
carry-pr50
Nov 19, 2026
Merged

Vasyl-Trefilov merged 2 commits into
mainfrom
carry-pr50

Conversation

@Vasyl-Trefilov

Copy link
Copy Markdown
Member

Carries #50 by ybenyedder. #50 was closed by accident when the history of main was rewritten, and GitHub cannot reopen it because the old branch and the new main share no commits.

This is the same single commit (3f3446d, author unchanged) rebased onto the current main (787935b). Conflicts resolved in the rebase:

  • src/vulkan/mod.rs gemm_raw: kept the stale-q8 fix already on main (f2cb13e). The early if !same { q8.1 = None; } from the commit is dropped because both branches below already clear the cache.
  • src/main.rs generate and chat: kept the new Hy4-flash dispatch and the commit's engine.aux_model_path().

Checked here: cargo check --all-targets --locked passes. The test suite was not run on this branch.

Original description from #50 follows.


Summary

  • Native DFlash block-diffusion speculative decoding (src/config.rs, src/gguf.rs, src/gpu_model.rs, src/gpu_model/prefill.rs, src/main.rs, src/model.rs, src/ops.rs): auto-converts HF DFlash safetensors + config.json into native Q4_0 (223.1 MiB, default) or Q8_0 (391.1 MiB) GGUF, extracts concatenated post-layer target hidden states across prefill_chunk / forward, caches persistent DFlash KV across turns, supports lossless greedy (gemv_rows_n 143K shortlist) & Leviathan residual rejection sampling (draft_dist/accept_dist), includes Adaptive Block Controller {8, 12, 16}, and integrates FlashCascade (--spec-type suffix,ngram-simple,ngram-cache with DFlash).
  • Register-only moe_gemv_q, unrolled gemv_f, & strided DeltaNet rollback (src/vulkan/shaders/{moe_gemv_q.comp,gemv_f.comp,deltanet.comp}, src/backend.rs, src/vulkan/{mod.rs,preflight.rs}, src/gpu_model.rs): specializes MAX_NM {1,4,8,17} + dedicated nm <= 4 fast path eliminating register/scratch spills (Scratch size 0), adds gemv_rows_n / upload_raw, and strides DeltaNet state snapshots (SNAP_STRIDE = 4 + exact 0..3 token GPU replay on rollback, saving -738 MB VRAM & -51% DeltaNet kernel time).
  • RDNA 3.5 16x16x16 INT8 coopmat (gemmi_16/gemmi_s_16), 2 GiB expert pools, --kv q4/--kv q2, & Zen 5 AVX-512 VNNI+VBMI: unlocks 100% fast-path GPU expert residency (10240/10240 on 27 GiB unified APU), 256K–1M KV context, --thinking-budget, --short, DeltaNet GEMV dedup, exact MTP shortlist, and AVX-512 Q8_0/Q4_*/Q5_*/IQ4_*/TQ1_0/IQ1_S/IQ2_XXS/IQ3_S kernels.

Verification (AMD Ryzen AI 9 HX 470 + Radeon 890M gfx1150)

  • ./tools/gen_spirv.sh && cargo test --release --lib --bins --tests: all 20 release test suites pass (0 failures, 0 warnings), verified both on fork/main and the squashed PR branch on origin/main (b29ab99).
  • STRATA_VK_STATS=1: Spilled SGPRs 0, Spilled VGPRs 0, Scratch size 0 on all moe_gemv_q, gemv_f, and deltanet pipelines.
  • Greedy --temp 0 parity vs --mtp 0: 100% SHA-256 bit-identical outputs verified across Rust SPSC (-n 256: 13.83 -> 24.49 tok/s), Math --thinking-budget 512 (14.23 -> 30.89 tok/s), accented French prose (14.12 -> 26.86 tok/s), and FlashCascade contextual refactoring (14.18 -> 43.36 tok/s, 72.3% acceptance).
  • bench --gpu --backend vulkan --kv q4 --chunk 512 --tg 256: 14.2 tok/s (--mtp 0) -> 58.5–65.5 tok/s (DFlash Q4_0, 90.4%) -> 66.3–78.6 tok/s (+ FlashCascade).

Wave 4 — Community Architectures & Speculative Decoding (sensenova_loop, External Draft Models & Looped DFlash)

  • Native sensenova_loop (SenseNova-Loop-9B) Architecture:
    • Supports shared-weight looped middle layers (phys_layer mapping virtual layers 28..52 to physical blk.4..27.*) with zero duplicate VRAM allocation via Arc-backed GpuTensor cloning while maintaining independent KV and DeltaNet states across all 56 virtual layers.
    • Implements Manifold-Constrained Hyper-Connections (mHC) with 20-step log-space Sinkhorn-Knopp doubly-stochastic stream mixing (mhc.streams = 2 | 4, mhc.layer_kinds enter/carry/leave phases) across CPU (src/model.rs), Vulkan (hc_mix.comp, hc_write.comp), and CUDA (kernels.cu).
  • Standalone External Draft Model Speculative Decoding (--draft-model <standalone-model.gguf>):
    • Allows any standalone causal LM GGUF sharing the target vocabulary (e.g. nvidia/Qwen3.5-4B-Qwen3.8-27B-Draft, Qwen3.5-0.8B, Qwen3-0.6B) to act as an external speculative drafter (ext_draft in GpuModel) sharing the same GPU device (Backend::share), with cross-turn KV/DeltaNet state reuse (ext_draft_sync_prompt), FlashCascade (hybrid_spec_step) integration, and lossless greedy (--temp 0) / Leviathan rejection sampling (--temp > 0).
  • Looped Multi-Step DFlash Block-Diffusion Refinement (STRATA_DFLASH_STEPS=2..4):
    • Re-anchors prefix positions of the diffusion block with Pass-1 predicted token embeddings and re-runs the shared 6-layer DFlash drafter in-place.

Wave 5 — Register-Zero Vulkan Decode Attention, Reduced-Spill HD=256 Prefill & Chunked mHC Prefill

  • Split-Stage attn_decode (src/vulkan/shaders/attn_decode.comp + attn_decode.spv):
    • Splits K-dot and V-accumulate stages so kr/vr register live ranges do not overlap, achieving Spilled VGPRs 0, Scratch size 0 (Subgroups per SIMD 16) under STRATA_VK_STATS=1 across all tested spec widths ([0,2,..], [0,4,..], [0,8,..]).
  • 2-Register Packed Indexing & Streaming HD>128 attn_prefill (src/vulkan/shaders/attn_prefill.glsl + attn_prefill_{64,128,256}.spv):
    • Packs ri/ci into 2-register r_pack/c_pack bitfields, streams K/V for HD > 128, and adds subgroupMemoryBarrierShared() every 2 tiles, cutting attn_prefill_256 spills from 127 (Scratch size 16384, Latency 340146) to 81 (Scratch size 10496, Latency 197502).
  • Chunked FP16 WMMA Prefill & CLI Inspection for sensenova_loop (src/gpu_model.rs, src/gpu_model/prefill.rs, src/model.rs, src/main.rs):
    • Enables prefill_chunk for mhc_streams > 0 models using non-aliasing stream scratch (hg/mix/mo past total), dense WMMA projections (gemm_dense when cols % 64 == 0), and mhc_load batch sizing (chunk.min(ctx).max(TMAX)).
    • Adds sensenova_loop summary reporting to check subcommand and validates 4-token chunked prefill (chunk=64, short=2), GPU decode, and CPU parity (max_err <= 0.0080, < 1e-2 tolerance).
    • Full cargo test --release --lib --bins --tests: 20 test binaries run (261 passed; 0 failed; 6 ignored bench/external-model tests), 0 compiler warnings.

Wave 6 — Speculative rollback / kernel cleanups (measured neutral)

  • Vk::dtod skips the per-copy barrier inside batch; deltanet supports o == 0 (state-only replay); moe_down_combine hoists xq/xd/sx for nsb == 2 (0 spills, 16 subgroups/SIMD); DeltaNet rollback runs as a graph; dflash_commit_forward batches its 6 layers; FlashCascade can splice an n-gram prefix (order >= 6) into DFlash drafts.
  • A/B on identical prompts, GPU DPM high, --temp 0 -n 256 (Wave 3 27ed6db vs this branch, 2 runs each): decode speed is the same within noise (rust 18.9 vs 18.9-19.0, math 41.3-42.4 vs 41.5-42.1, french 17.0-17.2 vs 17.1-17.2, ctx 50.2-50.3 vs 50.3-50.5 tok/s) and the draft acceptance is identical (174/701, 240/408, 163/791, 241/351). Outputs are byte-identical to a no-draft run (SHA-256 equal on all 4 prompts, baseline 14.1-14.2 tok/s). So Wave 6 is lossless but gives no measurable speedup; an earlier +14.7% figure compared different prompts and is retracted.

…va-Loop mHC, external draft models, q4/q2 KV, RDNA3.5 WMMA & AVX-512
@aletheore

aletheore Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["backend.rs"]
    n1["config.rs"]
    n2["src/cpu_moe.rs"]
    n3["src/cuda/mod.rs"]
    n4["expert_cache.rs"]
    n5["src/gguf.rs"]
    n6["src/gpu_model.rs"]
    n7["src/model.rs"]
    n8["ngram.rs"]
    n9["ops.rs"]
    n10["serve.rs"]
    n11["src/vulkan/mod.rs"]
    n12["weights.rs"]
    n13["gguf_dump.rs"]
    n14["src/chat.rs"]
    n15["cpu_worker.rs"]
    n16["src/deepseek/chat.rs"]
    n17["src/deepseek/forward.rs"]
    n18["mat.rs"]
    n19["src/deepseek/mod.rs"]
    n20["src/gemma4/forward.rs"]
    n21["src/gemma4/mod.rs"]
    n22["src/glm/forward.rs"]
    n23["src/glm/mod.rs"]
    n24["hf.rs"]
    n25["src/hy4/chat.rs"]
    n26["src/hy4/forward.rs"]
    n27["src/hy4/mod.rs"]
    n28["lib.rs"]
    n29["tokenizer.rs"]
    n30["mmproj.rs"]
    n31["projector.rs"]
    n32["prompt.rs"]
    n33["tests/common/mod.rs"]
    n34["tests/cpu_moe.rs"]
    n35["tests/gguf.rs"]
    n36["tests/gpu_model.rs"]
    n37["llama_numerics.rs"]
    n38["tests/model.rs"]
    n39["quant.rs"]
    n40["vision.rs"]
    n0 --> n15
    n0 --> n18
    n0 --> n28
    n0 --> n33
    n0 --> n36
    n0 --> n37
    n1 --> n28
    n1 --> n35
    n1 --> n39
    n2 --> n15
    n2 --> n18
    n2 --> n20
    n2 --> n28
    n2 --> n34
    n2 --> n39
    n3 --> n28
    n3 --> n33
    n3 --> n36
    n3 --> n37
    n4 --> n28
    n5 --> n13
    n5 --> n16
    n5 --> n17
    n5 --> n18
    n5 --> n19
    n5 --> n20
    n5 --> n21
    n5 --> n22
    n5 --> n23
    n5 --> n26
    n5 --> n27
    n5 --> n28
    n5 --> n29
    n5 --> n30
    n5 --> n39
    n5 --> n40
    n6 --> n28
    n6 --> n40
    n7 --> n15
    n7 --> n28
    n7 --> n32
    n7 --> n36
    n7 --> n38
    n7 --> n40
    n8 --> n28
    n8 --> n36
    n9 --> n28
    n9 --> n31
    n9 --> n36
    n9 --> n38
    n9 --> n40
    n10 --> n14
    n10 --> n16
    n10 --> n21
    n10 --> n23
    n10 --> n24
    n10 --> n25
    n10 --> n28
    n11 --> n28
    n11 --> n33
    n11 --> n36
    n11 --> n37
    n12 --> n15
    n12 --> n18
    n12 --> n26
    n12 --> n28
    n12 --> n30
    n12 --> n31
    n12 --> n35
    n12 --> n39
    classDef changed fill:#d9772c,color:#fff,stroke:#b85f1c;
    classDef changedFinding fill:#c0392b,color:#fff,stroke:#922b21;
    classDef dependent fill:#f2f0ec,color:#18140f,stroke:#d6d0c5;
    class n0 changed
    class n1 changed
    class n2 changed
    class n3 changed
    class n4 changed
    class n5 changed
    class n6 changed
    class n7 changed
    class n8 changed
    class n9 changed
    class n10 changed
    class n11 changed
    class n12 changed
    class n13 dependent
    class n14 dependent
    class n15 dependent
    class n16 dependent
    class n17 dependent
    class n18 dependent
    class n19 dependent
    class n20 dependent
    class n21 dependent
    class n22 dependent
    class n23 dependent
    class n24 dependent
    class n25 dependent
    class n26 dependent
    class n27 dependent
    class n28 dependent
    class n29 dependent
    class n30 dependent
    class n31 dependent
    class n32 dependent
    class n33 dependent
    class n34 dependent
    class n35 dependent
    class n36 dependent
    class n37 dependent
    class n38 dependent
    class n39 dependent
    class n40 dependent
Loading

What changed
Deterministic, computed from this commit's real scan and import graph - posted on every run, whether or not Flash Review found anything.

  • docs/USAGE.md · modified · +34/-0
  • src/backend.rs · modified · +35/-3 · +8 functions · 6 other dependents
  • src/config.rs · modified · +107/-8 · +1 function, +1 class · 3 other dependents
  • src/cpu_moe.rs · modified · +502/-4 · +16 functions · 6 other dependents
  • src/cuda/kernels.cu · modified · +176/-20
  • src/cuda/mod.rs · modified · +76/-30 · +3 functions · 4 other dependents
  • src/expert_cache.rs · modified · +1/-1 · 1 other dependent
  • src/gguf.rs · modified · +342/-140 · +7 functions · 16 other dependents
  • src/gpu_model.rs · modified · +1493/-120 · +17 functions, +6 classes · 2 other dependents
  • src/gpu_model/prefill.rs · modified · +138/-20
  • src/main.rs · modified · +181/-47 · +2 functions
  • src/model.rs · modified · +568/-12 · +6 functions · 6 other dependents
  • src/ngram.rs · modified · +66/-11 · +2 functions · 2 other dependents
  • src/ops.rs · modified · +29/-0 · +2 functions · 5 other dependents
  • src/serve.rs · modified · +16/-2 · 7 other dependents
  • src/vulkan/mod.rs · modified · +125/-42 · +10 functions · 4 other dependents
  • src/vulkan/preflight.rs · modified · +11/-7
  • src/vulkan/shaders/attn_decode.comp · modified · +44/-32
  • src/vulkan/shaders/attn_prefill.glsl · modified · +54/-31
  • src/vulkan/shaders/common.glsl · modified · +84/-11
  • +49 more changed file(s)

Aggregate deltas

  • Modules: +2
  • Dependency graph edges: +4
  • Commits: 2

@aletheore

aletheore Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Aletheore Flash review

No issues found in this diff.

Blast radius: 41 other file(s) depend on what this PR changes (21 directly, 20 indirectly)

Computed from the import graph of this exact commit, not guessed by a model. It sees imports only, so dynamic imports, reflection and non-code references are not counted.

  • src/model.rs is imported by src/cpu_worker.rs, src/expert_cache.rs, src/gpu_model/prefill.rs, src/lib.rs (+6 more)
  • src/cuda/mod.rs is imported by src/backend.rs, src/lib.rs, src/vulkan/mod.rs, src/vulkan/preflight.rs (+4 more)
  • src/cpu_moe.rs is imported by src/cpu_worker.rs, src/deepseek/mat.rs, src/gemma4/forward.rs, src/lib.rs (+2 more)
  • src/gpu_model.rs is imported by src/gpu_model/prefill.rs, src/lib.rs, src/main.rs, src/vulkan/mod.rs (+2 more)

Indirectly affected: examples/gguf_dump.rs, src/config.rs, src/deepseek/chat.rs, src/deepseek/forward.rs, src/deepseek/mod.rs, src/gemma4/mod.rs (+14 more)

@Vasyl-Trefilov

Copy link
Copy Markdown
Member Author

@ybenyedder here, I reopened it with all your changes and resolved conflicts, sorry again for this trouble!

ybenyedder pushed a commit to ybenyedder/RustingStrata that referenced this pull request Nov 19, 2026
Co-authored-by: Andrew Pavlov <100561417+andrew-pavlov-ua@users.noreply.github.com>
@ybenyedder

Copy link
Copy Markdown
Contributor

@Vasyl-Trefilov Thank you so much for rebasing #50 onto the new main (787935b) and resolving the src/vulkan/mod.rs and src/main.rs conflicts in #56 — no worries at all about #50!

I checked out carry-pr50 (daec86d3) locally and verified your conflict resolutions + full build/test suite + live Vulkan GPU parity:

  • SPIR-V & Build: ./tools/gen_spirv.sh clean (0 diffs); cargo check --all-targets --locked and cargo build --release --bin rustingstrata both pass with 0 warnings.
  • Full Release Test Suite (cargo test --release --lib --bins --tests): 21 test binaries — 267 passed, 0 failed, 6 ignored, 0 compiler warnings (including tests/hy4_forward.rs, hy4::chat::template_and_calls, model::tests::dflash_safetensors_auto_converts_and_loads, model::tests::sensenova_loop_mhc_cpu_matches_gpu, model::tests::standalone_external_draft_model_spec_step_matches_greedy, tests/kv_q8.rs, and tests/serve.rs).
  • Live Vulkan RDNA 3.5 (AMD Radeon 890M) on daec86d3:
    • Qwen3.6-35B-A3B-UD-IQ4_XS (--kv q4 --ctx 4096 --chunk 512 --temp 0 -n 128, 10240/10240 resident experts): 100% SHA-256 greedy parity (c352930ffe32579491ea3392f9684ac310be91e645742a97ac2f0b013691d389) across baseline --mtp 0 (14.15 tok/s), DFlash Q4_0 (24.74 tok/s, 101/329 = 30.7% accept), and DFlash2 Q4_0 (30.18 tok/s, 101/189 = 53.4% accept).
    • SenseNova-Loop-9B-Q8_0.gguf (--gpu --backend vulkan): coherent output (12.12 tok/s prompt, 2.58 tok/s decode).
    • Live serve (Qwen3.6-35B-A3B-UD-Q4_K_M + DFlash Q4_0 + --ctx 131072 --kv q4): 10240/10240 resident experts, HTTP 200 verified.

Everything looks completely solid and I am finished with changes on this PR — it is ready for review/merge! (Also, if you'd prefer the scratch/design notes in docs/WP*.md and docs/PR_*.md trimmed from the PR before merge, just let me know and I'm happy to drop them.)

…; notes and personal scripts removed

- DFlash metadata is checked against the tensor shapes at load (dflash_check): GPU buffers were sized from the
  metadata and written by the real tensor shape.
- CUDA: dflash2_conv was missing from the kernel list, so the first DFlash2 block panicked.
- A DFlash draft now replaces the target's own MTP head; it was ignored without a message for a target GGUF
  that has one.
- Vulkan: conv_seq runs before the batch in the DeltaNet mixer. It is two dependent dispatches and a batch has
  no barrier between them (verify_matches_steps and qwen4exp_mtp_matches failed).
- The AVX-512 CPU expert kernels are opt-in (STRATA_AVX512=1): their last-bit differences changed greedy output
  (mtp_spec_matches_greedy and qwen4exp_mtp_matches failed on a native build).
- STRATA_EXPERT_MB=0 means no expert slots again (split_matches_single).
- The adaptive block size no longer panics for a block size under 8.
- docs/USAGE.md: --draft-model, --spec-type, --kv q4/q2, --thinking-budget, --short, STRATA_AVX512.
- Removed docs/WP*.md, docs/PR_*.md and three benchmark scripts with machine-specific paths.
@Vasyl-Trefilov
Vasyl-Trefilov merged commit 9e00944 into main Nov 19, 2026
3 checks passed
@Vasyl-Trefilov

Copy link
Copy Markdown
Member Author

So, I found some issues and bugs and fixed them, but I didn't really understood why you did it? I mean I have not got any speed up or something like that, so I don't really understand what it was used for? And why do you want it?

@ybenyedder

Copy link
Copy Markdown
Contributor

@Vasyl-Trefilov Thanks a lot for merging #56 and especially for catching & fixing those real bugs in 5675724 (CUDA dflash2_conv, the conv_seq barrier in DeltaNet batches, dflash_check, STRATA_AVX512=1 opt-in, replacing an embedded MTP head when --draft-model is passed, and cleaning up the scratch notes/scripts)!

Totally fair question — here is why nothing would look faster in a plain single-token NVIDIA run, and what each part of the PR is actually for:

1. Target hardware: AMD RDNA 3.5 iGPU (Radeon 890M, unified LPDDR5X)

  • On a discrete NVIDIA GPU with fast VRAM, single-token decode won't change from this PR.
  • My machine is a mini-PC with an AMD Radeon 890M iGPU (gfx1150) sharing ~27 GiB of LPDDR5X (~100 GB/s practical bandwidth), where RADV only reports ~11.8 GiB free heap by default.
  • gemmi_16 (RDNA 3.5 16x16x16 int8 WMMA), zero-spill RADV decode/MoE shaders (moe_gemv_q, moe_down_combine, attn_decode), strided DeltaNet snapshots (SNAP_STRIDE=4), and STRATA_EXPERT_MB were written so Qwen3.6-35B-A3B fits 100% resident (10240/10240 experts) in Vulkan on AMD APUs without spilling to CPU or OOMing.

2. Where the 1.7x–4.5x speedup comes from (--draft-model + --spec-type)

  • Plain generate without a draft model is still memory-bandwidth bound (~14.1 tok/s on 890M).
  • The speedup only turns on when passing a DFlash/DFlash2 block-diffusion drafter + context cascade, e.g.:
    rustingstrata serve --gpu --backend vulkan \
      --model Qwen3.6-35B-A3B-UD-IQ4_XS.gguf \
      --draft-model ./zlab-dflash \
      --spec-type suffix,ngram-simple,ngram-cache --spec-draft-n-max 16 \
      --ctx 262144 --kv q4
    (Note: on GGUFs that already ship an embedded MTP head, DFlash was actually being silently ignored before your fix in 5675724 — which may also be why you didn't see any speedup if you tested on one!)
  • Measured on Qwen3.6-35B-A3B-UD-IQ4_XS on Radeon 890M (100% SHA-256 bit-identical to --mtp 0 at 14.1 tok/s):
    • DFlash v1 (z-lab/Qwen3.5-35B-A3B-DFlash, auto-converted to Q4_0 223 MiB): 18.9–24.7 tok/s fresh Rust code, ~41 tok/s math, ~50.3 tok/s code refactoring, 65.5 tok/s synthetic bench (88.7% accept).
    • DFlash2 (incoai/Qwen3.6-35B-A3B-DFlash2, 360 MiB Q4_0): 20.7–30.2 tok/s fresh Rust (53.4% accept).

3. Capacity & community model support

  • --kv q4 / --kv q2: lets Qwen3.6-35B-A3B + 128K–512K context stay 100% resident in 27 GiB unified RAM (instead of evicting experts at f16/q8 KV).
  • --thinking-budget: optional cap on runaway <think> loops when serving.
  • sensenova_loop (SenseNova-Loop-9B mHC) & standalone external causal drafts (--draft-model <standalone.gguf>, e.g. nvidia/Qwen3.5-4B-Qwen3.8-27B-Draft): new architectures/drafters from recent releases.

I just synced onto main (a416fc3) and verified cargo check --all-targets --locked (0 warnings), cargo build --release --bin rustingstrata (0 warnings), cargo test --release --lib --bins --tests (21 test binaries: 267 passed, 0 failed, 6 ignored), and live serve on :8011 (Qwen3.6-35B-A3B-UD-Q4_K_M + DFlash Q4_0 + --ctx 131072 --kv q4, HTTP 200) — everything with your fixes passes cleanly!

@Vasyl-Trefilov

Copy link
Copy Markdown
Member Author

okay, this is very cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants