已合并
feat: 提供 AICPU CpuKernel V2 注册与查询接口 #1522
pantong创建于 4月11日
feat: 提供 AICPU CpuKernel V2 注册与查询接口 #1522
已合并
pantong创建于 4月11日
2 个文件变更+83-0
@@ -27,6 +27,8 @@ using KERNEL_CREATOR_FUN = std::function<std::shared_ptr<CpuKernel>(void)>;
27 27 
28AICPU_VISIBILITY bool RegistCpuKernel(const std::string &type, const KERNEL_CREATOR_FUN &fun);28AICPU_VISIBILITY bool RegistCpuKernel(const std::string &type, const KERNEL_CREATOR_FUN &fun);
29 29 
30+AICPU_VISIBILITY bool RegistCpuKernelV2(const std::string &type, const KERNEL_CREATOR_FUN &fun);
T
Ttingwood5月18日

V1,V2参数一样。建议通过预留参数,增加V2的扩展性。同时建议V2能兼容收编原接口

likedislike
31+ 
30template <typename T, typename... Args> static inline std::shared_ptr<T> MakeShared(Args &&... args)32template <typename T, typename... Args> static inline std::shared_ptr<T> MakeShared(Args &&... args)
31{33{
32 using T_NC = typename std::remove_const<T>::type;34 using T_NC = typename std::remove_const<T>::type;
@@ -41,5 +43,13 @@ template <typename T, typename... Args> static inline std::shared_ptr<T> MakeSha
41 return ptr; \43 return ptr; \
42 } \44 } \
43 bool g_##type##_Kernel_Creator __attribute__((unused)) = RegistCpuKernel(type, Creator_##type##_Kernel)45 bool g_##type##_Kernel_Creator __attribute__((unused)) = RegistCpuKernel(type, Creator_##type##_Kernel)
46+ 
47+#define REGISTER_CPU_KERNELV2(type, clazz) std::shared_ptr<CpuKernel> Creator_##type##_Kernel() \
48+ { \
49+ std::shared_ptr<clazz> ptr = nullptr; \
50+ ptr = MakeShared<clazz>(); \
51+ return ptr; \
52+ } \
53+ bool g_##type##_Kernel_Creator __attribute__((unused)) = RegistCpuKernelV2(type, Creator_##type##_Kernel)
44}54}
45#endif // CPU_KERNEL_H55#endif // CPU_KERNEL_H
@@ -32,11 +32,39 @@ class AICPU_VISIBILITY CpuKernelRegister {
32 */32 */
33 std::shared_ptr<CpuKernel> GetCpuKernel(const std::string &op_type);33 std::shared_ptr<CpuKernel> GetCpuKernel(const std::string &op_type);
34 34 
35+ /*
36+ * get cpu kernel V2.
37+ * param op_type: the op type of kernel
38+ * @return shared_ptr<CpuKernel>: cpu kernel ptr
39+ *
40+ * V2 查找仅访问 creatorMapV2_, 未命中返回 nullptr, 不回退到 V1.
41+ * 本函数不打印"未注册"日志, 由调用方根据命中情况输出更有信息量的日志
42+ * (例如命中哪个 so).
43+ */
44+ std::shared_ptr<CpuKernel> GetCpuKernelV2(const std::string &op_type);
45+ 
46+ /*
47+ * check whether the op type is registered in V2 without instantiating kernel.
48+ * param op_type: the op type of kernel
49+ * @return bool: true if registered in V2, otherwise false
50+ *
51+ * 轻量查询接口: 仅查 creatorMapV2_ 是否存在该 op_type, 不会通过
52+ * creator 函数构造 kernel. 供上层在"V2 优先, V1 兜底"的路由场景使用,
53+ * 避免重复构造带来的开销.
54+ */
55+ bool IsRegisteredV2(const std::string &op_type) const;
56+ 
35 /*57 /*
36 * get all cpu kernel registered op types.58 * get all cpu kernel registered op types.
37 * @return std::vector<string>: all cpu kernel registered op type59 * @return std::vector<string>: all cpu kernel registered op type
38 */60 */
39 std::vector<std::string> GetAllRegisteredOpTypes() const;61 std::vector<std::string> GetAllRegisteredOpTypes() const;
62+
63+ /*
64+ * get all cpu kernel registered op types V2.
65+ * @return std::vector<string>: all cpu kernel registered op type
66+ */
67+ std::vector<std::string> GetAllRegisteredOpTypesV2() const;
40 68 
41 /*69 /*
42 * run cpu kernel.70 * run cpu kernel.
@@ -45,6 +73,13 @@ class AICPU_VISIBILITY CpuKernelRegister {
45 */73 */
46 uint32_t RunCpuKernel(CpuKernelContext &ctx);74 uint32_t RunCpuKernel(CpuKernelContext &ctx);
47 75 
76+ /*
77+ * run cpu kernel V2.
78+ * param ctx: context of kernel
79+ * @return uint32_t: 0->success other->failed
80+ */
81+ uint32_t RunCpuKernelV2(CpuKernelContext &ctx);
82+ 
48 /*83 /*
49 * run async cpu kernel.84 * run async cpu kernel.
50 * @param ctx: context of kernel85 * @param ctx: context of kernel
@@ -58,6 +93,19 @@ class AICPU_VISIBILITY CpuKernelRegister {
58 const uint32_t wait_id,93 const uint32_t wait_id,
59 std::function<uint32_t()> cb);94 std::function<uint32_t()> cb);
60 95 
96+ /*
97+ * run async cpu kernel V2.
98+ * @param ctx: context of kernel
99+ * @param wait_type : event wait type
100+ * @param wait_id : event wait id
101+ * @param cb : callback function
102+ * @return uint32_t: 0->success other->failed
103+ */
104+ uint32_t RunCpuKernelAsyncV2(CpuKernelContext &ctx,
105+ const uint8_t wait_type,
106+ const uint32_t wait_id,
107+ std::function<uint32_t()> cb);
108+ 
61 // CpuKernel registration function to register different types of kernel to109 // CpuKernel registration function to register different types of kernel to
62 // the factory110 // the factory
63 class Registerar {111 class Registerar {
@@ -71,6 +119,19 @@ class AICPU_VISIBILITY CpuKernelRegister {
71 Registerar &operator=(Registerar &&) = delete;119 Registerar &operator=(Registerar &&) = delete;
72 };120 };
73 121 
122+ // CpuKernel registration function V2 to register different types of kernel to
123+ // the factory
124+ class RegisterarV2 {
125+ public:
126+ RegisterarV2(const std::string &type, const KERNEL_CREATOR_FUN &fun);
127+ ~RegisterarV2() = default;
128+ 
129+ RegisterarV2(const RegisterarV2 &) = delete;
130+ RegisterarV2(RegisterarV2 &&) = delete;
131+ RegisterarV2 &operator=(const RegisterarV2 &) = delete;
132+ RegisterarV2 &operator=(RegisterarV2 &&) = delete;
133+ };
134+ 
74 protected:135 protected:
75 CpuKernelRegister() = default;136 CpuKernelRegister() = default;
76 ~CpuKernelRegister() = default;137 ~CpuKernelRegister() = default;
@@ -83,8 +144,20 @@ class AICPU_VISIBILITY CpuKernelRegister {
83 // register creator, this function will call in the constructor144 // register creator, this function will call in the constructor
84 void Register(const std::string &type, const KERNEL_CREATOR_FUN &fun);145 void Register(const std::string &type, const KERNEL_CREATOR_FUN &fun);
85 146 
147+ // register creator V2, this function will call in the constructor
148+ void RegisterV2(const std::string &type, const KERNEL_CREATOR_FUN &fun);
149+ 
86 private:150 private:
151+ uint32_t RunCpuKernelCommon(CpuKernelContext &ctx, const std::string type, const std::shared_ptr<CpuKernel> kernel);
152+ uint32_t SetAsyncKernelContext(const std::string &type, const uint8_t wait_type,
153+ const uint32_t wait_id);
154+ uint32_t RunCpuKernelAsyncCommon(CpuKernelContext &ctx,
155+ const uint8_t wait_type,
156+ const uint32_t wait_id,
157+ std::function<uint32_t()> cb,
158+ const std::shared_ptr<CpuKernel> kernel);
87 std::map<std::string, KERNEL_CREATOR_FUN> creatorMap_; // kernel map159 std::map<std::string, KERNEL_CREATOR_FUN> creatorMap_; // kernel map
160+ std::map<std::string, KERNEL_CREATOR_FUN> creatorMapV2_; // kernel map V2
88};161};
89} // namespace aicpu162} // namespace aicpu
90#endif // AICPU_CONTEXT_INC_REGISTAR_H_163#endif // AICPU_CONTEXT_INC_REGISTAR_H_