Repository navigation
npu: the GDN delta-rule recurrence walks contiguous rows (host 107 -> 18 ms/token, decode 0.53 -> 0.44 s/token) - #750
Conversation
… ~38 ms a token) gdn_layer's recurrence did its two matrix-vector products and its rank-1 update with the KEY index innermost, so every inner step jumped VHD floats (512 bytes) through the 128x128 state: 107 ms of a token, 21 % of that 510 ms decode step, at ~1.4 GFLOP/s on a Zen 5 core. Same arithmetic, value index innermost: kvm += kh[kd] * sh[kd][:] (was: kvm[vd] += sh[kd][vd] * kh[kd], kd inner) sh[kd][:] += kh[kd] * delta[:] (delta now precomputed into kvm, one write per element) core[:] += qh[kd] * sh[kd][:] (was: core[vd] = sum_kd sh[kd][vd]*qh[kd]) It is bit-for-bit the same result: each output element still accumulates over kd in ascending order from the same products, and the rank-1 update still writes every element exactly once from the same precomputed delta. Only the loop nest and the index that walks memory changed. Measured (strixhalo, npu-hold, Release build; window11 acceptance on the same run): window11, 3 greedy prompts vs the shipped b8 rung: 3/3 text-identical (same bytes as before) layer host 140-155 -> 63-64 ms/token ([lh]: gdn_layer non-gemm 118 -> 38.6) step wall 510-531 -> 445-447 ms decode 0.53 -> 0.44 s/token n=1 warm / n=5 1.69 / 3.83 -> 1.71 / 3.49 s device 298 ms unchanged (12.93 GB at 43 GB/s), head 21 ms unchanged The host is no longer the GDN recurrence; what is left there is gemm host (amax + dequant, 24 ms) and the MLP/norm items (3.8 ms).
PR Reviewer Guide 🔍(Review updated until commit a5582c6)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: READY
At 9779525c — first review, build green. One file, npu/forward/src/q4nx_forward.cpp, +28 −12.
What it does: the GDN delta-rule recurrence's two mat-vecs and its rank-1 update were written with the key index innermost, so every inner step walked VHD floats of sh — the comment states the cost as measured (512-byte stride, 107 ms/token, 21 % of the decode step, ~1.4 GFLOP/s on a Zen 5 core). The rewrite puts the value index innermost, which is contiguous and vectorisable.
The bit-exactness claim is true by construction, and I checked that rather than the prose. Per output element the accumulation is still over kd in ascending order (for kd { for vd { kvp[vd] += kk * row[vd] } } gives each vd the same sequence of FP additions as for vd { s = 0; for kd { s += sh[kd*VHD+vd] * kh[kd] } }), and the rank-1 update still writes each element exactly once from the same precomputed delta — now staged in kvp in place, which is the only change in the intermediate representation. FP addition is not associative, so loop-nest changes usually break bit-identity; this one preserves the order, so it does not.
Compiles clean in the own-runtime lane:
g++-16 -std=c++26 -O1 -fsyntax-only -DONEBIT_NPU_RUNTIME_OWN -isystem npu/forward/include … q4nx_forward.cpp
-> 0 errors
Not verified: the 107 ms/token figure and the resulting speed-up — that needs the array and a model. Also worth a gate when someone has the window: this is the recurrence the whole GDN path depends on, so one text-identity run against the pre-change binary would confirm the "bit-for-bit" claim empirically rather than by argument. The argument is solid; the run is cheap insurance on a host path that runs every token.
tprs-ready applied.
|
Persistent review updated to latest commit 8fa74c5 |
|
Persistent review updated to latest commit a5582c6 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
At a5582c60 — final head after the branch was brought up to date, build green, and I recompiled rather than trusting the rebase:
g++-16 -std=c++26 -O1 -fsyntax-only -DONEBIT_NPU_RUNTIME_OWN \
-isystem npu/forward/include -isystem npu/forward/engine-src -isystem npu -I npu/ort -fopenmp \
npu/forward/src/q4nx_forward.cpp -> 0 errors
The change, from my first review: the GDN delta-rule recurrence's two mat-vecs and its rank-1 update were written with the key index innermost, so every inner step walked VHD floats of sh (512-byte stride; the comment states 107 ms/token, 21 % of the decode step, ~1.4 GFLOP/s on a Zen 5 core). The rewrite puts the value index innermost, which is contiguous and vectorisable.
The bit-exactness claim holds by construction, and that is the part worth checking rather than the prose: per output element the accumulation is still over kd in ascending order (for kd { for vd { kvp[vd] += kk * row[vd] } } gives each vd the same FP addition sequence as for vd { s = 0; for kd { s += sh[kd*VHD+vd] * kh[kd] } }), and the rank-1 update still writes each element exactly once from the same precomputed delta — now staged in kvp in place. FP addition is not associative, so a loop-nest change usually breaks bit-identity; this one preserves the order and therefore does not.
Not verified: the 107 ms/token figure and the resulting speed-up (needs the array and a model). Worth a gate when someone has the window: one text-identity run against the pre-change binary would confirm the claim empirically — the argument is solid, and the run is cheap insurance on a host path that executes every token.
tprs-ready is on it.
What changes
gdn_layer's delta-rule recurrence ran its two matrix-vector products and its rank-1 update with the key index innermost, so every inner step jumpedVHDfloats (512 bytes) through the 128×128 state. That was 107 ms of a 510 ms decode step — 21 % of every token — at ~1.4 GFLOP/s on a core that does two orders of magnitude more.Same arithmetic, value index innermost:
It is bit-for-bit the same result: each output element still accumulates over
kdin ascending order from the same products, and the rank-1 update still writes every element exactly once from the same precomputed delta. Only the loop nest and the index that walks memory changed — no reassociation, no fused multiply-add change, no precision change.One file,
npu/forward/src/q4nx_forward.cpp, +28/−12, insidegdn_layeronly. The batched (gdn_layer_batch) path calls the same per-row code and is unchanged.Measured (strixhalo,
npu-hold, Release build of this tree at mainfcdaf4b)window11acceptance on the same run — 3 greedy prompts, shipped b8 rung vs this route: 3/3 text-identical, byte for byte the same completions as before the change.[lh])What is left on the host: gemm host (amax scan + dequant) 24 ms, MLP act*up / rmsnorm / attention / residuals 3.8 ms. The recurrence is no longer a target.