fix: Slow output stage after "saving output to ..." when using --dialize (-di) - #3976
Open
mitslabo wants to merge 2 commits into
Open
fix: Slow output stage after "saving output to ..." when using --dialize (-di)#3976mitslabo wants to merge 2 commits into
mitslabo wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a performance bottleneck in whisper-cli when running with diarization enabled on long audio: the output-writing stage was slow due to repeated copying of the full stereo PCM buffer.
Changes:
- Switched
estimate_diarization_speaker()to take the stereo PCM buffer asconst std::vector<std::vector<float>> &instead of by value. - Switched all
output_*functions that receivepcmf32sto also take it byconst &, eliminating redundant full-buffer copies during output generation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Slow output stage after "saving output to ..." when using
--diarize(-di)Description
When running
whisper-cliwith--diarize(-di) on long audio files with manysegments, the transcription (encoder/decoder) itself completes quickly, but the
final output-writing stage — the part that prints
saving output to '<file>'—takes a noticeably long time, sometimes much longer than the transcription itself.
Root cause
In
examples/cli/cli.cpp, all of the output-writing functions(
output_txt,output_vtt,output_srt,output_csv,output_json,output_wts,output_lrc,output_score) take the stereo PCM bufferby value:
pcmf32sholds the entire decoded stereo audio (twofloatchannels spanningthe whole input duration), so every call to one of these functions copies the
complete audio buffer once.
More importantly,
estimate_diarization_speaker()also takespcmf32sby value:and it is called once per segment whenever
params.diarizeis true, frominside the per-segment loop of
output_txt,output_vtt,output_srt,output_csv,output_json, andoutput_wts(and from the livewhisper_print_segment_callbackduring transcription).This means the entire stereo audio buffer gets copied
O(n_segments)times peroutput format. For a long recording with diarization enabled and hundreds of
segments, this results in copying gigabytes of audio data, which is exactly what
shows up as a slow, CPU/memory-bound stall right after the
saving output to '<file>'message and before the program actually exits.The slowdown scales with:
pcmf32s)estimate_diarization_speaker)-otxt -ovtt -osrt -ocsv ...eachre-copies
pcmf32sagain)Steps to reproduce
Observe that after
saving output to ...is printed for each format, there is amulti-second (or longer, depending on length/segment count) pause before the
program returns, disproportionate to the file size/model used.
Proposed fix
Change
pcmf32sparameters from by-value toconst std::vector<std::vector<float>> &in
estimate_diarization_speaker()and in everyoutput_*function(
output_txt,output_vtt,output_srt,output_csv,output_json,output_wts,output_lrc,output_score). These parameters are only ever read,never mutated, so switching to a const reference is behavior-preserving and
removes all of the redundant copying.
Patch attached:
fix-diarization-pcmf32s-copy.patchEnvironment
examples/cli/cli.cpp)-di/--diarizewith one or more-o*output flags