|
| 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 | +} |
0 commit comments