|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Audio tagging pipeline benchmarking script. |
| 16 | +
|
| 17 | +Runs the core audio tagging pipeline end-to-end: |
| 18 | + ManifestReader -> Resample -> Diarize -> Split -> ASR Align -> |
| 19 | + Join -> Merge -> Write |
| 20 | +
|
| 21 | +Exercises the core stages of the tagging pipeline for regression tracking. |
| 22 | +""" |
| 23 | + |
| 24 | +import argparse |
| 25 | +import time |
| 26 | +from pathlib import Path |
| 27 | +from typing import Any |
| 28 | + |
| 29 | +from loguru import logger |
| 30 | +from utils import RepeatEntriesStage, setup_executor, write_benchmark_results |
| 31 | + |
| 32 | +from nemo_curator.pipeline import Pipeline |
| 33 | +from nemo_curator.stages.audio.common import ManifestReader, ManifestWriterStage |
| 34 | +from nemo_curator.stages.audio.inference.speaker_diarization.pyannote import PyAnnoteDiarizationStage |
| 35 | +from nemo_curator.stages.audio.tagging.inference.nemo_asr_align import NeMoASRAlignerStage |
| 36 | +from nemo_curator.stages.audio.tagging.merge_alignment_diarization import MergeAlignmentDiarizationStage |
| 37 | +from nemo_curator.stages.audio.tagging.resample_audio import ResampleAudioStage |
| 38 | +from nemo_curator.stages.audio.tagging.split import JoinSplitAudioMetadataStage, SplitLongAudioStage |
| 39 | +from nemo_curator.stages.resources import Resources |
| 40 | + |
| 41 | + |
| 42 | +def run_audio_tagging_benchmark( # noqa: PLR0913 |
| 43 | + benchmark_results_path: str, |
| 44 | + input_manifest: str, |
| 45 | + repeat_factor: int, |
| 46 | + hf_token: str, |
| 47 | + max_segment_length: float, |
| 48 | + asr_batch_size: int, |
| 49 | + executor: str, |
| 50 | + cpus: int, |
| 51 | + **kwargs, # noqa: ARG001 |
| 52 | +) -> dict[str, Any]: |
| 53 | + """Run the full audio tagging pipeline benchmark.""" |
| 54 | + benchmark_results_path = Path(benchmark_results_path) |
| 55 | + results_dir = benchmark_results_path / "results" |
| 56 | + |
| 57 | + resampled_audio_dir = str(benchmark_results_path / "audio_resampled") |
| 58 | + final_manifest = str(results_dir / "tagging_output.jsonl") |
| 59 | + |
| 60 | + logger.info("Starting audio tagging pipeline benchmark") |
| 61 | + logger.info(f"CPUs: {cpus}") |
| 62 | + logger.info(f"Max segment length: {max_segment_length}s") |
| 63 | + |
| 64 | + exc = setup_executor(executor, config={"execution_mode": "streaming"}) |
| 65 | + run_start_time = time.perf_counter() |
| 66 | + |
| 67 | + pipeline = Pipeline( |
| 68 | + name="audio_tagging_benchmark", |
| 69 | + description="Audio tagging core benchmark: FLEURS -> core tagging pipeline", |
| 70 | + ) |
| 71 | + |
| 72 | + pipeline.add_stage(ManifestReader(manifest_path=input_manifest)) |
| 73 | + if repeat_factor > 1: |
| 74 | + pipeline.add_stage(RepeatEntriesStage(repeat_factor=repeat_factor)) |
| 75 | + logger.info(f"Repeat factor: {repeat_factor}x (entries multiplied after reading from manifest)") |
| 76 | + |
| 77 | + # Resample audio to 16 kHz mono WAV |
| 78 | + pipeline.add_stage( |
| 79 | + ResampleAudioStage( |
| 80 | + resampled_audio_dir=resampled_audio_dir, |
| 81 | + input_format="wav", |
| 82 | + target_sample_rate=16000, |
| 83 | + target_format="wav", |
| 84 | + target_nchannels=1, |
| 85 | + ).with_(resources=Resources(cpus=cpus)) |
| 86 | + ) |
| 87 | + |
| 88 | + # Speaker diarization and overlap detection (PyAnnote) |
| 89 | + pipeline.add_stage( |
| 90 | + PyAnnoteDiarizationStage( |
| 91 | + name="PyAnnoteDiarization", |
| 92 | + hf_token=hf_token, |
| 93 | + max_length=max_segment_length, |
| 94 | + ).with_(resources=Resources(cpus=cpus, gpus=0.5)) |
| 95 | + ) |
| 96 | + |
| 97 | + # Split long audio segments |
| 98 | + pipeline.add_stage( |
| 99 | + SplitLongAudioStage( |
| 100 | + name="SplitLongAudio", |
| 101 | + suggested_max_len=max_segment_length, |
| 102 | + min_len=1.0, |
| 103 | + ).with_(resources=Resources(cpus=cpus)) |
| 104 | + ) |
| 105 | + |
| 106 | + # ASR forced alignment (NeMo FastConformer) |
| 107 | + pipeline.add_stage( |
| 108 | + NeMoASRAlignerStage( |
| 109 | + name="ASRAlignment", |
| 110 | + is_fastconformer=True, |
| 111 | + decoder_type="rnnt", |
| 112 | + batch_size=asr_batch_size, |
| 113 | + ).with_(resources=Resources(cpus=cpus, gpus=0.45)) |
| 114 | + ) |
| 115 | + |
| 116 | + # Rejoin split audio metadata |
| 117 | + pipeline.add_stage(JoinSplitAudioMetadataStage(name="JoinSplitMetadata").with_(resources=Resources(cpus=cpus))) |
| 118 | + |
| 119 | + # Merge alignment with diarization |
| 120 | + pipeline.add_stage( |
| 121 | + MergeAlignmentDiarizationStage( |
| 122 | + name="MergeAlignmentDiar", |
| 123 | + text_key="text", |
| 124 | + words_key="words", |
| 125 | + ).with_(resources=Resources(cpus=cpus)) |
| 126 | + ) |
| 127 | + |
| 128 | + # Write output manifest |
| 129 | + pipeline.add_stage(ManifestWriterStage(output_path=final_manifest).with_(resources=Resources(cpus=cpus))) |
| 130 | + |
| 131 | + results = pipeline.run(exc) |
| 132 | + |
| 133 | + run_time_taken = time.perf_counter() - run_start_time |
| 134 | + |
| 135 | + total_duration = sum(task.data["duration"] for task in results) / 3600 |
| 136 | + |
| 137 | + logger.success("Audio tagging benchmark completed successfully!!") |
| 138 | + logger.success(f"Processed {len(results)} tasks") |
| 139 | + logger.success(f"Total audio duration processed: {total_duration:.2f} hours") |
| 140 | + logger.success(f"Throughput: {len(results) / run_time_taken:.2f} tasks per second") |
| 141 | + logger.success(f"Total time taken: {run_time_taken / 60:.2f} minutes") |
| 142 | + |
| 143 | + return { |
| 144 | + "metrics": { |
| 145 | + "is_success": True, |
| 146 | + "time_taken_s": run_time_taken, |
| 147 | + "num_tasks_processed": len(results), |
| 148 | + "throughput_tasks_per_sec": len(results) / run_time_taken if run_time_taken > 0 else 0, |
| 149 | + "total_audio_duration_hours": total_duration, |
| 150 | + }, |
| 151 | + "tasks": results, |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | +def main() -> int: |
| 156 | + parser = argparse.ArgumentParser( |
| 157 | + description="Audio tagging pipeline e2e benchmark (FLEURS -> full tagging pipeline)" |
| 158 | + ) |
| 159 | + parser.add_argument("--input-manifest", required=True, help="Path to input manifest") |
| 160 | + parser.add_argument("--repeat-factor", type=int, default=1, help="Repeat factor for the input manifest entries") |
| 161 | + parser.add_argument("--benchmark-results-path", required=True, help="Path to write benchmark results") |
| 162 | + parser.add_argument("--hf-token", default="", help="HuggingFace token for PyAnnote") |
| 163 | + parser.add_argument( |
| 164 | + "--max-segment-length", type=float, default=40.0, help="Maximum segment duration (seconds) to infer ASR" |
| 165 | + ) |
| 166 | + parser.add_argument("--asr-batch-size", type=int, default=100, help="Batch size for ASR alignment") |
| 167 | + parser.add_argument("--executor", default="xenna", choices=["xenna", "ray_data", "ray_actors"], help="Executor") |
| 168 | + parser.add_argument("--cpus", type=int, default=10, help="Number of CPUs to use for the pipeline") |
| 169 | + |
| 170 | + args = parser.parse_args() |
| 171 | + |
| 172 | + logger.info("=== Audio Tagging Pipeline Benchmark Starting ===") |
| 173 | + logger.info(f"Arguments: {vars(args)}") |
| 174 | + |
| 175 | + success_code = 1 |
| 176 | + |
| 177 | + result_dict: dict[str, Any] = { |
| 178 | + "params": vars(args), |
| 179 | + "metrics": {"is_success": False}, |
| 180 | + "tasks": [], |
| 181 | + } |
| 182 | + try: |
| 183 | + result_dict.update(run_audio_tagging_benchmark(**vars(args))) |
| 184 | + success_code = 0 if result_dict["metrics"]["is_success"] else 1 |
| 185 | + finally: |
| 186 | + write_benchmark_results(result_dict, args.benchmark_results_path) |
| 187 | + return success_code |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + raise SystemExit(main()) |
0 commit comments