Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_GIGAAM_NO_KEEP_WARM` | gigaam: do not spin a host thread during a GPU encode. The thread keeps the calling core clocked up, so the host decode that follows does not start on a slow core. |
| `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=<dir>` | Enable the per-stage tensor dumper; writes `<name>.f32` + `<name>.json` per dumped tensor into `<dir>`. The basis for the numerical-comparison harness (`scripts/compare_tensors.py`). |
Expand Down
2 changes: 1 addition & 1 deletion docs/tools/quantization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |

Expand Down
18 changes: 16 additions & 2 deletions src/arch/parakeet/weights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) \
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion src/conformer/conformer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
# -----------------------------------------------------------------------------
Expand Down
182 changes: 182 additions & 0 deletions tests/conformer_pw_layout_unit.cpp
Original file line number Diff line number Diff line change
@@ -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 <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>

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<float> wave(size_t n, float scale, float phase) {
std::vector<float> v(n);
for (size_t i = 0; i < n; ++i) {
v[i] = scale * std::sin(0.37f * static_cast<float>(i) + phase);
}
return v;
}

std::vector<float> read(ggml_tensor * t) {
std::vector<float> v(static_cast<size_t>(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<float> & src, int64_t n_per_row, int64_t n_rows) {
std::vector<uint8_t> 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<int>(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<float> xs = wave(static_cast<size_t>(d * kFrames), 1.0f, 0.1f);
const std::vector<float> w1 = wave(static_cast<size_t>(2 * d * d), 0.15f, 0.7f);
const std::vector<float> w2 = wave(static_cast<size_t>(d * d), 0.15f, 1.9f);
const std::vector<float> wd = wave(static_cast<size_t>(kKernel * d), 0.3f, 2.3f);
const std::vector<float> scale = wave(static_cast<size_t>(d), 0.2f, 0.4f);
const std::vector<float> bias = wave(static_cast<size_t>(d), 0.1f, 1.1f);
std::vector<float> 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<float> r3 = read(y3);
const std::vector<float> r2 = read(y2);
const std::vector<float> 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<double>(r2[i]) - r3[i]));
err_q += (static_cast<double>(rq[i]) - r3[i]) * (static_cast<double>(rq[i]) - r3[i]);
ref += static_cast<double>(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;
}