Skip to content

miotts: apply repetition_penalty once per distinct token (HF semantics) - #906

Merged
0xShug0 merged 1 commit into
0xShug0:mainfrom
zlatkoviz-png:miotts-repetition-penalty-once
Oct 14, 2026
Merged

0xShug0 merged 1 commit into
0xShug0:mainfrom
zlatkoviz-png:miotts-repetition-penalty-once

Conversation

@zlatkoviz-png

Copy link
Copy Markdown
Contributor

What

apply_repetition_penalty in src/models/miotts/causal_lm.cpp walks the whole history and rescales a logit once per occurrence. A speech token that has already been generated n times is therefore penalized penalty^n instead of penalty.

transformers' RepetitionPenaltyLogitsProcessor (gather at input_ids, scatter back) and this repo's own HfLogitsProcessor::apply_repetition_penalty rescale each distinct token in the history exactly once. This PR does the same for MioTTS: it remembers which speech tokens (and EOS) were already rescaled in the current step and skips the repeats. No other code is touched.

Impact (honest summary)

  • repetition_penalty = 1.0 (the default when the checkpoint has no repetition_penalty in generation_config.json, as with Aratako/MioTTS-1.7B): the function returns early, so nothing changes.
  • At 1.02 and 1.05 (greedy, on the sentences below), we measured no output difference. MioTTS never generated the same speech token twice within a sentence there, so "once per occurrence" and "once per distinct token" are the same thing.
  • The two diverge only when a token is already in the history at least twice. We could trigger that with greedy decoding at repetition_penalty = 1.005. At that point the old code's choice no longer matches transformers (details below).

So this is a parity/correctness fix with a small practical footprint at common settings, not a quality fix you would hear at 1.05.

Validation

Everything below is CPU only, with public assets only:

  • model: Aratako/MioTTS-1.7B converted with audiocpp_gguf --type f16;
  • codec: MioCodec-25Hz-44.1kHz-v2 GGUF;
  • voice: samples/en_ref1.wav from the MioTTS-1.7B HF repo;
  • decoding: greedy (do_sample=false), --seed 0, --threads 8, --backend cpu;
  • machine: DGX Spark (aarch64, GB10).

The base is main @ 571c92cf; "old" is main, "new" is this branch.

Build:

cmake -S . -B build-cpu -DCMAKE_BUILD_TYPE=Release
cmake --build build-cpu -j 8 --target audiocpp_cli audiocpp_gguf

Run (one batch session per setting):

audiocpp_cli --task tts --family miotts --model stock-mio-1.7b-f16.gguf --backend cpu --threads 8 \
  --session-option miotts.codec_model_path=miocodec-v2-f16.gguf \
  --batch-text-dir <dir with one .txt per sentence> --voice-ref en_ref1.wav --seed 0 \
  --request-option do_sample=false --request-option repetition_penalty=<P> \
  --out-dir <out> --batch-manifest-out <out>/manifest.json --metrics --log

Sentences (8, English):

  1. The old library was silent, save for the gentle ticking of a clock somewhere in the shadows.
  2. Please leave the keys on the kitchen table before you go.
  3. We waited, and waited, and waited, but the train never came.
  4. Hello? Is anyone there? Hello?
  5. The results were clear: no, no, and again no.
  6. She counted slowly: one, two, three, four, five, six, seven, eight.
  7. After a long pause, he finally said: I think we should go home now.
  8. Thank you very much for your patience. We will be with you shortly.

The 3-sentence runs use 03, 04 and 05 (the ones that loop at 1.0).

1. Regression: outputs unchanged where the bug cannot trigger

repetition_penalty sentences old vs new WAV
1.0 8 bit-identical 8/8
1.05 8 bit-identical 8/8 (max multiplicity of any speech token: 1)
1.02 3 bit-identical 3/3 (max multiplicity: 1)

The 1.0 runs use plain builds of main and of this branch. The 1.02/1.05 runs (and section 2) use the same two builds with the diagnostic print below added, so that the token multiplicity could be counted; the print does not touch any computation. As a control, cmp does report a difference between outputs that really differ, e.g. sentence 03 at 1.0 vs 1.05.

At 1.0 (greedy) the stock model runs into a token loop and hits max_tokens (700) on 5 of the 8 sentences. That is the same for old and new and is not related to this PR.

2. A case where the bug triggers, compared with transformers

To see the generated token ids I used a local, not included, diagnostic patch. It prints out.token_ids (as <|s_N|> indices) to stderr when AUDIOCPP_MIOTTS_DUMP_TOKENS=1 is set; the patch is below. The transformers reference is AutoModelForCausalLM in fp32 on CPU:

generate(do_sample=False, repetition_penalty=P)

The prompt is built with apply_chat_template(..., add_generation_prompt=True) and add_special_tokens=False, which is the same prompt audio.cpp builds.

repetition_penalty = 1.005, greedy:

sentence old: tokens new: tokens transformers: tokens old vs transformers new vs transformers
03 135 135 135 identical identical
04 83 83 83 identical identical
05 153 147 147 diverges at step 61 identical (147/147)

At step 61 of sentence 05, speech token 6449 had already been generated twice. transformers and the new code penalize it once (/1.005), and it stays the argmax. The old code penalizes it twice (/1.005^2), so it picks 12273 instead. From that step on, the old sequence differs, and it ends 6 tokens longer. In the reference, 6449 ends up occurring 6 times in that sentence; in the old output, no token occurs more than twice. The WAV for 05 differs between old and new; 03 and 04 are bit-identical.

The transformers run was transformers 5.14.1 with torch 2.10.0 on CPU in fp32, against audio.cpp F16 on CPU. They agree token for token on all three sentences, so the only disagreement in this table is the bug.

diagnostic patch (local only, not part of this PR)
diff --git a/src/models/miotts/causal_lm.cpp b/src/models/miotts/causal_lm.cpp
index d3a39b57..dde394be 100644
--- a/src/models/miotts/causal_lm.cpp
+++ b/src/models/miotts/causal_lm.cpp
@@ -21,6 +21,8 @@
 #include <chrono>
 #include <cmath>
 #include <cstdint>
+#include <cstdio>
+#include <cstdlib>
 #include <limits>
 #include <memory>
 #include <optional>
@@ -986,6 +988,13 @@ struct MioTTSQwen3Runtime::Impl {
             decode_graph->run_step_into(token, logits);
             decode_step_ms += engine::debug::elapsed_ms(step_start, Clock::now());
         }
+        if (const char * dump = std::getenv("AUDIOCPP_MIOTTS_DUMP_TOKENS"); dump != nullptr && dump[0] != '\0') {
+            std::fprintf(stderr, "MIOTTS_TOKENS %zu:", out.token_ids.size());
+            for (const int32_t id : out.token_ids) {
+                std::fprintf(stderr, " %lld", static_cast<long long>(id) - static_cast<long long>(config.speech_token_start_id));
+            }
+            std::fprintf(stderr, "\n");
+        }
         debug::timing_log_scalar("miotts.lm.sampling_ms", sampling_ms);
         debug::timing_log_scalar("miotts.lm.decode_step_ms", decode_step_ms);
         decode_graph->log_timing();

Notes

  • HfLogitsProcessor already has a generation-stamped seen_ scratch. I kept the fix model-local, in line with CONTRIBUTING's guidance, instead of rewiring MioTTS onto the shared sampler. A per-step std::vector<uint8_t> of speech_token_count bytes is negligible next to a decode step.

🤖 Generated with Claude Code

apply_repetition_penalty walked the whole history and rescaled a logit
again for every occurrence, so a speech token (or EOS) that had already
appeared n times was penalized penalty^n. transformers'
RepetitionPenaltyLogitsProcessor, and this repo's own
HfLogitsProcessor::apply_repetition_penalty, rescale each distinct token
in the history exactly once.

Remember which speech tokens (and EOS) were already rescaled in this
step and skip the repeats.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@0xShug0
0xShug0 merged commit a1a34f0 into 0xShug0:main Oct 14, 2026
6 checks passed
@0xShug0

0xShug0 commented Oct 14, 2026

Copy link
Copy Markdown
Owner

@zlatkoviz-png Thanks! PR merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants