已合并
【fix】: OM2去除内核二进制重复加载 #4640
【fix】: OM2去除内核二进制重复加载 #4640
已合并
wangbin创建于 12 天前
17 个文件变更+526-98
@@ -284,7 +284,8 @@ Status ResolveStablePart(StablePartId id, std::string &output) {
284 "aclError Om2ModelCreate(GertModelHandle *model_handle, aclmdlRI *rt_model_handle, "284 "aclError Om2ModelCreate(GertModelHandle *model_handle, aclmdlRI *rt_model_handle, "
285 "const char **bin_files, const void **bin_data, uint64_t *bin_size, size_t bin_num, void **constants, "285 "const char **bin_files, const void **bin_data, uint64_t *bin_size, size_t bin_num, void **constants, "
286 "void **var_addrs, void *work_ptr, uint64_t *session_id, uint32_t model_id, void *instance_handle, "286 "void **var_addrs, void *work_ptr, uint64_t *session_id, uint32_t model_id, void *instance_handle, "
287- "int32_t priority, uint64_t reuse_zero_copy, aclmdlRI external_rt_model, "287+ "const GertModelLoadCallbacks *callbacks, int32_t priority, uint64_t reuse_zero_copy, aclmdlRI "
288+ "external_rt_model, "
288 "aclrtStream *external_streams, uint64_t external_stream_num, "289 "aclrtStream *external_streams, uint64_t external_stream_num, "
289 "aclrtNotify *external_notifies, uint64_t external_notify_num, "290 "aclrtNotify *external_notifies, uint64_t external_notify_num, "
290 "aclrtEvent *external_events, uint64_t external_event_num, "291 "aclrtEvent *external_events, uint64_t external_event_num, "
@@ -295,7 +296,7 @@ Status ResolveStablePart(StablePartId id, std::string &output) {
295 " return ACL_ERROR_FAILURE;\n"296 " return ACL_ERROR_FAILURE;\n"
296 " }\n"297 " }\n"
297 " auto *obj = new om2::Om2Model(bin_files, bin_data, bin_size, bin_num, constants, var_addrs, "298 " auto *obj = new om2::Om2Model(bin_files, bin_data, bin_size, bin_num, constants, var_addrs, "
298- "work_ptr, session_id, model_id, instance_handle, priority);\n"299+ "work_ptr, session_id, model_id, instance_handle, callbacks, priority);\n"
299 " if (obj == nullptr) {\n"300 " if (obj == nullptr) {\n"
300 " OM2_LOGE(\"Om2ModelCreate: new Om2Model failed\");\n"301 " OM2_LOGE(\"Om2ModelCreate: new Om2Model failed\");\n"
301 " return ACL_ERROR_FAILURE;\n"302 " return ACL_ERROR_FAILURE;\n"
@@ -370,6 +371,7 @@ Status ResolveStablePart(StablePartId id, std::string &output) {
370 " config->bin_size, static_cast<size_t>(config->bin_num), config->constants,\n"371 " config->bin_size, static_cast<size_t>(config->bin_num), config->constants,\n"
371 " config->var_addrs, config->work_ptr, config->session_id,\n"372 " config->var_addrs, config->work_ptr, config->session_id,\n"
372 " static_cast<uint32_t>(config->model_id), config->instance_handle,\n"373 " static_cast<uint32_t>(config->model_id), config->instance_handle,\n"
374+ " config->callbacks,\n"
373 " static_cast<int32_t>(config->priority), config->reuse_zero_copy,\n"375 " static_cast<int32_t>(config->priority), config->reuse_zero_copy,\n"
374 " config->external_rt_model,\n"376 " config->external_rt_model,\n"
375 " config->external_streams, config->external_stream_num,\n"377 " config->external_streams, config->external_stream_num,\n"
@@ -549,11 +551,21 @@ Status ResolveStablePart(StablePartId id, std::string &output) {
549 "};\n"551 "};\n"
550 "\n"552 "\n"
551 "using GertModelLaunchFunc = int32_t (*)(void *instance_handle, GertModelTaskLaunchInfo *launch_info);\n"553 "using GertModelLaunchFunc = int32_t (*)(void *instance_handle, GertModelTaskLaunchInfo *launch_info);\n"
554+ "using LockBinHandleStoreFunc = int32_t (*)();\n"
555+ "using UnlockBinHandleStoreFunc = int32_t (*)();\n"
556+ "using QueryBinHandleFromStoreFunc = int32_t (*)(const char *bin_id, aclrtBinHandle *bin_handle);\n"
557+ "using SaveBinHandleToStoreFunc = int32_t (*)(const char *bin_id, aclrtBinHandle bin_handle);\n"
558+ "using ReleaseBinHandleFromStoreFunc = int32_t (*)(const char *bin_id, uint8_t *need_unload);\n"
552 "\n"559 "\n"
553 "struct GertModelLoadCallbacks {\n"560 "struct GertModelLoadCallbacks {\n"
554 " uint64_t struct_size = sizeof(GertModelLoadCallbacks);\n"561 " uint64_t struct_size = sizeof(GertModelLoadCallbacks);\n"
555 " ReportModelBaseInfoFunc report_model_base_info = nullptr;\n"562 " ReportModelBaseInfoFunc report_model_base_info = nullptr;\n"
556 " GertModelLaunchFunc launch_func = nullptr;\n"563 " GertModelLaunchFunc launch_func = nullptr;\n"
564+ " LockBinHandleStoreFunc lock_bin_handle_store = nullptr;\n"
565+ " UnlockBinHandleStoreFunc unlock_bin_handle_store = nullptr;\n"
566+ " QueryBinHandleFromStoreFunc query_bin_handle_from_store = nullptr;\n"
567+ " SaveBinHandleToStoreFunc save_bin_handle_to_store = nullptr;\n"
568+ " ReleaseBinHandleFromStoreFunc release_bin_handle_from_store = nullptr;\n"
557 "};\n"569 "};\n"
558 "\n"570 "\n"
559 "using ReportModelRunFunc = int32_t (*)(void *instance_handle, const struct GertModelRunReportInfo "571 "using ReportModelRunFunc = int32_t (*)(void *instance_handle, const struct GertModelRunReportInfo "
@@ -148,7 +148,8 @@ ClassDecl *InterfaceFileCodeGenerator::BuildOm2ModelClass(const Om2CodegenModel
148 {ast_.Var("const char **", "bin_files"), ast_.Var("const void **", "bin_data"),148 {ast_.Var("const char **", "bin_files"), ast_.Var("const void **", "bin_data"),
149 ast_.Var("uint64_t *", "bin_size"), ast_.Var("size_t", "bin_num"), ast_.Var("void **", "constants"),149 ast_.Var("uint64_t *", "bin_size"), ast_.Var("size_t", "bin_num"), ast_.Var("void **", "constants"),
150 ast_.Var("void **", "var_addrs"), ast_.Var("void *", "work_ptr"), ast_.Var("uint64_t *", "session_id"),150 ast_.Var("void **", "var_addrs"), ast_.Var("void *", "work_ptr"), ast_.Var("uint64_t *", "session_id"),
151- ast_.Var("uint32_t", "model_id"), ast_.Var("void *", "instance_handle"), ast_.Var("int32_t", "priority")},151+ ast_.Var("uint32_t", "model_id"), ast_.Var("void *", "instance_handle"),
152+ ast_.Var("const GertModelLoadCallbacks *", "callbacks"), ast_.Var("int32_t", "priority")},
152 ""),153 ""),
153 ast_.DeclareMethod("~Om2Model", {}, ""),154 ast_.DeclareMethod("~Om2Model", {}, ""),
154 ast_.DeclareMethod("InitResources",155 ast_.DeclareMethod("InitResources",
@@ -186,6 +187,7 @@ ClassDecl *InterfaceFileCodeGenerator::BuildOm2ModelClass(const Om2CodegenModel
186 items.push_back(ast_.Field("uint64_t *", "session_id_"));187 items.push_back(ast_.Field("uint64_t *", "session_id_"));
187 items.push_back(ast_.Field("uint32_t", "model_id_"));188 items.push_back(ast_.Field("uint32_t", "model_id_"));
188 items.push_back(ast_.Field("void *", "instance_handle_"));189 items.push_back(ast_.Field("void *", "instance_handle_"));
190+ items.push_back(ast_.Field("GertModelLoadCallbacks", "callbacks_"));
189 items.push_back(ast_.Field("uint64_t", "kernel_id_"));191 items.push_back(ast_.Field("uint64_t", "kernel_id_"));
190 items.push_back(ast_.Field("std::vector<void *>", "dev_ext_info_mem_ptrs_"));192 items.push_back(ast_.Field("std::vector<void *>", "dev_ext_info_mem_ptrs_"));
191 items.push_back(ast_.Field("std::map<uint32_t, void *>", "mem_event_id_mem_map_"));193 items.push_back(ast_.Field("std::map<uint32_t, void *>", "mem_event_id_mem_map_"));
@@ -199,6 +201,7 @@ ClassDecl *InterfaceFileCodeGenerator::BuildOm2ModelClass(const Om2CodegenModel
199void InterfaceFileCodeGenerator::DealParamForOm2ModelClass(std::vector<DeclNode *> &items,201void InterfaceFileCodeGenerator::DealParamForOm2ModelClass(std::vector<DeclNode *> &items,
200 const RuntimeResourceSemantic &runtime) {202 const RuntimeResourceSemantic &runtime) {
201 if (runtime.kernel_bin_num > 0U) {203 if (runtime.kernel_bin_num > 0U) {
204+ items.push_back(ast_.Field("std::vector<std::string>", "bin_ids_"));
202 items.push_back(ast_.Field("std::vector<aclrtBinHandle>", "bin_handles_"));205 items.push_back(ast_.Field("std::vector<aclrtBinHandle>", "bin_handles_"));
203 }206 }
204 items.push_back(ast_.Field("std::vector<aclrtFuncHandle>", "func_handles_"));207 items.push_back(ast_.Field("std::vector<aclrtFuncHandle>", "func_handles_"));
@@ -21,6 +21,7 @@ StructDecl *KernelRegFileCodeGenerator::BuildBinaryBufferStruct() const {
21 21 
22StructDecl *KernelRegFileCodeGenerator::BuildAicoreRegisterInfoStruct() const {22StructDecl *KernelRegFileCodeGenerator::BuildAicoreRegisterInfoStruct() const {
23 return ast_.Struct("AicoreRegisterInfo", {23 return ast_.Struct("AicoreRegisterInfo", {
24+ ast_.Field("const char *", "bin_id"),
24 ast_.Field("uint32_t", "magic"),25 ast_.Field("uint32_t", "magic"),
25 ast_.Field("bool", "use_tiling_key", false),26 ast_.Field("bool", "use_tiling_key", false),
26 ast_.Field("uint64_t", "tiling_key", 0U),27 ast_.Field("uint64_t", "tiling_key", 0U),
@@ -31,6 +32,7 @@ StructDecl *KernelRegFileCodeGenerator::BuildAicoreRegisterInfoStruct() const {
31 32 
32StructDecl *KernelRegFileCodeGenerator::BuildAicpuRegisterInfoStruct() const {33StructDecl *KernelRegFileCodeGenerator::BuildAicpuRegisterInfoStruct() const {
33 return ast_.Struct("AicpuRegisterInfo", {34 return ast_.Struct("AicpuRegisterInfo", {
35+ ast_.Field("const char *", "bin_id"),
34 ast_.Field("const char *", "op_type"),36 ast_.Field("const char *", "op_type"),
35 ast_.Field("const char *", "so_name"),37 ast_.Field("const char *", "so_name"),
36 ast_.Field("const char *", "kernel_name"),38 ast_.Field("const char *", "kernel_name"),
@@ -40,6 +42,7 @@ StructDecl *KernelRegFileCodeGenerator::BuildAicpuRegisterInfoStruct() const {
40 42 
41StructDecl *KernelRegFileCodeGenerator::BuildCustAicpuRegisterInfoStruct() const {43StructDecl *KernelRegFileCodeGenerator::BuildCustAicpuRegisterInfoStruct() const {
42 return ast_.Struct("CustAicpuRegisterInfo", {44 return ast_.Struct("CustAicpuRegisterInfo", {
45+ ast_.Field("const char *", "bin_id"),
43 ast_.Field("std::string", "file"),46 ast_.Field("std::string", "file"),
44 ast_.Field("const char *", "op_type"),47 ast_.Field("const char *", "op_type"),
45 ast_.Field("const char *", "func_name"),48 ast_.Field("const char *", "func_name"),
@@ -61,6 +64,7 @@ FunctionDef *KernelRegFileCodeGenerator::BuildAssembleAicpuLoadOptions() const {
61}64}
62 65 
63FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicoreKernel() const {66FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicoreKernel() const {
67+ auto bin_id = ast_.Var("std::string &", "bin_id");
64 auto bin_handle = ast_.Var("aclrtBinHandle &", "bin_handle");68 auto bin_handle = ast_.Var("aclrtBinHandle &", "bin_handle");
65 auto func_handle = ast_.Var("aclrtFuncHandle &", "func_handle");69 auto func_handle = ast_.Var("aclrtFuncHandle &", "func_handle");
66 auto register_info = ast_.Var("const AicoreRegisterInfo &", "register_info");70 auto register_info = ast_.Var("const AicoreRegisterInfo &", "register_info");
@@ -68,9 +72,13 @@ FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicoreKernel() const {
68 auto bin_info = ast_.Var("auto &", "bin_info");72 auto bin_info = ast_.Var("auto &", "bin_info");
69 auto load_options = ast_.Var("aclrtBinaryLoadOptions", "load_options");73 auto load_options = ast_.Var("aclrtBinaryLoadOptions", "load_options");
70 auto option = ast_.Var("aclrtBinaryLoadOption", "option");74 auto option = ast_.Var("aclrtBinaryLoadOption", "option");
75+ auto callbacks = ast_.Var("const GertModelLoadCallbacks &", "callbacks");
76+ auto bin_lock_guard = ast_.Lambda({ast_.CaptureRef(callbacks)},
77+ {ast_.IgnoreOutput(ast_.Call("callbacks.unlock_bin_handle_store", {}))});
71 return ast_.DefineFunction(78 return ast_.DefineFunction(
72- "RegisterAicoreKernel", {bin_handle, func_handle, register_info, bin_info_map}, "aclError",79+ "RegisterAicoreKernel", {bin_id, bin_handle, func_handle, register_info, bin_info_map, callbacks}, "aclError",
73 {80 {
81+ ast_.Assign(bin_id, register_info.Attr("bin_id")),
74 ast_.VarDecl(bin_info, bin_info_map[register_info.Attr("file")]),82 ast_.VarDecl(bin_info, bin_info_map[register_info.Attr("file")]),
75 ast_.VarDecl(load_options),83 ast_.VarDecl(load_options),
76 ast_.VarDecl(option),84 ast_.VarDecl(option),
@@ -78,8 +86,14 @@ FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicoreKernel() const {
78 ast_.Assign(load_options.Attr("options"), option.Addr()),86 ast_.Assign(load_options.Attr("options"), option.Addr()),
79 ast_.Assign(option.Attr("type"), "ACL_RT_BINARY_LOAD_OPT_MAGIC"),87 ast_.Assign(option.Attr("type"), "ACL_RT_BINARY_LOAD_OPT_MAGIC"),
80 ast_.Assign(option.Attr("value").Attr("magic"), register_info.Attr("magic")),88 ast_.Assign(option.Attr("value").Attr("magic"), register_info.Attr("magic")),
81- ChkStatus(AclrtBinaryLoadFromData(bin_info.Attr("data"), bin_info.Attr("size"), load_options.Addr(),89+ ChkStatus(ast_.Call("callbacks.lock_bin_handle_store", {})),
82- bin_handle.Addr())),90+ MakeGuard("bin_lock_guard", bin_lock_guard),
91+ ChkStatus(
92+ ast_.Call("callbacks.query_bin_handle_from_store", {register_info.Attr("bin_id"), bin_handle.Addr()})),
93+ ast_.If(bin_handle == "nullptr",
94+ {ChkStatus(AclrtBinaryLoadFromData(bin_info.Attr("data"), bin_info.Attr("size"), load_options.Addr(),
95+ bin_handle.Addr()))}),
96+ ChkStatus(ast_.Call("callbacks.save_bin_handle_to_store", {register_info.Attr("bin_id"), bin_handle})),
83 ast_.If(97 ast_.If(
84 register_info.Attr("use_tiling_key"),98 register_info.Attr("use_tiling_key"),
85 {99 {
@@ -94,6 +108,7 @@ FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicoreKernel() const {
94}108}
95 109 
96FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicpuKernel() const {110FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicpuKernel() const {
111+ auto bin_id = ast_.Var("std::string &", "bin_id");
97 auto bin_handle = ast_.Var("aclrtBinHandle &", "bin_handle");112 auto bin_handle = ast_.Var("aclrtBinHandle &", "bin_handle");
98 auto func_handle = ast_.Var("aclrtFuncHandle &", "func_handle");113 auto func_handle = ast_.Var("aclrtFuncHandle &", "func_handle");
99 auto register_info = ast_.Var("const AicpuRegisterInfo &", "register_info");114 auto register_info = ast_.Var("const AicpuRegisterInfo &", "register_info");
@@ -102,9 +117,13 @@ FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicpuKernel() const {
102 auto json_path = ast_.Var("std::string", "json_path");117 auto json_path = ast_.Var("std::string", "json_path");
103 auto cleanup_guard =118 auto cleanup_guard =
104 ast_.Lambda({ast_.CaptureRef(json_path)}, {ast_.IgnoreOutput(ast_.RemoveFile(json_path.CStr()))});119 ast_.Lambda({ast_.CaptureRef(json_path)}, {ast_.IgnoreOutput(ast_.RemoveFile(json_path.CStr()))});
120+ auto callbacks = ast_.Var("const GertModelLoadCallbacks &", "callbacks");
121+ auto bin_lock_guard = ast_.Lambda({ast_.CaptureRef(callbacks)},
122+ {ast_.IgnoreOutput(ast_.Call("callbacks.unlock_bin_handle_store", {}))});
105 return ast_.DefineFunction(123 return ast_.DefineFunction(
106- "RegisterAicpuKernel", {bin_handle, func_handle, register_info}, "aclError",124+ "RegisterAicpuKernel", {bin_id, bin_handle, func_handle, register_info, callbacks}, "aclError",
107 {125 {
126+ ast_.Assign(bin_id, register_info.Attr("bin_id")),
108 ast_.VarDecl(json_path),127 ast_.VarDecl(json_path),
109 ChkStatus(GenerateJsonFile(register_info, json_path)),128 ChkStatus(GenerateJsonFile(register_info, json_path)),
110 MakeGuard("json_guard", cleanup_guard),129 MakeGuard("json_guard", cleanup_guard),
@@ -115,13 +134,22 @@ FunctionDef *KernelRegFileCodeGenerator::BuildRegisterAicpuKernel() const {
115 ast_.Assign(load_options.Attr("options"), option.Addr()),134 ast_.Assign(load_options.Attr("options"), option.Addr()),
116 ast_.Assign(option.Attr("type"), "ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE"),135 ast_.Assign(option.Attr("type"), "ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE"),
117 ast_.Assign(option.Attr("value").Attr("cpuKernelMode"), 0),136 ast_.Assign(option.Attr("value").Attr("cpuKernelMode"), 0),
118- ChkStatus(AclrtBinaryLoadFromFile(json_path.CStr(), load_options.Addr(), bin_handle.Addr())),137+ ChkStatus(ast_.Call("callbacks.lock_bin_handle_store", {})),
138+ MakeGuard("bin_lock_guard", bin_lock_guard),
139+ ChkStatus(
140+ ast_.Call("callbacks.query_bin_handle_from_store", {register_info.Attr("bin_id"), bin_handle.Addr()})),
141+ ast_.If(bin_handle == "nullptr",
142+ {
143+ ChkStatus(AclrtBinaryLoadFromFile(json_path.CStr(), load_options.Addr(), bin_handle.Addr())),
144+ }),
145+ ChkStatus(ast_.Call("callbacks.save_bin_handle_to_store", {register_info.Attr("bin_id"), bin_handle})),
119 ChkStatus(AclrtBinaryGetFunction(bin_handle, register_info.Attr("op_type"), func_handle.Addr())),146 ChkStatus(AclrtBinaryGetFunction(bin_handle, register_info.Attr("op_type"), func_handle.Addr())),
120 ast_.Return("ACL_SUCCESS"),147 ast_.Return("ACL_SUCCESS"),
121 });148 });
122}149}
123 150 
124FunctionDef *KernelRegFileCodeGenerator::BuildRegisterCustAicpuKernel() const {151FunctionDef *KernelRegFileCodeGenerator::BuildRegisterCustAicpuKernel() const {
152+ auto bin_id = ast_.Var("std::string &", "bin_id");
125 auto bin_handle = ast_.Var("aclrtBinHandle &", "bin_handle");153 auto bin_handle = ast_.Var("aclrtBinHandle &", "bin_handle");
126 auto func_handle = ast_.Var("aclrtFuncHandle &", "func_handle");154 auto func_handle = ast_.Var("aclrtFuncHandle &", "func_handle");
127 auto register_info = ast_.Var("const CustAicpuRegisterInfo &", "register_info");155 auto register_info = ast_.Var("const CustAicpuRegisterInfo &", "register_info");
@@ -129,22 +157,34 @@ FunctionDef *KernelRegFileCodeGenerator::BuildRegisterCustAicpuKernel() const {
129 auto bin_info = ast_.Var("auto &", "bin_info");157 auto bin_info = ast_.Var("auto &", "bin_info");
130 auto load_options = ast_.Var("aclrtBinaryLoadOptions", "load_options");158 auto load_options = ast_.Var("aclrtBinaryLoadOptions", "load_options");
131 auto option = ast_.Var("aclrtBinaryLoadOption", "option");159 auto option = ast_.Var("aclrtBinaryLoadOption", "option");
132- return ast_.DefineFunction("RegisterCustAicpuKernel", {bin_handle, func_handle, register_info, bin_info_map},160+ auto callbacks = ast_.Var("const GertModelLoadCallbacks &", "callbacks");
133- "aclError",161+ auto bin_lock_guard = ast_.Lambda({ast_.CaptureRef(callbacks)},
134- {162+ {ast_.IgnoreOutput(ast_.Call("callbacks.unlock_bin_handle_store", {}))});
135- ast_.VarDecl(bin_info, bin_info_map[register_info.Attr("file")]),163+ return ast_.DefineFunction(
136- ast_.VarDecl(load_options),164+ "RegisterCustAicpuKernel", {bin_id, bin_handle, func_handle, register_info, bin_info_map, callbacks}, "aclError",
137- ast_.VarDecl(option),165+ {
138- ast_.Assign(load_options.Attr("numOpt"), 1),166+ ast_.Assign(bin_id, register_info.Attr("bin_id")),
139- ast_.Assign(load_options.Attr("options"), option.Addr()),167+ ast_.VarDecl(bin_info, bin_info_map[register_info.Attr("file")]),
140- ast_.Assign(option.Attr("type"), "ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE"),168+ ast_.VarDecl(load_options),
141- ast_.Assign(option.Attr("value").Attr("cpuKernelMode"), 2),169+ ast_.VarDecl(option),
142- ChkStatus(AclrtBinaryLoadFromData(bin_info.Attr("data"), bin_info.Attr("size"),170+ ast_.Assign(load_options.Attr("numOpt"), 1),
143- load_options.Addr(), bin_handle.Addr())),171+ ast_.Assign(load_options.Attr("options"), option.Addr()),
144- ChkStatus(AclrtRegisterCpuFunc(bin_handle, register_info.Attr("func_name"),172+ ast_.Assign(option.Attr("type"), "ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE"),
145- register_info.Attr("op_type"), func_handle.Addr())),173+ ast_.Assign(option.Attr("value").Attr("cpuKernelMode"), 2),
146- ast_.Return("ACL_SUCCESS"),174+ ChkStatus(ast_.Call("callbacks.lock_bin_handle_store", {})),
147- });175+ MakeGuard("bin_lock_guard", bin_lock_guard),
176+ ChkStatus(
177+ ast_.Call("callbacks.query_bin_handle_from_store", {register_info.Attr("bin_id"), bin_handle.Addr()})),
178+ ast_.If(bin_handle == "nullptr",
179+ {
180+ ChkStatus(AclrtBinaryLoadFromData(bin_info.Attr("data"), bin_info.Attr("size"),
181+ load_options.Addr(), bin_handle.Addr())),
182+ }),
183+ ChkStatus(ast_.Call("callbacks.save_bin_handle_to_store", {register_info.Attr("bin_id"), bin_handle})),
184+ ChkStatus(AclrtRegisterCpuFunc(bin_handle, register_info.Attr("func_name"), register_info.Attr("op_type"),
185+ func_handle.Addr())),
186+ ast_.Return("ACL_SUCCESS"),
187+ });
148}188}
149 189 
150MethodDef *KernelRegFileCodeGenerator::BuildRegisterKernels(const Om2CodegenModel &codegen_model) {190MethodDef *KernelRegFileCodeGenerator::BuildRegisterKernels(const Om2CodegenModel &codegen_model) {
@@ -153,26 +193,32 @@ MethodDef *KernelRegFileCodeGenerator::BuildRegisterKernels(const Om2CodegenMode
153 for (const auto &binary : codegen_model.kernel_registry.binaries) {193 for (const auto &binary : codegen_model.kernel_registry.binaries) {
154 if (binary.kind == KernelBinaryKind::kAicore || binary.kind == KernelBinaryKind::kAllKernel) {194 if (binary.kind == KernelBinaryKind::kAicore || binary.kind == KernelBinaryKind::kAllKernel) {
155 const bool use_tiling_key = (binary.kind == KernelBinaryKind::kAllKernel);195 const bool use_tiling_key = (binary.kind == KernelBinaryKind::kAllKernel);
156- (void)items.emplace_back(ChkStatus(CallRegisterAicoreKernel(196+ (void)items.emplace_back(
157- bin_handles_[binary.func_handle_index], func_handles_[binary.func_handle_index],197+ ChkStatus(CallRegisterAicoreKernel(bin_ids_[binary.func_handle_index], bin_handles_[binary.func_handle_index],
158- {binary.magic, use_tiling_key, binary.tiling_key, ast_.Str(binary.kernel_name), ast_.Str(binary.file_name)},198+ func_handles_[binary.func_handle_index],
159- bin_info_map_)));199+ {ast_.Str(binary.bin_id), binary.magic, use_tiling_key, binary.tiling_key,
200+ ast_.Str(binary.kernel_name), ast_.Str(binary.file_name)},
201+ bin_info_map_, callbacks_)));
160 continue;202 continue;
161 }203 }
162 if (binary.kind == KernelBinaryKind::kAicpu) {204 if (binary.kind == KernelBinaryKind::kAicpu) {
163 (void)items.emplace_back(ChkStatus(205 (void)items.emplace_back(ChkStatus(
164- CallRegisterAicpuKernel(bin_handles_[binary.func_handle_index], func_handles_[binary.func_handle_index],206+ CallRegisterAicpuKernel(bin_ids_[binary.func_handle_index], bin_handles_[binary.func_handle_index],
165- {ast_.Str(binary.op_type), ast_.Str(binary.so_name), ast_.Str(binary.kernel_name),207+ func_handles_[binary.func_handle_index],
166- ast_.Str(binary.op_kernel_lib)})));208+ {ast_.Str(binary.bin_id), ast_.Str(binary.op_type), ast_.Str(binary.so_name),
209+ ast_.Str(binary.kernel_name), ast_.Str(binary.op_kernel_lib)},
210+ callbacks_)));
167 continue;211 continue;
168 }212 }
169 (void)items.emplace_back(ChkStatus(CallRegisterCustAicpuKernel(213 (void)items.emplace_back(ChkStatus(CallRegisterCustAicpuKernel(
170- bin_handles_[binary.func_handle_index], func_handles_[binary.func_handle_index],214+ bin_ids_[binary.func_handle_index], bin_handles_[binary.func_handle_index],
171- {ast_.Str(binary.file_name), ast_.Str(binary.op_type), ast_.Str(binary.kernel_name)}, bin_info_map_)));215+ func_handles_[binary.func_handle_index],
216+ {ast_.Str(binary.bin_id), ast_.Str(binary.file_name), ast_.Str(binary.op_type), ast_.Str(binary.kernel_name)},
217+ bin_info_map_, callbacks_)));
172 }218 }
173 (void)items.emplace_back(ast_.Call("OM2_LOGI", {ast_.Str("RegisterKernels done")}));219 (void)items.emplace_back(ast_.Call("OM2_LOGI", {ast_.Str("RegisterKernels done")}));
174 (void)items.emplace_back(ast_.Return("ACL_SUCCESS"));220 (void)items.emplace_back(ast_.Return("ACL_SUCCESS"));
175- return ast_.DefineMethod("Om2Model", "RegisterKernels", std::vector<VarRef>{}, "aclError", items);221+ return ast_.DefineMethod("Om2Model", "RegisterKernels", {}, "aclError", items);
176}222}
177 223 
178ExprRef KernelRegFileCodeGenerator::GenerateJsonFile(Arg register_info, Arg json_path) const {224ExprRef KernelRegFileCodeGenerator::GenerateJsonFile(Arg register_info, Arg json_path) const {
@@ -187,17 +233,20 @@ ExprRef KernelRegFileCodeGenerator::AssembleAicpuLoadOptionsCall(Arg load_option
187 return ast_.Call("AssembleAicpuLoadOptions", {load_options, cpu_kernel_mode});233 return ast_.Call("AssembleAicpuLoadOptions", {load_options, cpu_kernel_mode});
188}234}
189 235 
190-ExprRef KernelRegFileCodeGenerator::CallRegisterAicoreKernel(Arg bin_handle, Arg func_handle, Arg register_info,236+ExprRef KernelRegFileCodeGenerator::CallRegisterAicoreKernel(Arg bin_id, Arg bin_handle, Arg func_handle,
191- Arg bin_info_map) const {237+ Arg register_info, Arg bin_info_map, Arg callbacks) const {
192- return ast_.Call("RegisterAicoreKernel", {bin_handle, func_handle, register_info, bin_info_map});238+ return ast_.Call("RegisterAicoreKernel", {bin_id, bin_handle, func_handle, register_info, bin_info_map, callbacks});
193}239}
194 240 
195-ExprRef KernelRegFileCodeGenerator::CallRegisterAicpuKernel(Arg bin_handle, Arg func_handle, Arg register_info) const {241+ExprRef KernelRegFileCodeGenerator::CallRegisterAicpuKernel(Arg bin_id, Arg bin_handle, Arg func_handle,
196- return ast_.Call("RegisterAicpuKernel", {bin_handle, func_handle, register_info});242+ Arg register_info, Arg callbacks) const {
243+ return ast_.Call("RegisterAicpuKernel", {bin_id, bin_handle, func_handle, register_info, callbacks});
197}244}
198 245 
199-ExprRef KernelRegFileCodeGenerator::CallRegisterCustAicpuKernel(Arg bin_handle, Arg func_handle, Arg register_info,246+ExprRef KernelRegFileCodeGenerator::CallRegisterCustAicpuKernel(Arg bin_id, Arg bin_handle, Arg func_handle,
200- Arg bin_info_map) const {247+ Arg register_info, Arg bin_info_map,
201- return ast_.Call("RegisterCustAicpuKernel", {bin_handle, func_handle, register_info, bin_info_map});248+ Arg callbacks) const {
249+ return ast_.Call("RegisterCustAicpuKernel",
250+ {bin_id, bin_handle, func_handle, register_info, bin_info_map, callbacks});
202}251}
203} // namespace ge252} // namespace ge
@@ -33,9 +33,11 @@ class KernelRegFileCodeGenerator : public Om2ModelClassGeneratorBase {
33 ExprRef GenerateJsonFile(Arg register_info, Arg json_path) const;33 ExprRef GenerateJsonFile(Arg register_info, Arg json_path) const;
34 ExprRef ReadBinaryFileToBuffer(Arg file_path) const;34 ExprRef ReadBinaryFileToBuffer(Arg file_path) const;
35 ExprRef AssembleAicpuLoadOptionsCall(Arg load_options, Arg cpu_kernel_mode) const;35 ExprRef AssembleAicpuLoadOptionsCall(Arg load_options, Arg cpu_kernel_mode) const;
36- ExprRef CallRegisterAicoreKernel(Arg bin_handle, Arg func_handle, Arg register_info, Arg bin_info_map) const;36+ ExprRef CallRegisterAicoreKernel(Arg bin_id, Arg bin_handle, Arg func_handle, Arg register_info, Arg bin_info_map,
37- ExprRef CallRegisterAicpuKernel(Arg bin_handle, Arg func_handle, Arg register_info) const;37+ Arg callbacks) const;
38- ExprRef CallRegisterCustAicpuKernel(Arg bin_handle, Arg func_handle, Arg register_info, Arg bin_info_map) const;38+ ExprRef CallRegisterAicpuKernel(Arg bin_id, Arg bin_handle, Arg func_handle, Arg register_info, Arg callbacks) const;
39+ ExprRef CallRegisterCustAicpuKernel(Arg bin_id, Arg bin_handle, Arg func_handle, Arg register_info, Arg bin_info_map,
40+ Arg callbacks) const;
39};41};
40} // namespace ge42} // namespace ge
41 43 
@@ -25,6 +25,7 @@ MethodDef *ResourcesFileCodeGenerator::BuildOm2ModelConstructor(const Om2Codegen
25 auto session_id = ast_.Var("uint64_t *", "session_id");25 auto session_id = ast_.Var("uint64_t *", "session_id");
26 auto model_id = ast_.Var("uint32_t", "model_id");26 auto model_id = ast_.Var("uint32_t", "model_id");
27 auto instance_handle = ast_.Var("void *", "instance_handle");27 auto instance_handle = ast_.Var("void *", "instance_handle");
28+ auto callbacks = ast_.Var("const GertModelLoadCallbacks *", "callbacks");
28 auto priority = ast_.Var("int32_t", "priority");29 auto priority = ast_.Var("int32_t", "priority");
29 auto i = ast_.Var("size_t", "i");30 auto i = ast_.Var("size_t", "i");
30 std::vector<BodyItem> body = {31 std::vector<BodyItem> body = {
@@ -35,6 +36,7 @@ MethodDef *ResourcesFileCodeGenerator::BuildOm2ModelConstructor(const Om2Codegen
35 };36 };
36 const auto &runtime = codegen_model.runtime;37 const auto &runtime = codegen_model.runtime;
37 if (runtime.kernel_bin_num > 0U) {38 if (runtime.kernel_bin_num > 0U) {
39+ (void)body.emplace_back(bin_ids_.Resize(runtime.kernel_bin_num));
38 (void)body.emplace_back(bin_handles_.Resize(runtime.kernel_bin_num));40 (void)body.emplace_back(bin_handles_.Resize(runtime.kernel_bin_num));
39 (void)body.emplace_back(func_handles_.Resize(runtime.kernel_bin_num));41 (void)body.emplace_back(func_handles_.Resize(runtime.kernel_bin_num));
40 }42 }
@@ -54,16 +56,16 @@ MethodDef *ResourcesFileCodeGenerator::BuildOm2ModelConstructor(const Om2Codegen
54 return ast_.DefineMethod(56 return ast_.DefineMethod(
55 "Om2Model", "Om2Model",57 "Om2Model", "Om2Model",
56 {bin_files, bin_data, bin_size, bin_num, constants, var_addrs, work_ptr, session_id, model_id, instance_handle,58 {bin_files, bin_data, bin_size, bin_num, constants, var_addrs, work_ptr, session_id, model_id, instance_handle,
57- priority},59+ callbacks, priority},
58 "",60 "",
59 {ast_.MemberInit("constants_", constants), ast_.MemberInit("var_addrs_", var_addrs),61 {ast_.MemberInit("constants_", constants), ast_.MemberInit("var_addrs_", var_addrs),
60 ast_.MemberInit("total_dev_mem_ptr_", work_ptr), ast_.MemberInit("owns_total_dev_mem_", false),62 ast_.MemberInit("total_dev_mem_ptr_", work_ptr), ast_.MemberInit("owns_total_dev_mem_", false),
61 ast_.MemberInit("session_id_", session_id), ast_.MemberInit("model_id_", model_id),63 ast_.MemberInit("session_id_", session_id), ast_.MemberInit("model_id_", model_id),
62- ast_.MemberInit("instance_handle_", instance_handle), ast_.MemberInit("kernel_id_", 0),64+ ast_.MemberInit("instance_handle_", instance_handle), ast_.MemberInit("callbacks_", ast_.Deref(callbacks)),
63- ast_.MemberInit("session_scope_mem_ptr_", nullptr), ast_.MemberInit("priority_", priority),65+ ast_.MemberInit("kernel_id_", 0), ast_.MemberInit("session_scope_mem_ptr_", nullptr),
64- ast_.MemberInit("is_external_rt_model_", false), ast_.MemberInit("is_external_streams_", false),66+ ast_.MemberInit("priority_", priority), ast_.MemberInit("is_external_rt_model_", false),
65- ast_.MemberInit("is_external_notifies_", false), ast_.MemberInit("is_external_events_", false),67+ ast_.MemberInit("is_external_streams_", false), ast_.MemberInit("is_external_notifies_", false),
66- ast_.MemberInit("is_external_labels_", false)},68+ ast_.MemberInit("is_external_events_", false), ast_.MemberInit("is_external_labels_", false)},
67 body);69 body);
68}70}
69 71 
@@ -301,11 +303,19 @@ MethodDef *ResourcesFileCodeGenerator::BuildReleaseResourcesMethod(const Om2Code
301 {}, false));303 {}, false));
302 }304 }
303 if (runtime.kernel_bin_num > 0U) {305 if (runtime.kernel_bin_num > 0U) {
304- auto bin_handle = ast_.Var("auto", "bin_handle");306+ auto i = ast_.Var("size_t", "i");
305- (void)body.emplace_back(ast_.RangeFor(bin_handle, bin_handles_,307+ auto need_unload = ast_.Var("uint8_t", "need_unload");
306- {308+ (void)body.emplace_back(ast_.For(
307- ChkStatus(AclrtBinaryUnLoad(bin_handle)),309+ ast_.VarDecl(i, 0), i < bin_handles_.Size(), ast_.PostInc(i),
308- }));310+ {
311+ ChkStatus(ast_.Call("callbacks_.lock_bin_handle_store", {})),
312+ MakeGuard("bin_lock_guard",
313+ ast_.Lambda({LambdaCaptureSpec{"this", LambdaCaptureSpec::Kind::kByValue}},
314+ {ast_.IgnoreOutput(ast_.Call("callbacks_.unlock_bin_handle_store", {}))})),
315+ ast_.VarDecl(need_unload, 0),
316+ ChkStatus(ast_.Call("callbacks_.release_bin_handle_from_store", {bin_ids_[i].CStr(), need_unload.Addr()})),
317+ ast_.If(need_unload != 0, {ChkStatus(AclrtBinaryUnLoad(bin_handles_[i]))}),
318+ }));
309 }319 }
310 BuildReleaseResourcesMethodForControlTask(body, runtime);320 BuildReleaseResourcesMethodForControlTask(body, runtime);
311 auto i = ast_.Var("int", "i");321 auto i = ast_.Var("int", "i");
@@ -33,6 +33,7 @@ namespace ge {
33namespace {33namespace {
34constexpr uint32_t kTrueBranchStreamCount = 1U;34constexpr uint32_t kTrueBranchStreamCount = 1U;
35const std::string kTfSessionTask = "TfSessionTask";35const std::string kTfSessionTask = "TfSessionTask";
36+constexpr const ge::char_t *kAttrMemsetKernelBinId = "_memset_kernel_bin_id";
36 37 
37Node *GetPeerNode(const Node &netoutput_node, size_t input_index) {38Node *GetPeerNode(const Node &netoutput_node, size_t input_index) {
38 const auto &in_anchors = netoutput_node.GetAllInDataAnchorsPtr();39 const auto &in_anchors = netoutput_node.GetAllInDataAnchorsPtr();
@@ -53,6 +54,20 @@ bool CompareInputModelIoItem(const InputModelIoItem &lhs, const InputModelIoItem
53 return lhs.visit_order < rhs.visit_order;54 return lhs.visit_order < rhs.visit_order;
54}55}
55 56 
57+std::string BuildAicoreKernelBinId(const Om2CodegenModel &codegen_model, const domi::TaskDef &task_def,
58+ const OpDescPtr &op_desc) {
59+ std::string bin_id;
60+ const std::string kernel_id_attr = Om2CodegenUtils::IsSeparatelyCleanTask(op_desc, task_def.kernel().kernel_name())
61+ ? kAttrMemsetKernelBinId
62+ : ATTR_NAME_KERNEL_BIN_ID;
63+ (void)AttrUtils::GetStr(op_desc, kernel_id_attr, bin_id);
64+ if (bin_id.empty()) {
65+ (void)AttrUtils::GetStr(op_desc, ATTR_NAME_SESSION_GRAPH_ID, bin_id);
66+ bin_id += std::string("_" + codegen_model.model_name + op_desc->GetName());
67+ }
68+ return bin_id;
69+}
70+ 
56} // namespace71} // namespace
57 72 
58Status Om2CodegenModelBuilder::BuildHostArgsOffsets(const std::multimap<uint64_t, uint64_t> &io_addr_offset_map,73Status Om2CodegenModelBuilder::BuildHostArgsOffsets(const std::multimap<uint64_t, uint64_t> &io_addr_offset_map,
@@ -713,7 +728,7 @@ Status Om2CodegenModelBuilder::BuildKernelRegistry(const GeModelPtr &model,
713 728 
714 std::string kernel_name;729 std::string kernel_name;
715 if (is_aicore) {730 if (is_aicore) {
716- GE_ASSERT_SUCCESS(BuildKernelRegistryForAicore(codegen_model, op_desc, task_type));731+ GE_ASSERT_SUCCESS(BuildKernelRegistryForAicore(codegen_model, task_def, op_desc, task_type));
717 continue;732 continue;
718 }733 }
719 734 
@@ -753,7 +768,8 @@ Status Om2CodegenModelBuilder::BuildKernelRegistry(const GeModelPtr &model,
753 return SUCCESS;768 return SUCCESS;
754}769}
755 770 
756-Status Om2CodegenModelBuilder::BuildKernelRegistryForAicore(Om2CodegenModel &codegen_model, const OpDescPtr &op_desc,771+Status Om2CodegenModelBuilder::BuildKernelRegistryForAicore(Om2CodegenModel &codegen_model,
772+ const domi::TaskDef &task_def, const OpDescPtr &op_desc,
757 ModelTaskType task_type) {773 ModelTaskType task_type) {
758 const auto kernel_name_ptr = AttrUtils::GetStr(op_desc, "_kernelname");774 const auto kernel_name_ptr = AttrUtils::GetStr(op_desc, "_kernelname");
759 GE_ASSERT_NOTNULL(kernel_name_ptr, "[OM2] Failed to get kernel_name from op_desc, op=%s", op_desc->GetName().c_str());775 GE_ASSERT_NOTNULL(kernel_name_ptr, "[OM2] Failed to get kernel_name from op_desc, op=%s", op_desc->GetName().c_str());
@@ -771,8 +787,10 @@ Status Om2CodegenModelBuilder::BuildKernelRegistryForAicore(Om2CodegenModel &cod
771 aicore_sign = kernel_name + "#" + std::to_string(tiling_key);787 aicore_sign = kernel_name + "#" + std::to_string(tiling_key);
772 kind = KernelBinaryKind::kAllKernel;788 kind = KernelBinaryKind::kAllKernel;
773 }789 }
774- const auto RegisterKernel = [&codegen_model, &op_desc, &kind](const std::string &sign, const std::string &name,790+ const std::string bin_id = BuildAicoreKernelBinId(codegen_model, task_def, op_desc);
775- uint64_t tiling_key, bool is_atomic) -> Status {791+ const auto RegisterKernel = [&codegen_model, &op_desc, &kind](const std::string &sign, const std::string &bin_id,
792+ const std::string &name, uint64_t tiling_key,
793+ bool is_atomic) -> Status {
776 if (codegen_model.kernel_registry.func_handle_indices.find(sign) !=794 if (codegen_model.kernel_registry.func_handle_indices.find(sign) !=
777 codegen_model.kernel_registry.func_handle_indices.end()) {795 codegen_model.kernel_registry.func_handle_indices.end()) {
778 return SUCCESS;796 return SUCCESS;
@@ -780,16 +798,16 @@ Status Om2CodegenModelBuilder::BuildKernelRegistryForAicore(Om2CodegenModel &cod
780 std::string magic;798 std::string magic;
781 GE_CHK_STATUS(Om2CodegenUtils::GetMagic(op_desc, magic, is_atomic));799 GE_CHK_STATUS(Om2CodegenUtils::GetMagic(op_desc, magic, is_atomic));
782 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());800 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());
783- GELOGI("[OM2] RegisterKernel: op=%s, sign=%s, func_idx=%u, tiling_key=%lu", op_desc->GetNamePtr(), sign.c_str(),801+ GELOGI("[OM2] RegisterKernel: bin_id=%s, op=%s, sign=%s, func_idx=%u, tiling_key=%lu", bin_id.c_str(),
784- func_handle_index, tiling_key);802+ op_desc->GetNamePtr(), sign.c_str(), func_handle_index, tiling_key);
785 codegen_model.kernel_registry.binaries.push_back(803 codegen_model.kernel_registry.binaries.push_back(
786- KernelBinaryRecord{kind, name, Om2CodegenUtils::GetKernelNameWithExtension(name), "", "", "", magic, tiling_key,804+ KernelBinaryRecord{kind, bin_id, name, Om2CodegenUtils::GetKernelNameWithExtension(name), "", "", "", magic,
787- func_handle_index});805+ tiling_key, func_handle_index});
788 codegen_model.kernel_registry.func_handle_indices.emplace(sign, func_handle_index);806 codegen_model.kernel_registry.func_handle_indices.emplace(sign, func_handle_index);
789 return SUCCESS;807 return SUCCESS;
790 };808 };
791 809 
792- GE_CHK_STATUS(RegisterKernel(aicore_sign, kernel_name, tiling_key, false));810+ GE_CHK_STATUS(RegisterKernel(aicore_sign, bin_id, kernel_name, tiling_key, false));
793 811 
794 std::string atomic_kernel_name;812 std::string atomic_kernel_name;
795 const auto atomic_kernel_name_ptr = AttrUtils::GetStr(op_desc, ATOMIC_ATTR_TBE_KERNEL_NAME);813 const auto atomic_kernel_name_ptr = AttrUtils::GetStr(op_desc, ATOMIC_ATTR_TBE_KERNEL_NAME);
@@ -797,7 +815,7 @@ Status Om2CodegenModelBuilder::BuildKernelRegistryForAicore(Om2CodegenModel &cod
797 atomic_kernel_name = *atomic_kernel_name_ptr;815 atomic_kernel_name = *atomic_kernel_name_ptr;
798 }816 }
799 if (!atomic_kernel_name.empty()) {817 if (!atomic_kernel_name.empty()) {
800- GE_CHK_STATUS(RegisterKernel(atomic_kernel_name + "_atomic", atomic_kernel_name, 0U, true));818+ GE_CHK_STATUS(RegisterKernel(atomic_kernel_name + "_atomic", bin_id, atomic_kernel_name, 0U, true));
801 }819 }
802 return SUCCESS;820 return SUCCESS;
803}821}
@@ -807,12 +825,13 @@ Status Om2CodegenModelBuilder::BuildKernelRegistryForAicpu(Om2CodegenModel &code
807 const std::string &kernel_name,825 const std::string &kernel_name,
808 const std::string &aicpu_kernel_sign) {826 const std::string &aicpu_kernel_sign) {
809 const std::string &so_name = task_def.kernel().so_name();827 const std::string &so_name = task_def.kernel().so_name();
828+ const std::string bin_id = op_type + "_" + so_name + "_AicpuKernel";
810 const std::string op_kernel_lib = "AICPUKernel";829 const std::string op_kernel_lib = "AICPUKernel";
811 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());830 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());
812- GELOGI("[OM2] RegisterAicpu: op_type=%s, kernel=%s, sign=%s, func_idx=%u", op_type.c_str(), kernel_name.c_str(),831+ GELOGI("[OM2] RegisterAicpu: bin_id=%s, op_type=%s, kernel=%s, sign=%s, func_idx=%u", bin_id.c_str(), op_type.c_str(),
813- aicpu_kernel_sign.c_str(), func_handle_index);832+ kernel_name.c_str(), aicpu_kernel_sign.c_str(), func_handle_index);
814 codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{833 codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{
815- KernelBinaryKind::kAicpu, kernel_name, "", op_type, so_name, op_kernel_lib, "", 0U, func_handle_index});834+ KernelBinaryKind::kAicpu, bin_id, kernel_name, "", op_type, so_name, op_kernel_lib, "", 0U, func_handle_index});
816 (void)codegen_model.kernel_registry.func_handle_indices.emplace(aicpu_kernel_sign, func_handle_index);835 (void)codegen_model.kernel_registry.func_handle_indices.emplace(aicpu_kernel_sign, func_handle_index);
817 return SUCCESS;836 return SUCCESS;
818}837}
@@ -825,24 +844,26 @@ Status Om2CodegenModelBuilder::BuildKernelRegistryForCustAicpu(Om2CodegenModel &
825 GE_ASSERT_NOTNULL(cust_aicpu_bin_ptr);844 GE_ASSERT_NOTNULL(cust_aicpu_bin_ptr);
826 const size_t hash_id = std::hash<std::string>{}(std::string(845 const size_t hash_id = std::hash<std::string>{}(std::string(
827 cust_aicpu_bin_ptr->GetBinData(), cust_aicpu_bin_ptr->GetBinData() + cust_aicpu_bin_ptr->GetBinDataSize()));846 cust_aicpu_bin_ptr->GetBinData(), cust_aicpu_bin_ptr->GetBinData() + cust_aicpu_bin_ptr->GetBinDataSize()));
847+ const std::string bin_id = std::to_string(hash_id) + "_CustAicpuKernel";
828 const std::string file_name = std::to_string(hash_id) + "_CustAicpuKernel.o";848 const std::string file_name = std::to_string(hash_id) + "_CustAicpuKernel.o";
829 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());849 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());
830- GELOGI("[OM2] RegisterCustAicpu: op_type=%s, kernel=%s, sign=%s, func_idx=%u", op_type.c_str(), kernel_name.c_str(),850+ GELOGI("[OM2] RegisterCustAicpu: bin_id=%s, op_type=%s, kernel=%s, sign=%s, func_idx=%u", bin_id.c_str(),
831- kernel_sign.c_str(), func_handle_index);851+ op_type.c_str(), kernel_name.c_str(), kernel_sign.c_str(), func_handle_index);
832 codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{852 codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{
833- KernelBinaryKind::kCustAicpu, kernel_name, file_name, op_type, "", "", "", 0U, func_handle_index});853+ KernelBinaryKind::kCustAicpu, bin_id, kernel_name, file_name, op_type, "", "", "", 0U, func_handle_index});
834 (void)codegen_model.kernel_registry.func_handle_indices.emplace(kernel_sign, func_handle_index);854 (void)codegen_model.kernel_registry.func_handle_indices.emplace(kernel_sign, func_handle_index);
835 return SUCCESS;855 return SUCCESS;
836}856}
837 857 
838Status Om2CodegenModelBuilder::BuildKernelRegistryForTFAicpu(Om2CodegenModel &codegen_model, const std::string &op_type,858Status Om2CodegenModelBuilder::BuildKernelRegistryForTFAicpu(Om2CodegenModel &codegen_model, const std::string &op_type,
839 const std::string &tf_aicpu_kernel_sign) {859 const std::string &tf_aicpu_kernel_sign) {
860+ const std::string bin_id = op_type + "_libtf_kernels.so_AicpuKernel";
840 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());861 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());
841- GELOGI("[OM2] RegisterTFAicpu: op_type=%s, sign=%s, func_idx=%u", op_type.c_str(), tf_aicpu_kernel_sign.c_str(),862+ GELOGI("[OM2] RegisterTFAicpu: bin_id=%s, op_type=%s, sign=%s, func_idx=%u", bin_id.c_str(), op_type.c_str(),
842- func_handle_index);863+ tf_aicpu_kernel_sign.c_str(), func_handle_index);
843- codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{KernelBinaryKind::kAicpu, "TFOperateAPI", "",864+ codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{KernelBinaryKind::kAicpu, bin_id, "TFOperateAPI",
844- op_type, "libtf_kernels.so", "TFKernel", "", 0U,865+ "", op_type, "libtf_kernels.so", "TFKernel", "",
845- func_handle_index});866+ 0U, func_handle_index});
846 (void)codegen_model.kernel_registry.func_handle_indices.emplace(tf_aicpu_kernel_sign, func_handle_index);867 (void)codegen_model.kernel_registry.func_handle_indices.emplace(tf_aicpu_kernel_sign, func_handle_index);
847 return SUCCESS;868 return SUCCESS;
848}869}
@@ -850,12 +871,13 @@ Status Om2CodegenModelBuilder::BuildKernelRegistryForTFAicpu(Om2CodegenModel &co
850Status Om2CodegenModelBuilder::BuildKernelRegistryForTFAicpuSession(Om2CodegenModel &codegen_model,871Status Om2CodegenModelBuilder::BuildKernelRegistryForTFAicpuSession(Om2CodegenModel &codegen_model,
851 const std::string &op_type,872 const std::string &op_type,
852 const std::string &tf_aicpu_kernel_sign) {873 const std::string &tf_aicpu_kernel_sign) {
874+ const std::string bin_id = op_type + "_libtf_kernels.so_AicpuKernel";
853 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());875 const uint32_t func_handle_index = static_cast<uint32_t>(codegen_model.kernel_registry.func_handle_indices.size());
854- GELOGI("[OM2] RegisterTFAicpuSession: op_type=%s, sign=%s, func_idx=%u", op_type.c_str(),876+ GELOGI("[OM2] RegisterTFAicpuSession: bin_id=%s, op_type=%s, sign=%s, func_idx=%u", bin_id.c_str(), op_type.c_str(),
855 tf_aicpu_kernel_sign.c_str(), func_handle_index);877 tf_aicpu_kernel_sign.c_str(), func_handle_index);
856- codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{KernelBinaryKind::kAicpu, "TFOperateAPI", "",878+ codegen_model.kernel_registry.binaries.push_back(KernelBinaryRecord{KernelBinaryKind::kAicpu, bin_id, "TFOperateAPI",
857- op_type, "libtf_kernels.so", "TFKernel", "", 0U,879+ "", op_type, "libtf_kernels.so", "TFKernel", "",
858- func_handle_index});880+ 0U, func_handle_index});
859 (void)codegen_model.kernel_registry.func_handle_indices.emplace(tf_aicpu_kernel_sign, func_handle_index);881 (void)codegen_model.kernel_registry.func_handle_indices.emplace(tf_aicpu_kernel_sign, func_handle_index);
860 return SUCCESS;882 return SUCCESS;
861}883}
@@ -73,8 +73,8 @@ class Om2CodegenModelBuilder {
73 std::vector<om2::MemInfo> GetAllMemoryTypeSize(const GeModelPtr &model) const;73 std::vector<om2::MemInfo> GetAllMemoryTypeSize(const GeModelPtr &model) const;
74 static void ReportUnsupportedTask(TaskCodeBuilderPtr &task_builder, domi::TaskDef *const task_def,74 static void ReportUnsupportedTask(TaskCodeBuilderPtr &task_builder, domi::TaskDef *const task_def,
75 std::unordered_map<int64_t, OpDescPtr> &op_desc_by_index, ModelTaskType task_type);75 std::unordered_map<int64_t, OpDescPtr> &op_desc_by_index, ModelTaskType task_type);
76- static Status BuildKernelRegistryForAicore(Om2CodegenModel &codegen_model, const OpDescPtr &op_desc,76+ static Status BuildKernelRegistryForAicore(Om2CodegenModel &codegen_model, const domi::TaskDef &task_def,
77- ModelTaskType task_type);77+ const OpDescPtr &op_desc, ModelTaskType task_type);
78 static Status BuildKernelRegistryForAicpu(Om2CodegenModel &codegen_model, const domi::TaskDef &task_def,78 static Status BuildKernelRegistryForAicpu(Om2CodegenModel &codegen_model, const domi::TaskDef &task_def,
79 const std::string &op_type, const std::string &kernel_name,79 const std::string &op_type, const std::string &kernel_name,
80 const std::string &aicpu_kernel_sign);80 const std::string &aicpu_kernel_sign);
@@ -145,6 +145,7 @@ enum class KernelBinaryKind : int32_t {
145 145 
146struct KernelBinaryRecord {146struct KernelBinaryRecord {
147 KernelBinaryKind kind{KernelBinaryKind::kAicore};147 KernelBinaryKind kind{KernelBinaryKind::kAicore};
148+ std::string bin_id;
148 std::string kernel_name;149 std::string kernel_name;
149 std::string file_name;150 std::string file_name;
150 std::string op_type;151 std::string op_type;
@@ -16,6 +16,7 @@ Om2ModelClassGeneratorBase::Om2ModelClassGeneratorBase(AstBuildContext &ast)
16 constants_(ast.Var("void **", "constants_")),16 constants_(ast.Var("void **", "constants_")),
17 var_addrs_(ast.Var("void **", "var_addrs_")),17 var_addrs_(ast.Var("void **", "var_addrs_")),
18 model_handle_(ast.Var("aclmdlRI", "model_handle_")),18 model_handle_(ast.Var("aclmdlRI", "model_handle_")),
19+ bin_ids_(ast.Var("std::vector<std::string>", "bin_ids_")),
19 bin_handles_(ast.Var("std::vector<aclrtBinHandle>", "bin_handles_")),20 bin_handles_(ast.Var("std::vector<aclrtBinHandle>", "bin_handles_")),
20 func_handles_(ast.Var("std::vector<aclrtFuncHandle>", "func_handles_")),21 func_handles_(ast.Var("std::vector<aclrtFuncHandle>", "func_handles_")),
21 stream_list_(ast.Var("std::vector<aclrtStream>", "stream_list_")),22 stream_list_(ast.Var("std::vector<aclrtStream>", "stream_list_")),
@@ -31,6 +32,7 @@ Om2ModelClassGeneratorBase::Om2ModelClassGeneratorBase(AstBuildContext &ast)
31 session_id_(ast.Var("uint64_t *", "session_id_")),32 session_id_(ast.Var("uint64_t *", "session_id_")),
32 model_id_(ast.Var("uint32_t", "model_id_")),33 model_id_(ast.Var("uint32_t", "model_id_")),
33 instance_handle_(ast.Var("void *", "instance_handle_")),34 instance_handle_(ast.Var("void *", "instance_handle_")),
35+ callbacks_(ast.Var("GertModelLoadCallbacks", "callbacks_")),
34 kernel_id_(ast.Var("uint64_t", "kernel_id_")),36 kernel_id_(ast.Var("uint64_t", "kernel_id_")),
35 dev_ext_info_mem_ptrs_(ast.Var("std::vector<void *>", "dev_ext_info_mem_ptrs_")),37 dev_ext_info_mem_ptrs_(ast.Var("std::vector<void *>", "dev_ext_info_mem_ptrs_")),
36 label_switch_label_list_(ast_.Var("std::map<uint32_t, aclrtLabelList>", "label_switch_label_list_")),38 label_switch_label_list_(ast_.Var("std::map<uint32_t, aclrtLabelList>", "label_switch_label_list_")),
@@ -23,6 +23,7 @@ class Om2ModelClassGeneratorBase : public CodeGeneratorBase {
23 VarRef constants_;23 VarRef constants_;
24 VarRef var_addrs_;24 VarRef var_addrs_;
25 VarRef model_handle_;25 VarRef model_handle_;
26+ VarRef bin_ids_;
26 VarRef bin_handles_;27 VarRef bin_handles_;
27 VarRef func_handles_;28 VarRef func_handles_;
28 VarRef stream_list_;29 VarRef stream_list_;
@@ -38,6 +39,7 @@ class Om2ModelClassGeneratorBase : public CodeGeneratorBase {
38 VarRef session_id_;39 VarRef session_id_;
39 VarRef model_id_;40 VarRef model_id_;
40 VarRef instance_handle_;41 VarRef instance_handle_;
42+ VarRef callbacks_;
41 VarRef kernel_id_;43 VarRef kernel_id_;
42 VarRef dev_ext_info_mem_ptrs_;44 VarRef dev_ext_info_mem_ptrs_;
43 VarRef label_switch_label_list_;45 VarRef label_switch_label_list_;
@@ -13,6 +13,7 @@
13 13 
14#include <stdint.h>14#include <stdint.h>
15 15 
16+#include "acl/acl_rt.h"
16#include "framework/runtime/gert_model/gert_model_executor_types.h"17#include "framework/runtime/gert_model/gert_model_executor_types.h"
17 18 
18#ifdef __cplusplus19#ifdef __cplusplus
@@ -21,6 +22,42 @@ extern "C" {
21 22 
22int32_t GertModelLaunchTask(void *instance_handle, GertModelTaskLaunchInfo *launch_info);23int32_t GertModelLaunchTask(void *instance_handle, GertModelTaskLaunchInfo *launch_info);
23 24 
25+/**
26+ * @brief 对BinHandle缓存集合加锁, 需要加解锁成对出现, 需要调用者保证锁释放
27+ * @return 返回 0 表示成功, 返回其他值表示失败
28+ */
29+int32_t LockBinHandleStore();
30+ 
31+/**
32+ * @brief 对BinHandle缓存集合解锁, 需要加解锁成对出现, 需要调用者保证锁释放
33+ * @return 返回 0 表示成功, 返回其他值表示失败
34+ */
35+int32_t UnlockBinHandleStore();
36+ 
37+/**
38+ * @brief 从缓存集合查询BinHandle, 查询不到BinHandle返回空
39+ * @param bin_id 输入
40+ * @param bin_handle 输出
41+ * @return 返回 0 表示成功, 返回其他值表示失败
42+ */
43+int32_t QueryBinHandleFromStore(const char *bin_id, aclrtBinHandle *bin_handle);
44+ 
45+/**
46+ * @brief 释放缓存集合中的BinHandle引用, 当引用计数减到0时从集合中移除
47+ * @param bin_id 输入
48+ * @param need_unload 输出, 1表示需要卸载, 0表示不需要卸载
49+ * @return 返回 0 表示成功, 返回其他值表示失败
50+ */
51+int32_t ReleaseBinHandleFromStore(const char *bin_id, uint8_t *need_unload);
52+ 
53+/**
54+ * @brief 保存BinHandle到缓存集合, 查询到则引用计数自动加1, 查询不到则保存且引用计数为1
55+ * @param bin_id 输入
56+ * @param bin_handle 输入
57+ * @return 返回 0 表示成功, 返回其他值表示失败
58+ */
59+int32_t SaveBinHandleToStore(const char *bin_id, const aclrtBinHandle bin_handle);
60+ 
24#ifdef __cplusplus61#ifdef __cplusplus
25}62}
26#endif63#endif
@@ -63,6 +63,12 @@ extern "C" {
63 63 
64typedef int32_t (*GertModelLaunchFunc)(void *instance_handle, GertModelTaskLaunchInfo *launch_info);64typedef int32_t (*GertModelLaunchFunc)(void *instance_handle, GertModelTaskLaunchInfo *launch_info);
65 65 
66+using LockBinHandleStoreFunc = int32_t (*)();
67+using UnlockBinHandleStoreFunc = int32_t (*)();
68+using QueryBinHandleFromStoreFunc = int32_t (*)(const char *bin_id, aclrtBinHandle *bin_handle);
69+using SaveBinHandleToStoreFunc = int32_t (*)(const char *bin_id, aclrtBinHandle bin_handle);
70+using ReleaseBinHandleFromStoreFunc = int32_t (*)(const char *bin_id, uint8_t *need_unload);
71+ 
66#ifdef __cplusplus72#ifdef __cplusplus
67}73}
68#endif74#endif
@@ -74,6 +80,11 @@ struct GertModelLoadCallbacks {
74 // executor 收到后完成 ReportModelBaseInfo(组装 ModelDumpInfo → SetModelDumpInfo)80 // executor 收到后完成 ReportModelBaseInfo(组装 ModelDumpInfo → SetModelDumpInfo)
75 ReportModelBaseInfoFunc report_model_base_info = nullptr;81 ReportModelBaseInfoFunc report_model_base_info = nullptr;
76 GertModelLaunchFunc launch_func = nullptr;82 GertModelLaunchFunc launch_func = nullptr;
83+ LockBinHandleStoreFunc lock_bin_handle_store = nullptr;
84+ UnlockBinHandleStoreFunc unlock_bin_handle_store = nullptr;
85+ QueryBinHandleFromStoreFunc query_bin_handle_from_store = nullptr;
86+ SaveBinHandleToStoreFunc save_bin_handle_to_store = nullptr;
87+ ReleaseBinHandleFromStoreFunc release_bin_handle_from_store = nullptr;
77};88};
78 89 
79#endif // GE_FRAMEWORK_RUNTIME_GERT_MODEL_GERT_MODEL_EXECUTOR_TYPES_H_90#endif // GE_FRAMEWORK_RUNTIME_GERT_MODEL_GERT_MODEL_EXECUTOR_TYPES_H_
@@ -0,0 +1,126 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+#include "framework/runtime/gert_model/gert_model_executor_callbacks.h"
12+ 
13+#include <mutex>
14+#include <string>
15+#include <unordered_map>
16+ 
17+#include "common/ge_common/debug/ge_log.h"
18+ 
19+namespace ge {
20+struct BinHandleInfo {
21+ aclrtBinHandle bin_handle{nullptr};
22+ int64_t refer_count{0};
23+};
24+ 
25+class Om2KernelHandlesManager {
26+ public:
27+ static Om2KernelHandlesManager &Instance() {
28+ static Om2KernelHandlesManager manager;
29+ return manager;
30+ }
31+ 
32+ int32_t LockBinHandleStore() {
33+ GELOGD("[OM2][KernelHandles] Lock bin handle store.");
34+ mutex_.lock();
35+ return 0;
36+ }
37+ 
38+ int32_t UnlockBinHandleStore() {
39+ GELOGD("[OM2][KernelHandles] Unlock bin handle store.");
40+ mutex_.unlock();
41+ return 0;
42+ }
43+ 
44+ int32_t QueryBinHandleFromStore(const char *bin_id, aclrtBinHandle *bin_handle) {
45+ if (bin_id == nullptr) {
46+ return -1;
47+ }
48+ GELOGD("[OM2][KernelHandles] Query bin handle from store, bin_id=%s.", bin_id);
49+ const std::string bin_id_str(bin_id);
50+ auto iter = global_bin_handle_store_.find(bin_id_str);
51+ if (iter != global_bin_handle_store_.end()) {
52+ *bin_handle = iter->second.bin_handle;
53+ return 0;
54+ }
55+ *bin_handle = nullptr;
56+ return 0;
57+ }
58+ 
59+ int32_t ReleaseBinHandleFromStore(const char *bin_id, uint8_t *need_unload) {
60+ if (bin_id == nullptr || need_unload == nullptr) {
61+ return -1;
62+ }
63+ *need_unload = 0U;
64+ GELOGD("[OM2][KernelHandles] Release bin handle from store, bin_id=%s.", bin_id);
65+ const std::string bin_id_str(bin_id);
66+ auto iter = global_bin_handle_store_.find(bin_id_str);
67+ if (iter != global_bin_handle_store_.end()) {
68+ iter->second.refer_count--;
69+ if (iter->second.refer_count <= 0) {
70+ *need_unload = 1U;
71+ (void)global_bin_handle_store_.erase(iter);
72+ return 0;
73+ }
74+ return 0;
75+ }
76+ return 0;
77+ }
78+ 
79+ int32_t SaveBinHandleToStore(const char *bin_id, const aclrtBinHandle bin_handle) {
80+ if (bin_id == nullptr) {
81+ return -1;
82+ }
83+ const std::string bin_id_str(bin_id);
84+ GELOGD("[OM2][KernelHandles] Save bin handle to store, bin_id=%s.", bin_id);
85+ auto iter = global_bin_handle_store_.find(bin_id_str);
86+ if (iter != global_bin_handle_store_.end()) {
87+ iter->second.refer_count++;
88+ return 0;
89+ }
90+ BinHandleInfo kernel_bin_handle;
91+ kernel_bin_handle.bin_handle = bin_handle;
92+ kernel_bin_handle.refer_count = 1;
93+ global_bin_handle_store_.emplace(bin_id_str, kernel_bin_handle);
94+ return 0;
95+ }
96+ 
97+ private:
98+ Om2KernelHandlesManager() = default;
99+ 
100+ std::recursive_mutex mutex_;
101+ std::unordered_map<std::string, BinHandleInfo> global_bin_handle_store_;
102+};
103+} // namespace ge
104+ 
105+extern "C" {
106+int32_t LockBinHandleStore() {
107+ return ge::Om2KernelHandlesManager::Instance().LockBinHandleStore();
108+}
109+ 
110+int32_t UnlockBinHandleStore() {
111+ return ge::Om2KernelHandlesManager::Instance().UnlockBinHandleStore();
112+}
113+ 
114+int32_t QueryBinHandleFromStore(const char *bin_id, aclrtBinHandle *bin_handle) {
115+ return ge::Om2KernelHandlesManager::Instance().QueryBinHandleFromStore(bin_id, bin_handle);
116+}
117+ 
118+int32_t ReleaseBinHandleFromStore(const char *bin_id, uint8_t *need_unload) {
119+ return ge::Om2KernelHandlesManager::Instance().ReleaseBinHandleFromStore(bin_id, need_unload);
120+}
121+ 
122+int32_t SaveBinHandleToStore(const char *bin_id, const aclrtBinHandle bin_handle) {
123+ return ge::Om2KernelHandlesManager::Instance().SaveBinHandleToStore(bin_id, bin_handle);
124+}
125+ 
126+} // extern "C"
@@ -963,6 +963,17 @@ ge::Status ValidateVarMetas(const std::vector<ge::Om2VarMeta> &var_metas) {
963 return ge::SUCCESS;963 return ge::SUCCESS;
964}964}
965 965 
966+GertModelLoadCallbacks CreateGertModelLoadCallbacks() {
967+ return {.struct_size = sizeof(GertModelLoadCallbacks),
968+ .report_model_base_info = ReportModelBaseInfo,
969+ .launch_func = GertModelLaunchTask,
970+ .lock_bin_handle_store = LockBinHandleStore,
971+ .unlock_bin_handle_store = UnlockBinHandleStore,
972+ .query_bin_handle_from_store = QueryBinHandleFromStore,
973+ .save_bin_handle_to_store = SaveBinHandleToStore,
974+ .release_bin_handle_from_store = ReleaseBinHandleFromStore};
975+}
976+ 
966} // namespace977} // namespace
967 978 
968class Om2ModelExecutor::Impl {979class Om2ModelExecutor::Impl {
@@ -1178,9 +1189,7 @@ class Om2ModelExecutor::Impl {
1178 GE_ASSERT_SUCCESS(PrepareVarAddrs(model_data, static_cast<uint32_t>(load_arg.device_id), var_addrs));1189 GE_ASSERT_SUCCESS(PrepareVarAddrs(model_data, static_cast<uint32_t>(load_arg.device_id), var_addrs));
1179 1190 
1180 GE_ASSERT_NOTNULL(run_model_info_.load_func);1191 GE_ASSERT_NOTNULL(run_model_info_.load_func);
1181- GertModelLoadCallbacks callbacks = {.struct_size = sizeof(GertModelLoadCallbacks),1192+ const GertModelLoadCallbacks callbacks = CreateGertModelLoadCallbacks();
1182- .report_model_base_info = ReportModelBaseInfo,
1183- .launch_func = GertModelLaunchTask};
1184 struct GertModelLoadConfig config = {.struct_size = sizeof(GertModelLoadConfig),1193 struct GertModelLoadConfig config = {.struct_size = sizeof(GertModelLoadConfig),
1185 .bin_files = bin_files.data(),1194 .bin_files = bin_files.data(),
1186 .bin_data = bin_data.data(),1195 .bin_data = bin_data.data(),
@@ -126,6 +126,7 @@ set(DISTINCT_GRAPH_LOAD_TEST_FILES
126 "graph/load/model_parser_base_unittest.cc"126 "graph/load/model_parser_base_unittest.cc"
127 "executor/ge_executor_unittest.cc"127 "executor/ge_executor_unittest.cc"
128 "executor/om2_model_executor_unittest.cc"128 "executor/om2_model_executor_unittest.cc"
129+ "executor/om2_kernel_handles_unittest.cc"
129 "executor/om2_rt_var_manager_unittest.cc"130 "executor/om2_rt_var_manager_unittest.cc"
130 "executor/rt_var_resource_unittest.cc"131 "executor/rt_var_resource_unittest.cc"
131 "executor/om2_model_manager_unittest.cc"132 "executor/om2_model_manager_unittest.cc"
@@ -2668,6 +2668,7 @@ struct BinaryBuffer {
2668};2668};
2669 2669 
2670struct AicoreRegisterInfo {2670struct AicoreRegisterInfo {
2671+ const char *bin_id;
2671 uint32_t magic;2672 uint32_t magic;
2672 bool use_tiling_key = false;2673 bool use_tiling_key = false;
2673 uint64_t tiling_key = 0;2674 uint64_t tiling_key = 0;
@@ -2676,6 +2677,7 @@ struct AicoreRegisterInfo {
2676};2677};
2677 2678 
2678struct AicpuRegisterInfo {2679struct AicpuRegisterInfo {
2680+ const char *bin_id;
2679 const char *op_type;2681 const char *op_type;
2680 const char *so_name;2682 const char *so_name;
2681 const char *kernel_name;2683 const char *kernel_name;
@@ -2683,6 +2685,7 @@ struct AicpuRegisterInfo {
2683};2685};
2684 2686 
2685struct CustAicpuRegisterInfo {2687struct CustAicpuRegisterInfo {
2688+ const char *bin_id;
2686 std::string file;2689 std::string file;
2687 const char *op_type;2690 const char *op_type;
2688 const char *func_name;2691 const char *func_name;
@@ -2744,7 +2747,8 @@ void AssembleAicpuLoadOptions(aclrtBinaryLoadOptions &load_options, int32_t cpu_
2744 option.value.cpuKernelMode = cpu_kernel_mode;2747 option.value.cpuKernelMode = cpu_kernel_mode;
2745}2748}
2746 2749 
2747-aclError RegisterAicoreKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const AicoreRegisterInfo &register_info, std::unordered_map<std::string, BinDataInfo> &bin_info_map) {2750+aclError RegisterAicoreKernel(std::string &bin_id, aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const AicoreRegisterInfo &register_info, std::unordered_map<std::string, BinDataInfo> &bin_info_map, const GertModelLoadCallbacks &callbacks) {
2751+ bin_id = register_info.bin_id;
2748 auto &bin_info = bin_info_map[register_info.file];2752 auto &bin_info = bin_info_map[register_info.file];
2749 aclrtBinaryLoadOptions load_options;2753 aclrtBinaryLoadOptions load_options;
2750 aclrtBinaryLoadOption option;2754 aclrtBinaryLoadOption option;
@@ -2752,7 +2756,15 @@ aclError RegisterAicoreKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_
2752 load_options.options = &option;2756 load_options.options = &option;
2753 option.type = ACL_RT_BINARY_LOAD_OPT_MAGIC;2757 option.type = ACL_RT_BINARY_LOAD_OPT_MAGIC;
2754 option.value.magic = register_info.magic;2758 option.value.magic = register_info.magic;
2755- OM2_CHK_STATUS(aclrtBinaryLoadFromData(bin_info.data, bin_info.size, &load_options, &bin_handle));2759+ OM2_CHK_STATUS(callbacks.lock_bin_handle_store());
2760+ OM2_MAKE_GUARD(bin_lock_guard, [&callbacks]() {
2761+ (void)callbacks.unlock_bin_handle_store();
2762+ });
2763+ OM2_CHK_STATUS(callbacks.query_bin_handle_from_store(register_info.bin_id, &bin_handle));
2764+ if ((bin_handle == nullptr)) {
2765+ OM2_CHK_STATUS(aclrtBinaryLoadFromData(bin_info.data, bin_info.size, &load_options, &bin_handle));
2766+ }
2767+ OM2_CHK_STATUS(callbacks.save_bin_handle_to_store(register_info.bin_id, bin_handle));
2756 if (register_info.use_tiling_key) {2768 if (register_info.use_tiling_key) {
2757 OM2_CHK_STATUS(aclrtBinaryGetFunctionByEntry(bin_handle, register_info.tiling_key, &func_handle));2769 OM2_CHK_STATUS(aclrtBinaryGetFunctionByEntry(bin_handle, register_info.tiling_key, &func_handle));
2758 } else {2770 } else {
@@ -2761,7 +2773,8 @@ aclError RegisterAicoreKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_
2761 return ACL_SUCCESS;2773 return ACL_SUCCESS;
2762}2774}
2763 2775 
2764-aclError RegisterAicpuKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const AicpuRegisterInfo &register_info) {2776+aclError RegisterAicpuKernel(std::string &bin_id, aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const AicpuRegisterInfo &register_info, const GertModelLoadCallbacks &callbacks) {
2777+ bin_id = register_info.bin_id;
2765 std::string json_path;2778 std::string json_path;
2766 OM2_CHK_STATUS(GenerateJsonFile(register_info, json_path));2779 OM2_CHK_STATUS(GenerateJsonFile(register_info, json_path));
2767 OM2_MAKE_GUARD(json_guard, [&json_path]() {2780 OM2_MAKE_GUARD(json_guard, [&json_path]() {
@@ -2774,12 +2787,21 @@ aclError RegisterAicpuKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_h
2774 load_options.options = &option;2787 load_options.options = &option;
2775 option.type = ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE;2788 option.type = ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE;
2776 option.value.cpuKernelMode = 0;2789 option.value.cpuKernelMode = 0;
2777- OM2_CHK_STATUS(aclrtBinaryLoadFromFile(json_path.c_str(), &load_options, &bin_handle));2790+ OM2_CHK_STATUS(callbacks.lock_bin_handle_store());
2791+ OM2_MAKE_GUARD(bin_lock_guard, [&callbacks]() {
2792+ (void)callbacks.unlock_bin_handle_store();
2793+ });
2794+ OM2_CHK_STATUS(callbacks.query_bin_handle_from_store(register_info.bin_id, &bin_handle));
2795+ if ((bin_handle == nullptr)) {
2796+ OM2_CHK_STATUS(aclrtBinaryLoadFromFile(json_path.c_str(), &load_options, &bin_handle));
2797+ }
2798+ OM2_CHK_STATUS(callbacks.save_bin_handle_to_store(register_info.bin_id, bin_handle));
2778 OM2_CHK_STATUS(aclrtBinaryGetFunction(bin_handle, register_info.op_type, &func_handle));2799 OM2_CHK_STATUS(aclrtBinaryGetFunction(bin_handle, register_info.op_type, &func_handle));
2779 return ACL_SUCCESS;2800 return ACL_SUCCESS;
2780}2801}
2781 2802 
2782-aclError RegisterCustAicpuKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const CustAicpuRegisterInfo &register_info, std::unordered_map<std::string, BinDataInfo> &bin_info_map) {2803+aclError RegisterCustAicpuKernel(std::string &bin_id, aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const CustAicpuRegisterInfo &register_info, std::unordered_map<std::string, BinDataInfo> &bin_info_map, const GertModelLoadCallbacks &callbacks) {
2804+ bin_id = register_info.bin_id;
2783 auto &bin_info = bin_info_map[register_info.file];2805 auto &bin_info = bin_info_map[register_info.file];
2784 aclrtBinaryLoadOptions load_options;2806 aclrtBinaryLoadOptions load_options;
2785 aclrtBinaryLoadOption option;2807 aclrtBinaryLoadOption option;
@@ -2787,14 +2809,22 @@ aclError RegisterCustAicpuKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &fu
2787 load_options.options = &option;2809 load_options.options = &option;
2788 option.type = ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE;2810 option.type = ACL_RT_BINARY_LOAD_OPT_CPU_KERNEL_MODE;
2789 option.value.cpuKernelMode = 2;2811 option.value.cpuKernelMode = 2;
2790- OM2_CHK_STATUS(aclrtBinaryLoadFromData(bin_info.data, bin_info.size, &load_options, &bin_handle));2812+ OM2_CHK_STATUS(callbacks.lock_bin_handle_store());
2813+ OM2_MAKE_GUARD(bin_lock_guard, [&callbacks]() {
2814+ (void)callbacks.unlock_bin_handle_store();
2815+ });
2816+ OM2_CHK_STATUS(callbacks.query_bin_handle_from_store(register_info.bin_id, &bin_handle));
2817+ if ((bin_handle == nullptr)) {
2818+ OM2_CHK_STATUS(aclrtBinaryLoadFromData(bin_info.data, bin_info.size, &load_options, &bin_handle));
2819+ }
2820+ OM2_CHK_STATUS(callbacks.save_bin_handle_to_store(register_info.bin_id, bin_handle));
2791 OM2_CHK_STATUS(aclrtRegisterCpuFunc(bin_handle, register_info.func_name, register_info.op_type, &func_handle));2821 OM2_CHK_STATUS(aclrtRegisterCpuFunc(bin_handle, register_info.func_name, register_info.op_type, &func_handle));
2792 return ACL_SUCCESS;2822 return ACL_SUCCESS;
2793}2823}
2794} // namespace2824} // namespace
2795aclError Om2Model::RegisterKernels() {2825aclError Om2Model::RegisterKernels() {
2796 OM2_LOGI("RegisterKernels begin");2826 OM2_LOGI("RegisterKernels begin");
2797- OM2_CHK_STATUS(RegisterAicoreKernel(bin_handles_[0], func_handles_[0], {ACL_RT_BINARY_MAGIC_ELF_VECTOR_CORE, false, 0, "add1_faked_kernel", "add1_faked_kernel.o"}, bin_info_map_));2827+ OM2_CHK_STATUS(RegisterAicoreKernel(bin_ids_[0], bin_handles_[0], func_handles_[0], {"_g1add1", ACL_RT_BINARY_MAGIC_ELF_VECTOR_CORE, false, 0, "add1_faked_kernel", "add1_faked_kernel.o"}, bin_info_map_, callbacks_));
2798 OM2_LOGI("RegisterKernels done");2828 OM2_LOGI("RegisterKernels done");
2799 return ACL_SUCCESS;2829 return ACL_SUCCESS;
2800}2830}
@@ -7262,12 +7292,18 @@ TEST_F(ProgramGeneratorUt, GenerateKernelRegistryForCustAicpu_Ok) {
7262 std::cout << "=== kernel_reg content ===" << std::endl << kernel_reg << std::endl << "=== end ===" << std::endl;7292 std::cout << "=== kernel_reg content ===" << std::endl << kernel_reg << std::endl << "=== end ===" << std::endl;
7263 ASSERT_FALSE(kernel_reg.empty());7293 ASSERT_FALSE(kernel_reg.empty());
7264 EXPECT_NE(kernel_reg.find("struct CustAicpuRegisterInfo"), std::string::npos);7294 EXPECT_NE(kernel_reg.find("struct CustAicpuRegisterInfo"), std::string::npos);
7295+ EXPECT_NE(kernel_reg.find("const char *bin_id"), std::string::npos);
7265 EXPECT_NE(kernel_reg.find("std::string file"), std::string::npos);7296 EXPECT_NE(kernel_reg.find("std::string file"), std::string::npos);
7266 EXPECT_NE(kernel_reg.find("const char *op_type"), std::string::npos);7297 EXPECT_NE(kernel_reg.find("const char *op_type"), std::string::npos);
7267 EXPECT_NE(kernel_reg.find("const char *func_name"), std::string::npos);7298 EXPECT_NE(kernel_reg.find("const char *func_name"), std::string::npos);
7268- EXPECT_NE(kernel_reg.find(7299+ EXPECT_NE(kernel_reg.find("aclError RegisterCustAicpuKernel(std::string &bin_id, aclrtBinHandle &bin_handle, "
7269- "aclError RegisterCustAicpuKernel(aclrtBinHandle &bin_handle, aclrtFuncHandle &func_handle, const "7300+ "aclrtFuncHandle &func_handle, const "
7270- "CustAicpuRegisterInfo &register_info, std::unordered_map<std::string, BinDataInfo> &bin_info_map) {"),7301+ "CustAicpuRegisterInfo &register_info, std::unordered_map<std::string, BinDataInfo> "
7302+ "&bin_info_map, const GertModelLoadCallbacks &callbacks) {"),
7303+ std::string::npos);
7304+ EXPECT_NE(kernel_reg.find("bin_id = register_info.bin_id;"), std::string::npos);
7305+ EXPECT_NE(kernel_reg.find("OM2_CHK_STATUS(callbacks.lock_bin_handle_store());"), std::string::npos);
7306+ EXPECT_NE(kernel_reg.find("OM2_CHK_STATUS(callbacks.save_bin_handle_to_store(register_info.bin_id, bin_handle));"),
7271 std::string::npos);7307 std::string::npos);
7272 EXPECT_NE(kernel_reg.find("auto &bin_info = bin_info_map[register_info.file];"), std::string::npos);7308 EXPECT_NE(kernel_reg.find("auto &bin_info = bin_info_map[register_info.file];"), std::string::npos);
7273 EXPECT_NE(kernel_reg.find("aclrtBinaryLoadOptions load_options;"), std::string::npos);7309 EXPECT_NE(kernel_reg.find("aclrtBinaryLoadOptions load_options;"), std::string::npos);
@@ -7283,10 +7319,11 @@ TEST_F(ProgramGeneratorUt, GenerateKernelRegistryForCustAicpu_Ok) {
7283 "register_info.op_type, &func_handle));"),7319 "register_info.op_type, &func_handle));"),
7284 std::string::npos);7320 std::string::npos);
7285 const size_t cust_aicpu_hash_id = std::hash<std::string>{}(std::string(64, '\0'));7321 const size_t cust_aicpu_hash_id = std::hash<std::string>{}(std::string(64, '\0'));
7322+ const std::string cust_aicpu_bin_id = std::to_string(cust_aicpu_hash_id) + "_CustAicpuKernel";
7286 const std::string cust_aicpu_file_name = std::to_string(cust_aicpu_hash_id) + "_CustAicpuKernel.o";7323 const std::string cust_aicpu_file_name = std::to_string(cust_aicpu_hash_id) + "_CustAicpuKernel.o";
7287 const std::string expected_reg_call =7324 const std::string expected_reg_call =
7288- "OM2_CHK_STATUS(RegisterCustAicpuKernel(bin_handles_[0], func_handles_[0], {\"" + cust_aicpu_file_name +7325+ "OM2_CHK_STATUS(RegisterCustAicpuKernel(bin_ids_[0], bin_handles_[0], func_handles_[0], {\"" + cust_aicpu_bin_id +
7289- "\", \"Add\", \"name\"}, bin_info_map_));";7326+ "\", \"" + cust_aicpu_file_name + "\", \"Add\", \"name\"}, bin_info_map_, callbacks_));";
7290 EXPECT_NE(kernel_reg.find(expected_reg_call), std::string::npos);7327 EXPECT_NE(kernel_reg.find(expected_reg_call), std::string::npos);
7291 7328 
7292 const auto &load_and_run = outputs[GeneratedFileIndex::kLoadingAndRunningFile];7329 const auto &load_and_run = outputs[GeneratedFileIndex::kLoadingAndRunningFile];
@@ -0,0 +1,104 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+#include "framework/runtime/gert_model/gert_model_executor_callbacks.h"
12+ 
13+#include <gtest/gtest.h>
14+ 
15+namespace ge {
16+namespace {
17+ 
18+class Om2KernelHandlesTest : public testing::Test {};
19+ 
20+class BinHandleStoreLockGuard {
21+ public:
22+ BinHandleStoreLockGuard() {
23+ EXPECT_EQ(::LockBinHandleStore(), 0);
24+ }
25+ 
26+ ~BinHandleStoreLockGuard() {
27+ EXPECT_EQ(::UnlockBinHandleStore(), 0);
28+ }
29+};
30+ 
31+TEST_F(Om2KernelHandlesTest, QueryMissingBinReturnsNull) {
32+ BinHandleStoreLockGuard lock_guard;
33+ aclrtBinHandle bin_handle = reinterpret_cast<aclrtBinHandle>(0x1234);
34+ EXPECT_EQ(::QueryBinHandleFromStore("om2_missing_bin", &bin_handle), 0);
35+ EXPECT_EQ(bin_handle, nullptr);
36+}
37+ 
38+TEST_F(Om2KernelHandlesTest, NullArgumentsReturnError) {
39+ uint8_t need_unload = 0U;
40+ aclrtBinHandle bin_handle = nullptr;
41+ 
42+ EXPECT_EQ(::QueryBinHandleFromStore(nullptr, &bin_handle), -1);
43+ EXPECT_EQ(::ReleaseBinHandleFromStore(nullptr, &need_unload), -1);
44+ EXPECT_EQ(::ReleaseBinHandleFromStore("om2_null_bin", nullptr), -1);
45+ EXPECT_EQ(::SaveBinHandleToStore(nullptr, reinterpret_cast<aclrtBinHandle>(0x1)), -1);
46+}
47+ 
48+TEST_F(Om2KernelHandlesTest, SaveAndQueryReturnsStoredHandle) {
49+ BinHandleStoreLockGuard lock_guard;
50+ constexpr auto kBinId = "om2_kernel_handles_save_query";
51+ const auto bin_handle = reinterpret_cast<aclrtBinHandle>(0x12345678);
52+ 
53+ EXPECT_EQ(::SaveBinHandleToStore(kBinId, bin_handle), 0);
54+ 
55+ aclrtBinHandle queried_handle = nullptr;
56+ EXPECT_EQ(::QueryBinHandleFromStore(kBinId, &queried_handle), 0);
57+ EXPECT_EQ(queried_handle, bin_handle);
58+}
59+ 
60+TEST_F(Om2KernelHandlesTest, SaveSameBinIncrementsReferenceAndReleasePairs) {
61+ BinHandleStoreLockGuard lock_guard;
62+ constexpr auto kBinId = "om2_kernel_handles_ref_count";
63+ const auto bin_handle = reinterpret_cast<aclrtBinHandle>(0x22334455);
64+ uint8_t need_unload = 0U;
65+ aclrtBinHandle queried_handle = nullptr;
66+ 
67+ EXPECT_EQ(::SaveBinHandleToStore(kBinId, bin_handle), 0);
68+ EXPECT_EQ(::SaveBinHandleToStore(kBinId, reinterpret_cast<aclrtBinHandle>(0x1)), 0);
69+ 
70+ EXPECT_EQ(::QueryBinHandleFromStore(kBinId, &queried_handle), 0);
71+ EXPECT_EQ(queried_handle, bin_handle);
72+ 
73+ EXPECT_EQ(::ReleaseBinHandleFromStore(kBinId, &need_unload), 0);
74+ EXPECT_EQ(need_unload, 0U);
75+ 
76+ queried_handle = reinterpret_cast<aclrtBinHandle>(0xdeadbeef);
77+ EXPECT_EQ(::QueryBinHandleFromStore(kBinId, &queried_handle), 0);
78+ EXPECT_EQ(queried_handle, bin_handle);
79+ 
80+ EXPECT_EQ(::ReleaseBinHandleFromStore(kBinId, &need_unload), 0);
81+ EXPECT_EQ(need_unload, 1U);
82+ 
83+ queried_handle = reinterpret_cast<aclrtBinHandle>(0xdeadbeef);
84+ EXPECT_EQ(::QueryBinHandleFromStore(kBinId, &queried_handle), 0);
85+ EXPECT_EQ(queried_handle, nullptr);
86+}
87+ 
88+TEST_F(Om2KernelHandlesTest, ReleaseMissingBinDoesNotUnload) {
89+ BinHandleStoreLockGuard lock_guard;
90+ uint8_t need_unload = 1U;
91+ 
92+ EXPECT_EQ(::ReleaseBinHandleFromStore("om2_missing_release_bin", &need_unload), 0);
93+ EXPECT_EQ(need_unload, 0U);
94+}
95+ 
96+TEST_F(Om2KernelHandlesTest, RecursiveLockAllowsNestedLockAndUnlock) {
97+ EXPECT_EQ(::LockBinHandleStore(), 0);
98+ EXPECT_EQ(::LockBinHandleStore(), 0);
99+ EXPECT_EQ(::UnlockBinHandleStore(), 0);
100+ EXPECT_EQ(::UnlockBinHandleStore(), 0);
101+}
102+ 
103+} // namespace
104+} // namespace ge