main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * Copyright (C) 2025 Midokura Japan KK. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <sys/stat.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <getopt.h>
  10. #include <inttypes.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <wamr/wasi_ephemeral_nn.h>
  16. #include "fileio.h"
  17. #include "map.h"
  18. static struct map graphs;
  19. static struct map contexts;
  20. static void
  21. load_graph(char *options)
  22. {
  23. int target = wasi_ephemeral_nn_target_cpu;
  24. int encoding = wasi_ephemeral_nn_encoding_openvino;
  25. const char *id = "default";
  26. wasi_ephemeral_nn_graph_builder *builders = NULL;
  27. size_t nbuilders = 0;
  28. enum {
  29. opt_id,
  30. opt_file,
  31. opt_encoding,
  32. opt_target,
  33. };
  34. static char *const keylistp[] = {
  35. [opt_id] = "id",
  36. [opt_file] = "file",
  37. [opt_encoding] = "encoding",
  38. [opt_target] = "target",
  39. NULL,
  40. };
  41. while (*options) {
  42. extern char *suboptarg;
  43. char *value;
  44. const char *saved = options;
  45. switch (getsubopt(&options, keylistp, &value)) {
  46. case opt_id:
  47. if (value == NULL) {
  48. fprintf(stderr, "no value for %s\n", saved);
  49. exit(2);
  50. }
  51. id = value;
  52. break;
  53. case opt_file:
  54. if (value == NULL) {
  55. fprintf(stderr, "no value for %s\n", saved);
  56. exit(2);
  57. }
  58. builders =
  59. realloc(builders, (nbuilders + 1) * sizeof(*builders));
  60. if (builders == NULL) {
  61. exit(1);
  62. }
  63. wasi_ephemeral_nn_graph_builder *b = &builders[nbuilders++];
  64. int ret = map_file(value, (void *)&b->buf, (void *)&b->size);
  65. if (ret != 0) {
  66. fprintf(stderr, "map_file \"%s\" failed: %s\n", value,
  67. strerror(ret));
  68. exit(1);
  69. }
  70. break;
  71. case opt_encoding:
  72. if (value == NULL) {
  73. fprintf(stderr, "no value for %s\n", saved);
  74. exit(2);
  75. }
  76. encoding = atoi(value);
  77. break;
  78. case opt_target:
  79. if (value == NULL) {
  80. fprintf(stderr, "no value for %s\n", saved);
  81. exit(2);
  82. }
  83. target = atoi(value);
  84. break;
  85. case -1:
  86. fprintf(stderr, "unknown subopt %s\n", saved);
  87. exit(2);
  88. }
  89. }
  90. wasi_ephemeral_nn_error nnret;
  91. wasi_ephemeral_nn_graph g;
  92. nnret = wasi_ephemeral_nn_load(builders, nbuilders, encoding, target, &g);
  93. size_t i;
  94. for (i = 0; i < nbuilders; i++) {
  95. wasi_ephemeral_nn_graph_builder *b = &builders[i];
  96. unmap_file(b->buf, b->size);
  97. }
  98. if (nnret != wasi_ephemeral_nn_error_success) {
  99. fprintf(stderr, "load failed with %d\n", (int)nnret);
  100. exit(1);
  101. }
  102. map_set(&graphs, id, g);
  103. }
  104. static void
  105. init_execution_context(char *options)
  106. {
  107. const char *id = "default";
  108. const char *graph_id = "default";
  109. enum {
  110. opt_id,
  111. opt_graph_id,
  112. };
  113. static char *const keylistp[] = {
  114. [opt_id] = "id",
  115. [opt_graph_id] = "graph-id",
  116. NULL,
  117. };
  118. while (*options) {
  119. extern char *suboptarg;
  120. char *value;
  121. const char *saved = options;
  122. switch (getsubopt(&options, keylistp, &value)) {
  123. case opt_id:
  124. if (value == NULL) {
  125. fprintf(stderr, "no value for %s\n", saved);
  126. exit(2);
  127. }
  128. id = value;
  129. break;
  130. case opt_graph_id:
  131. if (value == NULL) {
  132. fprintf(stderr, "no value for %s\n", saved);
  133. exit(2);
  134. }
  135. graph_id = value;
  136. break;
  137. case -1:
  138. fprintf(stderr, "unknown subopt %s\n", saved);
  139. exit(2);
  140. }
  141. }
  142. wasi_ephemeral_nn_graph g = map_get(&graphs, graph_id);
  143. wasi_ephemeral_nn_graph_execution_context c;
  144. wasi_ephemeral_nn_error nnret;
  145. nnret = wasi_ephemeral_nn_init_execution_context(g, &c);
  146. if (nnret != wasi_ephemeral_nn_error_success) {
  147. fprintf(stderr, "init_execution_context failed with %d\n", (int)nnret);
  148. exit(1);
  149. }
  150. map_set(&contexts, id, c);
  151. }
  152. static void
  153. set_input(char *options)
  154. {
  155. int ret;
  156. const char *context_id = "default";
  157. uint32_t idx = 0;
  158. wasi_ephemeral_nn_tensor tensor = {
  159. .dimensions = { .buf = NULL, .size = 0, },
  160. .type = wasi_ephemeral_nn_type_fp32,
  161. .data = NULL,
  162. };
  163. void *buf = NULL;
  164. size_t sz = 0;
  165. enum {
  166. opt_context_id,
  167. opt_dim,
  168. opt_type,
  169. opt_idx,
  170. opt_file,
  171. };
  172. static char *const keylistp[] = {
  173. [opt_context_id] = "context-id",
  174. [opt_dim] = "dim",
  175. [opt_type] = "type",
  176. [opt_idx] = "idx",
  177. [opt_file] = "file",
  178. NULL,
  179. };
  180. while (*options) {
  181. extern char *suboptarg;
  182. char *value;
  183. const char *saved = options;
  184. switch (getsubopt(&options, keylistp, &value)) {
  185. case opt_context_id:
  186. if (value == NULL) {
  187. fprintf(stderr, "no value for %s\n", saved);
  188. exit(2);
  189. }
  190. context_id = value;
  191. break;
  192. case opt_dim:
  193. if (value == NULL) {
  194. fprintf(stderr, "no value for %s\n", saved);
  195. exit(2);
  196. }
  197. wasi_ephemeral_nn_tensor_dimensions *dims = &tensor.dimensions;
  198. dims->buf =
  199. realloc(dims->buf, (dims->size + 1) * sizeof(*dims->buf));
  200. if (dims->buf == NULL) {
  201. exit(1);
  202. }
  203. dims->buf[dims->size++] = atoi(value);
  204. break;
  205. case opt_type:
  206. if (value == NULL) {
  207. fprintf(stderr, "no value for %s\n", saved);
  208. exit(2);
  209. }
  210. tensor.type = atoi(value);
  211. break;
  212. case opt_file:
  213. if (value == NULL) {
  214. fprintf(stderr, "no value for %s\n", saved);
  215. exit(2);
  216. }
  217. if (buf != NULL) {
  218. fprintf(stderr, "duplicated tensor data\n");
  219. exit(2);
  220. }
  221. ret = map_file(value, &buf, &sz);
  222. if (ret != 0) {
  223. fprintf(stderr, "map_file \"%s\" failed: %s\n", value,
  224. strerror(ret));
  225. exit(1);
  226. }
  227. break;
  228. case opt_idx:
  229. if (value == NULL) {
  230. fprintf(stderr, "no value for %s\n", saved);
  231. exit(2);
  232. }
  233. idx = atoi(value);
  234. break;
  235. case -1:
  236. fprintf(stderr, "unknown subopt %s\n", saved);
  237. exit(2);
  238. }
  239. }
  240. if (tensor.dimensions.size == 0) {
  241. fprintf(stderr, "no dimension is given\n");
  242. exit(2);
  243. }
  244. if (buf == NULL) {
  245. fprintf(stderr, "no tensor is given\n");
  246. exit(2);
  247. }
  248. /*
  249. * REVISIT: we can check the tensor size against type/dimensions
  250. * and warn the user if unexpected.
  251. */
  252. wasi_ephemeral_nn_error nnret;
  253. wasi_ephemeral_nn_graph_execution_context c =
  254. map_get(&contexts, context_id);
  255. tensor.data.buf = buf;
  256. tensor.data.size = sz;
  257. nnret = wasi_ephemeral_nn_set_input(c, idx, &tensor);
  258. unmap_file(buf, sz);
  259. if (nnret != wasi_ephemeral_nn_error_success) {
  260. fprintf(stderr, "set_input failed with %d\n", (int)nnret);
  261. exit(1);
  262. }
  263. }
  264. static void
  265. compute(char *options)
  266. {
  267. const char *context_id = "default";
  268. enum {
  269. opt_context_id,
  270. };
  271. static char *const keylistp[] = {
  272. [opt_context_id] = "context-id",
  273. NULL,
  274. };
  275. while (*options) {
  276. extern char *suboptarg;
  277. char *value;
  278. const char *saved = options;
  279. switch (getsubopt(&options, keylistp, &value)) {
  280. case opt_context_id:
  281. if (value == NULL) {
  282. fprintf(stderr, "no value for %s\n", saved);
  283. exit(2);
  284. }
  285. context_id = value;
  286. break;
  287. case -1:
  288. fprintf(stderr, "unknown subopt %s\n", saved);
  289. exit(2);
  290. }
  291. }
  292. wasi_ephemeral_nn_graph_execution_context c =
  293. map_get(&contexts, context_id);
  294. wasi_ephemeral_nn_error nnret;
  295. nnret = wasi_ephemeral_nn_compute(c);
  296. if (nnret != wasi_ephemeral_nn_error_success) {
  297. fprintf(stderr, "compute failed with %d\n", (int)nnret);
  298. exit(1);
  299. }
  300. }
  301. static void
  302. get_output(char *options)
  303. {
  304. int ret;
  305. const char *outfile = NULL;
  306. const char *context_id = "default";
  307. uint32_t idx = 0;
  308. enum {
  309. opt_context_id,
  310. opt_idx,
  311. opt_file,
  312. };
  313. static char *const keylistp[] = {
  314. [opt_context_id] = "context-id",
  315. [opt_idx] = "idx",
  316. [opt_file] = "file",
  317. NULL,
  318. };
  319. while (*options) {
  320. extern char *suboptarg;
  321. char *value;
  322. const char *saved = options;
  323. switch (getsubopt(&options, keylistp, &value)) {
  324. case opt_context_id:
  325. if (value == NULL) {
  326. fprintf(stderr, "no value for %s\n", saved);
  327. exit(2);
  328. }
  329. context_id = value;
  330. break;
  331. case opt_file:
  332. if (value == NULL) {
  333. fprintf(stderr, "no value for %s\n", saved);
  334. exit(2);
  335. }
  336. outfile = value;
  337. break;
  338. case opt_idx:
  339. if (value == NULL) {
  340. fprintf(stderr, "no value for %s\n", saved);
  341. exit(2);
  342. }
  343. idx = atoi(value);
  344. break;
  345. case -1:
  346. fprintf(stderr, "unknown subopt %s\n", saved);
  347. exit(2);
  348. }
  349. }
  350. int outfd = -1;
  351. if (outfile != NULL) {
  352. outfd = open(outfile, O_CREAT | O_TRUNC | O_WRONLY);
  353. if (outfd == -1) {
  354. fprintf(stderr, "failed to open output file \"%s\": %s\n", outfile,
  355. strerror(errno));
  356. exit(1);
  357. }
  358. }
  359. wasi_ephemeral_nn_error nnret;
  360. wasi_ephemeral_nn_graph_execution_context c =
  361. map_get(&contexts, context_id);
  362. void *resultbuf = NULL;
  363. size_t resultbufsz = 256;
  364. uint32_t resultsz;
  365. retry:
  366. resultbuf = realloc(resultbuf, resultbufsz);
  367. if (resultbuf == NULL) {
  368. exit(1);
  369. }
  370. nnret =
  371. wasi_ephemeral_nn_get_output(c, 0, resultbuf, resultbufsz, &resultsz);
  372. if (nnret == wasi_ephemeral_nn_error_too_large) {
  373. resultbufsz *= 2;
  374. goto retry;
  375. }
  376. if (nnret != wasi_ephemeral_nn_error_success) {
  377. fprintf(stderr, "get_output failed with %d\n", (int)nnret);
  378. exit(1);
  379. }
  380. if (outfd != -1) {
  381. ssize_t written = write(outfd, resultbuf, resultsz);
  382. if (written == -1) {
  383. fprintf(stderr, "failed to write: %s\n", strerror(errno));
  384. exit(1);
  385. }
  386. if (written == -1) {
  387. fprintf(stderr, "unexpetecd write length %zu (expected %zu)\n",
  388. written, (size_t)resultsz);
  389. exit(1);
  390. }
  391. ret = close(outfd);
  392. if (ret != 0) {
  393. fprintf(stderr, "failed to close: %s\n", strerror(errno));
  394. exit(1);
  395. }
  396. }
  397. else {
  398. fprintf(stderr, "WARNING: discarding %zu bytes output\n",
  399. (size_t)resultsz);
  400. }
  401. }
  402. enum longopt {
  403. opt_load_graph = 0x100,
  404. opt_init_execution_context,
  405. opt_set_input,
  406. opt_compute,
  407. opt_get_output,
  408. };
  409. static const struct option longopts[] = {
  410. {
  411. "load-graph",
  412. required_argument,
  413. NULL,
  414. opt_load_graph,
  415. },
  416. {
  417. "init-execution-context",
  418. optional_argument,
  419. NULL,
  420. opt_init_execution_context,
  421. },
  422. {
  423. "set-input",
  424. required_argument,
  425. NULL,
  426. opt_set_input,
  427. },
  428. {
  429. "compute",
  430. optional_argument,
  431. NULL,
  432. opt_compute,
  433. },
  434. {
  435. "get-output",
  436. optional_argument,
  437. NULL,
  438. opt_get_output,
  439. },
  440. {
  441. NULL,
  442. 0,
  443. NULL,
  444. 0,
  445. },
  446. };
  447. int
  448. main(int argc, char **argv)
  449. {
  450. extern char *optarg;
  451. int ch;
  452. int longidx;
  453. while ((ch = getopt_long(argc, argv, "", longopts, &longidx)) != -1) {
  454. switch (ch) {
  455. case opt_load_graph:
  456. load_graph(optarg);
  457. break;
  458. case opt_init_execution_context:
  459. init_execution_context(optarg ? optarg : "");
  460. break;
  461. case opt_set_input:
  462. set_input(optarg);
  463. break;
  464. case opt_compute:
  465. compute(optarg ? optarg : "");
  466. break;
  467. case opt_get_output:
  468. get_output(optarg ? optarg : "");
  469. break;
  470. default:
  471. exit(2);
  472. }
  473. }
  474. exit(0);
  475. }