rt_ai.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-01 liqiwen the first version
  9. */
  10. #include <rt_ai_runtime.h>
  11. #include <rt_ai.h>
  12. #include <rt_ai_log.h>
  13. #include <rt_ai_core.h>
  14. #ifdef RT_USING_AI_OPS
  15. #define ai_init (ai->ops->init)
  16. #define ai_load (ai->ops->load)
  17. #define ai_run (ai->ops->run)
  18. #define ai_output (ai->ops->get_output)
  19. #define ai_get_info (ai->ops->get_info)
  20. #define ai_config (ai->ops->config)
  21. #else
  22. #define ai_init (ai->init)
  23. #define ai_input (ai->get_input)
  24. #define ai_run (ai->run)
  25. #define ai_output (ai->get_output)
  26. #define ai_get_info (ai->get_info)
  27. #define ai_config (ai->config)
  28. #endif
  29. /**
  30. * This function finds a ai handle by specified name.
  31. *
  32. * @param name the ai handle's name
  33. *
  34. * @return the registered ai handle on successful, or RT_AI_NULL on failure.
  35. */
  36. rt_ai_t rt_ai_find(const char *name)
  37. {
  38. RT_AI_ASSERT_RETURN_NULL(name);
  39. rt_ai_core_t core = RT_NULL;
  40. core = rt_ai_core_find(name,RT_AI_CLASS_HANDLE);
  41. if(core == RT_NULL){
  42. AI_LOG_I("rt_ai_core_find return NULL %s,%d!\n",__FILE__,__LINE__);
  43. return RT_AI_NULL;
  44. }
  45. return (rt_ai_t) core;
  46. }
  47. /**
  48. * This function registers a ai handle with specified name.
  49. *
  50. * @param ai the pointer of ai handle structure
  51. * @param name the ai handle's name
  52. * @param flags Temporary unused
  53. * @param call backend construct function
  54. * @param arg arg of backend construct function
  55. *
  56. * @return the error code, RT_AI_OK on register successfully.
  57. */
  58. rt_err_t rt_ai_register(rt_ai_t ai, const char *name, rt_uint16_t flags, int (*call)(void *arg), void *arg)
  59. {
  60. RT_AI_ASSERT_RETURN_0(ai != RT_AI_NULL);
  61. if (ai == RT_AI_NULL)
  62. return -RT_AI_ERROR;
  63. if (call(arg) != RT_AI_OK)
  64. {
  65. AI_LOG_E("register callback err! %s,%d\n",__FILE__,__LINE__);
  66. return -RT_AI_ERROR;
  67. }
  68. AI_LOG_I("register model %s\n", name);
  69. if(rt_ai_core_register(&(ai->parent),RT_AI_CLASS_STATIC_HANDLE,name) == RT_NULL){
  70. AI_LOG_E("rt_ai_core_register err!%s,%d\n",__FILE__,__LINE__);
  71. }
  72. return RT_AI_OK;
  73. }
  74. /**
  75. * This function removes a previously registered ai handle
  76. *
  77. * @param ai the pointer of ai handle structure
  78. *
  79. * @return the error code, RT_AI_OK on successfully.
  80. */
  81. rt_err_t rt_ai_unregister(rt_ai_t ai)
  82. {
  83. //todo
  84. return RT_AI_OK;
  85. }
  86. /**
  87. * This function will initialize the specified ai & backend & memery of runtime
  88. *
  89. * @param ai the pointer of ai handle structure
  90. * @param work_buf runtime memery
  91. *
  92. * @return the result
  93. */
  94. rt_err_t rt_ai_init(rt_ai_t ai, rt_ai_buffer_t *work_buf)
  95. {
  96. RT_AI_ASSERT_RETURN_0(ai != RT_AI_NULL);
  97. RT_AI_ASSERT_RETURN_0(ai_init != RT_AI_NULL);
  98. rt_err_t result = RT_AI_OK;
  99. ai->flag = 0;
  100. /* get ai_init handler */
  101. ai->workbuffer = work_buf;
  102. if (ai_init == RT_AI_NULL)
  103. {
  104. AI_LOG_E("ai init interface is None!\n");
  105. return RT_AI_ERROR;
  106. }
  107. result = ai_init(ai, work_buf);
  108. if (result != RT_AI_OK)
  109. {
  110. AI_LOG_E("ai init interface return a err!\n", result);
  111. return result;
  112. }
  113. ai->flag |= RT_AI_FLAG_INITED;
  114. return RT_AI_OK;
  115. }
  116. /**
  117. * This function will get a input addr of ai
  118. *
  119. * @param ai the pointer of ai handle structure
  120. * @param index the index of input of ai
  121. *
  122. * @return addr if get input success; RT_AI_NULL if fail
  123. */
  124. rt_ai_buffer_t *rt_ai_input(rt_ai_t ai, rt_ai_uint32_t index)
  125. {
  126. RT_AI_ASSERT_RETURN_NULL(ai != RT_AI_NULL);
  127. rt_err_t result = RT_AI_OK;
  128. /* call ai_open interface */
  129. if (ai_input == RT_AI_NULL)
  130. {
  131. AI_LOG_E("ai input interface is None!\n");
  132. return RT_AI_NULL;
  133. }
  134. result = ai_input(ai, index);
  135. /* set open flag */
  136. if (result != RT_AI_OK)
  137. {
  138. AI_LOG_E("ai input interface return a err!\n", result);
  139. return RT_AI_NULL;
  140. }
  141. return ai->input[index];
  142. }
  143. /**
  144. * This function will run a ai
  145. *
  146. * @param ai the pointer of ai handle structure
  147. * @param callback the callback func of run complete
  148. * @param arg the arg of callback func
  149. *
  150. * @return the result
  151. */
  152. rt_err_t rt_ai_run(rt_ai_t ai, void (*callback)(void *arg), void *arg)
  153. {
  154. RT_AI_ASSERT_RETURN_0(ai != RT_AI_NULL);
  155. RT_AI_ASSERT_RETURN_0(ai_run != RT_AI_NULL);
  156. rt_err_t result = RT_AI_OK;
  157. /* if ai is not initialized, initialize it. */
  158. if (!(ai->flag & RT_AI_FLAG_INITED))
  159. {
  160. AI_LOG_E("ai uninitialize!\n");
  161. return -RT_AI_ERROR;
  162. }
  163. /* call ai_close interface */
  164. if (ai_run == RT_AI_NULL)
  165. {
  166. AI_LOG_E("ai run interface is None!\n");
  167. return RT_AI_ERROR;
  168. }
  169. ai->done_callback_user = callback;
  170. ai->arg = arg;
  171. run_entry(ai);
  172. result = ai_run(ai, run_done, (void *)ai);
  173. /* set open flag */
  174. if (result != RT_AI_OK)
  175. {
  176. AI_LOG_E("ai run interface return a err!\n");
  177. return result;
  178. }
  179. ai->flag |= RT_AI_FLAG_RUN;
  180. ai->flag &= ~RT_AI_FLAG_OUTPUT;
  181. return RT_AI_OK;
  182. }
  183. /**
  184. * This function will get a output addr of ai
  185. *
  186. * @param ai the pointer of ai handle structure
  187. * @param index the index of output of ai
  188. *
  189. * @return addr if get input success; RT_AI_NULL if fail
  190. */
  191. rt_ai_buffer_t *rt_ai_output(rt_ai_t ai, rt_ai_uint32_t index)
  192. {
  193. RT_AI_ASSERT_RETURN_NULL(ai != RT_AI_NULL);
  194. RT_AI_ASSERT_RETURN_NULL(ai_output != RT_AI_NULL);
  195. if (!(ai->flag & RT_AI_FLAG_RUN) | !(ai->flag & RT_AI_FLAG_INITED))
  196. {
  197. AI_LOG_E("ai uninitialize or not run\n");
  198. return RT_AI_NULL;
  199. }
  200. if (ai_output == RT_AI_NULL)
  201. {
  202. AI_LOG_E("ai output interface is None!\n");
  203. return RT_AI_NULL;
  204. }
  205. /* call ai_read interface */
  206. int result = 0;
  207. result = ai_output(ai, index);
  208. if (result != 0)
  209. {
  210. AI_LOG_E("ai output interface return a err!\n");
  211. return RT_AI_NULL;
  212. // ai->output_size = *size;
  213. }
  214. ai->flag |= RT_AI_FLAG_OUTPUT;
  215. return ai->output[index];
  216. }
  217. /**
  218. * This function will print the info of ai.
  219. *
  220. * @param ai the pointer of ai handle structure
  221. *
  222. * @return the err code.
  223. *
  224. */
  225. rt_err_t rt_ai_info(rt_ai_t ai)
  226. {
  227. RT_AI_ASSERT_RETURN_0(ai != RT_AI_NULL);
  228. if (!(ai->flag & RT_AI_FLAG_INITED))
  229. {
  230. AI_LOG_E("error: uninitialize!");
  231. return -RT_AI_ERROR;
  232. }
  233. ai_log("**********model info:************\n");
  234. ai_log("%-15s:%d\n", "input num: ", ai->info.input_n);
  235. ai_log("%-15s:%d\n", "output num: ", ai->info.output_n);
  236. ai_log("%-15s:%d\n", "work_buf size: ", ai->info.work_buffer_size);
  237. for (int i = 0; i < RT_AI_INFO(ai).input_n; i++)
  238. {
  239. ai_log("%-15s%d: %d\n", "input size:", i, RT_AI_INFO(ai).input_n_stack[i]);
  240. }
  241. for (int i = 0; i < RT_AI_INFO(ai).output_n; i++)
  242. {
  243. ai_log("%-15s%d: %d\n", "output size", i, RT_AI_INFO(ai).output_n_stack[i]);
  244. }
  245. return RT_AI_OK;
  246. }
  247. /**
  248. * This function will perform a variety of cofig functions on ai.
  249. *
  250. * @param ai the pointer of ai handle structure
  251. * @param cmd the command sent to ai
  252. * @param arg the argument of command
  253. *
  254. * @return the result
  255. */
  256. rt_err_t rt_ai_config(rt_ai_t ai, int cmd, rt_ai_buffer_t *arg)
  257. {
  258. RT_AI_ASSERT_RETURN_0(ai != RT_AI_NULL);
  259. RT_AI_ASSERT_RETURN_0(ai_config != RT_AI_NULL);
  260. int result = 0;
  261. /* call ai_write interface */
  262. result = ai_config(ai, cmd, arg);
  263. if (result != RT_AI_OK)
  264. {
  265. return result;
  266. }
  267. return RT_AI_OK;
  268. }