Skip to content

feat(quant): support GGML_TYPE_NVFP4 (40), GGML_TYPE_F8_E4M3 (43), and scalar & per-expert .scale across CPU, Vulkan, and CUDA - #68

Merged
Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
ybenyedder:feat/nvfp4-f8e4m3-scale
Nov 22, 2026
Merged

Vasyl-Trefilov merged 1 commit into
RustingStudio:mainfrom
ybenyedder:feat/nvfp4-f8e4m3-scale

Conversation

@ybenyedder

Copy link
Copy Markdown
Contributor

Summary

Adds full end-to-end support for GGML_TYPE_NVFP4 (id 40), GGML_TYPE_F8_E4M3 (id 43), and companion <stem>.scale tensors (both scalar [1] F32 per-tensor scales and 1-D [n_expert] F32 per-expert scales) across CPU, Vulkan, and CUDA.

Changes

  1. GGML_TYPE_NVFP4 (id = 40, 36 bytes / 64 elements):
    • 4 sub-blocks of 16 elements per 36-byte block: 4 unsigned FP8 E4M3 (UE4M3, 0x00 and 0x7F -> 0.0) sub-block scales at bytes 0..4 and 4 × 8 bytes of packed 4-bit E2M1 nibbles (lo = 0..8, hi = 8..16) at bytes 4..36 using KVALUES_FP4 = [0, 1, 2, 3, 4, 6, 8, 12, 0, -1, -2, -3, -4, -6, -8, -12] with ue4m3_to_f32 halved to match 2 × E2M1.
    • Implemented in src/quant/mod.rs (dequant_row, nvfp4), src/cpu_moe.rs (dot_nvfp4 against Q8_K), src/vulkan/shaders/deq.glsl (qi8 and shared-memory 2-codes-per-lookup s_iq4 in qw), and src/cuda/kernels.cu (QI<40>, deq8<40>, fp4_lut).
  2. GGML_TYPE_F8_E4M3 (id = 43, 1 byte / element):
    • Signed OCP FP8 E4M3 (0x7F/0xFF NaN -> 0.0), classified as float_x() (decoded directly without block-integer Q8_1 activation quantization).
    • Implemented in src/quant/mod.rs (f8_e4m3_to_f32), src/vulkan/shaders/deq.glsl (deq8 T == 43), and src/cuda/kernels.cu (deq8<43>, FTYPES).
  3. Companion <stem>.scale Tensors (Scalar [1] F32 & Per-Expert 1-D [n_expert] F32):
    • Weights::get (src/weights.rs) and Mat::load / Mat::expert_host (src/deepseek/mat.rs) automatically load <stem>.scale (e.g. ModelOpt weight_scale_2 in Qwen3.8-27B-NVFP4-QAD.gguf or per-expert 1-D [n_expert] scales in Inkling-2-Mini mainline GGUFs) and attach scale: f32 to Tensor, Mat, and GpuTensor.
    • Applied across CPU (Tensor::row, cpu_moe::matvec, cpu_moe::batch_rows, Mat::row_into, Mat::matvec, matvec_rows), Vulkan (dequant.comp, gemv_f.comp, gemv_q.comp, gemm_f.glsl, gemmi.glsl), and CUDA (dequant_*, gemv_*, gemvn_*, gemm_*, gemms_*, gemmi_*, gemmis_*), including paired gemvq2 with distinct sc1/sc2 scales and act = 0, 1, 2 in prefill GEMM.

Verification

  • cargo test --release --lib --locked: 96/96 passed
  • cargo test --release --test quant --locked: 25/25 passed (nvfp4_and_f8_e4m3_blocks ok)
  • cargo test --release --test backend --locked: 10/10 passed (spirv_is_fresh ok)
  • cargo test --release --test kernels --locked: tensor_scale_applies_to_all_gpu_ops, vulkan_type_matrix (all 30 GgmlType variants), and gemv_partial_superblocks_and_q2_0 passed on AMD Radeon 890M (gfx1150, RADV Vulkan).
  • Live GPU verification on Qwen3.8-27B-NVFP4-QAD.gguf (--gpu --backend vulkan).
  • Note: CUDA changes in src/cuda/kernels.cu and src/cuda/mod.rs are kept in lockstep (cuda::tests::kernel_names passes) and not compiled on this AMD host.

@aletheore

aletheore Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Aletheore Flash review

No issues found in this diff.

Note: 14 of 36 changed file(s) were not included in this review (the review reads at most 30 files, and skips any file over 100KB). Issues in them would not be found, and citations pointing into them could not be checked against the real file: src/vulkan/spv/dequant.spv, src/vulkan/spv/gemm_f.spv, src/vulkan/spv/gemm_f_dp.spv, src/vulkan/spv/gemm_fs.spv, src/vulkan/spv/gemmi.spv, src/vulkan/spv/gemmi_16.spv, src/vulkan/spv/gemmi_dp.spv, src/vulkan/spv/gemmi_s.spv, src/vulkan/spv/gemmi_s_16.spv, src/vulkan/spv/gemv_f.spv….

Blast radius: 39 other file(s) depend on what this PR changes (31 directly, 8 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/backend.rs is imported by src/cpu_worker.rs, src/expert_cache.rs, src/gpu_model.rs, src/gpu_model/prefill.rs (+7 more)
  • src/model.rs is imported by src/cpu_worker.rs, src/expert_cache.rs, src/gpu_model.rs, src/gpu_model/prefill.rs (+6 more)
  • src/quant/mod.rs is imported by src/deepseek/mod.rs, src/expert_cache.rs, src/gguf.rs, src/glm/mod.rs (+5 more)
  • src/weights.rs is imported by src/cpu_worker.rs, src/expert_cache.rs, src/gguf.rs, src/gpu_model/prefill.rs (+3 more)
  • src/vulkan/mod.rs is imported by src/lib.rs, src/vulkan/preflight.rs, tests/common/mod.rs, tests/gpu_model.rs (+2 more)
  • src/cuda/mod.rs is imported by src/lib.rs, src/vulkan/preflight.rs, tests/common/mod.rs, tests/gpu_model.rs (+2 more)
  • ...and 5 more changed file(s) with dependents

Indirectly affected: examples/gguf_dump.rs, src/config.rs, src/deepseek/chat.rs, src/gemma4/mod.rs, src/ngram.rs, src/tokenizer.rs (+2 more)

@aletheore

aletheore Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

🔍 Aletheore evidence diff

graph LR
    n0["backend.rs"]
    n1["src/cpu_moe.rs"]
    n2["src/cuda/mod.rs"]
    n3["mat.rs"]
    n4["src/hy4/forward.rs"]
    n5["src/inkling2/forward.rs"]
    n6["src/model.rs"]
    n7["src/quant/mod.rs"]
    n8["mmproj.rs"]
    n9["src/vulkan/mod.rs"]
    n10["weights.rs"]
    n11["cpu_worker.rs"]
    n12["src/deepseek/forward.rs"]
    n13["src/deepseek/mod.rs"]
    n14["expert_cache.rs"]
    n15["src/gemma4/forward.rs"]
    n16["src/gguf.rs"]
    n17["src/glm/forward.rs"]
    n18["src/glm/mod.rs"]
    n19["src/gpu_model.rs"]
    n20["prefill.rs"]
    n21["src/hy4/chat.rs"]
    n22["src/hy4/mod.rs"]
    n23["src/inkling2/chat.rs"]
    n24["device.rs"]
    n25["src/inkling2/mod.rs"]
    n26["lib.rs"]
    n27["main.rs"]
    n28["src/vision/mod.rs"]
    n29["projector.rs"]
    n30["prompt.rs"]
    n31["preflight.rs"]
    n32["spv_smem.rs"]
    n33["tests/common/mod.rs"]
    n34["tests/cpu_moe.rs"]
    n35["tests/gguf.rs"]
    n36["tests/gpu_model.rs"]
    n37["kv_q8.rs"]
    n38["llama_numerics.rs"]
    n39["tests/model.rs"]
    n40["quant_ggml.rs"]
    n41["vision.rs"]
    n0 --> n11
    n0 --> n14
    n0 --> n19
    n0 --> n20
    n0 --> n24
    n0 --> n26
    n0 --> n27
    n0 --> n33
    n0 --> n36
    n0 --> n37
    n0 --> n38
    n1 --> n11
    n1 --> n15
    n1 --> n26
    n1 --> n34
    n2 --> n26
    n2 --> n31
    n2 --> n33
    n2 --> n36
    n2 --> n37
    n2 --> n38
    n3 --> n12
    n3 --> n13
    n3 --> n15
    n3 --> n17
    n3 --> n24
    n3 --> n27
    n4 --> n21
    n4 --> n22
    n4 --> n27
    n5 --> n23
    n5 --> n24
    n5 --> n25
    n5 --> n27
    n6 --> n11
    n6 --> n14
    n6 --> n19
    n6 --> n20
    n6 --> n26
    n6 --> n27
    n6 --> n30
    n6 --> n36
    n6 --> n39
    n6 --> n41
    n7 --> n13
    n7 --> n14
    n7 --> n16
    n7 --> n18
    n7 --> n19
    n7 --> n26
    n7 --> n31
    n7 --> n32
    n7 --> n40
    n8 --> n28
    n8 --> n29
    n8 --> n33
    n9 --> n26
    n9 --> n31
    n9 --> n33
    n9 --> n36
    n9 --> n37
    n9 --> n38
    n10 --> n11
    n10 --> n14
    n10 --> n16
    n10 --> n20
    n10 --> n26
    n10 --> n29
    n10 --> n35
    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 dependent
    class n12 dependent
    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
    class n41 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.

  • src/backend.rs · modified · +10/-7 · +1 function · 11 other dependents
  • src/cpu_moe.rs · modified · +84/-7 · +3 functions · 4 other dependents
  • src/cuda/kernels.cu · modified · +82/-37
  • src/cuda/mod.rs · modified · +22/-22 · 6 other dependents
  • src/deepseek/mat.rs · modified · +56/-25 · 6 other dependents
  • src/hy4/forward.rs · modified · +1/-1 · 3 other dependents
  • src/inkling2/forward.rs · modified · +1/-1 · 4 other dependents
  • src/model.rs · modified · +2/-2 · 10 other dependents
  • src/quant/mod.rs · modified · +56/-14 · +1 function · 9 other dependents
  • src/quant/tables.rs · modified · +1/-0
  • src/vision/mmproj.rs · modified · +1/-1 · 3 other dependents
  • src/vulkan/mod.rs · modified · +11/-11 · 6 other dependents
  • src/vulkan/shaders/deq.glsl · modified · +25/-4
  • src/vulkan/shaders/dequant.comp · modified · +2/-2
  • src/vulkan/shaders/gemm_f.glsl · modified · +2/-2
  • src/vulkan/shaders/gemmi.glsl · modified · +2/-2
  • src/vulkan/shaders/gemv_f.comp · modified · +2/-2
  • src/vulkan/shaders/gemv_q.comp · modified · +3/-2
  • src/vulkan/spv/dequant.spv · modified
  • src/vulkan/spv/gemm_f.spv · modified
  • +16 more changed file(s)

Aggregate deltas

  • Modules: +0
  • Dependency graph edges: +1
  • Commits: 1

@ybenyedder
ybenyedder force-pushed the feat/nvfp4-f8e4m3-scale branch from 45b8916 to dcb5411 Compare November 22, 2026 07:29
@ybenyedder
ybenyedder force-pushed the feat/nvfp4-f8e4m3-scale branch from dcb5411 to 0ceb068 Compare November 22, 2026 07:52
@Vasyl-Trefilov
Vasyl-Trefilov merged commit 4ff5f9d into RustingStudio:main Nov 22, 2026
6 checks passed
Vasyl-Trefilov added a commit that referenced this pull request Nov 22, 2026
…wice), the per-expert .scale of an Inkling 2 file is applied once (it was applied twice, which broke the output), and the generated tables.rs is as tools/gen_tables.py writes it
@Vasyl-Trefilov

Copy link
Copy Markdown
Member

@ybenyedder Merged, ty. Three fixes went on top in 19bfba8, all found by running it on a CUDA card:

  • src/cuda/kernels.cu defined QI<40> and deq8<40> twice, so NVRTC did not compile the source and no CUDA model loaded.
  • The per-expert .scale of an Inkling 2 file was applied twice, which broke the output of Inkling-2-Mini-UD-Q4_K_M.gguf. It is applied once now.
  • src/quant/tables.rs is generated by tools/gen_tables.py. The hand edit is replaced by what the script writes.

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