Repository navigation
Conversation
…nce per speaker pyannote's SpeakerDiarization.get_embeddings runs the WeSpeaker ResNet once for every (chunk, local speaker) pair, although the speaker mask only enters at the final statistics-pooling layer, which already accepts (batch, speakers, frames) weights. Run the frame-wise network once per chunk and pool all local speakers in the same call. The override is installed only when the pipeline's embedding model accepts stacked masks (probed with a single dummy forward); other backends keep pyannote's per-pair loop. Embeddings match the stock implementation up to float noise, and the diarization output is unchanged.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Summary
Speaker diarization is the slowest stage of the pipeline, and almost all of that time goes into speaker-embedding extraction. pyannote's
SpeakerDiarization.get_embeddingsruns the WeSpeaker ResNet once for every (chunk, local speaker) pair: each 10 s chunk passes through the full network 3 times, although the speaker mask only enters at the final statistics-pooling layer. That layer already accepts(batch, speakers, frames)weights.This PR overrides
get_embeddingson the loaded pipeline so the frame-wise network runs once per chunk, with all local speakers pooled in the same call. The model, weights and clustering are unchanged, and the output is identical.How
_supports_stacked_masks(embedding)makes one dummy forward pass with stacked masks and checks the output shape. Only pyannote-native embedding models that pass this get the override. SpeechBrain, NeMo and ONNX backends apply masks before the frame-wise network and keep pyannote's per-pair loop._get_embeddings_per_chunkreproduces pyannote's mask selection exactly, includingexclude_overlap(on by default in community-1) and NaN frames from partial stitching. Training mode falls back to the stock implementation.DiarizationPipeline.__init__binds the override to the pipeline instance only. Nothing in pyannote is patched globally.total/completedper batch, and the artifact is reshaped to(batch * speakers, dimension).embedding_batch_sizenow counts chunks rather than (chunk, speaker) pairs. The frame-wise activations per batch are the same size as before.Results
End to end through
DiarizationPipelinewithpyannote/speaker-diarization-community-1, on real meeting recordings (Apple Silicon, after warm-up):Speaker counts match on every file, and the maximum absolute embedding difference is about 3e-6 (float noise from the different batch shape).
Tests
New tests in
tests/test_diarize.pyuse a randomly initialisedWeSpeakerResNet34, so they need no model download or HF token:SpeakerDiarization.get_embeddingsfor batch sizes 1, 4 and 32, with and withoutexclude_overlap, on inputs that include a fully silent speaker slot and NaN framestotal/completedsequence and artifact shapesThe full suite passes on pyannote 4.0.4 / Python 3.10 (296 tests), and
tests/test_diarize.pypasses on pyannote 4.1.0 / Python 3.12. ruff and bandit are clean at the versions pinned in.pre-commit-config.yaml.Prior work
The same optimisation has been proposed upstream in pyannote/pyannote-audio#2048 (see also pyannote/pyannote-audio#2050 and pyannote/pyannote-audio#1996), where it was checked on VoxConverse dev with identical DER on all 216 files. None of it has been merged as of pyannote-audio 4.1.0. pyannote 4.1.0 also requires Python 3.11+, so whispermlx users on 3.10 would never receive an upstream fix. This override can be removed once a pyannote release that includes it becomes the minimum supported version.
Summary by CodeRabbit