Repository navigation
npu: the per-op forward runs GPT-OSS-20B-NPU2 on the host: D2560/MXFP4 chunks, sinks, o bias, YaRN, the top-k MoE (Refs #1786) - #2274
Conversation
…MX2560 MXFP4 experts) (Refs #1786) Step 1 of the gpt_oss forward: the container's two chunk kinds, each a 32 x 128 block with its parameters at g*32 + r and 4-bit codes in two 16-row halves at h*1024 + c*8 + (r%16)/2. D2560 (I8 [RB, CB, S, 2560]) is asymmetric bf16 scale/zero; MX2560 (U8, experts) is MXFP4 (E8M0 exponents, E2M1 codes). dequant_any now takes the tensor's descriptor, so a 4-D D2560 tensor is routed by its declared shape. Checked against ggml-org/gpt-oss-20b-GGUF on real chunks, through this C code: q_proj corr 0.99545 against its Q8_0 (4-bit vs 8-bit), the MXFP4 expert blocks bit-exact (max |d| 0). npu_dequant_bf16_test: d2560_chunks. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The MX2560 decoder of the previous commit halved every GPT-OSS expert. Its check was against my own GGUF reader, which had the same slip, so the two agreed bit for bit. ggml writes the weight as kvalues_fp4[q] * GGML_E8M0_TO_FP32_HALF(e), and kvalues_fp4 is the E2M1 table DOUBLED (0,1,2,3,4,6,8,12), so the weight is E2M1 * 2^(e-127): OCP MX's bias. Found because a numpy GPT-OSS forward on these weights answered '<|start|>assistant' with punctuation (<|channel|> at rank 2451); with the bias fixed, it puts <|channel|> first at 38.3 against 18.7, on both the container's dense weights and the GGUF's Q8_0 ones. npu_dequant_bf16_test pins the bias with one value (e = 127, E2M1 1.0 -> 1.0). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…, the MXFP4 top-k MoE (Refs #1786) GPT-OSS-20B-NPU2 now loads and its logits match a numpy GPT-OSS forward of the same weights (corr 1.000000, max |d| 6e-5 of 38.3; '<|channel|>' first after '<|start|>assistant'). Rules transcribed from our llama.cpp fork (openai-moe.cpp, build_moe_ffn SOFTMAX_WEIGHT, ggml-cpu swiglu_oai) and transformers' yarn: - attention sinks: head_context takes an optional per-head sink (null keeps every model bit-identical); - o_proj bias; YaRN (Q4nxRopeYarn: the ramp with truncate honoured, the attention factor on cos/sin); - gptoss_moe: top-k of the biased router logits, softmax over those k, swiglu_oai with the expert biases, reading the MX2560 experts in place; a batched prefill takes the per-token path. New CI arm gptoss_forward: a two-layer container written in the D2560/MX2560 format, against the reference on its bytes (corr 1.0000000); each of seven rule mutants fails it while batched == sequential passes. attn_math_test: the sink; npu_rope_llama3_test: yarn. ctest 270/270. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
ready for review at 878837d |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 878837d: READY
I checked the two places a new architecture goes wrong, and both are right — and the sink one is right in the way a test could not have told me.
float mx = sink ? *sink : -1e30f; // the sink joins the max
if (sink) sum += std::exp(*sink - mx); // denominator onlyA GPT-OSS sink is a per-head logit that takes probability mass and contributes no value. Both halves matter and both are handled: it enters mx, so exp(*sink - mx) <= 1 and the sink cannot make the softmax overflow when it is the largest logit; and it is added to sum only, never to the numerator, which is the part that is easy to get wrong by treating it as an extra value row. If the sink had been left out of the max, this would have passed every small-valued test and overflowed on a real pack.
The MXFP4 path is correct where it is easiest to be subtly wrong: the E2M1 codebook is the standard {0, .5, 1, 1.5, 2, 3, 4, 6} table including -0.0f at index 8, i.e. sign-magnitude nibbles rather than two's complement; and e8m0_scale returns 2^(e-127) exactly. d2560_code's (r/16)*1024 + c*8 + (r%16)/2 is self-consistent: 16 rows / 2 nibbles = 8 bytes per column, 128 columns = 1024 bytes per block, 2560/128 = 20 blocks wide.
The gate actually runs: gptoss_forward_ci.py fabricates the container — "CI has no such pack" — so SKIP_RETURN_CODE 77 fires only when numpy is missing, not because the pack is absent. And it is not a smoke test: logits after tokens 1..6 must reach corr >= 0.99999 and max|diff| <= 1e-3, with the header saying those were measured rather than chosen, and the reference transcribed rather than fitted.
Two limits I want on the record, neither a defect:
- The reference is transcribed by the same author as the C++ path, so it catches implementation slips — nibble order, chunk layout, the interleave, where the sink sits in the code — extremely well, and it cannot catch a shared misreading of the architecture. The sink semantics are exactly the kind of thing in that gap, which is why I verified them from the formula rather than from the two agreeing.
- Synthetic weights mean the container has a real pack's shape but not its magnitudes.
e8m0_scale(255)would returninf; MXFP4 reserves 255 as NaN, so a refusal there is cheap insurance, and realistic exponents/large logits are the case synthetic weights are least likely to produce.
Two small notes: e8m0_scale's doubling/halving loop is correct but runs up to 127 multiplies per block — ldexpf(1.0f, e - 127) is the same value in one instruction, and on a 20B host forward that is not a rounding concern. And SKIP_RETURN_CODE 77 is honest here; naming numpy as the reason makes it clear the test is not silently declining to check the architecture.
|
VERDICT: READY 848 lines is too much to verify line by line, so I checked the two places a port of this shape actually goes wrong: the borrowed formulas, and whether the correctness claim is text or token ids. The YaRN implementation is transformers' own rule, not an approximation.
The correctness claim is text, not token ids — the full Harmony-formatted reply with The sink line is worth keeping as written: Three small things, none blocking:
Not verified by me: the forward on the pack. Host-side and device-free, but it needs |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY -- four reverse-engineered pieces, each pinned against an outside reference, and one assertion that is stated rather than demonstrated
The four pieces are individually checkable and each is anchored to something outside this tree, which is the only kind of anchor that catches a shared misconception:
- D2560:
code * scale + zerowith both parameters atg*32 + r, the two 16-row halves ath*1024 + c*8 + (r%16)/2, the row-block order (tensor row rb*S*32 + s*32 + r comes from chunk [rb][cb][s]), and padding pastkwritten 0. Asymmetric scales are where a symmetric reader silently halves the answer, so pinning+ zeroexplicitly matters. - MX2560 / MXFP4:
E2M1 * 2^(e-127)with the E8M0 bias cross-checked against ggml's own reading --e = 127with code 2 is weight 1.0, "which ggml writes askvalues_fp4[2] = 2timesGGML_E8M0_TO_FP32_HALF(127) = 0.5". That is a reference from a different codebase, not a restatement of the implementation. - Attention sinks: the test computes its own reference -- an explicit
softmax over [scores, sink]with max subtraction andden += exp(sink - mx)-- and checks two things, both necessary: a null sink leaves every other model's head bit-identical, and a real sink (0.7) both matches that explicit softmax and took some mass (drop, so an implementation that simply ignored the sink fails). Getting the do-no-harm arm and the anti-vacuity arm on the same function is more than most sink patches do. - YaRN: the per-pair ramp of transformers'
_compute_yarn_parameters(truncate: falsehonoured, the attention factor on cos and sin), withyarnaccepted only when its numbers are usable and refused without them -- so a container that declares yarn but ships nothing is rejected rather than silently treated as plain rope.
The one assertion I would make demonstrable:
check(bad == 0, "mx2560: E2M1 code (same order) * 2^(e - 127), e at g*32 + r; bytes 128..511 unread");
That checks the decode against the expected values, and says in the message that bytes 128..511 are unread -- but the fixture does not appear to put anything in those bytes, so a reader that walked them and multiplied by zero would pass identically. The property is worth having (a wrong stride that starts reading at 128 would produce noise), and proving it costs one line:
std::memset(t.data() + 128, 0x7f, 256); // before the decode, after the scales are writtenthen the same check. With non-zero bytes in the "unused" region, "unread" becomes measured instead of claimed -- and if the reader is touching them, the arm fails immediately rather than at the first real pack.
Scope stated honestly: "No kernel set for its shapes was built or run" and the batched prefill takes the per-token path, so this is the host forward only -- and the end-to-end known answer (the Harmony reply in the body) is a _ci.py row that presumably skips without the 20B pack. Both are the right things to say out loud.
What
FastFlowLM's
GPT-OSS-20B-NPU2now loads on the per-op forward and answers correctly on the host. Before, it was refused atrope scaling "yarn", and past that point nothing in the forward read its container or its MoE. Refs #1786. Host-side only; the NPU is off.On the known-answer prompt (the pack's own Harmony template, reasoning low, greedy) the full reply is:
The pieces, each with its source
Two chunk kinds (
q4nx_dequant.h). Each chunk is 32 rows × 128 columns, with its parameters atg*32 + rand 4-bit codes in two 16-row halves ath*1024 + c*8 + (r%16)/2:[RB, CB, S, 2560]): attention q/k/v/o and the head, asymmetric bf16 scale and zero-point;[E, 69, CB, S, 2560]): the experts as MXFP4,E2M1 * 2^(e-127). Per expert, gate and up alternate by 128-row block, then 23 blocks of down.dequant_anynow takes the tensor descriptor, so the 4-D kind is routed by its declared shape.Attention sinks.
q4nx_attn::head_contexttakes an optional per-head sink logit that joins the softmax and carries no value. A null sink leaves every other model bit-identical (attn_math_test).o_proj bias.
YaRN (
Q4nxRopeYarn,q4nx_rope.h): transformers'_compute_yarn_parametersramp, honouringtruncate: false, with the attention factor on cos and sin.The MoE (
gptoss_moe), as in our llama.cpp fork (openai-moe.cpp,build_moe_ffnSOFTMAX_WEIGHT, ggml-cpuswiglu_oai):min(gate,7)·σ(1.702·gate)·(clamp(up,−7,7)+1), with the gate, up and down biases;A batched prefill of gpt_oss takes the per-token path.
How it was checked
Layouts, against
ggml-org/gpt-oss-20b-GGUF(fetched by HTTP range, not downloaded):MX2560 against ggml's own MXFP4 rule. An earlier version of this branch used bias 128, in both my decoder and my GGUF reader. The two agreed bit for bit while both were wrong: every expert was half its size. It was found end to end, when a numpy forward answered
<|start|>assistantwith punctuation; that is fixed in the second commit.npu_dequant_bf16_testnow pins one value through ggml's reading (kvalues_fp4[2] * 2^(127-128)= 1.0).Real pack. A numpy GPT-OSS forward of the pack's weights puts
<|channel|>first (38.3 against 18.7 for the next token). It gives the same top 5 on the GGUF's Q8_0 dense weights (logits corr 0.975). The C forward matches it: corr 1.000000, max |d| 6e-5 of 38.3.New CI arm
gptoss_forward. It writes a two-layer container in this format (4 experts top 2, sliding window 2, YaRN with a short original window so the rule is exercised within 6 positions, gate and up biases large enough that the clamp binds). It holds the forward to a numpy GPT-OSS forward of those bytes: corr 1.0000000, max |d| 1.9e-6. Each rule mutant fails it, while batched == sequential passes every time:The sinks, o bias and MXFP4 rows were measured on the arm's first data set; the others on the final one.
Unit tests:
npu_rope_llama3_testgains YaRN checks (pairs 0, 31 and one inside the ramp, by hand);attn_math_testgains the sink check;npu_dequant_bf16_testgainsd2560_chunksand the bias pin.Full ctest: 270/270, device tests excluded. Every
batch_check_state_ci.pyarm andgptoss_forwardpass at this head.tools/copyright.py --checkrc 0.Not in this PR: the MoE on the device, a kernel set, and a device row. The host forward runs at about 12 s/token on this box.
🤖 Generated with Claude Code