Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
96cdfa6
Add the way to set the target evenif we use load_by_name
Dec 10, 2025
60a8011
Add a new error check for wasi_nn_load_by_name
Dec 10, 2025
6dc9d01
Use clang-format-18 to format source files
zhanheng1 Dec 11, 2025
d42581b
Free model_name
zhanheng1 Dec 11, 2025
1cca0b8
Add new errno for new test cases
zhanheng1 Dec 16, 2025
9e03970
Fix bugs
zhanheng1 Dec 17, 2025
cd1c7f9
Rename some parameters
zhanheng1 Dec 19, 2025
c73e4aa
Revert tensorflow version
zhanheng1 Dec 19, 2025
0234fe0
CICD: retrigger checks
zhanheng1 Jan 6, 2026
4b93110
Remove internal function declarations from wasm_export.h
zhanheng1 Jan 23, 2026
6bc7512
Move wasi_nn parameters parse logic into libc_wasi.c
zhanheng1 Jan 23, 2026
12d8a18
Allow multiple --wasi-nn-graphs
zhanheng1 Jan 26, 2026
8dbba46
Use clang-format-18 to format files
zhanheng1 Jan 26, 2026
c024c94
CICD: retrigger checks
zhanheng1 Jan 26, 2026
736f357
Add model_name option for --wasi-nn-graphs to make it more flexible a…
zhanheng1 Jan 27, 2026
878c8ec
Add help info for wasi-nn test wasm
zhanheng1 Jan 27, 2026
c43fbeb
CICD: retrigger checks
zhanheng1 Jan 27, 2026
6501e4b
Use a specific error type wasi_nn_error_t instead of macro "WASI_NN_E…
zhanheng1 Feb 5, 2026
b122451
Unify WASINNGlobalContext and WASINNArguments into a single structure…
zhanheng1 Feb 5, 2026
a85079a
Check integer overflow for wasm_runtime_wasi_nn_registry_set_args
zhanheng1 Feb 5, 2026
7b62ec8
Remove strdup in wasi_nn_parse
zhanheng1 Feb 5, 2026
01641bf
Remove "encoding" for load_by_name
zhanheng1 Feb 5, 2026
eb55c28
Remove some internal functions used by wasi-nn from wasm_export.h
zhanheng1 Feb 6, 2026
ccee194
Removed unnecessary conditional checks.
zhanheng1 Feb 6, 2026
5357fb5
Move the error checks to an earlier stage.
zhanheng1 Feb 6, 2026
4747d61
Use clang-format-18 to format source file
zhanheng1 Feb 6, 2026
9e89828
Make wasi_nn_load_by_name and wasi_nn_load_by_name_with_config share …
zhanheng1 Feb 6, 2026
7822544
Fix compilation errors for nuttx platform.
zhanheng1 Feb 6, 2026
4aa5d19
use wasi_ephemeral_nn_* prefix in all test files.
zhanheng1 Mar 3, 2026
e98069e
CICD: retrigger checks
zhanheng1 Mar 3, 2026
59ed72e
CICD: retrigger checks
zhanheng1 Mar 3, 2026
c015ebe
Remove unsupported_target which is not supported in wasi-nn proposal.
zhanheng1 Mar 12, 2026
f055a50
Keep the legacy test apis for now. Maybe it is not a proper time to o…
zhanheng1 Mar 13, 2026
9790d34
Use clang-format to format source files
zhanheng1 Mar 13, 2026
9d8e31a
CICD: retrigger checks
zhanheng1 Mar 13, 2026
cc9520d
Use correct type when calling wasm_runtime_malloc for module_names an…
zhanheng1 Mar 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions core/iwasm/common/wasm_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ static NativeSymbolsList g_native_symbols_list = NULL;
static void *g_wasi_context_key;
#endif /* WASM_ENABLE_LIBC_WASI */

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
static void *g_wasi_nn_context_key;
#endif

uint32
get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);

Expand Down Expand Up @@ -473,6 +477,32 @@ wasi_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
}
#endif /* end of WASM_ENABLE_LIBC_WASI */

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
WASINNGlobalContext *
wasm_runtime_get_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst_comm)
{
return wasm_native_get_context(module_inst_comm, g_wasi_nn_context_key);
}

void
wasm_runtime_set_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst_comm,
WASINNGlobalContext *wasi_nn_ctx)
{
wasm_native_set_context(module_inst_comm, g_wasi_nn_context_key,
wasi_nn_ctx);
}

static void
wasi_nn_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
{
if (ctx == NULL) {
return;
}

wasm_runtime_destroy_wasi_nn_global_ctx(inst);
}
#endif

#if WASM_ENABLE_QUICK_AOT_ENTRY != 0
static bool
quick_aot_entry_init(void);
Expand Down Expand Up @@ -582,6 +612,12 @@ wasm_native_init()
#endif /* WASM_ENABLE_LIB_RATS */

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
g_wasi_nn_context_key =
wasm_native_create_context_key(wasi_nn_context_dtor);
if (g_wasi_nn_context_key == NULL) {
goto fail;
}

if (!wasi_nn_initialize())
goto fail;

Expand Down Expand Up @@ -648,6 +684,10 @@ wasm_native_destroy()
#endif

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
if (g_wasi_nn_context_key != NULL) {
wasm_native_destroy_context_key(g_wasi_nn_context_key);
g_wasi_nn_context_key = NULL;
}
wasi_nn_destroy();
#endif

Expand Down
220 changes: 220 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,83 @@ wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
}
#endif /* WASM_ENABLE_LIBC_WASI != 0 */

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
typedef struct WASINNArguments WASINNArguments;

void
wasm_runtime_wasi_nn_graph_registry_args_set_defaults(WASINNArguments *args)
{
memset(args, 0, sizeof(*args));
}

bool
wasi_nn_graph_registry_set_args(WASINNArguments *registry,
const char **model_names, const char **encoding,
const char **target, uint32_t n_graphs,
const char **graph_paths)
{
if (!registry || !model_names || !encoding || !target || !graph_paths) {
return false;
}

registry->n_graphs = n_graphs;
registry->target = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
registry->encoding = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
registry->model_names = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
registry->graph_paths = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
memset(registry->target, 0, sizeof(uint32_t *) * n_graphs);
memset(registry->encoding, 0, sizeof(uint32_t *) * n_graphs);
memset(registry->model_names, 0, sizeof(uint32_t *) * n_graphs);
memset(registry->graph_paths, 0, sizeof(uint32_t *) * n_graphs);

for (uint32_t i = 0; i < registry->n_graphs; i++) {
registry->graph_paths[i] = strdup(graph_paths[i]);
registry->model_names[i] = strdup(model_names[i]);
registry->encoding[i] = strdup(encoding[i]);
registry->target[i] = strdup(target[i]);
}

return true;
}

int
wasi_nn_graph_registry_create(WASINNArguments **registryp)
{
WASINNArguments *args = wasm_runtime_malloc(sizeof(*args));
if (args == NULL) {
return -1;
}
wasm_runtime_wasi_nn_graph_registry_args_set_defaults(args);
*registryp = args;
return 0;
}

void
wasi_nn_graph_registry_destroy(WASINNArguments *registry)
{
if (registry) {
for (uint32_t i = 0; i < registry->n_graphs; i++)
if (registry->graph_paths[i]) {
free(registry->graph_paths[i]);
if (registry->model_names[i])
free(registry->model_names[i]);
if (registry->encoding[i])
free(registry->encoding[i]);
if (registry->target[i])
free(registry->target[i]);
}
free(registry);
}
}

void
wasm_runtime_instantiation_args_set_wasi_nn_graph_registry(
struct InstantiationArgs2 *p, WASINNArguments *registry)
{
p->nn_registry = *registry;
}
#endif

WASMModuleInstanceCommon *
wasm_runtime_instantiate_ex2(WASMModuleCommon *module,
const struct InstantiationArgs2 *args,
Expand Down Expand Up @@ -8080,3 +8157,146 @@ wasm_runtime_check_and_update_last_used_shared_heap(
return false;
}
#endif

#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
bool
wasm_runtime_init_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst,
const char **model_names,
const char **encoding, const char **target,
const uint32_t n_graphs,
char *graph_paths[], char *error_buf,
uint32_t error_buf_size)
{
WASINNGlobalContext *ctx;
bool ret = false;

ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
if (!ctx)
return false;

ctx->n_graphs = n_graphs;

ctx->encoding = (uint32_t *)malloc(sizeof(uint32_t) * n_graphs);
memset(ctx->encoding, 0, sizeof(uint32_t) * n_graphs);
ctx->target = (uint32_t *)malloc(sizeof(uint32_t) * n_graphs);
memset(ctx->target, 0, sizeof(uint32_t) * n_graphs);
ctx->loaded = (uint32_t *)malloc(sizeof(uint32_t) * n_graphs);
memset(ctx->loaded, 0, sizeof(uint32_t) * n_graphs);
ctx->model_names = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
memset(ctx->model_names, 0, sizeof(uint32_t *) * n_graphs);
ctx->graph_paths = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
memset(ctx->graph_paths, 0, sizeof(uint32_t *) * n_graphs);

for (uint32_t i = 0; i < n_graphs; i++) {
ctx->graph_paths[i] = strdup(graph_paths[i]);
ctx->model_names[i] = strdup(model_names[i]);
ctx->target[i] = strdup(target[i]);
ctx->encoding[i] = strdup(encoding[i]);
}

wasm_runtime_set_wasi_nn_global_ctx(module_inst, ctx);

ret = true;

return ret;
}

void
wasm_runtime_destroy_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst)
{
WASINNGlobalContext *wasi_nn_global_ctx =
wasm_runtime_get_wasi_nn_global_ctx(module_inst);

for (uint32 i = 0; i < wasi_nn_global_ctx->n_graphs; i++) {
// All graphs will be unregistered in deinit()
if (wasi_nn_global_ctx->graph_paths[i])
free(wasi_nn_global_ctx->graph_paths[i]);
if (wasi_nn_global_ctx->model_names[i])
free(wasi_nn_global_ctx->model_names[i]);
if (wasi_nn_global_ctx->encoding[i])
free(wasi_nn_global_ctx->encoding[i]);
if (wasi_nn_global_ctx->target[i])
free(wasi_nn_global_ctx->target[i]);
}
free(wasi_nn_global_ctx->encoding);
free(wasi_nn_global_ctx->target);
free(wasi_nn_global_ctx->loaded);
free(wasi_nn_global_ctx->model_names);
free(wasi_nn_global_ctx->graph_paths);

if (wasi_nn_global_ctx) {
wasm_runtime_free(wasi_nn_global_ctx);
}
}

uint32_t
wasm_runtime_get_wasi_nn_global_ctx_ngraphs(
WASINNGlobalContext *wasi_nn_global_ctx)
{
if (wasi_nn_global_ctx)
return wasi_nn_global_ctx->n_graphs;

return -1;
}

char *
wasm_runtime_get_wasi_nn_global_ctx_model_names_i(
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
{
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
return wasi_nn_global_ctx->model_names[idx];

return NULL;
}

char *
wasm_runtime_get_wasi_nn_global_ctx_graph_paths_i(
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
{
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
return wasi_nn_global_ctx->graph_paths[idx];

return NULL;
}

uint32_t
wasm_runtime_get_wasi_nn_global_ctx_loaded_i(
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
{
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
return wasi_nn_global_ctx->loaded[idx];

return -1;
}

uint32_t
wasm_runtime_set_wasi_nn_global_ctx_loaded_i(
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx, uint32_t value)
{
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
wasi_nn_global_ctx->loaded[idx] = value;

return 0;
}

char *
wasm_runtime_get_wasi_nn_global_ctx_encoding_i(
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
{
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
return wasi_nn_global_ctx->encoding[idx];

return NULL;
}

char *
wasm_runtime_get_wasi_nn_global_ctx_target_i(
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
{
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
return wasi_nn_global_ctx->target[idx];

return NULL;
}

#endif
Loading
Loading