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

wasi-nn: add named models #6854

Merged
merged 3 commits into from
Aug 23, 2023
Merged

wasi-nn: add named models #6854

merged 3 commits into from
Aug 23, 2023

Conversation

abrown
Copy link
Contributor

@abrown abrown commented Aug 17, 2023

This implements named models in Wasmtime; see the commit messages for more details.

@abrown abrown requested review from a team as code owners August 17, 2023 00:50
@abrown abrown requested review from fitzgen and pchickey and removed request for a team August 17, 2023 00:50
cp target/wasm32-wasi/release/wasi-nn-example-named.wasm $TMP_DIR
popd
cargo run -- run --mapdir fixture::$TMP_DIR --graph openvino::$TMP_DIR \
--wasi-modules=experimental-wasi-nn $TMP_DIR/wasi-nn-example-named.wasm
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite happy with using this script for testing. It was good initially to get things working but I would prefer to be using cargo test instead. The problem is that there is a dependency expectation: this can only run if the system has OpenVINO installed, which will not always be the case for developers running cargo test.

I can think of two workarounds:

  • #[ignore] the OpenVINO-specific tests and add some comments telling developers to turn them on to fully test the system
  • add some checks at the beginning of each test to early-exit if OpenVINO is not available; the issue with this is that users may see "false success" if something goes wrong in the environment, so perhaps a FORCE_RUN_OPENVINO_TESTS env variable is necessary?

Interested in some feedback on whether one of those approaches is better than this script!

Copy link
Member

Choose a reason for hiding this comment

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

Personally I like your second suggestion and matches what I was going to suggest as well. We do run a higher risk of not actually running the tests in CI but I think running something by default as part of cargo test --workspace is a good mitigating factor for that.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the tests are for wasi-nn, we should just have a test backend that does something basic like an affine layer.

If this is testing integration we should think about how we handle multiple backends. This same issue will come up with pytorch, onnxruntime, tf, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexcrichton, I have a whole other set of commits ready for switching to that kind of testing. Just waiting on a review of this one because the new testing also covers the load_by_name logic added here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, check out #6895 if you're interested in where I went with this.

pub use in_memory::InMemoryRegistry;

pub trait GraphRegistry: Send + Sync {
fn get_mut(&mut self, name: &str) -> Option<&mut Graph>;
Copy link
Contributor Author

@abrown abrown Aug 17, 2023

Choose a reason for hiding this comment

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

Another thing I'm not quite sure of is whether this should return Option<&mut Graph>, Option<&Graph>, or even Option<Graph> (seeing as how Graph is just an Arc-wrapper). We immediately clone the graph and I removed the mutability requirement for BackendGraph::init_execution_context... @geekbeast, any opinions from implementing other kinds of GraphRegistry?

@fitzgen fitzgen removed their request for review August 17, 2023 16:58
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

I can't review the wasi-nn stuff specifically really, it all seems reasonable though. The src/commands/run.rs bits look good to me, although I might recommend renaming --graph to something like --wasi-nn-graph or --nn-graph or something like that perhaps to make it a bit more clear it's for wasi-nn as opposed to something else graph-related (not that we have much else like that right now)

This change adds a way to retrieve preloaded ML models (i.e., "graphs"
in wasi-nn terms) from a registry. The wasi-nn specification includes a
new function, `load_by_name`, that can be used to access these models
more efficiently than before; previously, a user's only option was to
read/download/etc. all of the bytes of an ML model and pass them to the
`load` function.

[named models]: WebAssembly/wasi-nn#36

In Wasmtime's implementation of wasi-nn, we call the registry that holds
the models a `GraphRegistry`. We include a simplistic `InMemoryRegistry`
for use in the Wasmtime CLI (more on this later) but the idea is that
production use will involve some more complex caching and thus a new
implementation of a registry--a `Box<dyn GraphRegistry>`--passed into
the wasi-nn context. Note that, because we now must be able to `clone` a
graph out of the registry and into the "used graphs" table, the OpenVINO
`BackendGraph` is updated to be easier to copy around.

To allow experimentation with this "preload a named model"
functionality, this change also adds a new Wasmtime CLI flag: `--graph
<encoding>:<host dir>`. Wasmtime CLI users can now preload a model from
a directory; the directory `basename` is used as the model name. Loading
models from a directory is probably not desired in Wasmtime embeddings
so it is cordoned off into a separate `BackendFromDir` extension trait.
Add a new example crate which loads a model by name and performs image
classification. It uses the same MobileNet model as the existing test
but a new version of the Rust bindings. The new crate is built and run
with the new CLI flag in the `ci/run-wasi-nn-example.sh` script.

prtest:full
@abrown abrown enabled auto-merge August 22, 2023 23:46
@abrown abrown added this pull request to the merge queue Aug 23, 2023
Merged via the queue into bytecodealliance:main with commit 770c5d0 Aug 23, 2023
31 checks passed
@abrown abrown deleted the registries branch August 23, 2023 00:55
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Sep 6, 2023
* wasi-nn: add [named models]

This change adds a way to retrieve preloaded ML models (i.e., "graphs"
in wasi-nn terms) from a registry. The wasi-nn specification includes a
new function, `load_by_name`, that can be used to access these models
more efficiently than before; previously, a user's only option was to
read/download/etc. all of the bytes of an ML model and pass them to the
`load` function.

[named models]: WebAssembly/wasi-nn#36

In Wasmtime's implementation of wasi-nn, we call the registry that holds
the models a `GraphRegistry`. We include a simplistic `InMemoryRegistry`
for use in the Wasmtime CLI (more on this later) but the idea is that
production use will involve some more complex caching and thus a new
implementation of a registry--a `Box<dyn GraphRegistry>`--passed into
the wasi-nn context. Note that, because we now must be able to `clone` a
graph out of the registry and into the "used graphs" table, the OpenVINO
`BackendGraph` is updated to be easier to copy around.

To allow experimentation with this "preload a named model"
functionality, this change also adds a new Wasmtime CLI flag: `--graph
<encoding>:<host dir>`. Wasmtime CLI users can now preload a model from
a directory; the directory `basename` is used as the model name. Loading
models from a directory is probably not desired in Wasmtime embeddings
so it is cordoned off into a separate `BackendFromDir` extension trait.

* wasi-nn: add "named model" test

Add a new example crate which loads a model by name and performs image
classification. It uses the same MobileNet model as the existing test
but a new version of the Rust bindings. The new crate is built and run
with the new CLI flag in the `ci/run-wasi-nn-example.sh` script.

prtest:full

* review: rename `--graph` to `--wasi-nn-graph`
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.

3 participants