From c8cc205f3360b5d363ad725bde59aaa77ac360f5 Mon Sep 17 00:00:00 2001 From: taoxiangdong Date: Thu, 31 Dec 2020 15:05:44 +0800 Subject: [PATCH] Free memory before return --- .../load/new_model_manager/model_manager.cc | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/ge/graph/load/new_model_manager/model_manager.cc b/ge/graph/load/new_model_manager/model_manager.cc index 80c6191b..e8a2f0ab 100755 --- a/ge/graph/load/new_model_manager/model_manager.cc +++ b/ge/graph/load/new_model_manager/model_manager.cc @@ -1558,6 +1558,12 @@ Status ModelManager::LaunchKernelCheckAicpuOp(std::vector &aicpu_op size_t aicpu_op_nums = aicpu_optype_list.size(); size_t tf_op_nums = aicpu_tf_optype_list.size(); size_t op_nums = aicpu_op_nums + tf_op_nums; + std::function callback = [&]() { + for (auto mem : allocated_mem) { + GE_CHK_RT(rtFree(mem)); + } + }; + GE_MAKE_GUARD(release, callback); // malloc sysOpInfoList in SysOpCheckInfo status = rtMalloc(&d_req_op_list, op_nums * sizeof(SysOpInfo), RT_MEMORY_HBM); if (status != RT_ERROR_NONE) { @@ -1637,8 +1643,8 @@ Status ModelManager::LaunchKernelCheckAicpuOp(std::vector &aicpu_op return RT_ERROR_TO_GE_STATUS(status); } allocated_mem.push_back(args); - GE_CHK_RT( - rtMemcpy(args, sizeof(SysOpCheckInfo), reinterpret_cast(&op_check_info_req), sizeof(SysOpCheckInfo), RT_MEMCPY_HOST_TO_DEVICE)); + GE_CHK_RT(rtMemcpy(args, sizeof(SysOpCheckInfo), reinterpret_cast(&op_check_info_req), sizeof(SysOpCheckInfo), + RT_MEMCPY_HOST_TO_DEVICE)); GE_CHK_RT(rtMemcpy(reinterpret_cast(static_cast(static_cast(reinterpret_cast(args)) + op_check_info_req.offSetLen)), sizeof(SysOpCheckResp), reinterpret_cast(&op_check_info_res), sizeof(SysOpCheckResp), RT_MEMCPY_HOST_TO_DEVICE)); GE_CHK_RT(rtStreamCreate(&stream, 0)); @@ -1647,24 +1653,21 @@ Status ModelManager::LaunchKernelCheckAicpuOp(std::vector &aicpu_op status = rtStreamSynchronize(stream); if (status != RT_ERROR_NONE) { GELOGE(RT_FAILED, "Call rt stream sync failed, status: 0x%x", status); + GE_CHK_RT(rtStreamDestroy(stream)); return RT_ERROR_TO_GE_STATUS(status); } // Check the response - SysOpCheckResp *d_op_check_info_res = reinterpret_cast(reinterpret_cast(static_cast(static_cast(reinterpret_cast(args)) + op_check_info_req.offSetLen))); + SysOpCheckResp *d_op_check_info_res = + reinterpret_cast(reinterpret_cast(static_cast(static_cast( + reinterpret_cast(args)) + op_check_info_req.offSetLen))); (void)memset_s(&op_check_info_res, sizeof(SysOpCheckResp), 0, sizeof(SysOpCheckResp)); GE_CHK_RT(rtMemcpy(&op_check_info_res, sizeof(SysOpCheckResp), d_op_check_info_res, sizeof(SysOpCheckResp), RT_MEMCPY_DEVICE_TO_HOST)); - std::function callback = [&]() { - for (auto mem : allocated_mem) { - GE_CHK_RT(rtFree(mem)); - } - GE_CHK_RT(rtStreamDestroy(stream)); - }; if (op_check_info_res.isWithoutJson) { GELOGI("No need to check aicpu in this scenoria."); - GE_MAKE_GUARD(release, callback); + GE_CHK_RT(rtStreamDestroy(stream)); return SUCCESS; } uint64_t res_op_nums = op_check_info_res.opListNum; @@ -1682,7 +1685,7 @@ Status ModelManager::LaunchKernelCheckAicpuOp(std::vector &aicpu_op sizeof(SysOpInfo) * res_op_nums, RT_MEMCPY_DEVICE_TO_HOST)); if (res_ret_code_list.size() != res_aicpu_op_info_list.size() || res_ret_code_list.size() != res_op_nums) { GELOGE(FAILED, "Number of retcode is not equal to number of op type."); - GE_MAKE_GUARD(release, callback); + GE_CHK_RT(rtStreamDestroy(stream)); return FAILED; } std::string fail_reason; @@ -1705,11 +1708,11 @@ Status ModelManager::LaunchKernelCheckAicpuOp(std::vector &aicpu_op } fail_reason += "not support."; GELOGE(FAILED, "Check aicpu op_type failed. details: %s", fail_reason.c_str()); - GE_MAKE_GUARD(release, callback); + GE_CHK_RT(rtStreamDestroy(stream)); return FAILED; } - GE_MAKE_GUARD(release, callback); + GE_CHK_RT(rtStreamDestroy(stream)); GELOGI("Cpu kernel launch check optype task success."); return SUCCESS; }