From 7fa3bc511b66ede06f698c7b38c20c1eb0c73f93 Mon Sep 17 00:00:00 2001 From: ZenAesthete Date: Sun, 29 Nov 2026 09:44:49 +0530 Subject: [PATCH] parakeet: load 2-D pointwise conv kernels so they can be quantized --- docs/environment-variables.md | 2 +- docs/tools/quantization.md | 2 +- src/arch/parakeet/weights.cpp | 18 ++- src/conformer/conformer.cpp | 5 +- tests/CMakeLists.txt | 20 ++++ tests/conformer_pw_layout_unit.cpp | 182 +++++++++++++++++++++++++++++ 6 files changed, 224 insertions(+), 5 deletions(-) create mode 100644 tests/conformer_pw_layout_unit.cpp diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 9fa946e6a..f8d86926f 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -30,7 +30,7 @@ of tests. | `TRANSCRIBE_NO_FLASH` | Disable flash attention on encoder and decoder (forces the manual F32 path). | | `TRANSCRIBE_FORCE_FLASH` | Force flash attention on. Wins over `TRANSCRIBE_NO_FLASH` if both are set. | | `TRANSCRIBE_CONV_DIRECT_DW` / `TRANSCRIBE_CONV_NO_DIRECT_DW` | Force the depthwise-conv dispatch to the direct `conv_2d_dw` path / the im2col path, overriding the per-family backend default. | -| `TRANSCRIBE_CONV_DIRECT_PW` / `TRANSCRIBE_CONV_NO_DIRECT_PW` | Force the pointwise-conv dispatch to direct `mul_mat` / im2col, overriding the backend default. | +| `TRANSCRIBE_CONV_DIRECT_PW` / `TRANSCRIBE_CONV_NO_DIRECT_PW` | Force the pointwise-conv dispatch to direct `mul_mat` / im2col, overriding the backend default. Pointwise kernels stored 2-D always use direct `mul_mat`. | | `TRANSCRIBE_NO_REPETITION_GUARD` | Turn off the stop for greedy decodes that start repeating themselves, and the trim of a repeating tail at a budget stop (see [`input-limits.md`](input-limits.md)). For byte-exact reference parity; a looping decode then runs to its budget and keeps its repeats. Read once per process. | | `TRANSCRIBE_DUMP_DIR=` | Enable the per-stage tensor dumper; writes `.f32` + `.json` per dumped tensor into ``. The basis for the numerical-comparison harness (`scripts/compare_tensors.py`). | | `TRANSCRIBE_PERF_DEBUG` | Print a per-stage timing breakdown to stderr (DEBUG log) on the families that profile (`cohere`, `granite`, `canary`, `canary_qwen`, `moonshine`, `moonshine_streaming`, `moss`, `qwen3_asr`, `whisper`). For whisper, a value containing `cpu` or `all` additionally prints the CPU sub-section breakdown. | diff --git a/docs/tools/quantization.md b/docs/tools/quantization.md index 2e65fe806..f8e9ad4e6 100644 --- a/docs/tools/quantization.md +++ b/docs/tools/quantization.md @@ -76,7 +76,7 @@ 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 in conformer blocks stored `[1, in, out]` (a 2-D `[in, out]` pointwise is `Linear`) | F16 (a 1-element row cannot be block-quantized) | | `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) | diff --git a/src/arch/parakeet/weights.cpp b/src/arch/parakeet/weights.cpp index 4247ec7cf..7eafe2a3e 100644 --- a/src/arch/parakeet/weights.cpp +++ b/src/arch/parakeet/weights.cpp @@ -830,6 +830,9 @@ using transcribe::weights::lname; // GET_LIN - mul_mat linear weights (TRANSCRIBE_QUANT_LINEAR_TYPES; // every family accepts the same set so a quantize preset // never produces a tensor the loader refuses). +// GET_PW picks GET_LIN or GET_CONV for the 1x1 pointwise convs: a 2-D +// [in, out] kernel is a linear weight (the quantizer classifies it as +// Linear), the [1, in, out] Conv1d layout stays a conv kernel. constexpr const char * kTag = kFamilyTag; #define GET_F32(slot, name, ...) \ @@ -859,6 +862,16 @@ constexpr const char * kTag = kFamilyTag; (slot) = _t; \ } while (0) +#define GET_PW(slot, name, n_in, n_out) \ + do { \ + const ggml_tensor * _p = ggml_get_tensor(ctx_meta, (name)); \ + if (_p != nullptr && ggml_n_dims(_p) == 2) { \ + GET_LIN(slot, (name), n_in, n_out); \ + } else { \ + GET_CONV(slot, (name), 1, n_in, n_out); \ + } \ + } while (0) + } // namespace transcribe_status build_parakeet_weights(ggml_context * ctx_meta, @@ -949,9 +962,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) { @@ -1083,5 +1096,6 @@ transcribe_status build_parakeet_weights(ggml_context * ctx_meta, #undef GET_F32 #undef GET_CONV #undef GET_LIN +#undef GET_PW } // namespace transcribe::parakeet diff --git a/src/conformer/conformer.cpp b/src/conformer/conformer.cpp index 9b6c2ded0..68362d7cb 100644 --- a/src/conformer/conformer.cpp +++ b/src/conformer/conformer.cpp @@ -367,7 +367,10 @@ 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; + // A 2-D [in, out] kernel (the quantizable layout) has no k axis for + // im2col, so it forces the direct path too. + const bool direct_pw = + policy.direct_pw || B > 1 || ggml_n_dims(b.conv_pw1_w) == 2 || ggml_n_dims(b.conv_pw2_w) == 2; if (direct_pw) { // Pointwise conv 1 as direct mul_mat in [d_model, T, B] layout. diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8cb28e327..46e5ab592 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -380,6 +380,26 @@ transcribe_apply_warnings(transcribe_conv_pw_promote_unit) add_test(NAME transcribe_conv_pw_promote_unit COMMAND transcribe_conv_pw_promote_unit) +# ----------------------------------------------------------------------------- +# conv_module() with 2-D pointwise kernels +# ----------------------------------------------------------------------------- +# +# A 2-D [in, out] pointwise kernel (F32 or quantized) must take the direct +# mul_mat path under an im2col policy and match the [1, in, out] layout. + +add_executable(transcribe_conformer_pw_layout_unit + conformer_pw_layout_unit.cpp) + +target_link_libraries(transcribe_conformer_pw_layout_unit PRIVATE transcribe ggml) + +target_include_directories(transcribe_conformer_pw_layout_unit PRIVATE + ${CMAKE_SOURCE_DIR}/src) + +transcribe_apply_warnings(transcribe_conformer_pw_layout_unit) + +add_test(NAME transcribe_conformer_pw_layout_unit + COMMAND transcribe_conformer_pw_layout_unit) + # ----------------------------------------------------------------------------- # causal_lm::pack_gate_up: alias / copy / mismatch, no model needed # ----------------------------------------------------------------------------- diff --git a/tests/conformer_pw_layout_unit.cpp b/tests/conformer_pw_layout_unit.cpp new file mode 100644 index 000000000..ccbe2fa68 --- /dev/null +++ b/tests/conformer_pw_layout_unit.cpp @@ -0,0 +1,182 @@ +// conformer_pw_layout_unit.cpp - conv_module() with 2-D pointwise kernels. +// +// A pointwise kernel stored 2-D ([in, out], the layout the quantizer +// classifies as Linear) has no k axis for im2col, so conv_module() must +// route it to the direct mul_mat path even when the backend policy asks +// for im2col. Runs on the CPU backend with policy.direct_pw = false: +// +// - [1, in, out] F32 kernels (im2col): reference output. +// - [in, out] F32 kernels, same values: matches the reference. +// - [in, out] Q8_0 kernels: close to the reference. + +#include "conformer/conformer.h" +#include "ggml-backend.h" +#include "ggml.h" + +#include +#include +#include +#include +#include + +namespace { + +int g_failures = 0; + +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + ++g_failures; \ + } \ + } while (0) + +#define REQUIRE(cond) \ + do { \ + if (!(cond)) { \ + std::fprintf(stderr, "SETUP FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + return EXIT_FAILURE; \ + } \ + } while (0) + +constexpr int64_t kDModel = 64; +constexpr int64_t kFrames = 12; +constexpr int kKernel = 9; + +std::vector wave(size_t n, float scale, float phase) { + std::vector v(n); + for (size_t i = 0; i < n; ++i) { + v[i] = scale * std::sin(0.37f * static_cast(i) + phase); + } + return v; +} + +std::vector read(ggml_tensor * t) { + std::vector v(static_cast(ggml_nelements(t))); + ggml_backend_tensor_get(t, v.data(), 0, v.size() * sizeof(float)); + return v; +} + +void set_q8_0(ggml_tensor * t, const std::vector & src, int64_t n_per_row, int64_t n_rows) { + std::vector q(ggml_row_size(GGML_TYPE_Q8_0, n_per_row) * n_rows); + ggml_quantize_chunk(GGML_TYPE_Q8_0, src.data(), q.data(), 0, n_rows, n_per_row, nullptr); + ggml_backend_tensor_set(t, q.data(), 0, q.size()); +} + +} // namespace + +int main() { + using namespace transcribe::conformer; + + ggml_backend_t backend = ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr); + if (backend == nullptr) { + std::fprintf(stderr, "SKIP: could not init CPU backend\n"); + return 77; + } + + ggml_init_params init_params{}; + init_params.mem_size = 4 * 1024 * 1024; + init_params.no_alloc = true; + ggml_context * ctx = ggml_init(init_params); + REQUIRE(ctx != nullptr); + + const int64_t d = kDModel; + ggml_tensor * x = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, d, kFrames, 1); + ggml_tensor * pw1_3d = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 1, d, 2 * d); + ggml_tensor * pw2_3d = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, 1, d, d); + ggml_tensor * pw1_2d = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, 2 * d); + ggml_tensor * pw2_2d = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, d, d); + ggml_tensor * pw1_q = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, d, 2 * d); + ggml_tensor * pw2_q = ggml_new_tensor_2d(ctx, GGML_TYPE_Q8_0, d, d); + ggml_tensor * dw = ggml_new_tensor_3d(ctx, GGML_TYPE_F32, kKernel, 1, d); + ggml_tensor * bn_scale = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, d); + ggml_tensor * bn_bias = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, d); + + BlockParams bp{}; + bp.d_model = static_cast(d); + bp.n_head = 1; + bp.conv_kernel = kKernel; + bp.use_flash = false; + bp.policy.direct_pw = false; + + BlockView v3{}; + v3.conv_pw1_w = pw1_3d; + v3.conv_pw2_w = pw2_3d; + v3.conv_dw_w = dw; + v3.conv_bn_fused_scale = bn_scale; + v3.conv_bn_fused_bias = bn_bias; + BlockView v2 = v3; + v2.conv_pw1_w = pw1_2d; + v2.conv_pw2_w = pw2_2d; + BlockView vq = v3; + vq.conv_pw1_w = pw1_q; + vq.conv_pw2_w = pw2_q; + + ggml_tensor * y3 = conv_module(ctx, x, v3, bp); + ggml_tensor * y2 = conv_module(ctx, x, v2, bp); + ggml_tensor * yq = conv_module(ctx, x, vq, bp); + REQUIRE(y3 != nullptr && y2 != nullptr && yq != nullptr); + + ggml_cgraph * graph = ggml_new_graph(ctx); + ggml_build_forward_expand(graph, y3); + ggml_build_forward_expand(graph, y2); + ggml_build_forward_expand(graph, yq); + + ggml_backend_buffer_t buffer = ggml_backend_alloc_ctx_tensors(ctx, backend); + REQUIRE(buffer != nullptr); + + // A [1, in, out] kernel and an [in, out] kernel share one memory order, + // so both layouts get the same values. + const std::vector xs = wave(static_cast(d * kFrames), 1.0f, 0.1f); + const std::vector w1 = wave(static_cast(2 * d * d), 0.15f, 0.7f); + const std::vector w2 = wave(static_cast(d * d), 0.15f, 1.9f); + const std::vector wd = wave(static_cast(kKernel * d), 0.3f, 2.3f); + const std::vector scale = wave(static_cast(d), 0.2f, 0.4f); + const std::vector bias = wave(static_cast(d), 0.1f, 1.1f); + std::vector scale_one(scale.size()); + for (size_t i = 0; i < scale.size(); ++i) { + scale_one[i] = 1.0f + scale[i]; + } + ggml_backend_tensor_set(x, xs.data(), 0, xs.size() * sizeof(float)); + ggml_backend_tensor_set(pw1_3d, w1.data(), 0, w1.size() * sizeof(float)); + ggml_backend_tensor_set(pw2_3d, w2.data(), 0, w2.size() * sizeof(float)); + ggml_backend_tensor_set(pw1_2d, w1.data(), 0, w1.size() * sizeof(float)); + ggml_backend_tensor_set(pw2_2d, w2.data(), 0, w2.size() * sizeof(float)); + set_q8_0(pw1_q, w1, d, 2 * d); + set_q8_0(pw2_q, w2, d, d); + ggml_backend_tensor_set(dw, wd.data(), 0, wd.size() * sizeof(float)); + ggml_backend_tensor_set(bn_scale, scale_one.data(), 0, scale_one.size() * sizeof(float)); + ggml_backend_tensor_set(bn_bias, bias.data(), 0, bias.size() * sizeof(float)); + + CHECK(ggml_backend_graph_compute(backend, graph) == GGML_STATUS_SUCCESS); + CHECK(ggml_are_same_shape(y2, y3)); + CHECK(ggml_are_same_shape(yq, y3)); + + if (ggml_are_same_shape(y2, y3) && ggml_are_same_shape(yq, y3)) { + const std::vector r3 = read(y3); + const std::vector r2 = read(y2); + const std::vector rq = read(yq); + + double max_abs_2d = 0.0; + double err_q = 0.0; + double ref = 0.0; + for (size_t i = 0; i < r3.size(); ++i) { + max_abs_2d = std::fmax(max_abs_2d, std::fabs(static_cast(r2[i]) - r3[i])); + err_q += (static_cast(rq[i]) - r3[i]) * (static_cast(rq[i]) - r3[i]); + ref += static_cast(r3[i]) * r3[i]; + } + CHECK(ref > 0.0); + CHECK(max_abs_2d < 1e-4); + CHECK(std::sqrt(err_q / ref) < 2e-2); + } + + ggml_backend_buffer_free(buffer); + ggml_free(ctx); + ggml_backend_free(backend); + + if (g_failures > 0) { + std::fprintf(stderr, "\n%d assertion(s) FAILED\n", g_failures); + } + + return g_failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +}