From 612fcd00085810726bc5b3e67ac1c198536f40cd Mon Sep 17 00:00:00 2001 From: patricksamuelevans Date: Tue, 1 Dec 2026 02:19:26 +0700 Subject: [PATCH] fix(parakeet): store pointwise convs 2-D so they quantize A 1x1 conv is a linear layer: [out, in, 1] and [out, in] have the same memory order. The converter now squeezes the conformer pointwise pair to 2-D, so transcribe-quantize classifies it as Linear instead of pinning 48 tensors per 0.6B model at F16 (about 100 MB at Q5_K_M and a ~290 MB F32 promotion on CPU). The loader accepts both layouts: GET_PW peeks at the stored rank and validates one shape. The conformer forces the direct mul_mat path when either pointwise weight is 2-D (the im2col fallback needs a kernel axis). Legacy 3-D GGUFs keep loading, and mixed layouts cannot select im2col by accident. The synthetic parakeet fixture stores pointwise1 2-D and pointwise2 legacy 3-D, covering both loader branches. Docs/tools/quantization.md now separates legacy 3-D ConvPw from the 2-D Linear layout. Fixes #281. --- docs/tools/quantization.md | 8 ++++++-- scripts/convert-parakeet.py | 13 ++++++++++++- src/arch/parakeet/weights.cpp | 26 ++++++++++++++++++++++++-- src/arch/parakeet/weights.h | 4 ++-- src/conformer/conformer.cpp | 11 +++++++++-- src/conformer/conformer.h | 4 ++-- tests/fixtures/make_gguf_fixtures.py | 5 ++++- 7 files changed, 59 insertions(+), 12 deletions(-) diff --git a/docs/tools/quantization.md b/docs/tools/quantization.md index 2e65fe806..94c0d80aa 100644 --- a/docs/tools/quantization.md +++ b/docs/tools/quantization.md @@ -76,14 +76,18 @@ per-preset bucket policy for every canonical tensor name. |----------|-----------------------------------------------------------------------|------------------------------------------------| | `Linear` | `ggml_mul_mat` operands: encoder FF, attention projections, predictor LSTM gates, joint projections, head | Block-quantized (Q4_K / Q5_K / Q6_K / Q8_0) | | `Embed` | Decoder token embedding (`dec.embed.token.weight`, `dec.token_embd.weight`) | Bumped to Q6_K in `_M` presets where shape-compatible, else `linear_fallback` | -| `ConvPw` | 1×1 pointwise conv kernels in conformer blocks | F16 (im2col+matmul supports F16 matmul) | +| `ConvPw` | 1×1 pointwise conv kernels still stored 3-D (`[1, in, out]`; legacy layouts only) | F16 (im2col+matmul supports F16 matmul) | | `Conv` | Non-pointwise conv kernels: 2D pre-encode, depthwise | F32 or F16 (no quantized im2col in ggml) | | `Norm` | Biases, LayerNorm/BatchNorm/RMSNorm weights, per-head q_norm/k_norm, positional bias/encoding, frontend buffers | F32 (precision-sensitive, tiny) | +Pointwise 1×1 convs converted to the 2-D `[in, out]` layout are ordinary +`Linear` operands and quantize; only legacy 3-D pointwise tensors stay +`ConvPw`/F16 because a 1-element row cannot be block-quantized. Classification rules live in `classify_tensor()` in `tools/transcribe-quantize/policy.cpp`. They are substring-based on the canonical tensor name (e.g. `norm_*`, `*.bias`, `*pointwise*.weight`, -`*.q_norm.weight`, `*.ln_pre.weight`). +`*.q_norm.weight`, `*.ln_pre.weight`), plus the `ne0 == 1` layout test +that routes legacy pointwise tensors to `ConvPw`. ## Preset table diff --git a/scripts/convert-parakeet.py b/scripts/convert-parakeet.py index 13faba26e..a3a1d2047 100644 --- a/scripts/convert-parakeet.py +++ b/scripts/convert-parakeet.py @@ -26,7 +26,10 @@ Layout notes: NeMo ships pure PyTorch tensors, so Conv2d/Conv1d weights are already in the OIHW / [O, I_per_group, k] layouts the loader - expects — no transposes here. + expects — no transposes here. The one reshape is the conformer + 1x1 pointwise pair: [out, in, 1] is squeezed to [out, in] so the + quantizer treats it as a linear layer (issue #281); memory order is + unchanged and the loader accepts the legacy 3-D layout too. PyTorch's LSTM stores per-layer weights as weight_ih_l{i} / weight_hh_l{i} and *two* bias vectors @@ -2065,6 +2068,14 @@ def add(nemo_name: str, gguf_name: str) -> None: if nemo_name not in sd_keys: raise KeyError(f"state_dict missing tensor: {nemo_name!r}") arr = tensor_to_fp32_numpy(sd[nemo_name]) + # A 1x1 conv is a linear layer: [out, in, 1] and [out, in] have + # the same memory order. Store the conformer pointwise pair 2-D + # ([out, in]) so the quantizer classifies them as Linear instead + # of pinning 48 tensors per 0.6B model at F16 (issue #281). The + # loader accepts both the legacy 3-D and the new 2-D layout. + if ".conv.pointwise1.weight" in gguf_name or ".conv.pointwise2.weight" in gguf_name: + if arr.ndim == 3 and arr.shape[2] == 1: + arr = np.ascontiguousarray(arr.squeeze(axis=2)) writer.add_tensor(gguf_name, arr) consumed.add(nemo_name) bytes_out += int(arr.nbytes) diff --git a/src/arch/parakeet/weights.cpp b/src/arch/parakeet/weights.cpp index 4247ec7cf..6d91a1afb 100644 --- a/src/arch/parakeet/weights.cpp +++ b/src/arch/parakeet/weights.cpp @@ -859,6 +859,28 @@ constexpr const char * kTag = kFamilyTag; (slot) = _t; \ } while (0) +// Pointwise 1x1 convs: legacy GGUFs store [1, in, out] (Conv1d k=1), +// new converters store squeezed 2-D [in, out] so the quantizer treats +// them as Linear (issue #281). Accept both; the graph reshapes to 2-D +// either way and the memory order is identical. Peek at the layout +// first so find_tensor is called once and never logs a spurious +// shape-mismatch for the layout that is not present. +#define GET_PW(slot, name, in_dim, out_dim) \ + do { \ + const ggml_tensor * _raw = ggml_get_tensor(ctx_meta, (name)); \ + ggml_tensor * _t = nullptr; \ + if (_raw != nullptr && _raw->ne[0] == 1 && _raw->ne[2] == (out_dim)) { \ + _t = transcribe::weights::find_tensor(ctx_meta, (name), { TRANSCRIBE_QUANT_CONV_TYPES }, \ + { 1, (in_dim), (out_dim) }, kTag); \ + } else { \ + _t = transcribe::weights::find_tensor(ctx_meta, (name), { TRANSCRIBE_QUANT_LINEAR_TYPES }, \ + { (in_dim), (out_dim) }, kTag); \ + } \ + if (_t == nullptr) \ + return TRANSCRIBE_ERR_GGUF; \ + (slot) = _t; \ + } while (0) + } // namespace transcribe_status build_parakeet_weights(ggml_context * ctx_meta, @@ -949,9 +971,9 @@ transcribe_status build_parakeet_weights(ggml_context * ctx_meta, // back to d_model. GET_F32(b.norm_conv_w, lname("enc.blocks.%d.norm_conv.weight", i), d_model); GET_F32(b.norm_conv_b, lname("enc.blocks.%d.norm_conv.bias", i), d_model); - GET_CONV(b.conv_pw1_w, lname("enc.blocks.%d.conv.pointwise1.weight", i), 1, d_model, 2 * d_model); + GET_PW(b.conv_pw1_w, lname("enc.blocks.%d.conv.pointwise1.weight", i), d_model, 2 * d_model); GET_CONV(b.conv_dw_w, lname("enc.blocks.%d.conv.depthwise.weight", i), k, 1, d_model); - GET_CONV(b.conv_pw2_w, lname("enc.blocks.%d.conv.pointwise2.weight", i), 1, d_model, d_model); + GET_PW(b.conv_pw2_w, lname("enc.blocks.%d.conv.pointwise2.weight", i), d_model, d_model); GET_F32(b.conv_bn_w, lname("enc.blocks.%d.conv.bn.weight", i), d_model); GET_F32(b.conv_bn_b, lname("enc.blocks.%d.conv.bn.bias", i), d_model); if (hp.enc_conv_norm_type == ParakeetHParams::ConvNormType::BatchNorm) { diff --git a/src/arch/parakeet/weights.h b/src/arch/parakeet/weights.h index 885dfea53..772ea4c2f 100644 --- a/src/arch/parakeet/weights.h +++ b/src/arch/parakeet/weights.h @@ -311,11 +311,11 @@ struct ParakeetBlock { // Convolution module: pointwise -> GLU -> depthwise -> BN -> activation -> pointwise. ggml_tensor * norm_conv_w = nullptr; // [d_model] ggml_tensor * norm_conv_b = nullptr; // [d_model] - ggml_tensor * conv_pw1_w = nullptr; // [2*d_model, d_model, 1] conv1d k=1; 2x for GLU + ggml_tensor * conv_pw1_w = nullptr; // [d_model, 2*d_model] 2-D, or legacy [1, d_model, 2*d_model]; 2x for GLU ggml_tensor * conv_pw1_b = nullptr; // [2*d_model], nullable (use_bias) ggml_tensor * conv_dw_w = nullptr; // [d_model, 1, k] depthwise conv1d ggml_tensor * conv_dw_b = nullptr; // [d_model], nullable (use_bias) - ggml_tensor * conv_pw2_w = nullptr; // [d_model, d_model, 1] conv1d k=1 + ggml_tensor * conv_pw2_w = nullptr; // [d_model, d_model] 2-D, or legacy [1, d_model, d_model] ggml_tensor * conv_pw2_b = nullptr; // [d_model], nullable (use_bias) ggml_tensor * conv_bn_w = nullptr; // [d_model] BN scale ggml_tensor * conv_bn_b = nullptr; // [d_model] BN shift diff --git a/src/conformer/conformer.cpp b/src/conformer/conformer.cpp index 9b6c2ded0..e6b3a3751 100644 --- a/src/conformer/conformer.cpp +++ b/src/conformer/conformer.cpp @@ -367,11 +367,18 @@ ggml_tensor * conv_module(ggml_context * ctx, ggml_tensor * x, const BlockView & // conv_pad_mask no longer broadcasts and ggml aborts. Force the // batch-aware direct path at B > 1 regardless of backend policy or // TRANSCRIBE_CONV_NO_DIRECT_PW (correctness beats the perf override). - const bool direct_pw = policy.direct_pw || B > 1; + // 2-D pointwise weights (issue #281, squeezed [in, out]) have no + // kernel axis for the im2col path, so they also force direct. + // 3-D legacy is ne=[1, in, out]; 2-D is ne=[in, out, 1, ...]. The + // loader accepts each weight's layout independently, so either one + // being 2-D makes the im2col path invalid. + const bool pw1_is_2d = (b.conv_pw1_w == nullptr) || (b.conv_pw1_w->ne[2] == 1); + const bool pw2_is_2d = (b.conv_pw2_w == nullptr) || (b.conv_pw2_w->ne[2] == 1); + const bool direct_pw = policy.direct_pw || B > 1 || pw1_is_2d || pw2_is_2d; if (direct_pw) { // Pointwise conv 1 as direct mul_mat in [d_model, T, B] layout. - // Kernel ne=[1, d_model, 2*d_model] → reshape to [d_model, 2*d_model]. + // 2-D [d_model, 2*d_model] or legacy [1, d_model, 2*d_model] → reshape. // Force F32 accumulation for F16 weights (see mul_mat_f32acc in // causal_lm.cpp for the CUDA COMPUTE_16F saturation rationale). { diff --git a/src/conformer/conformer.h b/src/conformer/conformer.h index 745416343..e08cb7d4d 100644 --- a/src/conformer/conformer.h +++ b/src/conformer/conformer.h @@ -74,11 +74,11 @@ struct BlockView { // Convolution module. Pointwise conv1 is 2*d_model for GLU. ggml_tensor * norm_conv_w = nullptr; // [d_model] ggml_tensor * norm_conv_b = nullptr; // [d_model] - ggml_tensor * conv_pw1_w = nullptr; // [1, d_model, 2*d_model] + ggml_tensor * conv_pw1_w = nullptr; // [1, d_model, 2*d_model] or 2-D [d_model, 2*d_model] ggml_tensor * conv_pw1_b = nullptr; // [2*d_model], nullable ggml_tensor * conv_dw_w = nullptr; // [k, 1, d_model] ggml_tensor * conv_dw_b = nullptr; // [d_model], nullable - ggml_tensor * conv_pw2_w = nullptr; // [1, d_model, d_model] + ggml_tensor * conv_pw2_w = nullptr; // [1, d_model, d_model] or 2-D [d_model, d_model] ggml_tensor * conv_pw2_b = nullptr; // [d_model], nullable // Post-depthwise normalisation. Exactly one pair is populated per block // per BlockParams::conv_norm_type: BatchNorm (conv_bn_fused_*, fused at diff --git a/tests/fixtures/make_gguf_fixtures.py b/tests/fixtures/make_gguf_fixtures.py index b643163f8..151e11d95 100644 --- a/tests/fixtures/make_gguf_fixtures.py +++ b/tests/fixtures/make_gguf_fixtures.py @@ -595,7 +595,10 @@ def _parakeet_tensor_descriptors(causal_pre_encode: bool = False) -> list[tuple[ (f"enc.blocks.{i}.norm_conv.weight", [d_model]), (f"enc.blocks.{i}.norm_conv.bias", [d_model]), - (f"enc.blocks.{i}.conv.pointwise1.weight", [1, d_model, 2 * d_model]), + # pointwise1 is stored 2-D (the converter's new layout, #281); + # pointwise2 stays legacy 3-D so the loader's fallback branch is + # covered too. Both have identical memory order. + (f"enc.blocks.{i}.conv.pointwise1.weight", [d_model, 2 * d_model]), (f"enc.blocks.{i}.conv.depthwise.weight", [k, 1, d_model]), (f"enc.blocks.{i}.conv.pointwise2.weight", [1, d_model, d_model]), (f"enc.blocks.{i}.conv.bn.weight", [d_model]),