Diarization: CPU with 8 threads and progress, drop CUDA attempt

- 8 threads (sherpa-onnx default is 1): 40-min call >10 min -> ~6 min.
- CUDA provider measured slower (>7 min, GPU ~5 W): tiny models and one
  embedding per segment (3474 here), so transfer/launch overhead dominates.
  Reverted the cuda12.cudnn9 wheel and extra nvidia-* deps.
- Progress callback prints every 10 % (segmentation, then embeddings).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pinokio
2026-11-02 15:40:11 +01:00
co-authored by Claude Opus 5.5
parent a64d3fb275
commit d9ec9c5b01
2 changed files with 20 additions and 19 deletions
+19 -16
View File
@@ -1,11 +1,13 @@
"""Label who speaks (sherpa-onnx, CUDA with CPU fallback): run(name) -> out/<name>.spk.txt, diar_segments.json, DB lines.""" """Label who speaks (sherpa-onnx, CPU): run(name) -> out/<name>.spk.txt, diar_segments.json, DB lines."""
import json, re, subprocess, time import json, re, subprocess, time
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
import common, db # common: puts the pip nvidia-* CUDA DLLs on PATH import db
M = Path(__file__).parent / "models" M = Path(__file__).parent / "models"
THREADS = 8 # CPU fallback; sherpa-onnx default is 1 thread: a 40-min call took >10 min # sherpa-onnx default is 1 thread (>10 min for a 40-min call); 8 threads: ~6 min. CUDA measured slower (>7 min, GPU ~5 W):
# tiny models + one embedding per segment -> transfer/launch overhead dominates.
THREADS = 8
def run(name, speakers=5, threshold=0.85): def run(name, speakers=5, threshold=0.85):
@@ -14,19 +16,20 @@ def run(name, speakers=5, threshold=0.85):
raw = subprocess.run(["ffmpeg", "-v", "error", "-i", str(src), "-vn", "-ac", "1", "-ar", "16000", "-f", "f32le", "-"], raw = subprocess.run(["ffmpeg", "-v", "error", "-i", str(src), "-vn", "-ac", "1", "-ar", "16000", "-f", "f32le", "-"],
capture_output=True, check=True).stdout capture_output=True, check=True).stdout
audio = np.frombuffer(raw, np.float32) audio = np.frombuffer(raw, np.float32)
def diarizer(provider): sd = sherpa_onnx.OfflineSpeakerDiarization(sherpa_onnx.OfflineSpeakerDiarizationConfig(
return sherpa_onnx.OfflineSpeakerDiarization(sherpa_onnx.OfflineSpeakerDiarizationConfig( segmentation=sherpa_onnx.OfflineSpeakerSegmentationModelConfig(
segmentation=sherpa_onnx.OfflineSpeakerSegmentationModelConfig( pyannote=sherpa_onnx.OfflineSpeakerSegmentationPyannoteModelConfig(model=str(M / "sherpa-onnx-pyannote-segmentation-3-0/model.onnx")),
pyannote=sherpa_onnx.OfflineSpeakerSegmentationPyannoteModelConfig(model=str(M / "sherpa-onnx-pyannote-segmentation-3-0/model.onnx")), num_threads=THREADS),
num_threads=THREADS, provider=provider), embedding=sherpa_onnx.SpeakerEmbeddingExtractorConfig(model=str(M / "emb.onnx"), num_threads=THREADS),
embedding=sherpa_onnx.SpeakerEmbeddingExtractorConfig(model=str(M / "emb.onnx"), num_threads=THREADS, provider=provider), clustering=sherpa_onnx.FastClusteringConfig(num_clusters=speakers, threshold=threshold),
clustering=sherpa_onnx.FastClusteringConfig(num_clusters=speakers, threshold=threshold), min_duration_on=0.3, min_duration_off=0.5))
min_duration_on=0.3, min_duration_off=0.5)) t0, last = time.time(), [-1]
t0 = time.time() def progress(done, total): # sherpa calls this per processed chunk; print every 10 %
try: sd, used = diarizer("cuda"), "cuda" # CUDA build: sherpa-onnx==...+cuda12.cudnn9 (flow.py deps) pct = done * 100 // max(total, 1)
except Exception as e: print("diarize: CUDA unavailable, using CPU:", e, flush=True); sd, used = diarizer("cpu"), "cpu" if pct // 10 > last[0]: last[0] = pct // 10; print(f"diarize {pct}% ({done}/{total}, {time.time() - t0:.0f} s)", flush=True)
segs = [(s.start, s.end, s.speaker) for s in sd.process(audio).sort_by_start_time()] return 0
print(len(segs), "segments,", len({s[2] for s in segs}), f"speakers ({used}, {time.time() - t0:.0f} s)", flush=True) segs = [(s.start, s.end, s.speaker) for s in sd.process(audio, callback=progress).sort_by_start_time()]
print(len(segs), "segments,", len({s[2] for s in segs}), f"speakers ({time.time() - t0:.0f} s)", flush=True)
lines = [] lines = []
for l in (db.OUT / f"{name}.txt").read_text(encoding="utf-8").splitlines(): for l in (db.OUT / f"{name}.txt").read_text(encoding="utf-8").splitlines():
+1 -3
View File
@@ -1,8 +1,6 @@
# /// script # /// script
# requires-python = ">=3.10,<3.13" # requires-python = ">=3.10,<3.13"
# dependencies = ["prefect>=3", "faster-whisper", "av>=14,<17", "nvidia-cublas-cu12", "nvidia-cudnn-cu12==9.*", "nvidia-cuda-runtime-cu12", "nvidia-cufft-cu12", "sherpa-onnx==2.0.1+cuda12.cudnn9", "numpy", "imagehash", "Pillow", "markdown", "openvino-genai", "huggingface_hub"] # dependencies = ["prefect>=3", "faster-whisper", "av>=14,<17", "nvidia-cublas-cu12", "nvidia-cudnn-cu12==9.*", "sherpa-onnx", "numpy", "imagehash", "Pillow", "markdown", "openvino-genai", "huggingface_hub"]
# [tool.uv]
# find-links = ["https://k2-fsa.github.io/sherpa/onnx/cuda.html"] # sherpa-onnx CUDA wheels (diarization on GPU, CPU fallback in diarize.py)
# /// # ///
"""Meeting pipeline. uv run --script flow.py <name> [--from STEP] [--speakers N] [--device cuda|npu] [--bench-npu]""" """Meeting pipeline. uv run --script flow.py <name> [--from STEP] [--speakers N] [--device cuda|npu] [--bench-npu]"""
import os import os