Both inference call sites previously called `num_cpus::get()` (logical
thread count). Established whisper.cpp / llama.cpp guidance is that
SMT siblings contend for shared FPU resources during heavy F16/F32
matmul, so going past physical core count anti-scales. Empirical
sweep on Whisper Tiny / 11s JFK clip / Ryzen 5 4650U (6c12t):
n_threads | xc_time | RTF | speedup_vs_1
----------|---------|--------|-------------
1 | 0.33s | 0.030 | 1.00x
2 | 0.33s | 0.030 | 1.00x
4 | 0.30s | 0.028 | 1.09x
6 | 0.32s | 0.029 | 1.04x
8 | 0.31s | 0.028 | 1.06x
12 | 0.32s | 0.029 | 1.03x
Tiny doesn't scale (work dominated by overhead) but the larger
Whisper variants and Qwen LLMs do. Sources: whisper.cpp #200, #1033,
#1252, #403; llama.cpp #3167, #572.
Changes:
- crates/core/src/constants.rs:
* MIN_INFERENCE_THREADS lowered 4 → 2 (research-derived floor)
* MAX_INFERENCE_THREADS = 8 added (research-derived ceiling)
* inference_thread_count() rewritten:
- reads MAGNOTIA_INFERENCE_THREADS env var (override)
- num_cpus::get_physical() with available_parallelism fallback
- clamped to [MIN_INFERENCE_THREADS, MAX_INFERENCE_THREADS]
- crates/core/Cargo.toml: + num_cpus = "1"
- crates/transcription/src/whisper_rs_backend.rs: call site uses helper.
- crates/llm/src/lib.rs: call site uses helper.
- crates/transcription/Cargo.toml: drop num_cpus from [features] +
[dependencies] (production no longer needs it). Move to
[dev-dependencies] for tests/thread_sweep.rs only.
- crates/llm/Cargo.toml: drop num_cpus = "1" (no longer used directly).
Per-machine maps (after this patch):
Ryzen 5 4650U 6c12t → 6
big-iron 12c24t → 8 (clamp; users can override)
cheap 2c2t laptop → 2
1c container/VM → 2
Adds crates/transcription/tests/thread_sweep.rs — env-gated like
jfk_bench, prints the table above against any model + WAV. Useful for
re-baselining on new hardware or when tuning the clamp values.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
126 lines
4.2 KiB
Rust
126 lines
4.2 KiB
Rust
//! Direct whisper-rs backend. Owns a WhisperContext; each call builds a
|
|
//! fresh WhisperState (state can be reused, but fresh-per-call is simpler
|
|
//! and matches the transcribe-rs call style we are replacing).
|
|
//!
|
|
//! Exists because transcribe-rs does not expose set_initial_prompt; this
|
|
//! wrapper is the only path that can pipe per-capture vocabulary context
|
|
//! into Whisper.
|
|
|
|
use std::path::Path;
|
|
|
|
use whisper_rs::{FullParams, SamplingStrategy, WhisperContext, WhisperContextParameters};
|
|
|
|
use magnotia_core::constants::inference_thread_count;
|
|
use magnotia_core::error::{MagnotiaError, Result};
|
|
use magnotia_core::types::{Segment, TranscriptionOptions};
|
|
|
|
use crate::transcriber::{Transcriber, TranscriberCapabilities};
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum WhisperBackendError {
|
|
#[error("whisper-rs load failed: {0}")]
|
|
Load(String),
|
|
#[error("whisper-rs state creation failed: {0}")]
|
|
State(String),
|
|
#[error("whisper-rs transcribe failed: {0}")]
|
|
Transcribe(String),
|
|
}
|
|
|
|
pub struct WhisperRsBackend {
|
|
ctx: WhisperContext,
|
|
}
|
|
|
|
impl WhisperRsBackend {
|
|
pub fn load(model_path: &Path) -> std::result::Result<Self, WhisperBackendError> {
|
|
let ctx = WhisperContext::new_with_params(model_path, WhisperContextParameters::default())
|
|
.map_err(|e| WhisperBackendError::Load(e.to_string()))?;
|
|
Ok(Self { ctx })
|
|
}
|
|
}
|
|
|
|
impl Transcriber for WhisperRsBackend {
|
|
fn capabilities(&self) -> TranscriberCapabilities {
|
|
TranscriberCapabilities {
|
|
sample_rate: magnotia_core::constants::WHISPER_SAMPLE_RATE,
|
|
channels: 1,
|
|
supports_initial_prompt: true,
|
|
}
|
|
}
|
|
|
|
/// Synchronously transcribe 16 kHz mono f32 PCM.
|
|
///
|
|
/// `options.initial_prompt` is piped directly to whisper-rs — this
|
|
/// is the only backend path that honours it; `SpeechModelAdapter`
|
|
/// discards it (Parakeet has no equivalent).
|
|
fn transcribe_sync(
|
|
&mut self,
|
|
samples: &[f32],
|
|
options: &TranscriptionOptions,
|
|
) -> Result<Vec<Segment>> {
|
|
tracing::info!(
|
|
language = ?options.language,
|
|
has_initial_prompt = options.initial_prompt.as_deref().map(|p| !p.is_empty()).unwrap_or(false),
|
|
"WhisperRsBackend::transcribe_sync entering"
|
|
);
|
|
|
|
let mut state = self.ctx.create_state().map_err(|e| {
|
|
MagnotiaError::TranscriptionFailed(WhisperBackendError::State(e.to_string()).to_string())
|
|
})?;
|
|
|
|
let mut params = FullParams::new(SamplingStrategy::Greedy { best_of: 1 });
|
|
if let Some(lang) = options.language.as_deref() {
|
|
if !lang.is_empty() {
|
|
params.set_language(Some(lang));
|
|
}
|
|
}
|
|
if let Some(prompt) = options.initial_prompt.as_deref() {
|
|
if !prompt.is_empty() {
|
|
params.set_initial_prompt(prompt);
|
|
}
|
|
}
|
|
params.set_n_threads(inference_thread_count() as i32);
|
|
params.set_print_special(false);
|
|
params.set_print_progress(false);
|
|
params.set_print_realtime(false);
|
|
|
|
state.full(params, samples).map_err(|e| {
|
|
MagnotiaError::TranscriptionFailed(
|
|
WhisperBackendError::Transcribe(e.to_string()).to_string(),
|
|
)
|
|
})?;
|
|
|
|
let n = state.full_n_segments();
|
|
|
|
let mut out = Vec::with_capacity(n.max(0) as usize);
|
|
for i in 0..n {
|
|
let Some(seg) = state.get_segment(i) else {
|
|
continue;
|
|
};
|
|
let text = seg
|
|
.to_str()
|
|
.map_err(|e| {
|
|
MagnotiaError::TranscriptionFailed(
|
|
WhisperBackendError::Transcribe(e.to_string()).to_string(),
|
|
)
|
|
})?
|
|
.to_string();
|
|
// whisper-rs timestamps are centiseconds (10ms units). Convert to seconds (f64).
|
|
let start = seg.start_timestamp() as f64 * 0.01;
|
|
let end = seg.end_timestamp() as f64 * 0.01;
|
|
out.push(Segment { start, end, text });
|
|
}
|
|
Ok(out)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn backend_error_displays() {
|
|
let e = WhisperBackendError::Load("oops".into());
|
|
assert!(e.to_string().contains("oops"));
|
|
}
|
|
}
|