You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

acl_op.h 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /**
  2. * @file acl_op.h
  3. *
  4. * Copyright (C) Huawei Technologies Co., Ltd. 2019-2020. All Rights Reserved.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. */
  10. #ifndef INC_EXTERNAL_ACL_ACL_OP_H_
  11. #define INC_EXTERNAL_ACL_ACL_OP_H_
  12. #include "acl_base.h"
  13. #include "acl_rt.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef struct aclopHandle aclopHandle;
  18. typedef struct aclopAttr aclopAttr;
  19. typedef struct aclopKernelDesc aclopKernelDesc;
  20. typedef void (*aclDataDeallocator)(void *data, size_t length);
  21. const int ACL_COMPILE_FLAG_BIN_SELECTOR = 1;
  22. typedef enum aclEngineType {
  23. ACL_ENGINE_SYS,
  24. ACL_ENGINE_AICORE,
  25. ACL_ENGINE_VECTOR,
  26. } aclopEngineType;
  27. /**
  28. * @ingroup AscendCL
  29. * @brief Set base directory that contains single op models
  30. *
  31. * @par Restriction
  32. * The aclopSetModelDir interface can be called only once in a process.
  33. * @param modelDir [IN] path of the directory
  34. * @retval ACL_ERROR_NONE The function is successfully executed.
  35. * @retval OtherValues Failure
  36. */
  37. ACL_FUNC_VISIBILITY aclError aclopSetModelDir(const char *modelDir);
  38. /**
  39. * @ingroup AscendCL
  40. * @brief load single op models from memory
  41. *
  42. * @par Restriction
  43. * The aclopLoad interface can be called more than one times in a process.
  44. * @param model [IN] address of single op models
  45. * @param modelSize [IN] size of single op models
  46. * @retval ACL_ERROR_NONE The function is successfully executed.
  47. * @retval OtherValues Failure
  48. */
  49. ACL_FUNC_VISIBILITY aclError aclopLoad(const void *model, size_t modelSize);
  50. /**
  51. * @ingroup AscendCL
  52. * @brief create data of type aclopAttr
  53. *
  54. * @retval pointer to created instance.
  55. * @retval nullptr if run out of memory
  56. */
  57. ACL_FUNC_VISIBILITY aclopAttr *aclopCreateAttr();
  58. /**
  59. * @ingroup AscendCL
  60. * @brief destroy data of typ aclopAttr
  61. *
  62. * @param attr [IN] pointer to the instance of aclopAttr
  63. */
  64. ACL_FUNC_VISIBILITY void aclopDestroyAttr(const aclopAttr *attr);
  65. /**
  66. * @ingroup AscendCL
  67. * @brief set an attribute. the type of the attribute is bool
  68. *
  69. * @param attr [IN] pointer to the instance of aclopAttr
  70. * @param attrName [IN] attribute name
  71. * @param attrValue [IN] attribute value
  72. * false if attrValue is 0, true otherwise.
  73. * @retval ACL_ERROR_NONE The function is successfully executed.
  74. * @retval OtherValues Failure
  75. */
  76. ACL_FUNC_VISIBILITY aclError aclopSetAttrBool(aclopAttr *attr, const char *attrName, uint8_t attrValue);
  77. /**
  78. * @ingroup AscendCL
  79. * @brief set an attribute. the type of the attribute is int64_t
  80. *
  81. * @param attr [IN] pointer to the instance of aclopAttr
  82. * @param attrName [IN] attribute name
  83. * @param attrValue [IN] attribute value
  84. * @retval ACL_ERROR_NONE The function is successfully executed.
  85. * @retval OtherValues Failure
  86. */
  87. ACL_FUNC_VISIBILITY aclError aclopSetAttrInt(aclopAttr *attr, const char *attrName, int64_t attrValue);
  88. /**
  89. * @ingroup AscendCL
  90. * @brief set an attribute. the type of the attribute is float
  91. *
  92. * @param attr [IN] pointer to the instance of aclopAttr
  93. * @param attrName [IN] attribute name
  94. * @param attrValue [IN] attribute value
  95. * @retval ACL_ERROR_NONE The function is successfully executed.
  96. * @retval OtherValues Failure
  97. */
  98. ACL_FUNC_VISIBILITY aclError aclopSetAttrFloat(aclopAttr *attr, const char *attrName, float attrValue);
  99. /**
  100. * @ingroup AscendCL
  101. * @brief set an attribute. the type of the attribute is string
  102. *
  103. * @param attr [IN] pointer to the instance of aclopAttr
  104. * @param attrName [IN] attribute name
  105. * @param attrValue [IN] attribute value
  106. * @retval ACL_ERROR_NONE The function is successfully executed.
  107. * @retval OtherValues Failure
  108. */
  109. ACL_FUNC_VISIBILITY aclError aclopSetAttrString(aclopAttr *attr, const char *attrName, const char *attrValue);
  110. /**
  111. * @ingroup AscendCL
  112. * @brief set an attribute. the type of the attribute is list of bools
  113. *
  114. * @param attr [IN] pointer to the instance of aclopAttr
  115. * @param attrName [IN] attribute name
  116. * @param numValues [IN] number of values. false if attrValue is 0, true otherwise.
  117. * @param values [IN] pointer to values
  118. * @retval ACL_ERROR_NONE The function is successfully executed.
  119. * @retval OtherValues Failure
  120. */
  121. ACL_FUNC_VISIBILITY aclError aclopSetAttrListBool(aclopAttr *attr, const char *attrName, int numValues,
  122. const uint8_t *values);
  123. /**
  124. * @ingroup AscendCL
  125. * @brief set an attribute. the type of the attribute is list of ints
  126. *
  127. * @param attr [IN] pointer to the instance of aclopAttr
  128. * @param attrName [IN] attribute name
  129. * @param numValues [IN] number of values
  130. * @param values [IN] pointer to values
  131. * @retval ACL_ERROR_NONE The function is successfully executed.
  132. * @retval OtherValues Failure
  133. */
  134. ACL_FUNC_VISIBILITY aclError aclopSetAttrListInt(aclopAttr *attr, const char *attrName, int numValues,
  135. const int64_t *values);
  136. /**
  137. * @ingroup AscendCL
  138. * @brief set an attribute. the type of the attribute is list of floats
  139. *
  140. * @param attr [IN] pointer to the instance of aclopAttr
  141. * @param attrName [IN] attribute name
  142. * @param numValues [IN] number of values
  143. * @param values [IN] pointer to values
  144. * @retval ACL_ERROR_NONE The function is successfully executed.
  145. * @retval OtherValues Failure
  146. */
  147. ACL_FUNC_VISIBILITY aclError aclopSetAttrListFloat(aclopAttr *attr, const char *attrName, int numValues,
  148. const float *values);
  149. /**
  150. * @ingroup AscendCL
  151. * @brief set an attribute. the type of the attribute is list of strings
  152. *
  153. * @param attr [IN] pointer to the instance of aclopAttr
  154. * @param attrName [IN] attribute name
  155. * @param numValues [IN] number of values
  156. * @param values [IN] pointer to values
  157. * @retval ACL_ERROR_NONE The function is successfully executed.
  158. * @retval OtherValues Failure
  159. */
  160. ACL_FUNC_VISIBILITY aclError aclopSetAttrListString(aclopAttr *attr, const char *attrName, int numValues,
  161. const char **values);
  162. /**
  163. * @ingroup AscendCL
  164. * @brief set an attribute. the type of the attribute is list of list of ints
  165. *
  166. * @param attr [IN] pointer to the instance of aclopAttr
  167. * @param attrName [IN] attribute name
  168. * @param numLists [IN] number of lists
  169. * @param numValues [IN] pointer to number of values of each list
  170. * @param values [IN] pointer to values
  171. * @retval ACL_ERROR_NONE The function is successfully executed.
  172. * @retval OtherValues Failure
  173. */
  174. ACL_FUNC_VISIBILITY aclError aclopSetAttrListListInt(aclopAttr *attr,
  175. const char *attrName,
  176. int numLists,
  177. const int *numValues,
  178. const int64_t *const values[]);
  179. /**
  180. * @ingroup AscendCL
  181. * @brief Load and execute the specified operator asynchronously
  182. *
  183. * @par Restriction
  184. * @li The input and output organization of each operator is different,
  185. * and the application needs to organize the operator strictly
  186. * according to the operator input and output parameters when calling.
  187. * @li When the user calls aclopExecute,
  188. * the ACL finds the corresponding task according to the optype,
  189. * the description of the input tesnsor,
  190. * the description of the output tesnsor, and attr, and issues the execution.
  191. * @param opType [IN] type of op
  192. * @param numInputs [IN] number of inputs
  193. * @param inputDesc [IN] pointer to array of input tensor descriptions
  194. * @param inputs [IN] pointer to array of input buffers
  195. * @param numOutputs [IN] number of outputs
  196. * @param outputDesc [IN] pointer to array of output tensor descriptions
  197. * @param outputs [OUT] pointer to array of output buffers
  198. * @param attr [IN] pointer to instance of aclopAttr.
  199. * may pass nullptr if the op has no attribute
  200. * @param stream [IN] stream
  201. * @retval ACL_ERROR_NONE The function is successfully executed.
  202. * @retval OtherValues Failure
  203. */
  204. ACL_FUNC_VISIBILITY aclError aclopExecute(const char *opType,
  205. int numInputs,
  206. const aclTensorDesc *const inputDesc[],
  207. const aclDataBuffer *const inputs[],
  208. int numOutputs,
  209. const aclTensorDesc *const outputDesc[],
  210. aclDataBuffer *const outputs[],
  211. const aclopAttr *attr,
  212. aclrtStream stream);
  213. /**
  214. * @ingroup AscendCL
  215. * @brief create a instance of aclopHandle.
  216. *
  217. * @param opType [IN] type of op
  218. * @param numInputs [IN] number of inputs
  219. * @param inputDesc [IN] pointer to array of input tensor descriptions
  220. * @param numOutputs [IN] number of outputs
  221. * @param outputDesc [IN] pointer to array of output tensor descriptions
  222. * @param opAttr [IN] pointer to instance of aclopAttr.
  223. * may pass nullptr if the op has no attribute
  224. * @param handle [OUT] pointer to the pointer to the handle
  225. * @retval ACL_ERROR_NONE The function is successfully executed.
  226. * @retval OtherValues Failure
  227. */
  228. ACL_FUNC_VISIBILITY aclError aclopCreateHandle(const char *opType,
  229. int numInputs,
  230. const aclTensorDesc *const inputDesc[],
  231. int numOutputs,
  232. const aclTensorDesc *const outputDesc[],
  233. const aclopAttr *opAttr,
  234. aclopHandle **handle);
  235. /**
  236. * @ingroup AscendCL
  237. * @brief destroy aclopHandle instance
  238. *
  239. * @param handle [IN] pointer to the instance of aclopHandle
  240. */
  241. ACL_FUNC_VISIBILITY void aclopDestroyHandle(aclopHandle *handle);
  242. /**
  243. * @ingroup AscendCL
  244. * @brief execute an op with the handle.
  245. * can save op model matching cost compared with aclopExecute
  246. *
  247. * @param handle [IN] pointer to the instance of aclopHandle.
  248. * The aclopCreateHandle interface has been called
  249. * in advance to create aclopHandle type data.
  250. * @param numInputs [IN] number of inputs
  251. * @param inputs [IN] pointer to array of input buffers.
  252. * The aclCreateDataBuffer interface has been called
  253. * in advance to create aclDataBuffer type data.
  254. * @param numOutputs [IN] number of outputs
  255. * @param outputs [IN] pointer to array of output buffers
  256. * @param stream [IN] stream
  257. * @retval ACL_ERROR_NONE The function is successfully executed.
  258. * @retval OtherValues Failure
  259. *
  260. * @see aclopCreateHandle | aclCreateDataBuffer
  261. */
  262. ACL_FUNC_VISIBILITY aclError aclopExecWithHandle(aclopHandle *handle,
  263. int numInputs,
  264. const aclDataBuffer *const inputs[],
  265. int numOutputs,
  266. aclDataBuffer *const outputs[],
  267. aclrtStream stream);
  268. /**
  269. * @ingroup AscendCL
  270. * @brief cast data type
  271. *
  272. * @param srcDesc [IN] source tensor desc
  273. * @param srcBuffer [IN] source tensor buffer
  274. * @param dstDesc [IN] destination tensor desc
  275. * @param dstBuffer [OUT] destination tensor buffer
  276. * @param truncate [IN] do not truncate if value is 0, truncate otherwise
  277. * @param stream [IN] stream
  278. * @retval ACL_ERROR_NONE The function is successfully executed.
  279. * @retval OtherValues Failure
  280. */
  281. ACL_FUNC_VISIBILITY aclError aclopCast(const aclTensorDesc *srcDesc,
  282. const aclDataBuffer *srcBuffer,
  283. const aclTensorDesc *dstDesc,
  284. aclDataBuffer *dstBuffer,
  285. uint8_t truncate,
  286. aclrtStream stream);
  287. /**
  288. * @ingroup AscendCL
  289. * @brief create a handle for casting datatype
  290. *
  291. * @param srcDesc [IN] source tensor desc
  292. * @param dstDesc [IN] destination tensor desc
  293. * @param truncate [IN] do not truncate if value is 0, truncate otherwise
  294. * @param handle [IN] pointer to the pointer to the handle
  295. * @retval ACL_ERROR_NONE The function is successfully executed.
  296. * @retval OtherValues Failure
  297. */
  298. ACL_FUNC_VISIBILITY aclError aclopCreateHandleForCast(aclTensorDesc *srcDesc,
  299. aclTensorDesc *dstDesc,
  300. uint8_t truncate,
  301. aclopHandle **handle);
  302. /**
  303. * @ingroup AscendCL
  304. * @brief create kernel
  305. *
  306. * @param opType [IN] op type
  307. * @param kernelId [IN] kernel id
  308. * @param kernelName [IN] kernel name
  309. * @param binData [IN] kernel bin data
  310. * @param binSize [IN] kernel bin size
  311. * @param enginetype [IN] enigne type
  312. * @param deallocator [IN] callback function for deallocating bin data,
  313. * null if bin data to be deallocated by caller
  314. * @retval ACL_ERROR_NONE The function is successfully executed.
  315. * @retval OtherValues Failure
  316. *
  317. * @see aclopCompile
  318. */
  319. ACL_FUNC_VISIBILITY aclError aclopCreateKernel(const char *opType,
  320. const char *kernelId,
  321. const char *kernelName,
  322. void *binData,
  323. int binSize,
  324. aclopEngineType enginetype,
  325. aclDataDeallocator deallocator);
  326. /**
  327. * @ingroup AscendCL
  328. * @brief create kernel
  329. *
  330. * @param numInputs [IN] number of inputs
  331. * @param inputDesc [IN] pointer to array of input tensor descriptions
  332. * @param numOutputs [IN] number of outputs
  333. * @param outputDesc [IN] pointer to array of output tensor descriptions
  334. * @param opAttr [IN] pointer to instance of aclopAttr
  335. * @param aclopKernelDesc [IN] pointer to instance of aclopKernelDesc
  336. * @retval ACL_ERROR_NONE The function is successfully executed.
  337. * @retval OtherValues Failure
  338. */
  339. typedef aclError (*aclopCompileFunc)(int numInputs,
  340. const aclTensorDesc *const inputDesc[],
  341. int numOutputs,
  342. const aclTensorDesc *const outputDesc[],
  343. const aclopAttr *opAttr,
  344. aclopKernelDesc *aclopKernelDesc);
  345. /**
  346. * @ingroup AscendCL
  347. * @brief register compile function
  348. *
  349. * @param opType [IN] op type
  350. * @param func [IN] compile function
  351. * @retval ACL_ERROR_NONE The function is successfully executed.
  352. * @retval OtherValues Failure
  353. *
  354. * @see aclopUnregisterCompileFunc
  355. */
  356. ACL_FUNC_VISIBILITY aclError aclopRegisterCompileFunc(const char *opType, aclopCompileFunc func);
  357. /**
  358. * @ingroup AscendCL
  359. * @brief unregister compile function
  360. *
  361. * @param opType [IN] op type
  362. * @retval ACL_ERROR_NONE The function is successfully executed.
  363. * @retval OtherValues Failure
  364. */
  365. ACL_FUNC_VISIBILITY aclError aclopUnregisterCompileFunc(const char *opType);
  366. /**
  367. * @ingroup AscendCL
  368. * @brief set kernel args
  369. *
  370. * @param kernelDesc [IN] pointer to instance of aclopKernelDesc
  371. * @param kernelId [IN] kernel id
  372. * @param blockDim [IN] block dim
  373. * @param args [IN] args
  374. * @param argSize [IN] size in bytes of args
  375. * @retval ACL_ERROR_NONE The function is successfully executed.
  376. * @retval OtherValues Failure
  377. */
  378. ACL_FUNC_VISIBILITY aclError aclopSetKernelArgs(aclopKernelDesc *kernelDesc,
  379. const char *kernelId,
  380. uint32_t blockDim,
  381. const void *args,
  382. uint32_t argSize);
  383. /**
  384. * @ingroup AscendCL
  385. * @brief set workspace sizes
  386. *
  387. * @param kernelDesc [IN] pointer to instance of aclopKernelDesc
  388. * @param numWorkspaces [IN] number of workspaces
  389. * @param workspaceSizes [IN] pointer to array of sizes of workspaces
  390. * @retval ACL_ERROR_NONE The function is successfully executed.
  391. * @retval OtherValues Failure
  392. */
  393. ACL_FUNC_VISIBILITY aclError aclopSetKernelWorkspaceSizes(aclopKernelDesc *kernelDesc, int numWorkspaces,
  394. size_t *workspaceSizes);
  395. /**
  396. * @ingroup AscendCL
  397. * @brief compile op with dynamic shape
  398. *
  399. * @param opType [IN] op type
  400. * @param numInputs [IN] number of inputs
  401. * @param inputDesc [IN] pointer to array of input tensor descriptions
  402. * @param numOutputs [IN] number of outputs
  403. * @param outputDesc [IN] pointer to array of output tensor descriptions
  404. * @param attr [IN] pointer to instance of aclopAttr.
  405. * may pass nullptr if the op has no attribute
  406. * @retval ACL_ERROR_NONE The function is successfully executed.
  407. * @retval OtherValues Failure
  408. */
  409. ACL_FUNC_VISIBILITY aclError aclopUpdateParams(const char *opType,
  410. int numInputs,
  411. const aclTensorDesc *const inputDesc[],
  412. int numOutputs,
  413. const aclTensorDesc *const outputDesc[],
  414. const aclopAttr *attr);
  415. #ifdef __cplusplus
  416. }
  417. #endif
  418. #endif // INC_EXTERNAL_ACL_ACL_OP_H_

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台