Skip to content

Commit b2175e7

Browse files
committed
wamr-wasi-extensions
tested with * wasmtime * iwasm with bytecodealliance#4308
1 parent 6b8a0ae commit b2175e7

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

wamr-wasi-extensions/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
project(wamr-wasi-extensions LANGUAGES C)
7+
8+
add_subdirectory(nn)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (C) 2025 Midokura Japan KK. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
add_library(wamr-wasi-nn INTERFACE)
5+
6+
set(wasi_nn_header_dir ${CMAKE_CURRENT_SOURCE_DIR}/../../core/iwasm/libraries/wasi-nn/include)
7+
8+
set(headers
9+
${wasi_nn_header_dir}/wasi_ephemeral_nn.h
10+
${wasi_nn_header_dir}/wasi_nn.h
11+
${wasi_nn_header_dir}/wasi_nn_types.h
12+
)
13+
14+
set_property(TARGET wamr-wasi-nn PROPERTY PUBLIC_HEADER ${headers})
15+
16+
target_include_directories(wamr-wasi-nn
17+
INTERFACE
18+
$<BUILD_INTERFACE:${wasi_nn_header_dir}>
19+
$<INSTALL_INTERFACE:include>)
20+
21+
install(TARGETS wamr-wasi-nn
22+
EXPORT wamr-wasi-nn-config
23+
PUBLIC_HEADER DESTINATION include/wamr)
24+
install(EXPORT wamr-wasi-nn-config
25+
DESTINATION lib/cmake/wamr-wasi-nn)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
set(CMAKE_C_EXTENSIONS NO)
9+
10+
project(nn-classification-openvino LANGUAGES C)
11+
add_executable(nn-classification-openvino "app.c")
12+
find_package(wamr-wasi-nn REQUIRED)
13+
target_link_libraries(nn-classification-openvino wamr-wasi-nn)

wamr-wasi-extensions/samples/nn/app.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (C) 2025 Midokura Japan KK. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
*/
5+
6+
#include <sys/stat.h>
7+
8+
#include <assert.h>
9+
#include <errno.h>
10+
#include <fcntl.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <unistd.h>
15+
16+
#include <wamr/wasi_ephemeral_nn.h>
17+
18+
/*
19+
* what this application does is basically same as:
20+
* https://github.com/bytecodealliance/wasmtime/tree/efa236e58d09570baaf27865da33fb852fcf40a5/crates/wasi-nn/examples/classification-example
21+
*
22+
* map_file/unmap_file are copy-and-pasted from:
23+
* https://github.com/yamt/toywasm/blob/0eaad8cacd0cc7692946ff19b25994f106113be8/lib/fileio.c
24+
*/
25+
26+
int
27+
map_file(const char *path, void **pp, size_t *sizep)
28+
{
29+
void *p;
30+
size_t size;
31+
ssize_t ssz;
32+
int fd;
33+
int ret;
34+
35+
fd = open(path, O_RDONLY);
36+
if (fd == -1) {
37+
ret = errno;
38+
assert(ret != 0);
39+
return ret;
40+
}
41+
struct stat st;
42+
ret = fstat(fd, &st);
43+
if (ret == -1) {
44+
ret = errno;
45+
assert(ret != 0);
46+
close(fd);
47+
return ret;
48+
}
49+
size = st.st_size;
50+
if (size > 0) {
51+
p = malloc(size);
52+
}
53+
else {
54+
/* Avoid a confusing error */
55+
p = malloc(1);
56+
}
57+
if (p == NULL) {
58+
close(fd);
59+
return ENOMEM;
60+
}
61+
ssz = read(fd, p, size);
62+
if (ssz != size) {
63+
ret = errno;
64+
assert(ret != 0);
65+
close(fd);
66+
return ret;
67+
}
68+
close(fd);
69+
*pp = p;
70+
*sizep = size;
71+
return 0;
72+
}
73+
74+
void
75+
unmap_file(void *p, size_t sz)
76+
{
77+
free(p);
78+
}
79+
80+
static void
81+
print_result(const float *result, size_t sz)
82+
{
83+
/*
84+
* just dump the raw result.
85+
* you can postprocess the output with eg. "sort -k2nr | head"
86+
*/
87+
int i;
88+
for (i = 0; i < sz / sizeof(float); i++) {
89+
printf("%d %f\n", i, result[i]);
90+
}
91+
}
92+
93+
int
94+
main(int argc, char **argv)
95+
{
96+
wasi_nn_error nnret;
97+
int ret;
98+
void *xml;
99+
size_t xmlsz;
100+
ret = map_file("fixture/model.xml", &xml, &xmlsz);
101+
if (ret != 0) {
102+
fprintf(stderr, "failed to load fixture/model.xml: %s\n",
103+
strerror(ret));
104+
exit(1);
105+
}
106+
void *weights;
107+
size_t weightssz;
108+
ret = map_file("fixture/model.bin", &weights, &weightssz);
109+
if (ret != 0) {
110+
fprintf(stderr, "failed to load fixture/model.bin: %s\n",
111+
strerror(ret));
112+
exit(1);
113+
}
114+
/* note: openvino takes two buffers, namely IR and weights */
115+
graph_builder builders[2] = { {
116+
.buf = xml,
117+
.size = xmlsz,
118+
},
119+
{
120+
.buf = weights,
121+
.size = weightssz,
122+
} };
123+
graph g;
124+
nnret = load(builders, 2, openvino, cpu, &g);
125+
unmap_file(xml, xmlsz);
126+
unmap_file(weights, weightssz);
127+
if (nnret != success) {
128+
fprintf(stderr, "load failed with %d\n", (int)nnret);
129+
exit(1);
130+
}
131+
graph_execution_context ctx;
132+
nnret = init_execution_context(g, &ctx);
133+
if (nnret != success) {
134+
fprintf(stderr, "init_execution_context failed with %d\n", (int)nnret);
135+
exit(1);
136+
}
137+
void *tensordata;
138+
size_t tensordatasz;
139+
ret = map_file("fixture/tensor.bgr", &tensordata, &tensordatasz);
140+
if (ret != 0) {
141+
fprintf(stderr, "failed to load fixture/tensor.bgr: %s\n",
142+
strerror(ret));
143+
exit(1);
144+
}
145+
tensor tensor = {
146+
.dimensions = { .buf = (uint32_t[]){1, 3, 224, 224,}, .size = 4, },
147+
.type = fp32,
148+
.data = tensordata,
149+
};
150+
nnret = set_input(ctx, 0, &tensor);
151+
unmap_file(tensordata, tensordatasz);
152+
if (nnret != success) {
153+
fprintf(stderr, "set_input failed with %d\n", (int)nnret);
154+
exit(1);
155+
}
156+
nnret = compute(ctx);
157+
if (nnret != success) {
158+
fprintf(stderr, "compute failed with %d\n", (int)nnret);
159+
exit(1);
160+
}
161+
float result[1001];
162+
uint32_t resultsz;
163+
nnret = get_output(ctx, 0, (void *)result, sizeof(result), &resultsz);
164+
if (nnret != success) {
165+
fprintf(stderr, "get_output failed with %d\n", (int)nnret);
166+
exit(1);
167+
}
168+
print_result(result, resultsz);
169+
}

wamr-wasi-extensions/test.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /bin/sh
2+
3+
# Copyright (C) 2025 Midokura Japan KK. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
PREFIX=/tmp/wamr
7+
WASI_SDK=${WASI_SDK:-/opt/wasi-sdk}
8+
9+
cmake -B build-lib \
10+
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake \
11+
-DCMAKE_INSTALL_PREFIX=${PREFIX} \
12+
.
13+
cmake --build build-lib -t install
14+
15+
cmake -B build-app-nn \
16+
-DCMAKE_TOOLCHAIN_FILE=${WASI_SDK}/share/cmake/wasi-sdk.cmake \
17+
-DCMAKE_PREFIX_PATH=${PREFIX} \
18+
samples/nn
19+
cmake --build build-app-nn

0 commit comments

Comments
 (0)