Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

Mixed expert formats: an NVFP4 projection beside another format in the same expert (e.g. NVFP4 gate/up + Q8_0 down)

sergiywithf235e394c17e
diff --git a/src/kernels/cpu/native_expert.cpp b/src/kernels/cpu/native_expert.cpp
index 5b54e4743..63998107a 100644
--- a/src/kernels/cpu/native_expert.cpp
+++ b/src/kernels/cpu/native_expert.cpp
@@ -76,11 +76,9 @@ bool native_fmt(int gu_type, int d_type, int64_t n_embd, int64_t n_ff, NativeFmt
f.up_off = f.gu_row * (size_t) n_ff;
f.down_off = 2 * f.up_off;
f.bytes = f.down_off + f.d_row * (size_t) n_embd;
+ // NVFP4 in any projection: one scale tail per blob; a mixed expert (e.g. NVFP4 gate/up + Q8_0 down) has 1.0 for
+ // the other projection's scale (tools/iq_pack.py), so every path below multiplies by the tail unconditionally
if (gu_type == kNvfp4Type || d_type == kNvfp4Type) {
- if (gu_type != kNvfp4Type || d_type != kNvfp4Type) {
- err = "native experts: NVFP4 must cover gate, up and down alike (one scale tail per blob)";
- return false;
- }
f.tail_off = f.bytes;
f.bytes += kNvfp4Tail;
}
diff --git a/src/kernels/cuda/iq_kernels.cu b/src/kernels/cuda/iq_kernels.cu
index 42ba5136f..c62d7f382 100644
--- a/src/kernels/cuda/iq_kernels.cu
+++ b/src/kernels/cuda/iq_kernels.cu
@@ -2881,7 +2881,9 @@ NativeExpertLayout native_expert_layout(int gu_type, int d_type, int64_t n_embd,
L.up_off = (size_t) n_ff * L.gu_row;
L.down_off = 2 * L.up_off;
L.bytes = L.down_off + (size_t) n_embd * L.d_row;
- if (gu_type == 40 && d_type == 40) { // NVFP4: {s_gate, s_up, s_down, 0} after down (tools/iq_pack.py)
+ // NVFP4: {s_gate, s_up, s_down, 0} after down (tools/iq_pack.py) whenever any projection is NVFP4; a mixed
+ // expert's other projection has 1.0 there, so the kernels below multiply by it unconditionally
+ if (gu_type == 40 || d_type == 40) {
L.tail_off = L.bytes;
L.bytes += 16;
}
diff --git a/src/kernels/native_expert_parity.cpp b/src/kernels/native_expert_parity.cpp
index 01133fad5..3c979ebb9 100644
--- a/src/kernels/native_expert_parity.cpp
+++ b/src/kernels/native_expert_parity.cpp
@@ -61,6 +61,13 @@ int check_blob(const cpu::NativeFmt& f, const std::vector<uint8_t>& blob, int se
tg->to_float(blob.data() + f.up_off + r * f.gu_row, U.data() + r * H, H);
}
for (int64_t r = 0; r < H; ++r) td->to_float(blob.data() + f.down_off + r * f.d_row, D.data() + r * FF, FF);
+ if (f.tail_off) { // the blob's scales, as the engine applies them (gate and up as read, down on its output)
+ float tail[4];
+ std::memcpy(tail, blob.data() + f.tail_off, sizeof tail);
+ for (auto& v : G) v *= tail[0];
+ for (auto& v : U) v *= tail[1];
+ for (auto& v : D) v *= tail[2];
+ }
std::mt19937 rng(11 + seed);
std::normal_distribution<float> nd(0.f, 1.f);
std::vector<float> x((size_t) NT * H);
@@ -404,6 +411,11 @@ std::vector<uint8_t> synthetic_blob(const cpu::NativeFmt& f, int seed) {
quant(f.gu_type, FF, H, blob.data());
quant(f.gu_type, FF, H, blob.data() + f.up_off);
quant(f.d_type, H, FF, blob.data() + f.down_off);
+ if (f.tail_off) { // NVFP4's {s_gate, s_up, s_down, 0}: non-unit for an NVFP4 projection, 1.0 for another format
+ const bool g4 = f.gu_type == 40, d4 = f.d_type == 40;
+ const float tail[4] = {g4 ? 0.75f : 1.f, g4 ? 1.25f : 1.f, d4 ? 0.5f : 1.f, 0.f};
+ std::memcpy(blob.data() + f.tail_off, tail, sizeof tail);
+ }
return blob;
}
diff --git a/tools/iq_pack.py b/tools/iq_pack.py
index 41ca26d18..cb3618247 100644
--- a/tools/iq_pack.py
+++ b/tools/iq_pack.py
@@ -458,9 +458,8 @@ def expert_layout(model: Model, src: pathlib.Path):
# no room for. Each blob gets a 16-byte tail {s_gate, s_up, s_down, 0} so every consumer that already
# moves whole blobs (the arena, the device cache, the prefill staging, a second GPU) moves the scales
# with the weights; the gate/up kernels read it. See tools/nvfp4_convert.py.
- if ts[0].type_name != "NVFP4" or ts[2].type_name != "NVFP4":
- return "layer %d: NVFP4 must cover gate, up and down alike" % l
- if any("blk.%d.ffn_%s_exps.scale" % (l, r) not in T for r in ROLES):
+ # a mixed expert (e.g. NVFP4 gate/up + Q8_0 down) has the tail too, with 1.0 for the other projection
+ if any(t.type_name == "NVFP4" and "blk.%d.ffn_%s_exps.scale" % (l, r) not in T for t, r in zip(ts, ROLES)):
return "layer %d: NVFP4 experts without their .scale tensors" % l
blob += NVFP4_TAIL
layout.append((l, ts[0].type_id, ts[2].type_id, offset, blob, ts))
@@ -538,7 +537,7 @@ def main() -> int:
print(got)
return 1
layout, text, n_expert, offset = got
- if any(ts[0].type_name == "NVFP4" for *_, ts in layout):
+ if any(t.type_name == "NVFP4" for *_, ts in layout for t in ts):
a.experts_bin = True # the GGUF holds the weights without the scale tails: the engine must read experts.bin
path = out / "experts.bin"
sidecar = out / "experts.bin.src.json"
@@ -546,8 +545,10 @@ def main() -> int:
reuse = path.exists() and path.stat().st_size == offset and read_json(sidecar) == want
def nvfp4_tails(l, ts):
- """(n_expert, NVFP4_TAIL) bytes {s_gate, s_up, s_down, 0} of an NVFP4 layer; None for a bad scale."""
- s = [np.frombuffer(model.bytes("blk.%d.ffn_%s_exps.scale" % (l, r)).tobytes(), dtype=np.float32) for r in ROLES]
+ """(n_expert, NVFP4_TAIL) bytes {s_gate, s_up, s_down, 0} of a layer with an NVFP4 projection; a projection in
+ another format gets exactly 1.0 (the engine multiplies by the tail unconditionally). None for a bad scale."""
+ s = [np.frombuffer(model.bytes("blk.%d.ffn_%s_exps.scale" % (l, r)).tobytes(), dtype=np.float32)
+ if t.type_name == "NVFP4" else np.ones(n_expert, dtype=np.float32) for t, r in zip(ts, ROLES)]
tail = np.zeros((n_expert, NVFP4_TAIL // 4), dtype=np.float32)
tail[:, 0], tail[:, 1], tail[:, 2] = s[0], s[1], s[2]
if not np.isfinite(tail).all() or (tail[:, :3] <= 0).any():
@@ -558,7 +559,7 @@ def main() -> int:
def layer_blobs(l, blob, ts):
"""(n_expert, blob): gate | up | down [| the NVFP4 scale tail] per expert; None for a bad NVFP4 scale."""
parts = [model.bytes(t.name).reshape(n_expert, -1) for t in ts]
- if ts[0].type_name == "NVFP4":
+ if any(t.type_name == "NVFP4" for t in ts):
tail = nvfp4_tails(l, ts)
if tail is None:
return None
@@ -576,7 +577,7 @@ def main() -> int:
for e in (0, n_expert - 1):
f.seek(off + e * blob)
want_blob = b"".join(model.bytes(t.name).reshape(n_expert, -1)[e].tobytes() for t in ts)
- if ts[0].type_name == "NVFP4":
+ if any(t.type_name == "NVFP4" for t in ts):
tail = nvfp4_tails(l, ts)
want_blob += tail[e].tobytes() if tail is not None else b""
if f.read(blob) != want_blob: