Commit eaa71ea09 for llama.cpp
commit eaa71ea095254169b3b39f452a9cb595a922089b
Author: Keroweir <79356629+luoji12103@users.noreply.github.com>
Date: Wed Dec 2 19:50:28 2026 +0800
mtmd : emit the first Drama 3 streaming audio frame immediately (#33512)
* mtmd : emit the first Drama 3 streaming audio frame immediately
* mtmd : rename mtmd-helper-fish-dac.h to mtmd-helper-impl.h
diff --git a/docs/multimodal/tts.md b/docs/multimodal/tts.md
index 195c19fb7..1e4ef116f 100644
--- a/docs/multimodal/tts.md
+++ b/docs/multimodal/tts.md
@@ -179,6 +179,7 @@ Available params:
- Voice cloning: `--tts-speaker-file` with the reference clip and `--tts-speaker-text` with its transcript (a transcript without a speaker tag is speaker 0). The clip is encoded by the codec encoder half of `codec.pth` and its codes are put in the prompt as one reference, as the reference does; the clip is truncated to the first 30 s. It needs an mmproj converted with the codec encoder
- Note: `--top-k` and `--top-p` are not used, frames are sampled with the settings of the reference (main token: temperature 1.0, top-p 0.9, repetition-aware redraw over the last 16 frames; other codes: temperature 1.0, top-k 30, top-p 0.9)
- Each frame is 2048 samples (about 46 ms), generation stops at the end-of-speech token
+- Streaming emits the first frame on its own, then seven frames at a time and any remaining frames at the end
- The codebook tower keeps the whole sequence in its KV cache, the prompt and the generated frames are limited to 4096 positions
Example usage:
diff --git a/tests/test-mtmd-impl.cpp b/tests/test-mtmd-impl.cpp
index d37884878..51de24402 100644
--- a/tests/test-mtmd-impl.cpp
+++ b/tests/test-mtmd-impl.cpp
@@ -5,6 +5,7 @@
#include "mtmd-audio.h"
#include "mtmd-subcontext.h"
#include "mtmd-helper.h"
+#include "mtmd-helper-impl.h"
#include "mtmd-image.h"
#include "mtmd-internal.h"
#include "models/models.h"
@@ -1188,6 +1189,62 @@ MAKE_TEST(test_kokoro_tts_template) {
}
}
+MAKE_TEST(test_fish_dac_chunks) {
+ const auto drama = MTMD_GEN_AUDIO_TYPE_DRAMA3;
+ const auto fish = MTMD_GEN_AUDIO_TYPE_FISH_S2;
+ struct test_case {
+ mtmd_gen_audio_type type;
+ bool stream;
+ size_t frames;
+ std::vector<size_t> before_eos;
+ std::vector<size_t> at_flush;
+ };
+ const std::vector<test_case> cases = {
+ { drama, true, 0, {}, {} },
+ { drama, true, 1, {1}, {} },
+ { drama, true, 2, {1}, {1} },
+ { drama, true, 7, {1}, {6} },
+ { drama, true, 8, {1, 7}, {} },
+ { drama, true, 17, {1, 7, 7}, {2} },
+ { fish, true, 2, {}, {2} },
+ { fish, true, 7, {7}, {} },
+ { fish, true, 17, {7, 7}, {3} },
+ { drama, false, 2, {}, {2} },
+ { drama, false, 70, {}, {64, 6} },
+ };
+ for (const auto & c : cases) {
+ const std::string name = std::string(c.type == drama ? "Drama 3" : "Fish S2") +
+ (c.stream ? " stream " : " full ") + std::to_string(c.frames);
+ size_t n_decoded = 0;
+ std::vector<size_t> chunks;
+ std::vector<float> pcm;
+ // A frame contains its 2048 sample indices, so a skipped or repeated frame breaks the ramp.
+ auto decode = [&](size_t first, size_t n) {
+ chunks.push_back(n);
+ for (size_t i = first * 2048; i < (first + n) * 2048; ++i) {
+ pcm.push_back((float) i);
+ }
+ return true;
+ };
+ for (size_t frames = 1; frames <= c.frames; ++frames) {
+ mtmd_helper_fish_dac_decode(c.type, frames, n_decoded, c.stream, false, decode);
+ }
+ t.assert_true(name + " before EOS", chunks == c.before_eos);
+ chunks.clear();
+ mtmd_helper_fish_dac_decode(c.type, c.frames, n_decoded, c.stream, true, decode);
+ t.assert_true(name + " flush", chunks == c.at_flush);
+ chunks.clear();
+ mtmd_helper_fish_dac_decode(c.type, c.frames, n_decoded, c.stream, true, decode);
+ t.assert_true(name + " second flush", chunks.empty());
+ t.assert_equal(name + " samples", c.frames * 2048, pcm.size());
+ bool contiguous = true;
+ for (size_t i = 0; i < pcm.size(); ++i) {
+ contiguous = contiguous && pcm[i] == (float) i;
+ }
+ t.assert_true(name + " sample order", contiguous);
+ }
+}
+
// every frame is the constant 0.25 (only the DC bin, of magnitude n_fft * 0.25)
// the overlap-add of hann windows at a quarter hop sums to 2, their squares to 1.5: the output is 0.25 * 2 / 1.5
MAKE_TEST(test_kokoro_istft) {
diff --git a/tools/mtmd/mtmd-helper-gen.cpp b/tools/mtmd/mtmd-helper-gen.cpp
index 03ab766d9..5ad139262 100644
--- a/tools/mtmd/mtmd-helper-gen.cpp
+++ b/tools/mtmd/mtmd-helper-gen.cpp
@@ -1,6 +1,7 @@
#include "mtmd.h"
#include "mtmd-helper.h"
#include "mtmd-helper-common.h"
+#include "mtmd-helper-impl.h"
#include "llama.h"
#include "../src/llama-ext.h"
@@ -2885,10 +2886,7 @@ public:
save_main_logits(-1);
*h_state_out = h_state_buf.data();
- if (stream && codes_buf.size() / n_cb - n_decoded >= stream_frames) {
- return decode_wav() ? 0 : 1;
- }
- return 0;
+ return decode_wav(false) ? 0 : 1;
}
int32_t get_output(int32_t * out_sample_rate, const char ** out_data, size_t * out_data_len, int64_t * out_n_samples) override {
@@ -2940,7 +2938,7 @@ public:
}
int32_t flush() override {
- return decode_wav() ? 0 : 1;
+ return decode_wav(true) ? 0 : 1;
}
protected:
@@ -3081,8 +3079,6 @@ protected:
static constexpr size_t n_cb = 10;
// codebook tower KV rows (mtmd_graph_drama3_gen::n_pos_max): one per prompt row but the last, one per frame
static constexpr size_t n_tower_rows = 4096;
- // frames per codec call when streaming (0.33 s)
- static constexpr size_t stream_frames = 7;
// top-p keeps the token that crosses the threshold, as the reference does
llama_token draw(const std::vector<float> & cand_logits, float temp, int32_t top_k, float top_p) {
@@ -3161,13 +3157,12 @@ protected:
}
// the codec is stateful, so frames can be decoded in pieces
- bool decode_wav() {
+ bool decode_wav(bool flush) {
const size_t n_frames = codes_buf.size() / n_cb;
- while (n_decoded < n_frames) {
- const size_t n = std::min(n_frames - n_decoded, (size_t) 64);
+ return mtmd_helper_fish_dac_decode(info.type, n_frames, n_decoded, stream, flush, [&](size_t start, size_t n) {
mtmd_gen_inp inp = mtmd_gen_inp_default(mctx);
inp.type = MTMD_GEN_PROCESS_TYPE_GEN_WAV;
- inp.codes = codes_buf.data() + n_decoded * n_cb;
+ inp.codes = codes_buf.data() + start * n_cb;
inp.n_codes = n * n_cb;
mtmd_gen_out out{};
if (mtmd_gen_audio_process(mctx, gen_state.get(), &inp, &out) != 0) {
@@ -3175,9 +3170,8 @@ protected:
return false;
}
audio_pcm.insert(audio_pcm.end(), out.audio, out.audio + out.n_samples);
- n_decoded += n;
- }
- return true;
+ return true;
+ });
}
static constexpr int n_cb_vocab = 4096; // <|semantic:0..4095|>
diff --git a/tools/mtmd/mtmd-helper-impl.h b/tools/mtmd/mtmd-helper-impl.h
new file mode 100644
index 000000000..49efbf71c
--- /dev/null
+++ b/tools/mtmd/mtmd-helper-impl.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "mtmd.h"
+
+#include <algorithm>
+#include <cstddef>
+
+// Drain complete frames in codec batches. Only advance past a batch after it decodes.
+template <typename Decode>
+bool mtmd_helper_fish_dac_decode(mtmd_gen_audio_type type, size_t n_frames, size_t & n_decoded,
+ bool stream, bool flush, Decode decode) {
+ const size_t chunk_frames = type == MTMD_GEN_AUDIO_TYPE_DRAMA3 && n_decoded == 0 ? 1 : 7;
+ if (!flush && (!stream || n_frames - n_decoded < chunk_frames)) {
+ return true;
+ }
+ while (n_decoded < n_frames) {
+ const size_t n = std::min(n_frames - n_decoded, (size_t) 64);
+ if (!decode(n_decoded, n)) {
+ return false;
+ }
+ n_decoded += n;
+ }
+ return true;
+}