Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add high level and safe API #81

Merged
merged 6 commits into from
Jun 20, 2023
Merged

Conversation

yanghaku
Copy link
Contributor

@yanghaku yanghaku commented Jun 14, 2023

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
Copy link
Collaborator

abrown commented Jun 15, 2023

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

Copy link
Collaborator

@abrown abrown left a comment

Choose a reason for hiding this comment

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

This is great! Thank you so much for this contribution; it definitely fulfills all the key points and closes #68. I added a couple of nitpicky comments below. Let's resolve those and then I will merge this. (As a note to myself: I need to build and look through this excellent documentation before I publish a new version.)

rust/src/graph.rs Show resolved Hide resolved
rust/src/graph.rs Outdated Show resolved Hide resolved
@abrown abrown merged commit faa2a52 into bytecodealliance:main Jun 20, 2023
@yanghaku yanghaku deleted the safe-api branch June 21, 2023 02:39
@abrown abrown mentioned this pull request Jun 23, 2023
@abrown abrown mentioned this pull request Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants