Skip to content

npu: the GDN delta-rule recurrence walks contiguous rows (host 107 -> 18 ms/token, decode 0.53 -> 0.44 s/token) - #750

Merged
bong-water-water-bong merged 3 commits into
mainfrom
npu/gdn-host-recurrence
Nov 1, 2026
Merged

bong-water-water-bong merged 3 commits into
mainfrom
npu/gdn-host-recurrence

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

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 jumped VHD floats (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:

kvm  += kh[kd] * sh[kd][:]        (was: kvm[vd] += sh[kd][vd] * kh[kd], kd inner)
sh[kd][:] += kh[kd] * delta[:]    (delta 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 — no reassociation, no fused multiply-add change, no precision change.

One file, npu/forward/src/q4nx_forward.cpp, +28/−12, inside gdn_layer only. 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 main fcdaf4b)

window11 acceptance 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.

before after
gdn recurrence ([lh]) 107.0 ms/token (2.23 ms/layer) 18.1 ms/token (0.377 ms/layer)
gdn_layer non-gemm 118.0 ms 38.6 ms
layer host, total 140-155 ms 63-64 ms
step wall 510-531 ms 445-447 ms
decode 0.53 s/token 0.44 s/token
n=1 warm / n=5 1.69 / 3.83 s 1.71 / 3.49 s
device wait 298 ms 298 ms (unchanged: 12.93 GB at 43 GB/s)
head 21 ms 21 ms (unchanged)

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.

… ~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).
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit a5582c6)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Loop Nest Optimization

The change restructures the loop nest to improve memory access patterns by making the value index innermost instead of the key index. This optimization results in contiguous memory access, which should improve cache locality and vectorization potential. The change is described as bit-for-bit identical in results, and the performance improvement is measured on the hardware (Strix Halo).

// The two matrix-vector products and the rank-1 update below were written with the
// KEY index innermost, i.e. every inner step jumped VHD floats in `sh` (a 512-byte
// stride): 107 ms a token, 21 % of the decode step, at ~1.4 GFLOP/s on a Zen 5 core.
// The same arithmetic with the VALUE index innermost is contiguous and vectorisable,
// and it is bit-for-bit the same: each output element still accumulates over kd in
// ascending order, and the rank-1 update still writes each element once from the same
// precomputed delta.  Only the loop nest changed.
float* kvp = kvm.data();
std::fill(kvp, kvp + VHD, 0.0f);
for (int kd = 0; kd < KHD; kd++) {
    const float kk = kh[kd];
    const float* row = sh + (size_t)kd * VHD;
    for (int vd = 0; vd < VHD; vd++) kvp[vd] += kk * row[vd];
}
for (int vd = 0; vd < VHD; vd++) kvp[vd] = (vh[vd] - kvp[vd]) * beta;   // delta, in place
for (int kd = 0; kd < KHD; kd++) {
    const float kk = kh[kd];
    float* row = sh + (size_t)kd * VHD;
    for (int vd = 0; vd < VHD; vd++) row[vd] += kk * kvp[vd];
}
float* cp = core.data() + (size_t)h * VHD;
std::fill(cp, cp + VHD, 0.0f);
for (int kd = 0; kd < KHD; kd++) {
    const float qk = qh[kd];
    const float* row = sh + (size_t)kd * VHD;
    for (int vd = 0; vd < VHD; vd++) cp[vd] += qk * row[vd];
}
float ss = 0;
for (int vd = 0; vd < VHD; vd++) {
    cp[vd] *= scale;
    ss += cp[vd] * cp[vd];
}
const float inv = 1.0f / sqrtf(ss / (float)VHD + sem_.eps);
for (int vd = 0; vd < VHD; vd++) {
    const float zz = z[(size_t)h * VHD + vd];

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 1, 2026

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 8fa74c5

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit a5582c6

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bong-water-water-bong
bong-water-water-bong merged commit 0ddf781 into main Nov 1, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu/gdn-host-recurrence branch November 1, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 3/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant