bytecodealliance / wasi-nn

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

Bindings for C/C++ #75

Open tonibofarull opened 1 year ago

tonibofarull commented 1 year ago

Hi, We have published an experimental version of WASI-NN for WAMR:

Since there is no automatic API generation in C from the WIT specification, we have adapted it manually.

Several parts do not translate directly from WIT to C:

Questions

  1. Is there any way to automatically generate bindings for C/C++ from the spec using wit-bindgen or any other tool? As far as I know, this is still an open topic https://github.com/WebAssembly/wasi-nn/issues/24
  2. How can I validate that the WAMR bindings match the WIT specification?

Proposal

Define the C/C++ in this repository.

abrown commented 1 year ago

First, I think that this repository would be a natural place to store the C bindings and if you want to contribute those I will gladly review it. Now, automatic generation might be a bit of a problem but less problematic than when https://github.com/WebAssembly/wasi-nn/issues/24 was first asked. I just experimented with generating guest-side C bindings for wasi-nn and it almost works:

~/Code/wit-bindgen$ cargo run guest c ../wasi-nn-spec/wasi-nn.wit.md 
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/wit-bindgen guest c ../wasi-nn-spec/wasi-nn.wit.md`
failed to parse wit file `../wasi-nn-spec/wasi-nn.wit.md`

Caused by:
    expected ':', found an identifier
         --> ../wasi-nn-spec/wasi-nn.wit.md:73:10
          |
       73 | resource graph
          |          ^
error: Invalid value '../wasi-nn-spec/wasi-nn.wit.md' for '<DOCUMENT>': failed to parse wit file `../wasi-nn-spec/wasi-nn.wit.md`

For more information try '--help

This was after adding an interface wasi-nn { prefix (and corresponding }) to the WIT markup. You could ask over in the wit-bindgen repository (or experiment yourself online, but my sense is that, for C, wit-bindgen is almost there with a few missing pieces like the one above (new interface syntax needed, some error re: resource, change expected to result) as well as some unknown missing pieces to be discovered. If you're interested in publishing C bindings, I think that experimenting with wit-bindgen a bit is the way to go.

Note that you could guest host-side bindings out of wit-bindgen as well: code that plumbs the Wasm raw values into something more palatable for WAMR. I think most of the work on the host-side has been done for other engines, though, not WAMR, so this may not be too fruitful. I think the guest-side bindings (what this repository is concerned with and what you are manually doing here) is more feasible.

tonibofarull commented 1 year ago

Thank you! If it's ok with everyone I'll take care of creating the bindings.