main.c 14 KB

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