Commit f505c30b9 for llama.cpp
commit f505c30b92b5579e00532c9e75baa3aa74b70432
Author: George Burton <8233643+gecBurton@users.noreply.github.com>
Date: Sun Nov 22 20:22:46 2026 +0000
mtmd: fix qwen3a audio tail (#32697)
* add: test to reporduce error reported in https://github.com/ggml-org/llama.cpp/issues/32689
* fix: remove extra tokens from zero-padding of Qwen3-Omni / Qwen3-ASR audio
* add: qwen3a_tokens_for_frames instead of magic numbers
* refactor: only ggml_pad when required
* refactor: move qwen3a_tokens_for_frames as static member of mtmd_graph_qwen3a and add test
* remove: trailing empty line
---------
Co-authored-by: george burton <georgeburton@georges-MacBook-Air.local>
diff --git a/tests/test-mtmd-impl.cpp b/tests/test-mtmd-impl.cpp
index e24365e59..0c34f9c2b 100644
--- a/tests/test-mtmd-impl.cpp
+++ b/tests/test-mtmd-impl.cpp
@@ -609,6 +609,50 @@ MAKE_TEST(test_audio_preprocessor_whisper_chunks) {
}
}
+MAKE_TEST(test_audio_preprocessor_qwen3a_tail) {
+ mtmd_hparams hparams;
+ hparams.n_mel_bins = 128;
+ hparams.audio_sample_rate = 16000;
+ hparams.audio_n_fft = 400;
+ hparams.audio_window_len = 400;
+ hparams.audio_hop_len = 160;
+
+ ggml_backend_ptr backend(ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr));
+ ggml_backend_t backends[] = { backend.get() };
+ ggml_backend_sched_ptr sched(ggml_backend_sched_new(backends, nullptr, 1, 256, false, true));
+
+ mtmd_audio_preprocessor_qwen3a preproc(hparams, sched.get());
+ preproc.initialize();
+
+ // one 800-frame window covers 128000 samples (hop 160), so longer audio is split;
+ // the last window is emitted with its exact frame count, the graph pads the final 100-frame chunk
+ // { sample count, expected n_len of each output window }
+ const std::vector<std::pair<size_t, std::vector<int64_t>>> cases = {
+ { 0, {} },
+ { 1, {1} },
+ { 32000, {200} },
+ { 128000, {800} },
+ { 128160, {800, 1} },
+ { 168000, {800, 250} },
+ { 170080, {800, 263} },
+ { 176000, {800, 300} },
+ };
+
+ for (const auto & [n_samples, expected] : cases) {
+ const std::string name = std::to_string(n_samples) + " samples";
+ std::vector<float> samples(n_samples, 0.0f);
+ if (n_samples > 0) {
+ samples.back() = 0.5f;
+ }
+ mtmd_audio_preproc_out out = preproc.preprocess(samples.data(), samples.size());
+ t.assert_equal("windows for " + name, expected.size(), out.entries.size());
+ for (size_t i = 0; i < out.entries.size(); i++) {
+ t.assert_equal("frames for " + name, expected[i], out.entries[i].nx());
+ t.assert_equal("mel bins for " + name, 128, out.entries[i].ny());
+ }
+ }
+}
+
MAKE_TEST(test_audio_preprocessor_conformer) {
mtmd_hparams hparams;
hparams.n_mel_bins = 128;
@@ -1062,6 +1106,27 @@ MAKE_TEST(test_kokoro_istft) {
t.assert_true("constant signal, max error " + std::to_string(max_err), max_err < 1e-4f);
}
+// 13 tokens per 100-frame chunk; the tail is ceil^3 (one halving per stride-2 conv layer)
+MAKE_TEST(test_qwen3a_tokens_for_frames) {
+ const std::vector<std::pair<int64_t, int64_t>> cases = {
+ { 0, 0 },
+ { 1, 1 },
+ { 99, 13 },
+ { 100, 13 },
+ { 101, 14 },
+ { 110, 15 },
+ { 200, 26 },
+ { 210, 28 },
+ { 1000, 130 },
+ { 1024, 133 },
+ };
+
+ for (const auto & [frames, expected] : cases) {
+ const std::string name = std::to_string(frames) + " frames";
+ t.assert_equal(name, expected, mtmd_graph_qwen3a::qwen3a_tokens_for_frames(frames));
+ }
+}
+
//
// main
//
diff --git a/tools/mtmd/models/models.h b/tools/mtmd/models/models.h
index 278fcdb27..c1feef493 100644
--- a/tools/mtmd/models/models.h
+++ b/tools/mtmd/models/models.h
@@ -387,6 +387,10 @@ struct mtmd_graph_mobilenetv5 : mtmd_graph {
struct mtmd_graph_qwen3a : mtmd_graph {
mtmd_graph_qwen3a(mtmd_subcontext * ctx, const mtmd_image_f32 & img) : mtmd_graph(ctx, img) {}
ggml_cgraph * build() override;
+
+ // audio tokens from n_frames mel frames: 13 per 100-frame chunk, ceil^3(tail) otherwise.
+ // shared with mtmd_model::n_output_tokens so they cannot drift
+ static int64_t qwen3a_tokens_for_frames(int64_t n_frames);
};
struct mtmd_graph_mimo_audio : mtmd_graph {
diff --git a/tools/mtmd/models/qwen3a.cpp b/tools/mtmd/models/qwen3a.cpp
index 0ed6b9a8f..24588f0c4 100644
--- a/tools/mtmd/models/qwen3a.cpp
+++ b/tools/mtmd/models/qwen3a.cpp
@@ -6,14 +6,19 @@ ggml_cgraph * mtmd_graph_qwen3a::build() {
// inp_raw: [n_frames, n_mel, 1] (nx=n_frames, ny=n_mel)
ggml_tensor * inp = build_inp_raw(1);
- const int64_t n_frames = inp->ne[0]; // total frames, padded to multiple of chunk_size
const int64_t n_mel = inp->ne[1]; // 128
const int64_t chunk_size = 100; // n_window * 2 (n_window=50 from model config)
- const int64_t n_chunks = n_frames / chunk_size;
+ const int64_t n_chunks = (inp->ne[0] + chunk_size - 1) / chunk_size;
+ const int64_t n_pre_pad = inp->ne[0];
- GGML_ASSERT(n_frames % chunk_size == 0); // preprocessor should already pad the input
GGML_ASSERT(inp->type == GGML_TYPE_F32);
+ // zero-pad the last chunk to chunk_size, as the reference pads all chunks to the longest one
+ if (n_pre_pad % chunk_size > 0) {
+ inp = ggml_pad(ctx0, inp, n_chunks * chunk_size - n_pre_pad, 0, 0, 0);
+ }
+ const int64_t n_frames = inp->ne[0];
+
// View mel spectrogram as batched 100-frame chunks: [chunk_size, n_mel, 1, n_chunks]
inp = ggml_view_4d(ctx0, inp,
chunk_size, n_mel, 1, n_chunks,
@@ -69,7 +74,11 @@ ggml_cgraph * mtmd_graph_qwen3a::build() {
inp = ggml_add(ctx0, inp, ggml_repeat(ctx0, pos_tmp, tgt));
}
- ggml_tensor * cur = build_vit(inp, n_pos,
+ // drop the outputs of the padding in the last chunk
+ const int64_t n_out = qwen3a_tokens_for_frames(n_pre_pad);
+ inp = ggml_view_2d(ctx0, inp, inp->ne[0], n_out, inp->nb[1], 0);
+
+ ggml_tensor * cur = build_vit(inp, n_out,
NORM_TYPE_NORMAL, hparams.ffn_op,
nullptr, // pos embd already added above
nullptr);
diff --git a/tools/mtmd/mtmd-audio.cpp b/tools/mtmd/mtmd-audio.cpp
index 53c2cbb27..e0b27b9a5 100644
--- a/tools/mtmd/mtmd-audio.cpp
+++ b/tools/mtmd/mtmd-audio.cpp
@@ -594,9 +594,10 @@ bool mtmd_audio_preprocessor_whisper::process(const float * samp
//
// Matches the Python WhisperFeatureExtractor called with truncation=False:
// - reflection padding of n_fft/2 samples at each end (center=True)
-// - Whisper-style log10 + (max-8)/4 normalization applied to full audio
-// - output split into ≤30s (3000 mel frames) windows, each padded to a
-// multiple of 200 frames (n_window * 2) for the cgraph batch view
+// - the last STFT frame is dropped, giving n_samples / hop_length frames
+// - Whisper-style log10 + (max-8)/4 normalization over the kept frames
+// - output split into 800-frame (n_window_infer) windows; the graph
+// zero-pads the last 100-frame chunk
//
void mtmd_audio_preprocessor_qwen3a::initialize() {
@@ -648,11 +649,17 @@ bool mtmd_audio_preprocessor_qwen3a::process(const float * sampl
return false;
}
- // Whisper-style normalization: clamp to (max - 8), scale to [-1, 1]
- {
+ // WhisperFeatureExtractor drops the last STFT frame, keeping n_samples / hop_length frames;
+ // sub-hop clips would give 0 frames, clamp to 1 so the cgraph stays non-empty
+ const int64_t n_eff = std::max<int64_t>(1, std::min(mel_full.n_len - 1, (int64_t)(n_samples / hparams.audio_hop_len)));
+
+ // Whisper-style normalization over the kept frames: clamp to (max - 8), scale to [-1, 1]
+ if (!shape_only) {
double mmax = -1e20;
- for (float v : mel_full.data) {
- if (v > mmax) mmax = v;
+ for (int64_t m = 0; m < mel_full.n_mel; m++) {
+ for (int64_t t = 0; t < n_eff; t++) {
+ mmax = std::max(mmax, (double)mel_full.data[m * mel_full.n_len + t]);
+ }
}
mmax -= 8.0;
for (float & v : mel_full.data) {
@@ -660,25 +667,16 @@ bool mtmd_audio_preprocessor_qwen3a::process(const float * sampl
}
}
- // The effective frame count: center-padded STFT gives ~n_samples/hop_length frames.
- // We take min(mel_full.n_len, n_samples/hop + 1) to avoid including excess frames.
- const int64_t n_eff = std::min(mel_full.n_len,
- (int64_t)(n_samples / hparams.audio_hop_len) + 1);
-
// Split into inference windows matching n_window_infer=800 from model config.
- // Each window is padded to the next multiple of chunk_size for the cgraph.
// The mtmd caller loops over output entries, so long audio is handled automatically.
- const int chunk_size = 100; // conv sub-chunk size (n_window * 2, n_window=50)
const int window_size = 800; // mel frames per forward pass (n_window_infer=800)
for (int64_t off = 0; off < n_eff; off += window_size) {
const int64_t win_eff = std::min((int64_t)window_size, n_eff - off);
- const int64_t n_chunks = (win_eff + chunk_size - 1) / chunk_size;
- const int64_t n_padded = n_chunks * chunk_size;
mtmd_audio_mel out;
out.n_mel = mel_full.n_mel;
- out.n_len = n_padded;
+ out.n_len = win_eff;
out.n_len_org = win_eff;
out.data.assign((size_t)out.n_mel * (size_t)out.n_len, 0.0f);
for (int64_t m = 0; m < out.n_mel; m++) {
diff --git a/tools/mtmd/mtmd-audio.h b/tools/mtmd/mtmd-audio.h
index 6d282721d..4ee788276 100644
--- a/tools/mtmd/mtmd-audio.h
+++ b/tools/mtmd/mtmd-audio.h
@@ -208,6 +208,7 @@ struct mtmd_audio_preprocessor_gemma4ua : mtmd_audio_preprocessor {
struct mtmd_audio_preprocessor_qwen3a : mtmd_audio_preprocessor {
mtmd_audio_preprocessor_qwen3a(const mtmd_model * model) : mtmd_audio_preprocessor(model) {}
+ mtmd_audio_preprocessor_qwen3a(const mtmd_hparams & hparams, ggml_backend_sched_t sched) : mtmd_audio_preprocessor(hparams, sched) {}
void initialize() override;
bool process(const float * samples, size_t n_samples, std::vector<mtmd_audio_mel> & output, bool shape_only) const override;
diff --git a/tools/mtmd/mtmd-model.cpp b/tools/mtmd/mtmd-model.cpp
index 570750b9b..97dc47598 100644
--- a/tools/mtmd/mtmd-model.cpp
+++ b/tools/mtmd/mtmd-model.cpp
@@ -4737,6 +4737,16 @@ int mtmd_model::n_output_tokens_y(const mtmd_image_f32 * img) const {
return 1;
}
+int64_t mtmd_graph_qwen3a::qwen3a_tokens_for_frames(int64_t n_frames) {
+ const int64_t chunk_size = 100; // n_window * 2
+ const int64_t tokens_per_chunk = 13; // 100 -> 50 -> 25 -> 13 after 3x stride-2 conv2d
+ const int64_t n_full = n_frames / chunk_size;
+ const int64_t n_tail = n_frames % chunk_size;
+ // ceil(ceil(ceil(n_tail / 2) / 2) / 2), one halving per conv layer
+ const int64_t n_tail_tokens = n_tail == 0 ? 0 : (((n_tail + 1) / 2 + 1) / 2 + 1) / 2;
+ return n_full * tokens_per_chunk + n_tail_tokens;
+}
+
int mtmd_model::n_output_tokens(const mtmd_image_f32 * img) const {
const auto & params = hparams;
@@ -4946,10 +4956,7 @@ int mtmd_model::n_output_tokens(const mtmd_image_f32 * img) const {
} break;
case PROJECTOR_TYPE_QWEN3A:
{
- // chunk_size=100 frames --> 3x stride-2 conv2d --> 13 tokens per chunk
- const int chunk_size = 100;
- const int tokens_per_chunk = 13;
- n_patches = (img->nx() / chunk_size) * tokens_per_chunk;
+ n_patches = (int) mtmd_graph_qwen3a::qwen3a_tokens_for_frames(img->nx());
} break;
case PROJECTOR_TYPE_GLMA:
{