Skip to content

Make the Pyannote segmentation window shift configurable - #3769

Merged
csukuangfj merged 2 commits into
k2-fsa:masterfrom
JulianPscheid:diarization-window-shift
Jul 23, 2026
Merged

csukuangfj merged 2 commits into
k2-fsa:masterfrom
JulianPscheid:diarization-window-shift

Conversation

@JulianPscheid

@JulianPscheid JulianPscheid commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

The pyannote segmentation sliding-window shift is currently hardcoded to 10% of the window size (a 10 s window sliding every 1 s, i.e. 90% overlap):

meta_data_.window_shift = static_cast<int32_t>(0.1 * meta_data_.window_size);

The shift directly scales the total work of the whole diarization pipeline: the number of chunks fed to BOTH the segmentation model and the embedding extractor is roughly proportional to 1/ratio. On CPU-only devices where offline diarization runs at RTF 0.25+, this is the single biggest speed knob, and it is not exposed anywhere today.

This PR adds window_shift_ratio to OfflineSpeakerSegmentationPyannoteModelConfig (CLI: --segmentation.pyannote-window-shift-ratio), default 0.1:

  • The default provably produces the identical int32_t shift as before (for the standard window_size=160000, both the old 0.1 * 160000 and the new float-stored ratio promoted to double truncate to 16000), so existing users see no change.
  • Validate() rejects values outside (0, 1] (NaN-safe); the computed shift is additionally clamped to [1, window_size] with a warning for library callers that bypass Validate(), so a degenerate ratio can never produce a zero shift and an infinite chunk loop.
  • No C API / binding changes in this PR to keep it reviewable; existing bindings keep the native default. Happy to add the field to the C API and bindings in a follow-up if you want it exposed there.

Measured effect (644 s two-speaker English WAV, sherpa-onnx-pyannote-segmentation-3-0 + CAM++ zh_en advanced embedding, --clustering.cluster-threshold=0.90, 4 threads each, macOS arm64, this repo's CLI):

ratio wall time speedup segments speakers frame-level label agreement vs 0.10
0.10 (default) 40 s 1.00x 60 2 —
0.15 27 s 1.48x 60 2 99.89%
0.20 20 s 2.00x 60 2 99.93%

(Agreement computed on a 10 ms grid over the full file including silence, best label mapping; segment boundaries move by at most a few tens of milliseconds on this material. Quality on harder audio will degrade sooner as overlap drops — which is exactly why the default stays 0.1 and this is opt-in.)

Summary by CodeRabbit

  • New Features
    • Added configurable window-shift ratio for Pyannote-based offline speaker segmentation (default: 0.1).
  • Bug Fixes
    • Validation now rejects window-shift ratios outside the supported range.
    • Window-shift calculations now clamp invalid values (e.g., NaN/out of bounds) to safe limits.
  • Documentation
    • Configuration summary output now includes the selected window-shift ratio.

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jul 19, 2026
@coderabbitai

coderabbitai Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a5a977db-1494-4e0a-9404-4cf2051e6bbf

📥 Commits

Reviewing files that changed from the base of the PR and between ea604d4 and f3a6816.

📒 Files selected for processing (1)
  • sherpa-onnx/csrc/offline-speaker-segmentation-pyannote-model.cc

📝 Walkthrough

Walkthrough

The Pyannote speaker segmentation configuration now supports a validated window_shift_ratio, reports it in configuration output, and uses it to calculate and clamp the model’s runtime window shift.

Changes

Pyannote window shift configuration

Layer / File(s) Summary
Configuration contract and validation
sherpa-onnx/csrc/offline-speaker-segmentation-pyannote-model-config.h, sherpa-onnx/csrc/offline-speaker-segmentation-pyannote-model-config.cc
Adds window_shift_ratio with a 0.1f default, validates values in (0, 1], and includes the value in ToString() output.
Runtime window shift calculation
sherpa-onnx/csrc/offline-speaker-segmentation-pyannote-model.cc
Calculates window_shift from the configured ratio, handles invalid numeric results, and clamps it between one sample and the model window size.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: csukuangfj

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: making Pyannote segmentation window shift configurable.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a configurable window shift ratio (pyannote-window-shift-ratio) for the Pyannote offline speaker segmentation model, replacing the previously hardcoded value of 0.1. It adds validation to ensure the ratio is within the range (0, 1] and clamps the computed window shift to valid bounds. The review feedback points out a potential off-by-one truncation issue when casting the computed floating-point window shift to an integer, suggesting rounding to the nearest integer instead.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

window_shift, meta_data_.window_size, meta_data_.window_size);
meta_data_.window_shift = meta_data_.window_size;
} else {
meta_data_.window_shift = static_cast<int32_t>(window_shift);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using static_cast<int32_t> directly on window_shift truncates the fractional part. Due to floating-point precision limitations, certain ratios can result in a computed window_shift that is slightly less than the expected integer value (for example, 0.13f * 160000 results in 20799.999237..., which truncates to 20799 instead of 20800).

To prevent these off-by-one errors, round the computed value to the nearest integer by adding 0.5 before casting.

      meta_data_.window_shift = static_cast<int32_t>(window_shift + 0.5);

@csukuangfj csukuangfj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left a minor comment.

const double window_shift =
static_cast<double>(config_.pyannote.window_shift_ratio) *
meta_data_.window_size;
if (!(window_shift >= 1)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use

if (window_shift < 1)

It is more readable.

@JulianPscheid JulianPscheid Jul 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f3a6816, thanks. That reads better.

I kept the NaN case explicit rather than leaving it to the negation:

if (std::isnan(window_shift) || window_shift < 1) {

Validate() already rejects a NaN ratio, so this only bites library callers
that build the config directly and skip validation. Without the check a NaN
falls past both branches into static_cast<int32_t>, which is undefined. If
you would rather keep the line simple, I will drop it.

Replace the negated comparison with the more readable form suggested in
review, keeping the NaN case explicit so a ratio that bypasses Validate()
still clamps instead of reaching the cast.

@csukuangfj csukuangfj left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

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

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants