Skip to content

Conversation

@zhanheng1
Copy link
Contributor

add a set of apis to configure wasi-nn via InstantiationArgs2:

  1. Add a new command line option for wasi-nn: --wasi-nn-graph=encoding:target:model_path1:model_path2:...:model_pathN;
  2. Add new structs to manage this cmd option: WASINNGlobalContext and wasi_nn_graph_registry;
  3. Add 2 new parameters for load_by_name(encoding and target). load_by_name() can get encoding and target from WASINNGlobalContext and set into backend.
  4. Update test codes.

@zhanheng1 zhanheng1 force-pushed the InstantiationArgs2-wasinn branch from 77794ae to c73e4aa Compare December 19, 2025 07:07
@zhanheng1 zhanheng1 marked this pull request as ready for review December 23, 2025 01:52
}

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
WASINNArguments;
Copy link
Contributor

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),
Copy link
Contributor

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);
Copy link
Contributor

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,
Copy link
Contributor

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 {
Copy link
Contributor

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"?

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; ......
Copy link
Contributor

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);
Copy link
Contributor

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:......
Copy link
Contributor

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.

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