-
Notifications
You must be signed in to change notification settings - Fork 750
add a set of apis to configure wasi-nn via InstantiationArgs2 #4764
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
base: main
Are you sure you want to change the base?
add a set of apis to configure wasi-nn via InstantiationArgs2 #4764
Conversation
77794ae to
c73e4aa
Compare
| } | ||
|
|
||
| #if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 | ||
| WASINNArguments; |
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_ERROR_NAME(unsupported_operation), | ||
| WASI_NN_ERROR_NAME(too_large), | ||
| WASI_NN_ERROR_NAME(not_found), | ||
| WASI_NN_ERROR_NAME(not_loaded), |
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.
please don't make abi changes lightly.
| __attribute__((visibility("default"))) wasi_nn_error | ||
| load_by_name(void *tflite_ctx, const char *name, uint32_t namelen, graph *g); | ||
| load_by_name(void *tflite_ctx, const char *name, uint32_t namelen, | ||
| graph_encoding encoding, execution_target target, graph *g); |
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.
why do you want to add encoding argument?
| uint32_t ns_lookup_pool_size); | ||
|
|
||
| WASM_RUNTIME_API_EXTERN bool | ||
| wasm_runtime_init_wasi_nn_global_ctx(wasm_module_inst_t module_inst, |
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.
please consider to use InstantiationArgs2 instead of adding new user-facing api configuration based on instance.
| } WASMModuleInstMemConsumption; | ||
|
|
||
| #if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 | ||
| typedef struct WASINNGlobalContext { |
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.
in what sense is this "global"?
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.
InstantiationArgs2 will be freed soon after wasm_runtime_instantiate_ex2()(in posix/main.c for example), and user cannot use InstantiationArgs2 in wasm app later.
So I add this new struct to handle these command line options, so load_by_name() can get these information(like target, encoding) through WASINNGlobalContext.
WASINNGlobalContext will be registered into WASMModuleInstanceExtraCommon->context by wasm_native_set_context() after init.
| wasm_runtime_get_wasi_nn_global_ctx_ngraphs(wasi_nn_global_ctx); | ||
| // Assume filename got from user wasm app : max; sum; average; ... | ||
| // Assume file path got from user cmd opt: /your/path1/max.tflite; | ||
| // /your/path2/sum.tflite; ...... |
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.
i don't fully understand what this super complex logic is doing.
i'd say it's simpler and more flexible to let user give the name explicitly when adding a model to a registry.
| #if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0 | ||
| wasi_nn_graph_registry_create(&nn_registry); | ||
| wasi_nn_graph_registry_set_args(nn_registry, encoding, target, n_models, | ||
| model_paths); |
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.
it's better to allow multiple --wasi-nn-graph=.
eg. if you have two --wasi-nn-graph=, the registry would contain two models.
--wasi-nn-graph=name=modelA,encoding=...,target=....
--wasi-nn-graph=name=modelB,encoding=...,target=....
conceptually a registry would be a map indexed by a string.
{
"modelA" -> Model(encodingA, targetA, filelistA, ...),
"modelB" -> Model(encodingB, targetB, filelistB, ...),
}
when a wasm module calls load-by-name("modelA"), it will get Model(encodingA, targetA, filelistA, ...).
| char *tokens[12] = { 0 }; | ||
|
|
||
| // encoding:tensorflowlite|openvino|llama target:cpu|gpu|tpu | ||
| // --wasi-nn-graph=encoding:target:model_file_path1:model_file_path2:model_file_path3:...... |
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.
complex argument parsing logic like this can be in libc_wasi.c so that it's shared among platforms.
add a set of apis to configure wasi-nn via InstantiationArgs2: