bytecodealliance / wasi-nn

High-level bindings for wasi-nn system calls
Apache License 2.0
41 stars 33 forks source link

Add high level and safe API #81

Closed yanghaku closed 1 year ago

yanghaku commented 1 year ago

This PR adds a wrapper high-level and safe rust API for the generated rust bindings.

The key changes

  1. Change the generated types and functions to private, such as wasi_ephemeral_nn::*.
  2. Add high-level API.
  3. Update the example code and add library documents.

Details

For Convenience

  1. Add Error and BackendError enum for error handling, and introduce a dependency thiserror. (replace NnErrno)
  2. Add GraphEncoding, ExecutionTarget, and TensorType enum to replace the corresponding types in generated.rs.
  3. Add GraphBuilder to build Graph instances, the methods can be chained on it in order to configure the graph conveniently.
  4. Use the generic parameter type function for set_input and get_output, so users can use a lot of types as parameters, such as [f32], [u8], Vec<f32>, Vec<u8>, etc.

For Safety

  1. Users cannot use raw pointers anymore.
  2. Add a lifetime parameter for GraphExecutionContext, it cannot live longer than Graph.
  3. Change the GraphExecutionContext as &mut self when running set_input and compute.

Misc

  1. Update the example rust/examples/classification-example/src/main.rs to adapt the high-level API.
  2. Add the documents for the library and structures, so the users can see them in docs.rs.
  3. Set the build targets in Cargo.toml for docs.rs.
  4. Add .cargo/config.toml to set the default build/publish target to wasm32-wasi.

This is a part work of WasmEdge's LFX mentorship, Thanks.

abrown commented 1 year ago

Thanks for the submission! I will take a look at this tomorrow.