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: use resources #8873

Merged
merged 5 commits into from
Jun 27, 2024
Merged

Conversation

abrown
Copy link
Contributor

@abrown abrown commented Jun 25, 2024

Recent discussion in the wasi-nn proposal (see wasi-nn#59, e.g.) has concluded that the right approach for representing wasi-nn "things" (tensors, graph, etc.) is with a component model resource. This sweeping change brings Wasmtime's implementation in line with that decision.

Initially I had structured this PR to remove all of the WITX-based implementation (#8530). But, after consulting in a Zulip thread on what other WASI proposals aim to do, this PR pivoted to support both` the WITX-based and WIT-based ABIs (e.g., preview1 era versus preview2, component model era). What is clear is that the WITX-based specification will remain "frozen in time" while the WIT-based implementation moves forward.

What that means for this PR is a "split world" paradigm. In many places, we have to distinguish between the wit and witx versions of the same thing. This change isn't the end state yet: it's a big step forward towards bringing Wasmtime back in line with the WIT spec but, despite my best efforts, doesn't fully fix all the TODOs left behind over several years of development. I have, however, taken the liberty to refactor and fix various parts as I came across them (e.g., the ONNX backend). I plan to continue working on this in future PRs to figure out a good error paradigm (the current one is too wordy) and device residence.

@alexcrichton
Copy link
Member

At a high level this looks reasonable to me, but one recommendation I might have is that the way we implemented WASIp1 was to have it be a wrapper effectively around WASIp2. Internally all of WASIp1 is implemented in terms of WASIp2's underlying functions. Would it be possible to do that here as well? For example storing a wit::WasiNnCtx inside of a witx::WasiNnCtx directly and then using the Host and such traits to implement the internals?

(not required of course just a suggestion)

@abrown abrown force-pushed the wasi-nn-resources branch 3 times, most recently from ee21e9c to 21beee4 Compare June 26, 2024 21:24
Recent discussion in the wasi-nn proposal (see [wasi-nn#59], e.g.) has
concluded that the right approach for representing wasi-nn "things"
(tensors, graph, etc.) is with a component model _resource_. This
sweeping change brings Wasmtime's implementation in line with that
decision.

Initially I had structured this PR to remove all of the WITX-based
implementation (bytecodealliance#8530). But, after consulting in a Zulip [thread] on
what other WASI proposals aim to do, this PR pivoted to support _both_`
the WITX-based and WIT-based ABIs (e.g., preview1 era versus preview2,
component model era). What is clear is that the WITX-based specification
will remain "frozen in time" while the WIT-based implementation moves
forward.

What that means for this PR is a "split world" paradigm. In many places,
we have to distinguish between the `wit` and `witx` versions of the same
thing. This change isn't the end state yet: it's a big step forward
towards bringing Wasmtime back in line with the WIT spec but, despite my
best efforts, doesn't fully fix all the TODOs left behind over several
years of development. I have, however, taken the liberty to refactor and
fix various parts as I came across them (e.g., the ONNX backend). I plan
to continue working on this in future PRs to figure out a good error
paradigm (the current one is too wordy) and device residence.

[wasi-nn#59]: WebAssembly/wasi-nn#59
[thread]: https://bytecodealliance.zulipchat.com/#narrow/stream/219900-wasi/topic/wasi-nn's.20preview1.20vs.20preview2.20timeline

prtest:full
@abrown abrown force-pushed the wasi-nn-resources branch from 21beee4 to 768c77e Compare June 26, 2024 21:34
abrown added 3 commits June 27, 2024 11:22
With @alexcrichton's help, this change removes the `trait WasiNnView`
and `struct WasiNnImpl` wrapping that the WIT-based implementation used
for accessing the host context. Instead, `WasiNnView` is now a `struct`
containing the mutable references it needs to make things work. This
unwraps one complex layer of abstraction, though it does have the
downside that it complicates CLI code to split borrows of `Host`.
This change simplifies the return types of the host implementations of
the WIT-based wasi-nn. There is more work to be done with errors, e.g.,
to catch up with the upstream decision to return errors as resources.
But this is better than the previous mess.
@abrown abrown marked this pull request as ready for review June 27, 2024 18:58
@abrown abrown requested review from a team as code owners June 27, 2024 18:58
@abrown abrown requested review from alexcrichton and removed request for a team June 27, 2024 18:58
@alexcrichton alexcrichton added this pull request to the merge queue Jun 27, 2024
Merged via the queue into bytecodealliance:main with commit 0f4ae88 Jun 27, 2024
119 checks passed
@abrown abrown deleted the wasi-nn-resources branch June 27, 2024 19:32
abrown added a commit to abrown/wasmtime that referenced this pull request Aug 1, 2024
In bytecodealliance#8873, we stopped tracking the wasi-nn's upstream WIT files
temporarily because it was not clear to me at the time how to implement
errors as CM resources. This PR fixes that, resuming tracking in the
`vendor-wit.sh` and implementing what is needed in the wasi-nn crate.

This leaves several threads unresolved, though:
- it looks like the `vendor-wit.sh` has a new mechanism for retrieving
  files into `wit/deps`--at some point wasi-nn should migrate to use
  that (?)
- it's not clear to me that "errors as resources" is even the best
  approach here; I've opened [bytecodealliance#75] to consider the possibility of using
  "errors as records" instead.

[bytecodealliance#75]: WebAssembly/wasi-nn#75
abrown added a commit to abrown/wasmtime that referenced this pull request Aug 1, 2024
In bytecodealliance#8873, we stopped tracking the wasi-nn's upstream WIT files
temporarily because it was not clear to me at the time how to implement
errors as CM resources. This PR fixes that, resuming tracking in the
`vendor-wit.sh` and implementing what is needed in the wasi-nn crate.

This leaves several threads unresolved, though:
- it looks like the `vendor-wit.sh` has a new mechanism for retrieving
  files into `wit/deps`--at some point wasi-nn should migrate to use
  that (?)
- it's not clear to me that "errors as resources" is even the best
  approach here; I've opened [bytecodealliance#75] to consider the possibility of using
  "errors as records" instead.

[bytecodealliance#75]: WebAssembly/wasi-nn#75

prtest:full
abrown added a commit to abrown/wasmtime that referenced this pull request Aug 1, 2024
In bytecodealliance#8873, we stopped tracking the wasi-nn's upstream WIT files
temporarily because it was not clear to me at the time how to implement
errors as CM resources. This PR fixes that, resuming tracking in the
`vendor-wit.sh` and implementing what is needed in the wasi-nn crate.

This leaves several threads unresolved, though:
- it looks like the `vendor-wit.sh` has a new mechanism for retrieving
  files into `wit/deps`--at some point wasi-nn should migrate to use
  that (?)
- it's not clear to me that "errors as resources" is even the best
  approach here; I've opened [bytecodealliance#75] to consider the possibility of using
  "errors as records" instead.
- this PR identifies that the constructor for errors is in fact
  unnecessary, prompting an upstream change ([bytecodealliance#76]) that should
  eventually be implemented here.

[bytecodealliance#75]: WebAssembly/wasi-nn#75
[bytecodealliance#76]: WebAssembly/wasi-nn#76

prtest:full
abrown added a commit to abrown/wasmtime that referenced this pull request Aug 1, 2024
In bytecodealliance#8873, we stopped tracking the wasi-nn's upstream WIT files
temporarily because it was not clear to me at the time how to implement
errors as CM resources. This PR fixes that, resuming tracking in the
`vendor-wit.sh` and implementing what is needed in the wasi-nn crate.

This leaves several threads unresolved, though:
- it looks like the `vendor-wit.sh` has a new mechanism for retrieving
  files into `wit/deps`--at some point wasi-nn should migrate to use
  that (?)
- it's not clear to me that "errors as resources" is even the best
  approach here; I've opened [bytecodealliance#75] to consider the possibility of using
  "errors as records" instead.
- this PR identifies that the constructor for errors is in fact
  unnecessary, prompting an upstream change ([bytecodealliance#76]) that should
  eventually be implemented here.

[bytecodealliance#75]: WebAssembly/wasi-nn#75
[bytecodealliance#76]: WebAssembly/wasi-nn#76

prtest:full
abrown added a commit to abrown/wasmtime that referenced this pull request Aug 1, 2024
In bytecodealliance#8873, we stopped tracking the wasi-nn's upstream WIT files
temporarily because it was not clear to me at the time how to implement
errors as CM resources. This PR fixes that, resuming tracking in the
`vendor-wit.sh` and implementing what is needed in the wasi-nn crate.

This leaves several threads unresolved, though:
- it looks like the `vendor-wit.sh` has a new mechanism for retrieving
  files into `wit/deps`--at some point wasi-nn should migrate to use
  that (?)
- it's not clear to me that "errors as resources" is even the best
  approach here; I've opened [bytecodealliance#75] to consider the possibility of using
  "errors as records" instead.
- this PR identifies that the constructor for errors is in fact
  unnecessary, prompting an upstream change ([bytecodealliance#76]) that should
  eventually be implemented here.

[bytecodealliance#75]: WebAssembly/wasi-nn#75
[bytecodealliance#76]: WebAssembly/wasi-nn#76

prtest:full
github-merge-queue bot pushed a commit that referenced this pull request Aug 1, 2024
In #8873, we stopped tracking the wasi-nn's upstream WIT files
temporarily because it was not clear to me at the time how to implement
errors as CM resources. This PR fixes that, resuming tracking in the
`vendor-wit.sh` and implementing what is needed in the wasi-nn crate.

This leaves several threads unresolved, though:
- it looks like the `vendor-wit.sh` has a new mechanism for retrieving
  files into `wit/deps`--at some point wasi-nn should migrate to use
  that (?)
- it's not clear to me that "errors as resources" is even the best
  approach here; I've opened [#75] to consider the possibility of using
  "errors as records" instead.
- this PR identifies that the constructor for errors is in fact
  unnecessary, prompting an upstream change ([#76]) that should
  eventually be implemented here.

[#75]: WebAssembly/wasi-nn#75
[#76]: WebAssembly/wasi-nn#76

prtest:full
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