Skip to content

sense_asr: return word timestamps from CTC emission frames - #1236

Merged
0xShug0 merged 2 commits into
0xShug0:mainfrom
jiabochao:sense-asr-word-timestamps
Nov 8, 2026
Merged

0xShug0 merged 2 commits into
0xShug0:mainfrom
jiabochao:sense-asr-word-timestamps

Conversation

@jiabochao

@jiabochao jiabochao commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds opt-in word timestamps to sense_asr (SenseVoice-Small).

SenseVoice decodes with greedy CTC. Since #1214 that decoding goes through CtcHostDecoder, which already reports each token's frame span, but sense_asr dropped the spans after building the text. This PR keeps the spans and, when return_timestamps=true is set, returns them as word_timestamps. The CLI sets that option automatically for --words-out. The approach matches GigaAM and Granite, which also derive word times from CTC emission frames.

My use case is meeting transcription: matching SenseVoice text against nemotron_3_diar speaker turns, word by word. Validation for that is below.

Changes

  • sense_asr/word_timestamps.h (new, model-local):
    • Groups CTC token spans into words.
      • Chinese, Japanese kana and Thai give one entry per character.
      • Other scripts join SentencePiece pieces at the ▁ marker.
      • Punctuation joins the preceding word and does not extend its time.
      • <|...|> tags are skipped.
    • Converts frames to samples. It first drops the 4 query-token frames (language, event, emotion, ITN). Each encoder frame is then 60 ms (frame_shift_ms × lfr_n), converted at the input's sample rate.
  • session.cpp: keeps the decoder spans. Chunked offline runs (vad, fixed, auto) and streaming windows shift the times to the whole input.
  • Streaming: windows are final once decoded, so each window uses append_transcript() with only its new text and words, the pattern finalized-segment streams use. Each stream event carries only that window's words; the final result has all of them.
  • model_specs/sense_asr.json: adds the word_timestamps capability and the return_timestamps request option.
  • New sense_asr_word_timestamps_test, plus docs in docs/models/sense_asr.md, docs/asr.md and the README model row.

Compatibility

  • Default output is unchanged. Without return_timestamps, no words are built. Transcripts with the option on and off are byte-identical; see the measurements below.
  • No C ABI declaration changes. Words are returned through the existing audiocpp_result_word* functions. Because the spec now declares word_timestamps, audiocpp_model_supports_timestamps() returns 1 for sense_asr.
  • No framework, backend or weight changes. The existing GGUF works as is.
  • The two CJK helpers in text/chunking.cpp (is_cjk_letter, is_cjk_punctuation_delimiter) are file-local. To keep this PR model-local, word_timestamps.h has its own small script and punctuation checks. I can follow up with a framework PR that exports and shares them, if you prefer.

Validation

Environment:

Base main @ 408935b
Host Linux 7.0, Intel Xeon (Skylake), 4 vCPU, GCC 15.2, CMake 4.2
Backend CPU (this host) and Metal (Apple M2 Pro, see the comment below). The timestamps are computed in host-side decoding after the encoder. CUDA and Vulkan have not been run.
Model sensevoice-small-q8-audiocpp-v1.gguf (FunAudioLLM/SenseVoiceSmall-GGUF-audiocpp, sha256 4dedf169…)
Audio SenseVoice's official examples zh, yue, ja and en (FunAudioLLM/SenseVoiceSmall/example/*.mp3, resampled to 16 kHz mono), plus assets/resources/sample_16k.wav and four_speaker_short.wav

Build:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DAUDIOCPP_MODEL_SET=custom \
  -DAUDIOCPP_MODELS="sense_asr;nemotron_3_diar" \
  -DENGINE_ENABLE_CUDA=OFF -DENGINE_ENABLE_VULKAN=OFF -DENGINE_BUILD_TESTS=ON
cmake --build build -j4 --target audiocpp_cli sense_asr_word_timestamps_test \
  ctc_host_decoder_test model_spec_system_test
ctest --test-dir build -R "sense_asr_word_timestamps_test|ctc_host_decoder_test|model_spec_system_test"
# 100% tests passed, 0 tests failed out of 3

Run:

audiocpp_cli --task asr --family sense_asr --model sensevoice-small-q8-audiocpp-v1.gguf \
  --backend cpu --audio zh.wav --request-option audio_chunk_mode=none --words-out words.json

Output

zh:  开放时间早上9点至下午5点。
     开@0.72 放@0.96 时@1.26 间@1.50 早@1.92 上@2.10 [email protected] 点@2.82 至@3.30 下@3.90 午@4.26 [email protected] 点。@4.80
yue: 呢@0.78 几@1.08 个@1.32 字@1.44 都@1.74 表@1.98 达@2.16 唔@2.40 到,@2.52 我@2.76 想@2.94 讲@3.12 嘅@3.36 意@3.54 思。@3.72
en (sample_16k.wav):
     [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
     I'[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] ...

Checks

Check Result
zh with 3.0 s of leading silence Every word moves by 3.00 s (one by 2.94 s, a single frame).
Start time vs. energy onset (−35 dB from peak) zh +0.02 s, yue −0.17 s, ja −0.03 s
End time vs. energy offset Up to 0.5 s early. Ends are the last emission frame (documented).
zh, en, yue and ja joined with 0.3 s gaps (26.1 s), run with audio_chunk_mode none, vad, fixed (7 s) and --mode streaming (7 s windows) Each word's midpoint falls inside its own clip in every mode; none falls in a gap or the wrong clip. Times increase monotonically.
Streaming through the C API (audiocpp_stream_push → event words; 167 s clip, 2 s and 5 s windows) Words summed over all events equal the final result minus the last window, which audiocpp_stream_finish returns: 476 of 479 and 480 of 487. At most 9 and 20 words per event. No duplicates.
Overhead on a 167 s clip (audio_chunk_mode=vad, 3 runs each) 17.42 s with timestamps off, 16.69 s on; peak RSS 414 MB either way; identical transcripts

Downstream use: speaker-attributed transcripts with nemotron_3_diar (Q8)

Each word was assigned to the diarization turn that contains its start time.

Audio Result
Synthetic zh→yue→ja→zh→yue (3 speakers return, 0.4 s gaps) All 5 utterances get the right speaker; returning speakers keep their IDs.
four_speaker_short.wav Every word lands on the speaker given in four_speaker_short.txt.
vibevoice_two_speaker_longform.wav (167 s) 48 turns; transcript alternates cleanly between the two speakers.

Known limitations

  • Times come from CTC emission frames at 60 ms resolution, not from a forced alignment. Starts track onsets; ends mark the last emission frame.
  • confidence is 0, as in GigaAM.
  • Concatenating the word texts does not reproduce the transcript exactly. Callers add spaces between Latin words, and leading punctuation before the first word is dropped.

🤖 Generated with Claude Code

jiabochao and others added 2 commits November 8, 2026 16:03
SenseVoice decodes with greedy CTC, and since 0xShug0#1214 its decoding goes
through CtcHostDecoder, which already reports each token's frame span.
Those spans were dropped after text decoding. Keep them and, when the
request sets return_timestamps (or the CLI gets --words-out), return them
as word timestamps.

- Encoder output frames start with the query-token prefix (language,
  event, emotion, ITN); drop it, then convert at one LFR frame
  (frame_shift_ms * lfr_n = 60 ms) to the input's sample rate.
- Chinese, Japanese kana and Thai give one entry per character; other
  scripts join SentencePiece pieces into words at the boundary marker.
  Punctuation joins the preceding word without extending its time;
  language/event/emotion tags are skipped.
- Chunked offline runs and streaming windows shift times to the whole
  input.
- Times are token emission frames, as for GigaAM and Granite, not a
  forced alignment: starts track onsets, ends fall at the last emission.

The default path is unchanged: without return_timestamps no words are
built and transcripts are identical. The spec gains the word_timestamps
capability and the return_timestamps request option, so the C API's
audiocpp_model_supports_timestamps() now reports 1 for sense_asr; no C
ABI declarations change.

Grouping and frame conversion live in sense_asr/word_timestamps.h,
covered by the new sense_asr_word_timestamps_test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Streaming windows are final once decoded, but each window republished
the whole word list through publish_transcript(), so every stream event
carried all words so far. On a 167 s clip with 2 s windows the events
held 19,857 words for a 479-word transcript, growing quadratically with
stream length, and a caller appending event words got duplicates.

Use append_transcript() with only the window's text delta and words, as
other finalized-segment streams do. The transcript is joined exactly as
before, and the session no longer keeps its own copy of the words. Also
make the word-boundary marker a constexpr string_view.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jiabochao

Copy link
Copy Markdown
Contributor Author

Metal validation for the PR head (dc53905).

Setup: Apple M2 Pro, macOS, AppleClang 21. Built with the repo's scripts/build_metal.sh (Metal on, embedded library, OpenMP off):

scripts/build_metal.sh --build-dir build-metal --build-type Release \
  --model-set custom --models "sense_asr,nemotron_3_diar" --with-tests \
  --target audiocpp_cli --target sense_asr_word_timestamps_test \
  --target ctc_host_decoder_test --target model_spec_system_test

Unit tests: sense_asr_word_timestamps_test, ctc_host_decoder_test and model_spec_system_test all pass.

CPU vs. Metal: the same requests ran with --backend cpu and --backend metal, using the same Q8 GGUF (sha256 4dedf169…) and --words-out.

Case Words (CPU / Metal) Same text Same words Max start diff CPU s Metal s
zh, audio_chunk_mode=none 13 / 13 ✓ ✓ 0.00 s 0.58 0.61
yue, none 15 / 15 ✓ ✓ 0.06 s 0.60 0.60
ja, none 33 / 33 ✓ ✓ 0.06 s 0.60 0.61
en, none 15 / 15 ✓ ✓ 0.00 s 0.60 0.60
sample_16k.wav, none 21 / 21 ✓ ✓ 0.06 s 1.10 0.60
vibevoice_two_speaker_longform.wav (24 kHz, 167 s), fixed 20 s 497 / 497 — — 0.06 s 7.17 1.61
zh, --mode streaming, 2 s windows 13 / 13 ✓ ✓ 0.00 s 0.59 0.60

The one difference in the 167 s clip is capitalization: CPU gives Where and Metal gives where, both timed at 12.78 s. This is backend numerics in the Q8 encoder. Times agree within one 60 ms encoder frame throughout, including the 24 kHz input and the chunked and streaming paths.

@0xShug0
0xShug0 merged commit f2520da into 0xShug0:main Nov 8, 2026
7 checks passed
@0xShug0

0xShug0 commented Nov 8, 2026

Copy link
Copy Markdown
Owner

@jiabochao 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