Skip to content

Commit ee26cb6

Browse files
grorge123hydai
authored andcommitted
feat(MLX): support whispler for MLX backend
Signed-off-by: grorge <[email protected]>
1 parent 88e798c commit ee26cb6

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

wasmedge-mlx/whisper/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "wasmedge-whisper"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
serde_json = "1.0"
8+
wasmedge-wasi-nn = "0.8.0"

wasmedge-mlx/whisper/src/main.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use std::env;
2+
use wasmedge_wasi_nn::{
3+
self, ExecutionTarget, GraphBuilder, GraphEncoding, GraphExecutionContext, TensorType,
4+
};
5+
6+
use serde_json::Value;
7+
use std::fs::File;
8+
use std::io::{self, BufReader};
9+
10+
fn get_data_from_context(context: &GraphExecutionContext, index: usize) -> String {
11+
// Preserve for 4096 tokens with average token length 6
12+
const MAX_OUTPUT_BUFFER_SIZE: usize = 4096 * 6;
13+
let mut output_buffer = vec![0u8; MAX_OUTPUT_BUFFER_SIZE];
14+
let mut output_size = context
15+
.get_output(index, &mut output_buffer)
16+
.expect("Failed to get output");
17+
output_size = std::cmp::min(MAX_OUTPUT_BUFFER_SIZE, output_size);
18+
19+
return String::from_utf8_lossy(&output_buffer[..output_size]).to_string();
20+
}
21+
22+
fn get_output_from_context(context: &GraphExecutionContext) -> String {
23+
get_data_from_context(context, 0)
24+
}
25+
26+
fn read_json(path: &str) -> io::Result<Value> {
27+
let file = File::open(path)?;
28+
let reader = BufReader::new(file);
29+
let v = serde_json::from_reader(reader)
30+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
31+
Ok(v)
32+
}
33+
fn main() {
34+
let args: Vec<String> = env::args().collect();
35+
let audio = "audio.mp3";
36+
let model_name: &str = &args[1];
37+
let model_dir = &args[2];
38+
let config = read_json(&format!("{}/config.json", model_dir)).unwrap();
39+
let graph = GraphBuilder::new(GraphEncoding::Mlx, ExecutionTarget::AUTO)
40+
.config(config.to_string())
41+
.build_from_cache(model_name)
42+
.expect("Failed to build graph");
43+
let mut context = graph
44+
.init_execution_context()
45+
.expect("Failed to init context");
46+
let tensor_data = audio.as_bytes().to_vec();
47+
context
48+
.set_input(0, TensorType::U8, &[1], &tensor_data)
49+
.expect("Failed to set input");
50+
context.compute().expect("Failed to compute");
51+
let output = get_output_from_context(&context);
52+
53+
println!("{}", output.trim());
54+
}

0 commit comments

Comments
 (0)