-
Notifications
You must be signed in to change notification settings - Fork 793
Add "load_by_name" API at wasi-nn #4267
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
Changes from 3 commits
1df9b7c
c5414fd
ff4ed2f
2831ff2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,14 +85,11 @@ is_valid_graph(TFLiteContext *tfl_ctx, graph g) | |
| NN_ERR_PRINTF("Invalid graph: %d >= %d.", g, MAX_GRAPHS_PER_INST); | ||
| return runtime_error; | ||
| } | ||
| if (tfl_ctx->models[g].model_pointer == NULL) { | ||
| if (tfl_ctx->models[g].model_pointer == NULL | ||
| && tfl_ctx->models[g].model == NULL) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original version can output different information based on various invalid argument cases. Is there a specific reason we need to merge them?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If call load_by_name(), there is no need to save the tflite buf to model_pointer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why not free model_point after below operation in load()
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is required to validate
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whether it is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why is that?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. after this operation, the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /// Builds a model based on a pre-loaded flatbuffer.
/// Caller retains ownership of the buffer and should keep it alive until
/// the returned object is destroyed. Caller also retains ownership of
/// `error_reporter` and must ensure its lifetime is longer than the
/// FlatBufferModelBase instance.
/// Returns a nullptr in case of failure.
/// NOTE: this does NOT validate the buffer so it should NOT be called on
/// invalid/untrusted input. Use VerifyAndBuildFromBuffer in that case
static std::unique_ptr<T> BuildFromBuffer(
const char* caller_owned_buffer, size_t buffer_size,
ErrorReporter* error_reporter = T::GetDefaultErrorReporter()) {
error_reporter = ValidateErrorReporter(error_reporter);
std::unique_ptr<Allocation> allocation(
new MemoryAllocation(caller_owned_buffer, buffer_size, error_reporter));
return BuildFromAllocation(std::move(allocation), error_reporter);
}If I understand correctly, |
||
| NN_ERR_PRINTF("Context (model) non-initialized."); | ||
| return runtime_error; | ||
| } | ||
| if (tfl_ctx->models[g].model == NULL) { | ||
| NN_ERR_PRINTF("Context (tflite model) non-initialized."); | ||
| return runtime_error; | ||
| } | ||
| return success; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,7 @@ wasm_load(char *model_name, graph *g, execution_target target) | |
| wasi_nn_error | ||
| wasm_load_by_name(const char *model_name, graph *g) | ||
| { | ||
| wasi_nn_error res = load_by_name(model_name, g); | ||
| wasi_nn_error res = load_by_name(model_name, strlen(model_name), g); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better be #if WASM_ENABLE_WASI_EPHEMERAL_NN != 0
wasi_nn_error
wasm_load_by_name(const char *model_name, graph *g)
{
wasi_nn_error res = load_by_name(model_name, strlen(model_name), g);
return res;
}
#endif |
||
| return res; | ||
| } | ||
|
|
||
|
|
@@ -108,7 +108,12 @@ run_inference(execution_target target, float *input, uint32_t *input_size, | |
| uint32_t num_output_tensors) | ||
| { | ||
| graph graph; | ||
|
|
||
| #if WASM_ENABLE_WASI_EPHEMERAL_NN == 0 | ||
| if (wasm_load(model_name, &graph, target) != success) { | ||
| #else | ||
| if (wasm_load_by_name(model_name, &graph) != success) { | ||
| #endif | ||
| NN_ERR_PRINTF("Error when loading model."); | ||
| exit(1); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a bug fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wasi_nn.h is a header for WebAssembly applications written in the C language. Is there a specific reason that we need to change it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, its a bugfix,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have two sets of APIs for historical reasons, we might remove one in another PR. For now, let's ensure both are functional.
WASM_ENABLE_WASI_EPHEMERAL_NNon the wasm side.native_symbols_wasi_nnin wasi_nn.cThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lum1n0us
Where can I find the function prototypes for
wasi_ephemeral_nn?For functions like
get_output, the signatures are different, so simply replacing wasi_nn with wasi_ephemeral_nn mechanically doesn't seem to work.wasm-micro-runtime/core/iwasm/libraries/wasi-nn/src/wasi_nn.c
Lines 690 to 704 in c018b8a
Even when referring to the wasi-nn specification, the signatures declared there don’t appear to match what is used for wasi_ephemeral_nn
https://github.com/WebAssembly/wasi-nn/blob/71320d95b8c6d43f9af7f44e18b1839db85d89b4/wasi-nn.witx#L59-L86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HongxiaWangSSSS I suggest we use
WASM_ENABLE_WASI_EPHEMERAL_NNon the wasm side and there will be two sets for wasi_ephemeral_nn and wasi_nn. wasi_nn will be deprecated in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a mismatch in the function signature?
wasm-micro-runtime/core/iwasm/libraries/wasi-nn/src/wasi_nn.c
Line 697 in c018b8a
and
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YES. there should be two versions, one for wasi_ephemeral_nn, another for wasi_nn. please refer to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If so, we need to define it at wasi_nn.h
But the backend definition doesn't look like it matches (signature might be ok)
wasm-micro-runtime/core/iwasm/libraries/wasi-nn/src/wasi_nn_tensorflowlite.cpp
Lines 366 to 368 in c018b8a
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I am suggestion this #4267 (comment) as the new content of wasi_nn.h. Plus, #4267 (comment).