Skip to content

Commit b3ce192

Browse files
authored
add nn-cli example (#4373)
an example application with flexible cli options which aims to allow us to perform any wasi-nn operations. eg. ``` --load-graph=file=fixture/model.xml,file=fixture/model.bin,id=graph --init-execution-context=graph-id=graph,id=ctx --set-input=file=fixture/tensor.bgr,context-id=ctx,dim=1,dim=3,dim=224,dim=224 --compute=context-id=ctx --get-output=context-id=ctx,file=output.bin ```
1 parent c9b8c16 commit b3ce192

File tree

7 files changed

+678
-0
lines changed

7 files changed

+678
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (C) 2025 Midokura Japan KK. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
cmake_minimum_required(VERSION 3.14)
5+
6+
set(CMAKE_C_STANDARD 99)
7+
set(CMAKE_C_STANDARD_REQUIRED YES)
8+
9+
project(nn-cli LANGUAGES C)
10+
add_executable(nn-cli main.c fileio.c map.c)
11+
find_package(wamr-wasi-nn REQUIRED)
12+
target_link_libraries(nn-cli wamr-wasi-nn)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2025 Midokura Japan KK. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
/*
7+
* modified copy-and-paste from:
8+
* https://github.com/yamt/toywasm/blob/0eaad8cacd0cc7692946ff19b25994f106113be8/lib/fileio.c
9+
*/
10+
11+
#include <sys/stat.h>
12+
13+
#include <assert.h>
14+
#include <errno.h>
15+
#include <fcntl.h>
16+
#include <stdlib.h>
17+
#include <unistd.h>
18+
19+
#include "fileio.h"
20+
21+
int
22+
map_file(const char *path, void **pp, size_t *sizep)
23+
{
24+
void *p;
25+
size_t size;
26+
ssize_t ssz;
27+
int fd;
28+
int ret;
29+
30+
fd = open(path, O_RDONLY);
31+
if (fd == -1) {
32+
ret = errno;
33+
assert(ret != 0);
34+
return ret;
35+
}
36+
struct stat st;
37+
ret = fstat(fd, &st);
38+
if (ret == -1) {
39+
ret = errno;
40+
assert(ret != 0);
41+
close(fd);
42+
return ret;
43+
}
44+
size = st.st_size;
45+
if (size > 0) {
46+
p = malloc(size);
47+
}
48+
else {
49+
/* Avoid a confusing error */
50+
p = malloc(1);
51+
}
52+
if (p == NULL) {
53+
close(fd);
54+
return ENOMEM;
55+
}
56+
ssz = read(fd, p, size);
57+
if (ssz != size) {
58+
ret = errno;
59+
assert(ret != 0);
60+
close(fd);
61+
return ret;
62+
}
63+
close(fd);
64+
*pp = p;
65+
*sizep = size;
66+
return 0;
67+
}
68+
69+
void
70+
unmap_file(void *p, size_t sz)
71+
{
72+
free(p);
73+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (C) 2025 Midokura Japan KK. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
/*
7+
* modified copy-and-paste from:
8+
* https://github.com/yamt/toywasm/blob/0eaad8cacd0cc7692946ff19b25994f106113be8/lib/fileio.h
9+
*/
10+
11+
int
12+
map_file(const char *filename, void **pp, size_t *szp);
13+
void
14+
unmap_file(void *p, size_t sz);

0 commit comments

Comments
 (0)