Skip to content

Commit 248e10b

Browse files
authored
nn-cli: add an option to use load_by_name (#4490)
by specifying a name for --load-graph. for example, ``` --load-graph=name=foo ```
1 parent 79408e5 commit 248e10b

File tree

1 file changed

+29
-10
lines changed
  • wamr-wasi-extensions/samples/nn-cli

1 file changed

+29
-10
lines changed

wamr-wasi-extensions/samples/nn-cli/main.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ load_graph(char *options)
3131
const char *id = "default";
3232
wasi_ephemeral_nn_graph_builder *builders = NULL;
3333
size_t nbuilders = 0;
34+
const char *name = NULL;
3435
enum {
3536
opt_id,
3637
opt_file,
38+
opt_name,
3739
opt_encoding,
3840
opt_target,
3941
};
4042
static char *const keylistp[] = {
41-
[opt_id] = "id",
42-
[opt_file] = "file",
43-
[opt_encoding] = "encoding",
44-
[opt_target] = "target",
45-
NULL,
43+
[opt_id] = "id", [opt_file] = "file",
44+
[opt_name] = "name", [opt_encoding] = "encoding",
45+
[opt_target] = "target", NULL,
4646
};
4747
while (*options) {
4848
extern char *suboptarg;
@@ -74,6 +74,13 @@ load_graph(char *options)
7474
exit(1);
7575
}
7676
break;
77+
case opt_name:
78+
if (value == NULL) {
79+
fprintf(stderr, "no value for %s\n", saved);
80+
exit(2);
81+
}
82+
name = value;
83+
break;
7784
case opt_encoding:
7885
if (value == NULL) {
7986
fprintf(stderr, "no value for %s\n", saved);
@@ -94,13 +101,25 @@ load_graph(char *options)
94101
}
95102
}
96103

104+
if (name != NULL && nbuilders != 0) {
105+
fprintf(stderr, "name and file are exclusive\n");
106+
exit(1);
107+
}
108+
97109
wasi_ephemeral_nn_error nnret;
98110
wasi_ephemeral_nn_graph g;
99-
nnret = wasi_ephemeral_nn_load(builders, nbuilders, encoding, target, &g);
100-
size_t i;
101-
for (i = 0; i < nbuilders; i++) {
102-
wasi_ephemeral_nn_graph_builder *b = &builders[i];
103-
unmap_file(b->buf, b->size);
111+
if (name != NULL) {
112+
/* we ignore encoding and target */
113+
nnret = wasi_ephemeral_nn_load_by_name(name, strlen(name), &g);
114+
}
115+
else {
116+
nnret =
117+
wasi_ephemeral_nn_load(builders, nbuilders, encoding, target, &g);
118+
size_t i;
119+
for (i = 0; i < nbuilders; i++) {
120+
wasi_ephemeral_nn_graph_builder *b = &builders[i];
121+
unmap_file(b->buf, b->size);
122+
}
104123
}
105124
if (nnret != wasi_ephemeral_nn_error_success) {
106125
fprintf(stderr, "load failed with %d\n", (int)nnret);

0 commit comments

Comments
 (0)