Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions crates/transcribe-whisper-local/src/service/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@ where
}
};

let languages = params
.languages
.iter()
.filter_map(|lang| lang.clone().try_into().ok())
.collect::<Vec<hypr_whisper::Language>>();

let vocabulary = params.vocabulary.clone();

let model = match hypr_whisper_local::Whisper::builder()
.model_path(model_path.to_str().unwrap())
.languages(
params
.languages
.iter()
.filter_map(|lang| lang.clone().try_into().ok())
.collect::<Vec<hypr_whisper::Language>>(),
)
.languages(languages)
.vocabulary(vocabulary)
.build()
{
Ok(model) => model,
Expand Down
8 changes: 7 additions & 1 deletion crates/whisper-local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ openmp = ["whisper-rs/openmp"]
[dev-dependencies]
hypr-data = { workspace = true }

criterion = { workspace = true }
dirs = { workspace = true }
futures-util = { workspace = true }
tokio = { workspace = true }

[[bench]]
name = "whisper_transcription"
harness = false

[dependencies]
hypr-audio-utils = { workspace = true }
hypr-whisper = { workspace = true }

dasp = { workspace = true }
kalosm-sound = { workspace = true, default-features = false }
rodio = { workspace = true }
whisper-rs = { version = "0.15.0", features = ["raw-api", "tracing_backend"] }
whisper-rs = { git = "https://codeberg.org/tazz4843/whisper-rs", rev = "3e6d3da", features = ["raw-api", "tracing_backend"] }

futures-util = { workspace = true }
tracing = { workspace = true }
Expand All @@ -37,6 +42,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
specta = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }
trie-rs = "0.4.2"

lazy_static = { workspace = true }
regex = { workspace = true }
88 changes: 88 additions & 0 deletions crates/whisper-local/benches/whisper_transcription.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::hint::black_box;
use std::time::Duration;

use criterion::{criterion_group, criterion_main, Criterion};
use hypr_whisper::Language;
use whisper_local::Whisper;

fn benchmark_whisper_transcription(c: &mut Criterion) {
let audio: Vec<f32> = hypr_data::english_1::AUDIO
.chunks_exact(2)
.map(|chunk| i16::from_le_bytes([chunk[0], chunk[1]]) as f32 / 32768.0)
.collect();

let model_path = concat!(env!("CARGO_MANIFEST_DIR"), "/model.bin");

let mut whisper_without_vocab = Whisper::builder()
.model_path(model_path)
.languages(vec![Language::En])
.build()
.unwrap();

let mut whisper_with_vocab = Whisper::builder()
.model_path(model_path)
.languages(vec![Language::En])
.vocabulary(
vec![
"profound",
"acquire",
"complementary",
"deeply",
"repositories",
"brilliant",
"pockets",
"thread",
"stumbling",
"stumble",
"communities",
"invested",
"undergrad",
"Googleable",
"exploring",
"neuroscientist",
"psychology",
"engineering",
"researcher",
"thinker",
"skill",
"invest",
"solved",
"entire",
"especially",
"actually",
"often",
"already",
"important",
"definitely",
"much",
]
.into_iter()
.map(|s| s.into())
.collect(),
)
.build()
.unwrap();

let mut group = c.benchmark_group("whisper_comparison");
group.measurement_time(Duration::from_secs(100));
group.sample_size(10);

group.bench_function("without_vocab", |b| {
b.iter(|| {
let segments = whisper_without_vocab.transcribe(black_box(&audio)).unwrap();
black_box(segments)
})
});

group.bench_function("with_vocab", |b| {
b.iter(|| {
let segments = whisper_with_vocab.transcribe(black_box(&audio)).unwrap();
black_box(segments)
})
});

group.finish();
}

criterion_group!(benches, benchmark_whisper_transcription);
criterion_main!(benches);
93 changes: 93 additions & 0 deletions crates/whisper-local/src/bias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use trie_rs::map::{Trie, TrieBuilder};
use whisper_rs::{WhisperContext, WhisperTokenId};

#[derive(Clone)]
pub struct BiasTrie {
trie: Trie<WhisperTokenId, f32>,
}

impl BiasTrie {
pub fn new(ctx: &WhisperContext, custom_vocab: &[&str]) -> Result<Self, crate::Error> {
let mut builder = TrieBuilder::new();

for word in custom_vocab {
let variants = Self::generate_tokenization_variants(ctx, word)?;

for tokens in variants {
for i in 1..=tokens.len() {
let progress = i as f32 / tokens.len() as f32;

let prefix_bias = 10.0 + 90.0 * progress.powi(2);

let prefix = &tokens[..i];
builder.push(prefix, prefix_bias);
}
}
}
Comment on lines +13 to +26
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Deduplicate overlapping prefixes across variants; skip empty tokenizations.

Multiple variants can yield identical prefixes; pushing each inflates/overwrites bias unpredictably. Aggregate by prefix and keep the max bias.

-        for word in custom_vocab {
-            let variants = Self::generate_tokenization_variants(ctx, word)?;
-            for tokens in variants {
-                for i in 1..=tokens.len() {
-                    let progress = i as f32 / tokens.len() as f32;
-
-                    let prefix_bias = 10.0 + 90.0 * progress.powi(2);
-
-                    let prefix = &tokens[..i];
-                    builder.push(prefix, prefix_bias);
-                }
-            }
-        }
+        let mut acc: HashMap<Vec<WhisperTokenId>, f32> = HashMap::new();
+        for word in custom_vocab {
+            let variants = Self::generate_tokenization_variants(ctx, word)?;
+            for tokens in variants {
+                if tokens.is_empty() { continue; }
+                for i in 1..=tokens.len() {
+                    let progress = i as f32 / tokens.len() as f32;
+                    let bias = 10.0 + 90.0 * progress.powi(2);
+                    let key = tokens[..i].to_vec();
+                    acc.entry(key).and_modify(|v| *v = v.max(bias)).or_insert(bias);
+                }
+            }
+        }
+        for (k, v) in acc {
+            builder.push(&k, v);
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for word in custom_vocab {
let variants = Self::generate_tokenization_variants(ctx, word)?;
for tokens in variants {
for i in 1..=tokens.len() {
let progress = i as f32 / tokens.len() as f32;
let prefix_bias = 10.0 + 90.0 * progress.powi(2);
let prefix = &tokens[..i];
builder.push(prefix, prefix_bias);
}
}
}
// Deduplicate overlapping prefixes across all variants, keeping the max bias per prefix.
let mut acc: HashMap<Vec<WhisperTokenId>, f32> = HashMap::new();
for word in custom_vocab {
let variants = Self::generate_tokenization_variants(ctx, word)?;
for tokens in variants {
if tokens.is_empty() { continue; }
for i in 1..=tokens.len() {
let progress = i as f32 / tokens.len() as f32;
let bias = 10.0 + 90.0 * progress.powi(2);
let key = tokens[..i].to_vec();
acc.entry(key)
.and_modify(|v| *v = v.max(bias))
.or_insert(bias);
}
}
}
for (k, v) in acc {
builder.push(&k, v);
}
🤖 Prompt for AI Agents
In crates/whisper-local/src/bias.rs around lines 13 to 26, the loop currently
pushes every prefix from every tokenization variant, which duplicates
overlapping prefixes and allows later variants to overwrite earlier bias values
unpredictably and also doesn't skip empty tokenizations; change the logic so you
skip any variant with zero tokens, then collect prefixes from all variants into
a temporary map (or hashmap) keyed by the prefix sequence and store the maximum
computed bias for that prefix (compare existing entry and keep the larger bias),
and after processing all variants push each unique prefix once to builder with
the aggregated max bias.


let trie = builder.build();
Ok(BiasTrie { trie })
}

fn generate_tokenization_variants(
ctx: &WhisperContext,
word: &str,
) -> Result<Vec<Vec<WhisperTokenId>>, crate::Error> {
let mut variants = Vec::new();

variants.push(ctx.tokenize(word, 99)?);
variants.push(ctx.tokenize(&format!(" {}", word), 99)?);

let lower = word.to_lowercase();
if lower != word {
variants.push(ctx.tokenize(&lower, 99)?);
variants.push(ctx.tokenize(&format!(" {}", lower), 99)?);
}

let upper = word.to_uppercase();
if upper != word {
variants.push(ctx.tokenize(&upper, 99)?);
}

variants.push(ctx.tokenize(&format!("'{}", word), 99)?);
variants.push(ctx.tokenize(&format!("\"{}", word), 99)?);

Ok(variants)
}

pub unsafe fn apply_bias_to_logits(
&self,
tokens: *const whisper_rs::whisper_rs_sys::whisper_token_data,
n_tokens: std::os::raw::c_int,
logits: *mut f32,
) {
if tokens.is_null() || n_tokens <= 0 {
return;
}

let current_tokens: Vec<WhisperTokenId> =
std::slice::from_raw_parts(tokens, n_tokens as usize)
.iter()
.map(|t| t.id)
.collect();

for suffix_len in 1..=std::cmp::min(10, current_tokens.len()) {
let suffix = &current_tokens[current_tokens.len() - suffix_len..];

for (full_sequence, bias_value_ref) in self.trie.predictive_search(suffix) {
let bias_value = *bias_value_ref;
let full_sequence: Vec<WhisperTokenId> = full_sequence;

if full_sequence.len() > suffix.len() {
let next_token = full_sequence[suffix.len()];
let current_logit = *logits.offset(next_token as isize);

let boost = bias_value.ln() * 2.0;
let new_logit = current_logit + boost;

*logits.offset(next_token as isize) = new_logit;
}
}
}
}
Comment on lines +58 to +92
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Update call sites to pass n_vocab and validate usage.

Use this script to find and fix all call sites:


🏁 Script executed:

#!/bin/bash
# Find apply_bias_to_logits call sites and show context
rg -n -C3 --type=rust '\.apply_bias_to_logits\s*\('

Length of output: 518


Update all call sites to pass n_vocab
In crates/whisper-local/src/model.rs:281, change

.apply_bias_to_logits(tokens, n_tokens, logits)

to

.apply_bias_to_logits(tokens, n_tokens, n_vocab, logits)

and ensure the method signature in bias.rs is updated accordingly.

🤖 Prompt for AI Agents
In crates/whisper-local/src/bias.rs around lines 58 to 92, update the unsafe fn
signature to accept n_vocab (std::os::raw::c_int) as a new parameter (e.g.,
tokens, n_tokens, n_vocab, logits), then use n_vocab to validate token indices
before indexing into the logits buffer: ensure next_token is within 0..n_vocab
(cast and compare as usize/int) and only read/write logits if in-bounds to avoid
OOB access; adjust pointer arithmetic to cast next_token safely to isize after
bounds checking; also update call sites (as noted in the review) to pass n_vocab
when invoking this method.

}
3 changes: 3 additions & 0 deletions crates/whisper-local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pub use model::*;
mod error;
pub use error::*;

mod bias;
use bias::*;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, specta::Type)]
pub struct GgmlBackend {
pub kind: String,
Expand Down
Loading