Skip to content

npu: fix a Q4NX GDN model on the device path (#587) - #618

Merged
bong-water-water-bong merged 1 commit into
mainfrom
fix/587-device-forward
Oct 27, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
fix/587-device-forward

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Follow-up to #617 (which shipped only the chunk check). forward-serve on Qwen3.8-27B-NPU2 (qwen3_5, 48 GDN + 16 full layers) now produces text on the NPU instead of "!!!!!!!!".

Three defects, each masked by the previous one

  1. alpha/beta dequant — ssm_alpha_proj/ssm_beta_proj are stored row-padded to the 32-row tile (48 value heads → 64 rows, Q8: 40 tiles × 8704 B). dequant_any() rejects a non-tile-aligned height, so deq_pad() dequantises at the stored height and keeps the first rows.
  2. weight-cache key collision — the cache is keyed (layer, int). The GDN path packed ssm_out_proj (N = H = 5120) under key 2, and the FFN tail of the same layer then claimed key 2 for gate_proj (N = IM = 17408). The gate GEMM got a BO packed for 5120 columns — its scale vector had 5120 entries for a 17408-wide output — and produced ~1e28, which is the garbage. ssm_out_proj → key 6. The MLA path had the same bug twice: key 2 held kv_a before gate_proj, key 3 held o_proj before up_proj → keys 6 and 7.
  3. per-token re-dequantisation — the three big GDN projections are only read via q4nx_assemble_B() when their packed BO is not cached, but load_gdn_layer() dequantised ~1.5 GB per layer per token anyway (dense_weights_cached requires lin_kd_ == 0, false for every GDN model). They are skipped when keys 0/1/6 are cached.

Evidence (strixhalo, ~/npu.lock held, accel0 free, 5/5):

prompt "The capital of France is", 8 greedy tokens
→ " Paris.\nThe capital of Germany is"
[gemm] tag=G l=0 K=5120 N=17408 gs ngs=17408 maxOut=1.527   (was ngs=5120, maxOut=5.8e28)
[prof] npu_gemm pack=9.2 ms                                 (was 184.7 ms)

Not fixed: speed. ~50 s/token remains, because every GEMM is a separate blocking NPU submit (wait≈31 ms × ~365 GEMMs/token). That is the next issue, not this one.

Not verified: the MLA keys have no checkpoint here (DeepSeek2), so that half is reasoned, not measured. docs/ untouched.

`forward-serve` on Qwen3.8-27B-NPU2 (qwen3_5, 64 layers of which 48 are
GDN) loaded all seven geometry-derived ELFs, reported "ready (full ELF)"
and then decoded `"!!!!!!!!"`.  Three separate defects, each hidden
behind the previous one:

- alpha/beta dequant: ssm_alpha_proj/ssm_beta_proj are stored row-padded
  to the 32-row tile (48 value heads -> 64 rows, Q8: 40 tiles x 8704 B),
  and dequant_any() refuses a height that is not tile-aligned.  New
  deq_pad() dequantises at the stored height and keeps the first `rows`.
- weight-cache key collision: the cache is keyed (layer, int), and the
  GDN path packed ssm_out_proj (N = H) under key 2, which the FFN tail of
  the same layer then claimed for gate_proj (N = IM = 17408).  The gate
  GEMM read a BO packed for 5120 columns -- its scale vector had 5120
  entries for a 17408-wide output -- and produced ~1e28, hence the
  garbage.  ssm_out_proj moves to key 6.  The MLA path had the same bug
  twice: key 2 held kv_a (a few hundred wide) before gate_proj, and key 3
  held o_proj before up_proj; they move to 6 and 7.
- per-token re-dequantisation: the three big GDN projections are only
  read through q4nx_assemble_B() when their packed BO is not cached yet,
  but load_gdn_layer() dequantised ~1.5 GB per layer per token anyway
  (dense_weights_cached requires lin_kd_ == 0, which is false for every
  GDN model).  They are now skipped when keys 0/1/6 are cached; the small
  members the host reads each token are still refreshed.

Measured on strixhalo (NPU, ~/npu.lock held, accel0 free): the 27B now
answers instead of emitting `!`; per-GEMM pack time drops 184.7 -> 9.2 ms.
Speed is still ~50 s/token and is a separate problem (one blocking NPU
submit per GEMM), not addressed here.
@bong-water-water-bong
bong-water-water-bong merged commit 41842fc into main Oct 27, 2026
7 of 9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the fix/587-device-forward branch October 27, 2026 12:08
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

617 - Partially compliant

Compliant requirements:

  • Validation of Q4NX directory chunks, sizes, and hashes is implemented
  • Error reporting for invalid models is included

Non-compliant requirements:

  • The PR does not include the actual runtime fixes for the 27B model, only the preflight checks

Requires further human verification:

  • The actual runtime behavior of the 27B model on the NPU needs to be verified manually

587 - Partially compliant

Compliant requirements:

  • Alpha/beta dequantization now correctly handles row-padded tensors
  • Weight-cache key collisions are fixed by changing keys for GDN projections
  • Per-token re-dequantization is optimized by checking cache status before dequantization

Non-compliant requirements:

  • The PR does not fully implement the runtime fixes for the 27B model, only the preflight checks and optimizations

Requires further human verification:

  • The actual runtime behavior of the 27B model on the NPU needs to be verified manually
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect Weight Cache Key Usage

The weight cache keys for GDN projections were incorrectly reused, causing GEMM operations to use incorrectly sized BO packed data. This led to garbage output. The fix changes keys from 2 to 6 for ssm_out_proj and from 2 to 6, 3 to 7 for MLA path projections to avoid collisions.

// Key 6, not 2: key 2 is the FFN gate_proj of this same layer (N = IM),
// so reusing it handed this N = H GEMM a BO packed for the other width.
if (!weight_cached(6) && q4nx_assemble_B(g, &p, B_.data()) != 0) { err_ = "assemble gdn out"; return false; }
npu_gemm(*ctx_[i_o], core.data(), g->K, g->N, B_.data(), out, 6);
Inefficient Re-dequantization

The GDN projections were being re-dequantized on every token, causing significant performance overhead (~1.5 GB per layer per token). The fix adds a check to only dequantize when the packed BO is not cached, improving efficiency.

const bool proj_cached = weight_cache_ && weight_cached(0) && weight_cached(1) &&
                         weight_cached(6);
if (!proj_cached) {
    if (!deq("linear_attn.qkv_proj.weight", wqkvlin_.data(), CD, H)) return false;
    if (!deq("self_attn.gate_proj.weight", wz_.data(), lin_vd_, H)) return false;
    if (!deq("linear_attn.ssm_out_proj.weight", woutlin_.data(), H, lin_vd_)) return false;
}
if (!deq_pad("linear_attn.ssm_alpha_proj.weight", wa_.data(), lin_nvh_, H)) return false;
if (!deq_pad("linear_attn.ssm_beta_proj.weight", wb_.data(), lin_nvh_, H)) return false;
Incorrect Tensor Padding Handling

The alpha/beta projections were stored row-padded to 32-row tiles, but the dequantization function did not handle this correctly. The fix introduces a deq_pad function that dequantizes at the stored height and copies only the required rows, preventing errors with non-tile-aligned dimensions.

// alpha/beta are stored row-padded to the 32-row tile (48 heads -> 64 rows,
// Q8: 40 tiles x 8704 B = 348160 B), so dequantise at the stored height and
// keep the first `rows`.  Short-circuits when rows already tile-align.
auto deq_pad = [&](const char* suffix, float* out, int rows, int cols) -> bool {
    if (rows % Q4NX_TILE_ROWS == 0) return deq(suffix, out, rows, cols);
    const int prows = ((rows + Q4NX_TILE_ROWS - 1) / Q4NX_TILE_ROWS) * Q4NX_TILE_ROWS;
    std::vector<float> tmp((size_t)prows * (size_t)cols);
    if (!deq(suffix, tmp.data(), prows, cols)) return false;
    memcpy(out, tmp.data(), (size_t)rows * (size_t)cols * sizeof(float));
    return true;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant