Files
strata/src/kernels/nvfp4_expert_gpu_parity.cpp
T
sergiywith e51b4379dc NVFP4 routed experts: converter, pack, decode, prompt path, AVX-512 CPU rows
A ModelOpt NVFP4 checkpoint of Qwen3.8-Flash-Next (its routed experts NVFP4, group 16) runs on the native path.
Rebased on 0.1.31: NVFP4 is type 40 in the per-role format lists (STRATA_GU_FMTS / STRATA_D_FMTS / STRATA_MMVQ_FMTS)
beside Q4_K/Q5_K/Q5_1/Q8_0, and 0.1.31's dq_q8_0 serves both.

- tools/nvfp4_convert.py: llama.cpp's converter with the engine's type policy (BF16 control projections, Q8_0 native
  tensors and token embedding, no PLE table); tools/nvfp4_verify.py / nvfp4_verify_gguf.py prove the experts bit for
  bit.
- tools/iq_pack.py: expert_layout() gives NVFP4 blobs a 16-byte tail {s_gate, s_up, s_down, 0} (the per-expert
  weight_scale_2), so every consumer that moves whole blobs moves the scales; an NVFP4 pack always writes experts.bin
  (the GGUF has no tails, so the in-place GGUF read does not apply).
- decode: NVFP4 in the native expert kernels (q8_1, UE4M3/E2M1 decoded in software, bit-identical to ggml); each scale
  on its own projection's FP32 output (folding s_down into up put the hidden into FP16's subnormals).
- CPU pool: AVX-512 NVFP4 rows (1.8-3.7x ggml-cpu's), ggml-cpu elsewhere.
- prompt path: llama.cpp MMQ - W4A8 by default (int8 MMQ with Blackwell's FP4 MMA hidden, mmq_nvfp4_w4a8.cu), W4A4
  and FP16 through STRATA_PREFILL_NVFP4; the scales applied where they are read.
- CMake: the architectures stay as given. Only mmq_nvfp4_w4a4.cu (the FP4 x FP4 kernel and its FP4 activation
  quantizer, symbols renamed) is built with 12x -> 12xa in strata_mmq_w4a4; without its image W4A4 falls back to W4A8.
  strata.exe: 55 sm_120 cubins + 1 sm_120a.
- 0.25 of the missed experts over PCIe for NVFP4 packs (their blobs are 2.76 MB).
- Parity tools (mmq_nvfp4_parity, nvfp4_avx512_parity, nvfp4_expert_gpu_parity); HIP guards; docs/NVFP4.md.

Checked on this rebase (RTX 5090): the parity tools as before (decode experts 1.09-1.16%, MMQ W4A8 0.53%, W4A4 8.6%
- the same numbers as the 120a-everywhere build); IQ2_XS 128 greedy tokens identical to 0.1.31 main; the NVFP4 pack
against the 0.1.29-based build at the same expert-cache size: identical tokens on a 24-token and an 8K prompt, in W4A8
and in W4A4 (prefill 4,231 / 4,583 tok/s on 8K).

Port note: the PCIe share base for NVFP4 packs (0.25) sits beside 0.1.43's PcieAuto start values.

(cherry picked from commit 1f8a12858cc4ed80a977767afef6b6c3a754ac63 of sergqwer/strata-nvfp4)
2026-10-12 20:29:45 +02:00

108 lines
5.6 KiB
C++

// src/kernels/nvfp4_expert_gpu_parity.cpp - the decode path's GPU experts (native_expert_grouped) on real NVFP4
// blobs with their global scales, against a double-precision expert.
//
// nvfp4_expert_gpu_parity pack/experts.bin [layer expert ...]
//
// Eight tokens of N(0,1) activations through one expert: q8_1 input, gate/up with s_gate / s_up, SwiGLU, q8_1 of the
// hidden, down with s_down on its output. Only the two activation roundings separate it from the reference (~1%).
#include "strata/kernels/iq_kernels.hpp"
#include "ggml.h"
#include <cuda_runtime.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <random>
#include <vector>
namespace k = strata::kernels;
namespace {
void ck(cudaError_t e, const char* what) {
if (e != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", what, cudaGetErrorString(e)); std::exit(1); }
}
} // namespace
int main(int argc, char** argv) {
if (argc < 2) { std::fprintf(stderr, "usage: nvfp4_expert_gpu_parity pack/experts.bin [layer expert ...]\n"); return 2; }
constexpr int64_t N = 2560, FF = 640;
constexpr int T = 8;
const k::NativeExpertLayout L = k::native_expert_layout(40, 40, N, FF);
const size_t layer_bytes = 512 * L.bytes; // L.bytes counts the 16-byte tail
std::vector<int> picks;
for (int i = 2; i + 1 < argc; i += 2) { picks.push_back(std::atoi(argv[i])); picks.push_back(std::atoi(argv[i + 1])); }
if (picks.empty()) picks = {0, 0, 0, 7, 12, 100, 24, 300, 40, 5, 47, 511};
std::ifstream f(argv[1], std::ios::binary);
std::mt19937 rng(9);
std::normal_distribution<float> nd(0.f, 1.f);
const ggml_type_traits* tt = ggml_get_type_traits(GGML_TYPE_NVFP4);
double worst = 0;
for (size_t pi = 0; pi < picks.size(); pi += 2) {
const int layer = picks[pi], e = picks[pi + 1];
std::vector<uint8_t> blob(L.bytes);
f.seekg((std::streamoff) (layer * layer_bytes + (size_t) e * L.bytes));
if (!f.read((char*) blob.data(), (std::streamsize) blob.size())) { std::fprintf(stderr, "cannot read\n"); return 2; }
float tail[4];
std::memcpy(tail, blob.data() + L.tail_off, sizeof tail);
std::vector<float> wg((size_t) FF * N), wu((size_t) FF * N), wd((size_t) N * FF), x((size_t) T * N);
tt->to_float(blob.data(), wg.data(), FF * N);
tt->to_float(blob.data() + L.up_off, wu.data(), FF * N);
tt->to_float(blob.data() + L.down_off, wd.data(), N * FF);
for (float& v : x) v = nd(rng);
std::vector<double> ref((size_t) T * N);
for (int t = 0; t < T; ++t) {
std::vector<double> h(FF);
for (int64_t r = 0; r < FF; ++r) {
double g = 0, u = 0;
for (int64_t c = 0; c < N; ++c) {
g += (double) wg[(size_t) r * N + c] * x[(size_t) t * N + c];
u += (double) wu[(size_t) r * N + c] * x[(size_t) t * N + c];
}
g *= tail[0];
u *= tail[1];
h[r] = g / (1 + std::exp(-g)) * u;
}
for (int64_t r = 0; r < N; ++r) {
double s = 0;
for (int64_t c = 0; c < FF; ++c) s += (double) wd[(size_t) r * FF + c] * h[c];
ref[(size_t) t * N + r] = s * tail[2];
}
}
uint8_t* db; float *dx, *dout; void *dxq, *scr; unsigned long long* dptr; int32_t *dstart, *dng, *ddst, *dtok;
ck(cudaMalloc(&db, blob.size()), "blob");
ck(cudaMemcpy(db, blob.data(), blob.size(), cudaMemcpyHostToDevice), "blob");
ck(cudaMalloc(&dx, x.size() * 4), "x");
ck(cudaMemcpy(dx, x.data(), x.size() * 4, cudaMemcpyHostToDevice), "x");
ck(cudaMalloc(&dxq, (size_t) T * N / 32 * 36), "xq");
ck(cudaMalloc(&dout, (size_t) T * N * 4), "out");
ck(cudaMalloc(&scr, k::native_expert_scratch_bytes(T, FF)), "scratch");
const unsigned long long ptr = (unsigned long long) db;
const int32_t start[2] = {0, T}, ng = 1, ids[T] = {0, 1, 2, 3, 4, 5, 6, 7};
ck(cudaMalloc(&dptr, 8), "p"); ck(cudaMemcpy(dptr, &ptr, 8, cudaMemcpyHostToDevice), "p");
ck(cudaMalloc(&dstart, 8), "s"); ck(cudaMemcpy(dstart, start, 8, cudaMemcpyHostToDevice), "s");
ck(cudaMalloc(&dng, 4), "n"); ck(cudaMemcpy(dng, &ng, 4, cudaMemcpyHostToDevice), "n");
ck(cudaMalloc(&ddst, 4 * T), "d"); ck(cudaMemcpy(ddst, ids, 4 * T, cudaMemcpyHostToDevice), "d");
ck(cudaMalloc(&dtok, 4 * T), "t"); ck(cudaMemcpy(dtok, ids, 4 * T, cudaMemcpyHostToDevice), "t");
k::quantize_q8_1_rows(dx, T, N, dxq, nullptr);
k::native_expert_grouped(L, dptr, dstart, dng, ddst, dtok, 1, T, dxq, scr, dout, nullptr);
ck(cudaDeviceSynchronize(), "run");
std::vector<float> got((size_t) T * N);
ck(cudaMemcpy(got.data(), dout, got.size() * 4, cudaMemcpyDeviceToHost), "out");
for (void* p : {(void*) db, (void*) dx, dxq, (void*) dout, scr, (void*) dptr, (void*) dstart, (void*) dng,
(void*) ddst, (void*) dtok})
cudaFree(p);
double num = 0, den = 0;
for (size_t i = 0; i < got.size(); ++i) { num += (got[i] - ref[i]) * (got[i] - ref[i]); den += ref[i] * ref[i]; }
const double rel = std::sqrt(num / den);
worst = std::fmax(worst, rel);
std::printf("layer %2d expert %3d: s_gate %.3g s_up %.3g s_down %.3g -> expert output rel. error %.3f%%\n", layer,
e, tail[0], tail[1], tail[2], 100 * rel);
}
std::printf("RESULT: %s (worst %.3f%%)\n", worst < 0.03 ? "ok" : "TOO FAR", 100 * worst);
return worst < 0.03 ? 0 : 1;
}