From 06499aaf2fd50a6617672edcf379b8b69af0a27a Mon Sep 17 00:00:00 2001 From: taoxiangdong Date: Wed, 30 Dec 2020 17:06:00 +0800 Subject: [PATCH] add submodel id in dynamic shape --- ge/graph/load/graph_loader.cc | 4 +-- ge/graph/load/graph_loader.h | 2 +- .../load/new_model_manager/davinci_model.h | 15 +++++++++ .../load/new_model_manager/model_manager.cc | 32 ++++++++++++------- .../load/new_model_manager/model_manager.h | 7 ++-- .../task_info/kernel_ex_task_info.cc | 3 +- ge/graph/manager/graph_manager.cc | 2 +- .../compiledsubgraph/known_node_executor.cc | 12 +++++-- ...el_manager_model_manager_aicpu_unittest.cc | 22 ++++++++++--- 9 files changed, 72 insertions(+), 27 deletions(-) diff --git a/ge/graph/load/graph_loader.cc b/ge/graph/load/graph_loader.cc index 93d1a1a9..6272e581 100755 --- a/ge/graph/load/graph_loader.cc +++ b/ge/graph/load/graph_loader.cc @@ -263,10 +263,10 @@ Status GraphLoader::GetMemoryInfo(int64_t &free) { return SUCCESS; } -Status GraphLoader::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id) { +Status GraphLoader::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id) { auto model_manager = ModelManager::GetInstance(); GE_CHECK_NOTNULL(model_manager); - Status ret = model_manager->DestroyAicpuKernel(session_id, model_id); + Status ret = model_manager->DestroyAicpuKernel(session_id, model_id, sub_model_id); if (ret != SUCCESS) { GELOGE(ret, "Destroy aicpu kernel failed."); return ret; diff --git a/ge/graph/load/graph_loader.h b/ge/graph/load/graph_loader.h index 3a13a113..3632a10a 100755 --- a/ge/graph/load/graph_loader.h +++ b/ge/graph/load/graph_loader.h @@ -62,7 +62,7 @@ class GraphLoader { const std::vector &input_desc, OutputData &output_data, std::vector &output_desc); - static Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id); + static Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id); static Status DestroyAicpuSessionForInfer(uint32_t model_id); diff --git a/ge/graph/load/new_model_manager/davinci_model.h b/ge/graph/load/new_model_manager/davinci_model.h index 76c5c8f0..ea71dfd9 100755 --- a/ge/graph/load/new_model_manager/davinci_model.h +++ b/ge/graph/load/new_model_manager/davinci_model.h @@ -173,6 +173,20 @@ class DavinciModel { /// void SetId(uint32_t model_id) { model_id_ = model_id; } + /// + /// @ingroup ge + /// @brief Get SubModelId + /// @return sub model ID + /// + uint32_t SubModelId() const { return sub_model_id_; } + + /// + /// @ingroup ge + /// @brief Get SubModelId + /// @return sub model ID + /// + void SetSubModelId(uint32_t sub_model_id) { sub_model_id_ = sub_model_id; } + static void *Run(DavinciModel *model_pointer); /// @@ -869,6 +883,7 @@ class DavinciModel { uint32_t model_id_; uint32_t runtime_model_id_; + uint32_t sub_model_id_ = 0; string name_; // used for inference data dump diff --git a/ge/graph/load/new_model_manager/model_manager.cc b/ge/graph/load/new_model_manager/model_manager.cc index 01075255..d189ff97 100755 --- a/ge/graph/load/new_model_manager/model_manager.cc +++ b/ge/graph/load/new_model_manager/model_manager.cc @@ -81,7 +81,8 @@ ModelManager::ModelManager() { session_id_bias_ = 0; } -Status ModelManager::KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id) { +Status ModelManager::KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id, + uint32_t sub_model_id) { STR_FWK_OP_KERNEL param_base = {}; void *devicebase = nullptr; void *aicpu_kernel_addr = nullptr; @@ -91,11 +92,12 @@ Status ModelManager::KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, u param_base.fwkKernelBase.fwk_kernel.sessionID = session_id; if (op_type == aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY) { std::vector v_aicpu_kernel; - std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id); + std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id) + "_" + + std::to_string(sub_model_id); std::lock_guard lock(map_mutex_); auto iter = model_aicpu_kernel_.find(model_key); if (iter != model_aicpu_kernel_.end()) { - GELOGD("kernel destroy session_id %lu, model_id %u.", session_id, model_id); + GELOGD("kernel destroy session_id %lu, model_id %u, sub_model_id %u..", session_id, model_id, sub_model_id); v_aicpu_kernel = model_aicpu_kernel_.at(model_key); // Insert size of aicpu kernel vector in the first element v_aicpu_kernel.insert(v_aicpu_kernel.begin(), v_aicpu_kernel.size()); @@ -193,7 +195,7 @@ void ModelManager::DestroyAicpuSession(uint64_t session_id) { GE_CHK_RT(rtSetDevice(static_cast(GetContext().DeviceId()))); } - Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_DESTROY, session_id, 0); + Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_DESTROY, session_id, 0, 0); if (ret != SUCCESS) { GELOGW("The session: %lu destroy failed.", session_id); } else { @@ -227,12 +229,14 @@ ge::Status ModelManager::DestroyAicpuSessionForInfer(uint32_t model_id) { return SUCCESS; } -ge::Status ModelManager::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id) { +ge::Status ModelManager::DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id) { GELOGD("destroy aicpu kernel in session_id %lu, model_id %u.", session_id, model_id); std::lock_guard lock(map_mutex_); - std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id); + std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id) + "_" + + std::to_string(sub_model_id); if (model_aicpu_kernel_.find(model_key) != model_aicpu_kernel_.end()) { - Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY, session_id, model_id); + Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_KERNEL_DESTROY, session_id, model_id, + sub_model_id); if (ret != SUCCESS) { GELOGE(FAILED, "Destroy aicpu kernel failed."); return FAILED; @@ -241,10 +245,12 @@ ge::Status ModelManager::DestroyAicpuKernel(uint64_t session_id, uint32_t model_ return SUCCESS; } -ge::Status ModelManager::CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint64_t kernel_id) { +ge::Status ModelManager::CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id, + uint64_t kernel_id) { std::lock_guard lock(map_mutex_); std::vector v_aicpu_kernel; - std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id); + std::string model_key = std::to_string(session_id) + "_" + std::to_string(model_id) + "_" + + std::to_string(sub_model_id); if (model_aicpu_kernel_.find(model_key) != model_aicpu_kernel_.end()) { v_aicpu_kernel = model_aicpu_kernel_.at(model_key); } @@ -379,7 +385,8 @@ Status ModelManager::DeleteModel(uint32_t id) { auto hybrid_model_it = hybrid_model_map_.find(id); if (it != model_map_.end()) { uint64_t session_id = it->second->GetSessionId(); - std::string model_key = std::to_string(session_id) + "_" + std::to_string(id); + std::string model_key = std::to_string(session_id) + "_" + std::to_string(id) + "_" + + std::to_string(it->second->SubModelId()); auto iter_aicpu_kernel = model_aicpu_kernel_.find(model_key); if (iter_aicpu_kernel != model_aicpu_kernel_.end()) { (void)model_aicpu_kernel_.erase(iter_aicpu_kernel); @@ -1230,7 +1237,8 @@ Status ModelManager::ExecuteModel(uint32_t model_id, rtStream_t stream, bool asy // Zero copy is enabled by default, no need to judge. uint64_t session_id_davinci = davinci_model->GetSessionId(); uint32_t model_id_davinci = davinci_model->GetModelId(); - Status status = DestroyAicpuKernel(session_id_davinci, model_id_davinci); + uint32_t sub_model_id = davinci_model->SubModelId(); + Status status = DestroyAicpuKernel(session_id_davinci, model_id_davinci, sub_model_id); if (status != SUCCESS) { GELOGW("Destroy specified aicpu kernel failed, session id is %lu, model id is %u.", session_id_davinci, model_id_davinci); @@ -1250,7 +1258,7 @@ Status ModelManager::CreateAicpuSession(uint64_t session_id) { auto it = sess_ids_.find(session_id); // never been created by any model if (it == sess_ids_.end()) { - Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_CREATE, session_id, 0); + Status ret = KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType::FWK_ADPT_SESSION_CREATE, session_id, 0, 0); if (ret == SUCCESS) { (void)sess_ids_.insert(session_id); GELOGI("The session: %lu create success.", session_id); diff --git a/ge/graph/load/new_model_manager/model_manager.h b/ge/graph/load/new_model_manager/model_manager.h index 088ea5fd..d67237be 100755 --- a/ge/graph/load/new_model_manager/model_manager.h +++ b/ge/graph/load/new_model_manager/model_manager.h @@ -273,7 +273,8 @@ class FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelManager { std::shared_ptr GetHybridModel(uint32_t id); - ge::Status KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id); + ge::Status KernelLaunchEx(aicpu::FWKAdapter::FWKOperateType op_type, uint64_t session_id, uint32_t model_id, + uint32_t sub_model_id); ge::Status CreateAicpuSession(uint64_t session_id); @@ -281,9 +282,9 @@ class FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ModelManager { void DestroyAicpuSession(uint64_t session_id); - ge::Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id); + ge::Status DestroyAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id); - ge::Status CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint64_t kernel_id); + ge::Status CreateAicpuKernel(uint64_t session_id, uint32_t model_id, uint32_t sub_model_id, uint64_t kernel_id); ge::Status DestroyAicpuSessionForInfer(uint32_t model_id); diff --git a/ge/graph/load/new_model_manager/task_info/kernel_ex_task_info.cc b/ge/graph/load/new_model_manager/task_info/kernel_ex_task_info.cc index 1f3a4592..ed9ddb89 100644 --- a/ge/graph/load/new_model_manager/task_info/kernel_ex_task_info.cc +++ b/ge/graph/load/new_model_manager/task_info/kernel_ex_task_info.cc @@ -92,7 +92,8 @@ Status KernelExTaskInfo::Init(const domi::TaskDef &task_def, DavinciModel *davin // 2.2 Collect aicpu kernel uint64_t kernel_id = fwk_op_kernel.fwkKernelBase.fwk_kernel.kernelID; - GE_IF_BOOL_EXEC(ModelManager::GetInstance()->CreateAicpuKernel(session_id, davinci_model->Id(), kernel_id) != SUCCESS, + GE_IF_BOOL_EXEC(ModelManager::GetInstance()->CreateAicpuKernel(session_id, davinci_model->Id(), + davinci_model->SubModelId(), kernel_id) != SUCCESS, GELOGE(FAILED, "CreateAicpuKernel error."); return FAILED;) // 2.3 Create session diff --git a/ge/graph/manager/graph_manager.cc b/ge/graph/manager/graph_manager.cc index beb7cd42..83a11f9f 100755 --- a/ge/graph/manager/graph_manager.cc +++ b/ge/graph/manager/graph_manager.cc @@ -2467,7 +2467,7 @@ Status GraphManager::CheckAndReleaseMemory(const GeModelPtr &ge_model, const Gra GELOGE(RT_FAILED, "[GraphManager:] rtSetDevice failed, modelId=%u, graphId=%u.", model_id, graph_id); continue; } - result = GraphLoader::DestroyAicpuKernel(session_id, model_id); + result = GraphLoader::DestroyAicpuKernel(session_id, model_id, 0); if (result != SUCCESS) { GELOGW("[GraphManager:] destroy aicpu kernel failed when dynamic memory, modelId=%u, graphId=%u.", model_id, graph_id); diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc index c914ac1b..7f2c6288 100755 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc @@ -127,11 +127,18 @@ Status KnownNodeTask::Init(TaskContext &context) { if (dump_properties.IsDumpOpen()) { davinci_model_->SetDumpProperties(dump_properties); } + int32_t device_id = 0; + rtError_t rt_ret = rtGetDevice(&device_id); + if (rt_ret != RT_ERROR_NONE || device_id < 0) { + GELOGE(rt_ret, "Call rtGetDevice failed, ret = 0x%X, device_id = %d.", rt_ret, device_id); + return RT_ERROR_TO_GE_STATUS(rt_ret); + } + davinci_model_->SetDeviceId(device_id); GE_CHK_STATUS_RET(davinci_model_->Init(), "KnownNodeExecutor::InitDavinciModel failed."); load_flag_ = true; } else { GE_CHK_STATUS_RET(ModelManager::GetInstance()->DestroyAicpuKernel(davinci_model_->GetSessionId(), - davinci_model_->Id()), "KnownNodeTask::Init destroy aicpu kernel failed."); + davinci_model_->Id(), davinci_model_->SubModelId()), "KnownNodeTask::Init destroy aicpu kernel failed."); } GELOGI("[%s] KnownNodeExecutor::Init success.", context.GetNodeName()); return SUCCESS; @@ -165,8 +172,9 @@ Status KnownNodeExecutor::LoadTask(const HybridModel &model, const NodePtr &node // set known node flag as true davinci_model->SetKnownNode(true); + davinci_model->SetId(model.GetModelId()); // set model id as root node's node id - davinci_model->SetId(node->GetOpDesc()->GetId()); + davinci_model->SetSubModelId(node->GetOpDesc()->GetId()); GELOGD("KnownNodeExecutor::LoadTask node id %ld.", node->GetOpDesc()->GetId()); GE_CHK_STATUS_RET(davinci_model->Assign(ge_model), "KnownNodeExecutor::LoadTask davincimodel assign failed."); diff --git a/tests/ut/ge/graph/load/new_model_manager_model_manager_aicpu_unittest.cc b/tests/ut/ge/graph/load/new_model_manager_model_manager_aicpu_unittest.cc index 0539bcdb..666d40f4 100644 --- a/tests/ut/ge/graph/load/new_model_manager_model_manager_aicpu_unittest.cc +++ b/tests/ut/ge/graph/load/new_model_manager_model_manager_aicpu_unittest.cc @@ -14,24 +14,23 @@ * limitations under the License. */ +#include #include -#include #include "common/debug/log.h" +#include "common/l2_cache_optimize.h" #include "common/model_parser/base.h" #include "common/properties_manager.h" #include "common/types.h" -#include "common/l2_cache_optimize.h" #define private public #define protected public -#include "graph/load/new_model_manager/model_manager.h" - #include "common/helper/om_file_helper.h" #include "common/op/ge_op_utils.h" #include "graph/load/graph_loader.h" #include "graph/load/new_model_manager/davinci_model.h" #include "graph/load/new_model_manager/davinci_model_parser.h" +#include "graph/load/new_model_manager/model_manager.h" //#include "new_op_test_utils.h" #undef private #undef protected @@ -60,7 +59,20 @@ TEST_F(UtestModelManagerModelManagerAicpu, checkAicpuOptype) { model_manager.LaunchKernelCheckAicpuOp(aicpu_op_list, aicpu_tf_list); // Load allow listener is null - //EXPECT_EQ(ge::FAILED, mm.LoadModelOffline(model_id, data, nullptr, nullptr)); + // EXPECT_EQ(ge::FAILED, mm.LoadModelOffline(model_id, data, nullptr, nullptr)); +} + +TEST_F(UtestModelManagerModelManagerAicpu, DestroyAicpuKernel) { + ModelManager model_manager; + uint32_t model_id = 0; + std::vector aicpu_op_list; + std::vector aicpu_tf_list; + aicpu_tf_list.emplace_back("FrameworkOp"); + aicpu_tf_list.emplace_back("Unique"); + + model_manager.DestroyAicpuKernel(0,0,0); + // Load allow listener is null + // EXPECT_EQ(ge::FAILED, mm.LoadModelOffline(model_id, data, nullptr, nullptr)); } } // namespace ge