mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2026-04-18 18:18:44 +00:00
Move wasi_nn parameters parse logic into libc_wasi.c
This commit is contained in:
parent
4b93110b56
commit
6bc75126be
|
|
@ -1696,65 +1696,6 @@ wasm_runtime_instantiation_args_destroy(struct InstantiationArgs2 *p)
|
|||
wasm_runtime_free(p);
|
||||
}
|
||||
|
||||
#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 *encoding,
|
||||
const char *target, uint32_t n_graphs,
|
||||
const char **graph_paths)
|
||||
{
|
||||
if (!registry || !encoding || !target || !graph_paths) {
|
||||
return false;
|
||||
}
|
||||
registry->encoding = strdup(encoding);
|
||||
registry->target = strdup(target);
|
||||
registry->n_graphs = n_graphs;
|
||||
registry->graph_paths = (uint32_t **)malloc(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]);
|
||||
|
||||
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]) {
|
||||
// wasi_nn_graph_registry_unregister_graph(registry,
|
||||
// registry->name[i]);
|
||||
free(registry->graph_paths[i]);
|
||||
}
|
||||
if (registry->encoding)
|
||||
free(registry->encoding);
|
||||
if (registry->target)
|
||||
free(registry->target);
|
||||
free(registry);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_default_stack_size(
|
||||
struct InstantiationArgs2 *p, uint32 v)
|
||||
|
|
@ -1853,7 +1794,65 @@ wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
|
|||
wasi_args->set_by_user = true;
|
||||
}
|
||||
#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 *encoding,
|
||||
const char *target, uint32_t n_graphs,
|
||||
const char **graph_paths)
|
||||
{
|
||||
if (!registry || !encoding || !target || !graph_paths) {
|
||||
return false;
|
||||
}
|
||||
registry->encoding = strdup(encoding);
|
||||
registry->target = strdup(target);
|
||||
registry->n_graphs = n_graphs;
|
||||
registry->graph_paths = (uint32_t **)malloc(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]);
|
||||
|
||||
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]) {
|
||||
// wasi_nn_graph_registry_unregister_graph(registry,
|
||||
// registry->name[i]);
|
||||
free(registry->graph_paths[i]);
|
||||
}
|
||||
if (registry->encoding)
|
||||
free(registry->encoding);
|
||||
if (registry->target)
|
||||
free(registry->target);
|
||||
free(registry);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wasm_runtime_instantiation_args_set_wasi_nn_graph_registry(
|
||||
struct InstantiationArgs2 *p, WASINNArguments *registry)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,13 @@ typedef struct {
|
|||
uint32 ns_lookup_pool_size;
|
||||
} libc_wasi_parse_context_t;
|
||||
|
||||
typedef struct {
|
||||
const char *encoding;
|
||||
const char *target;
|
||||
const char *graph_paths[10];
|
||||
uint32 n_graphs;
|
||||
} wasi_nn_parse_context_t;
|
||||
|
||||
typedef enum {
|
||||
LIBC_WASI_PARSE_RESULT_OK = 0,
|
||||
LIBC_WASI_PARSE_RESULT_NEED_HELP,
|
||||
|
|
@ -177,3 +184,58 @@ libc_wasi_set_init_args(struct InstantiationArgs2 *args, int argc, char **argv,
|
|||
wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
|
||||
args, ctx->ns_lookup_pool, ctx->ns_lookup_pool_size);
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
libc_wasi_parse_result_t
|
||||
wasi_nn_parse(char **argv, wasi_nn_parse_context_t *ctx)
|
||||
{
|
||||
char *token;
|
||||
char *saveptr = NULL;
|
||||
int token_count = 0, ret = 0;
|
||||
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:......
|
||||
token = strtok_r(argv[0] + 16, ":", &saveptr);
|
||||
while (token) {
|
||||
tokens[token_count] = token;
|
||||
token_count++;
|
||||
token = strtok_r(NULL, ":", &saveptr);
|
||||
}
|
||||
|
||||
if (token_count < 2) {
|
||||
ret = LIBC_WASI_PARSE_RESULT_NEED_HELP;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ctx->n_graphs = token_count - 2;
|
||||
ctx->encoding = strdup(tokens[0]);
|
||||
ctx->target = strdup(tokens[1]);
|
||||
for (int i = 0; i < ctx->n_graphs; i++) {
|
||||
ctx->graph_paths[i] = strdup(tokens[i + 2]);
|
||||
}
|
||||
|
||||
fail:
|
||||
if (token)
|
||||
free(token);
|
||||
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
wasi_nn_set_init_args(struct InstantiationArgs2 *args, struct WASINNArguments *nn_registry, wasi_nn_parse_context_t *ctx)
|
||||
{
|
||||
wasi_nn_graph_registry_create(&nn_registry);
|
||||
wasi_nn_graph_registry_set_args(nn_registry, ctx->encoding, ctx->target, ctx->n_graphs,
|
||||
ctx->graph_paths);
|
||||
wasm_runtime_instantiation_args_set_wasi_nn_graph_registry(args,
|
||||
nn_registry);
|
||||
|
||||
for (uint32_t i = 0; i < ctx->n_graphs; i++)
|
||||
if (ctx->graph_paths[i])
|
||||
free(ctx->graph_paths[i]);
|
||||
free(ctx->encoding);
|
||||
free(ctx->target);
|
||||
}
|
||||
#endif
|
||||
|
|
@ -648,10 +648,10 @@ main(int argc, char *argv[])
|
|||
#endif
|
||||
|
||||
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
wasi_nn_parse_context_t wasi_nn_parse_ctx;
|
||||
struct WASINNArguments *nn_registry;
|
||||
char *encoding, *target;
|
||||
uint32_t n_models = 0;
|
||||
char **model_paths;
|
||||
|
||||
memset(&wasi_nn_parse_ctx, 0, sizeof(wasi_nn_parse_ctx));
|
||||
#endif
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
|
|
@ -844,35 +844,18 @@ main(int argc, char *argv[])
|
|||
wasm_proposal_print_status();
|
||||
return 0;
|
||||
}
|
||||
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
#if WASM_ENABLE_LIBC_WASI != 0 && (WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0)
|
||||
else if (!strncmp(argv[0], "--wasi-nn-graph=", 16)) {
|
||||
char *token;
|
||||
char *saveptr = NULL;
|
||||
int token_count = 0;
|
||||
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:......
|
||||
token = strtok_r(argv[0] + 16, ":", &saveptr);
|
||||
while (token) {
|
||||
tokens[token_count] = token;
|
||||
token_count++;
|
||||
token = strtok_r(NULL, ":", &saveptr);
|
||||
libc_wasi_parse_result_t result =
|
||||
wasi_nn_parse(argv, &wasi_nn_parse_ctx);
|
||||
switch (result) {
|
||||
case LIBC_WASI_PARSE_RESULT_OK:
|
||||
continue;
|
||||
case LIBC_WASI_PARSE_RESULT_NEED_HELP:
|
||||
return print_help();
|
||||
case LIBC_WASI_PARSE_RESULT_BAD_PARAM:
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (token_count < 2) {
|
||||
return print_help();
|
||||
}
|
||||
|
||||
n_models = token_count - 2;
|
||||
encoding = strdup(tokens[0]);
|
||||
target = strdup(tokens[1]);
|
||||
model_paths = malloc(n_models * sizeof(void *));
|
||||
for (int i = 0; i < n_models; i++) {
|
||||
model_paths[i] = strdup(tokens[i + 2]);
|
||||
}
|
||||
if (token)
|
||||
free(token);
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
|
|
@ -1025,11 +1008,7 @@ main(int argc, char *argv[])
|
|||
#endif
|
||||
|
||||
#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);
|
||||
wasm_runtime_instantiation_args_set_wasi_nn_graph_registry(inst_args,
|
||||
nn_registry);
|
||||
wasi_nn_set_init_args(inst_args, nn_registry, &wasi_nn_parse_ctx);
|
||||
#endif
|
||||
/* instantiate the module */
|
||||
wasm_module_inst = wasm_runtime_instantiate_ex2(
|
||||
|
|
@ -1149,15 +1128,6 @@ fail5:
|
|||
#endif
|
||||
#if WASM_ENABLE_DEBUG_INTERP != 0
|
||||
fail4:
|
||||
#endif
|
||||
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
|
||||
wasi_nn_graph_registry_destroy(nn_registry);
|
||||
for (uint32_t i = 0; i < n_models; i++)
|
||||
if (model_paths[i])
|
||||
free(model_paths[i]);
|
||||
free(model_paths);
|
||||
free(encoding);
|
||||
free(target);
|
||||
#endif
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user