-
Notifications
You must be signed in to change notification settings - Fork 264
Fix Sortformer tutorial issues and add InferenceSortformerStage benchmark #1764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
81e54cb
fd87576
5c3be57
246067a
c5564bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Audio Sortformer diarization benchmarking script. | ||
|
|
||
| This script runs Streaming Sortformer diarization benchmarks with | ||
| comprehensive metrics collection including real-time factor (RTF), | ||
| per-file segment counts, and throughput. | ||
| """ | ||
|
|
||
| import argparse | ||
| import time | ||
| import traceback | ||
| from typing import Any | ||
|
|
||
| from loguru import logger | ||
| from utils import setup_executor, write_benchmark_results | ||
|
|
||
| from nemo_curator.pipeline import Pipeline | ||
| from nemo_curator.stages.audio.alm.alm_manifest_reader import ALMManifestReader | ||
| from nemo_curator.stages.audio.inference.sortformer import InferenceSortformerStage | ||
|
|
||
|
|
||
| def _collect_diarization_metrics(tasks: list, elapsed_s: float) -> dict[str, Any]: | ||
| """Extract diarization-specific metrics from output tasks.""" | ||
| num_files = len(tasks) if tasks else 0 | ||
| total_audio_duration_s = 0.0 | ||
| total_segments = 0 | ||
|
|
||
| for task in tasks or []: | ||
| data = task.data if hasattr(task, "data") else {} | ||
| total_audio_duration_s += float(data.get("duration", 0)) | ||
| segments = data.get("diar_segments", []) | ||
| total_segments += len(segments) | ||
|
|
||
| throughput = num_files / elapsed_s if elapsed_s > 0 else 0.0 | ||
| rtf = elapsed_s / total_audio_duration_s if total_audio_duration_s > 0 else 0.0 | ||
|
|
||
| return { | ||
| "is_success": num_files > 0, | ||
| "num_files_processed": num_files, | ||
| "exec_time_s": round(elapsed_s, 2), | ||
| "total_audio_duration_s": round(total_audio_duration_s, 2), | ||
| "total_segments_detected": total_segments, | ||
| "real_time_factor": round(rtf, 4), | ||
| "throughput_files_per_sec": round(throughput, 4), | ||
| } | ||
|
melllinia marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def run_audio_sortformer_benchmark( | ||
| manifest_path: str, | ||
| model_name: str, | ||
| rttm_out_dir: str | None = None, | ||
| executor: str = "xenna", | ||
| **kwargs, # noqa: ARG001 | ||
| ) -> dict[str, Any]: | ||
| """Run the audio Sortformer diarization benchmark and collect metrics.""" | ||
| logger.info("Starting audio Sortformer diarization benchmark") | ||
| logger.info(f"Executor: {executor}") | ||
| logger.info(f"Model: {model_name}") | ||
| logger.info(f"Manifest: {manifest_path}") | ||
|
|
||
| executor_obj = setup_executor(executor) | ||
| pipeline = Pipeline( | ||
| name="audio_sortformer_diarization", | ||
| description="Streaming Sortformer speaker diarization inference.", | ||
| ) | ||
|
|
||
| pipeline.add_stage(ALMManifestReader(manifest_path=manifest_path)) | ||
| pipeline.add_stage( | ||
| InferenceSortformerStage( | ||
| model_name=model_name, | ||
| rttm_out_dir=rttm_out_dir, | ||
| ), | ||
| ) | ||
|
|
||
| t0 = time.perf_counter() | ||
| results = pipeline.run(executor_obj) | ||
| elapsed_s = time.perf_counter() - t0 | ||
|
|
||
| metrics = _collect_diarization_metrics(results, elapsed_s) | ||
|
|
||
| logger.success( | ||
| f"Benchmark completed: {metrics['num_files_processed']} files in {elapsed_s:.1f}s " | ||
| f"(RTF={metrics['real_time_factor']:.3f}, {metrics['throughput_files_per_sec']:.2f} files/sec)" | ||
| ) | ||
|
|
||
| return { | ||
|
melllinia marked this conversation as resolved.
|
||
| "params": { | ||
| "executor": executor, | ||
| "manifest_path": manifest_path, | ||
| "model_name": model_name, | ||
| "rttm_out_dir": rttm_out_dir, | ||
| }, | ||
| "metrics": metrics, | ||
| "tasks": results, | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description="Audio Sortformer diarization benchmark for nightly benchmarking") | ||
| parser.add_argument("--benchmark-results-path", required=True, help="Path to benchmark results") | ||
| parser.add_argument("--manifest-path", required=True, help="Path to input JSONL manifest") | ||
| parser.add_argument( | ||
| "--model-name", | ||
| default="nvidia/diar_streaming_sortformer_4spk-v2.1", | ||
| help="HF Sortformer model id", | ||
| ) | ||
| parser.add_argument("--executor", default="xenna", choices=["xenna", "ray_data"], help="Executor to use") | ||
| parser.add_argument("--rttm-out-dir", default=None, help="Optional directory to write RTTM output files") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| logger.info("=== Audio Sortformer Diarization Benchmark Starting ===") | ||
| logger.info(f"Arguments: {vars(args)}") | ||
|
|
||
| success_code = 1 | ||
| result_dict: dict[str, Any] = { | ||
| "params": vars(args), | ||
| "metrics": { | ||
| "is_success": False, | ||
| }, | ||
| "tasks": [], | ||
| } | ||
| try: | ||
| result_dict.update(run_audio_sortformer_benchmark(**vars(args))) | ||
| success_code = 0 if result_dict["metrics"]["is_success"] else 1 | ||
| except Exception as e: | ||
| error_traceback = traceback.format_exc() | ||
| logger.error(f"Benchmark failed: {e}") | ||
| logger.debug(f"Full traceback:\n{error_traceback}") | ||
| finally: | ||
| write_benchmark_results(result_dict, args.benchmark_results_path) | ||
| return success_code | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,17 +87,18 @@ class InferenceSortformerStage(ProcessingStage[AudioTask, AudioTask]): | |
|
|
||
| Uses the NeMo SortformerEncLabelModel for end-to-end neural speaker | ||
| diarization with streaming support. See: | ||
| https://huggingface.co/nvidia/diar_streaming_sortformer_4spk-v2 | ||
| https://huggingface.co/nvidia/diar_streaming_sortformer_4spk-v2.1 | ||
|
|
||
| Args: | ||
| model_name: Hugging Face model id. Defaults to "nvidia/diar_streaming_sortformer_4spk-v2". | ||
| model_name: Hugging Face model id. Defaults to "nvidia/diar_streaming_sortformer_4spk-v2.1". | ||
| model_path: Local path to a .nemo checkpoint file; if set, takes precedence over model_name. | ||
| cache_dir: Directory for caching downloaded model weights. Defaults to HF hub default. | ||
| diar_model: Pre-loaded SortformerEncLabelModel; if provided, setup() is a no-op. | ||
| filepath_key: Key in data for path to audio file. Defaults to "audio_filepath". | ||
| diar_segments_key: Key in output data for diarization segments list. Defaults to "diar_segments". | ||
| rttm_out_dir: Optional directory to write RTTM files. Defaults to None. | ||
| chunk_len: Streaming chunk size in 80 ms frames. Defaults to 340 (~30.4 s latency). | ||
| chunk_left_context: Left context frames. Defaults to 1. | ||
| chunk_right_context: Right context frames. Defaults to 40. | ||
| fifo_len: FIFO queue size in frames. Defaults to 40. | ||
| spkcache_update_period: Speaker cache update period in frames. Defaults to 300. | ||
|
|
@@ -106,14 +107,15 @@ class InferenceSortformerStage(ProcessingStage[AudioTask, AudioTask]): | |
| name: Stage name. Defaults to "Sortformer_inference". | ||
| """ | ||
|
|
||
| model_name: str = "nvidia/diar_streaming_sortformer_4spk-v2" | ||
| model_name: str = "nvidia/diar_streaming_sortformer_4spk-v2.1" | ||
| model_path: str | None = None | ||
| cache_dir: str | None = None | ||
| diar_model: Any | None = None | ||
| filepath_key: str = "audio_filepath" | ||
| diar_segments_key: str = "diar_segments" | ||
| rttm_out_dir: str | None = None | ||
| chunk_len: int = 340 | ||
| chunk_left_context: int = 1 | ||
| chunk_right_context: int = 40 | ||
| fifo_len: int = 40 | ||
| spkcache_update_period: int = 300 | ||
|
|
@@ -126,42 +128,69 @@ class InferenceSortformerStage(ProcessingStage[AudioTask, AudioTask]): | |
| def setup_on_node( | ||
| self, _node_info: NodeInfo | None = None, _worker_metadata: WorkerMetadata | None = None | ||
| ) -> None: | ||
| """Pre-download model weights on the node so actors load from cache.""" | ||
| """Pre-download model weights on the node so workers load from cache.""" | ||
| if self.model_path is not None: | ||
| return | ||
| try: | ||
| repo_dir = snapshot_download(repo_id=self.model_name, cache_dir=self.cache_dir) | ||
| nemo_files = [f for f in os.listdir(repo_dir) if f.endswith(".nemo")] | ||
| if nemo_files: | ||
| self.model_path = os.path.join(repo_dir, nemo_files[0]) | ||
| else: | ||
| logger.warning(f"No .nemo file found in {repo_dir}; setup() will fail") | ||
| except Exception: # noqa: BLE001 | ||
| logger.info(f"Could not pre-cache {self.model_name}; actors will download on first use") | ||
| snapshot_download(repo_id=self.model_name, cache_dir=self.cache_dir) | ||
|
|
||
| def _resolve_model_path(self) -> str: | ||
| """Resolve the path to the .nemo checkpoint from the HF cache.""" | ||
| if self.model_path is not None: | ||
| return self.model_path | ||
| repo_dir = snapshot_download(repo_id=self.model_name, cache_dir=self.cache_dir) | ||
| nemo_files = sorted(f for f in os.listdir(repo_dir) if f.endswith(".nemo")) | ||
| if not nemo_files: | ||
| msg = f"No .nemo file found in {repo_dir} for model {self.model_name}" | ||
| raise FileNotFoundError(msg) | ||
| return os.path.join(repo_dir, nemo_files[0]) | ||
|
|
||
| def setup(self, _worker_metadata: WorkerMetadata | None = None) -> None: | ||
| """Load Sortformer model from Hugging Face or a local .nemo file.""" | ||
| if self.diar_model is not None: | ||
| self.diar_model.eval() | ||
| self._configure_streaming() | ||
| self._extend_pos_enc_for_long_audio() | ||
| return | ||
|
|
||
| resolved_path = self._resolve_model_path() | ||
| self.diar_model = SortformerEncLabelModel.restore_from( | ||
| restore_path=self.model_path, | ||
| restore_path=resolved_path, | ||
| map_location="cuda", | ||
| strict=False, | ||
| ) | ||
|
|
||
| self.diar_model.eval() | ||
| self._configure_streaming() | ||
| self._extend_pos_enc_for_long_audio() | ||
|
|
||
| def _extend_pos_enc_for_long_audio(self, max_len: int = 30000) -> None: | ||
| """Extend RelPositionalEncoding buffer to handle long audio files. | ||
|
|
||
| NeMo's streaming Sortformer initialises pos_enc sized for one chunk (~35 | ||
| conformer frames). Files longer than a few seconds overflow it at inference | ||
| time. extend_pe() is a NeMo method that resizes the buffer safely — it just | ||
| isn't called automatically. max_len=30000 covers ~1000 s at any subsampling. | ||
| """ | ||
| pos_enc = getattr(getattr(self.diar_model, "encoder", None), "pos_enc", None) | ||
| if pos_enc is None or not hasattr(pos_enc, "extend_pe"): | ||
| logger.warning("pos_enc not found or no extend_pe method — skipping extension") | ||
| return | ||
| params = next(self.diar_model.parameters()) | ||
| try: | ||
| pos_enc.extend_pe(max_len, params.device, params.dtype) | ||
| logger.info(f"Extended encoder pos_enc to max_len={max_len} for long-form audio") | ||
| except Exception as e: # noqa: BLE001 | ||
| logger.warning(f"Could not extend pos_enc: {e}") | ||
|
|
||
| def _configure_streaming(self) -> None: | ||
| """Apply streaming configuration to the loaded model.""" | ||
| sm = self.diar_model.sortformer_modules | ||
| sm.chunk_len = self.chunk_len | ||
| sm.chunk_right_context = self.chunk_right_context | ||
| sm.fifo_len = self.fifo_len | ||
| sm.spkcache_update_period = self.spkcache_update_period | ||
| sm.chunk_left_context = self.chunk_left_context | ||
| if hasattr(sm, "spkcache_update_period"): | ||
| sm.spkcache_update_period = self.spkcache_update_period | ||
| sm.spkcache_len = self.spkcache_len | ||
|
Comment on lines
-164
to
194
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following up on some greptile comments, why is there a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Older NeMo versions don't have |
||
|
|
||
| def inputs(self) -> tuple[list[str], list[str]]: | ||
|
|
@@ -189,9 +218,7 @@ def process(self, task: AudioTask) -> AudioTask: | |
|
|
||
| file_path = task.data[self.filepath_key] | ||
| sess_name = task.data.get("session_name") | ||
| resolved_sess_name = ( | ||
| sess_name if sess_name is not None else os.path.splitext(os.path.basename(file_path))[0] | ||
| ) | ||
| resolved_sess_name = sess_name if sess_name is not None else os.path.splitext(os.path.basename(file_path))[0] | ||
|
|
||
| all_segments = self.diarize([file_path]) | ||
| segments = all_segments[0] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.