Skip to content

Commit

Permalink
Better error handling and more robust output handling
Browse files Browse the repository at this point in the history
  • Loading branch information
juntao committed Oct 9, 2023
1 parent 316f02d commit 02cf6d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions wasmedge-ggml-llama/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@ fn main() {
let graph =
wasi_nn::GraphBuilder::new(wasi_nn::GraphEncoding::Ggml, wasi_nn::ExecutionTarget::AUTO)
.build_from_cache(model_name)
.unwrap();
.expect("Failed to load the model");
println!("Loaded model into wasi-nn with ID: {:?}", graph);

let mut context = graph.init_execution_context().unwrap();
let mut context = graph.init_execution_context().expect("Failed to init context");
println!("Created wasi-nn execution context with ID: {:?}", context);

let tensor_data = prompt.as_bytes().to_vec();
println!("Read input tensor, size in bytes: {}", tensor_data.len());
context
.set_input(0, wasi_nn::TensorType::U8, &[1], &tensor_data)
.unwrap();
.expect("Failed to set prompt as the input tensor");

// Execute the inference.
context.compute().unwrap();
context.compute().expect("Failed to complete inference");
println!("Executed model inference");

// Retrieve the output.
let mut output_buffer = vec![0u8; 1000];
context.get_output(0, &mut output_buffer).unwrap();
let output = String::from_utf8(output_buffer.clone()).unwrap();
let max_output_size = 4096*6;
let mut output_buffer = vec![0u8; max_output_size];
let mut output_size = context.get_output(0, &mut output_buffer).expect("Failed to get inference output");
output_size = std::cmp::min(max_output_size, output_size);
let output = String::from_utf8_lossy(&output_buffer[..output_size]).to_string();
println!("Output: {}", output);
}
Binary file modified wasmedge-ggml-llama/wasmedge-ggml-llama.wasm
Binary file not shown.

0 comments on commit 02cf6d6

Please sign in to comment.