From 3f0839b26aabeb6e8feba68e9b623c7713e31753 Mon Sep 17 00:00:00 2001 From: chuxing Date: Mon, 22 Mar 2021 12:06:13 +0800 Subject: [PATCH 1/7] load davinci model in load phase --- .../compiledsubgraph/known_node_executor.cc | 40 +++++++++++-------- .../compiledsubgraph/known_node_executor.h | 5 +-- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc index bb96c275..918603f0 100755 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc @@ -95,13 +95,6 @@ Status KnownNodeTask::UpdateArgs(TaskContext &context) { Status KnownNodeTask::Init(TaskContext &context) { // allocate output mem GE_CHK_STATUS_RET(context.AllocateOutputs(), "known node task allocate output failed."); - - // init davinicmodel - if (!load_flag_) { - davinci_model_->InitRuntimeParams(); - GE_CHK_STATUS_RET(davinci_model_->InitVariableMem(), "init variable mem failed."); - } - // allocate mem base void *buffer = nullptr; if (davinci_model_->TotalMemSize() != 0) { @@ -129,14 +122,6 @@ Status KnownNodeTask::Init(TaskContext &context) { void *global_step = context.GetExecutionContext()->global_step; davinci_model_->SetKnownShapeGlobalStep(global_step); } - 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(), @@ -146,6 +131,25 @@ Status KnownNodeTask::Init(TaskContext &context) { return SUCCESS; } +Status KnownNodeTask::InitDavinciModel() { + davinci_model_->InitRuntimeParams(); + GE_CHK_STATUS_RET(davinci_model_->InitVariableMem(), "init variable mem failed"); + auto mem_size = davinci_model_->TotalMemSize(); + if (mem_size > 0) { + auto buffer = TensorBuffer::Create(NpuMemoryAllocator::GetAllocator(), mem_size); + GE_CHECK_NOTNULL(buffer); + davinci_model_->UpdateMemBase(static_cast(buffer->GetData())); + GELOGI("KnownNodeTask::Init mem base is %p, size %lu.", + davinci_model_->GetRuntimeParam().mem_base, davinci_model_->GetRuntimeParam().mem_size); + } + + int32_t device_id = 0; + GE_CHK_RT_RET(rtGetDevice(&device_id)); + davinci_model_->SetDeviceId(static_cast(device_id)); + GE_CHK_STATUS_RET(davinci_model_->Init(), "[Init][Model] Failed to init davinci model."); + return SUCCESS; +} + Status KnownNodeExecutor::PrepareTask(NodeTask &task, TaskContext &context) const { GELOGD("[%s] KnownNodeExecutor::PrepareTask in.", context.GetNodeName()); RECORD_EXECUTION_EVENT(context.GetExecutionContext(), context.GetNodeName(), "[KnownNodeExecutorPrepareTask] Start"); @@ -182,9 +186,11 @@ Status KnownNodeExecutor::LoadTask(const HybridModel &model, const NodePtr &node GE_CHK_STATUS_RET(davinci_model->Assign(ge_model), "KnownNodeExecutor::LoadTask davincimodel assign failed."); - task = MakeShared(davinci_model); - GE_CHECK_NOTNULL(task); + auto known_node_task = MakeShared(davinci_model); + GE_CHECK_NOTNULL(known_node_task); + GE_CHK_STATUS_RET_NOLOG(known_node_task->InitDavinciModel()); GELOGI("[%s] KnownNodeExecutor::LoadTask success.", node->GetName().c_str()); + task = std::move(known_node_task); return SUCCESS; } diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h index 6e9740ad..762e9991 100644 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h @@ -31,11 +31,12 @@ class KnownNodeTask : public NodeTask { : davinci_model_(davinci_model) {} - ~KnownNodeTask() {} + ~KnownNodeTask() = default; Status UpdateArgs(TaskContext &context) override; Status ExecuteAsync(TaskContext &context, std::function done_callback) override; Status Init(TaskContext &context) override; + Status InitDavinciModel(); private: std::shared_ptr davinci_model_ = nullptr; bool load_flag_ = false; @@ -47,8 +48,6 @@ class KnownNodeExecutor : public NodeExecutor { Status PrepareTask(NodeTask &task, TaskContext &context) const; Status ExecuteTask(NodeTask &task, TaskContext &context, const std::function &callback) const; ~KnownNodeExecutor() {} - private: - std::shared_ptr davinci_model_ = nullptr; }; } // namespace hybrid } // namespace ge From 241b2166ced7fb0fa661b0e34f688ae560940b81 Mon Sep 17 00:00:00 2001 From: chuxing Date: Mon, 22 Mar 2021 15:44:45 +0800 Subject: [PATCH 2/7] add ut --- .../compiledsubgraph/known_node_executor.cc | 6 +- .../compiledsubgraph/known_node_executor.h | 3 + tests/ut/ge/CMakeLists.txt | 1 + .../ge/hybrid/known_node_executor_unittest.cc | 65 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/ut/ge/hybrid/known_node_executor_unittest.cc diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc index 918603f0..d79fcc86 100755 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc @@ -146,10 +146,14 @@ Status KnownNodeTask::InitDavinciModel() { int32_t device_id = 0; GE_CHK_RT_RET(rtGetDevice(&device_id)); davinci_model_->SetDeviceId(static_cast(device_id)); - GE_CHK_STATUS_RET(davinci_model_->Init(), "[Init][Model] Failed to init davinci model."); + GE_CHK_STATUS_RET(DoInitDavinciModel(), "[Init][Model] Failed to init davinci model."); return SUCCESS; } +Status KnownNodeTask::DoInitDavinciModel() { + return davinci_model_->Init(); +} + Status KnownNodeExecutor::PrepareTask(NodeTask &task, TaskContext &context) const { GELOGD("[%s] KnownNodeExecutor::PrepareTask in.", context.GetNodeName()); RECORD_EXECUTION_EVENT(context.GetExecutionContext(), context.GetNodeName(), "[KnownNodeExecutorPrepareTask] Start"); diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h index 762e9991..5eed528a 100644 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.h @@ -37,6 +37,9 @@ class KnownNodeTask : public NodeTask { Status ExecuteAsync(TaskContext &context, std::function done_callback) override; Status Init(TaskContext &context) override; Status InitDavinciModel(); + + protected: + virtual Status DoInitDavinciModel(); private: std::shared_ptr davinci_model_ = nullptr; bool load_flag_ = false; diff --git a/tests/ut/ge/CMakeLists.txt b/tests/ut/ge/CMakeLists.txt index 3caba788..4c4a72a3 100755 --- a/tests/ut/ge/CMakeLists.txt +++ b/tests/ut/ge/CMakeLists.txt @@ -793,6 +793,7 @@ set(PROFILING_MNG_TEST_FILES set(HYBRID_TEST_FILES "hybrid/ge_hybrid_unittest.cc" + "hybrid/known_node_executor_unittest.cc" ) set(OTHERS_TEST_FILES diff --git a/tests/ut/ge/hybrid/known_node_executor_unittest.cc b/tests/ut/ge/hybrid/known_node_executor_unittest.cc new file mode 100644 index 00000000..21b3faa9 --- /dev/null +++ b/tests/ut/ge/hybrid/known_node_executor_unittest.cc @@ -0,0 +1,65 @@ +/** + * Copyright 2019-2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#define protected public +#define private public +#include "hybrid/node_executor/compiledsubgraph/known_node_executor.h" +#undef private +#undef protected +#include "graph/manager/graph_mem_allocator.h" + +using namespace std; +using namespace testing; +using namespace ge; +using namespace hybrid; + +class UnknownNodeExecutorTest : public testing::Test { + protected: + void SetUp() {} + void TearDown() {} +}; + +namespace { +class KnownNodeTaskMock : public KnownNodeTask { + public: + KnownNodeTaskMock(std::shared_ptr davinci_model): KnownNodeTask(davinci_model) {}; + ~KnownNodeTaskMock() override = default; + MOCK_METHOD0(DoInitDavinciModel, Status()); +}; +} + +TEST_F(UnknownNodeExecutorTest, test_init_davinci_model) { + auto davinci_model = std::make_shared(0, nullptr); + davinci_model->SetDeviceId(0); + davinci_model->SetKnownNode(true); + + MemManager::Instance().Initialize(std::vector({RT_MEMORY_HBM})); + + auto ge_model = make_shared(); + AttrUtils::SetInt(ge_model, ATTR_MODEL_VAR_SIZE, 0); + AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, 1024); + davinci_model->Assign(ge_model); + + KnownNodeTaskMock mock(davinci_model); + EXPECT_CALL(mock, DoInitDavinciModel).WillOnce(::testing::Return(SUCCESS)); + ASSERT_EQ(mock.InitDavinciModel(), SUCCESS); + MemManager::Instance().Finalize(); +} \ No newline at end of file From 62ff5490777f8c35882472ee2e41d31ea936e8ab Mon Sep 17 00:00:00 2001 From: chuxing Date: Mon, 22 Mar 2021 15:59:36 +0800 Subject: [PATCH 3/7] fix ut --- tests/ut/ge/hybrid/ge_hybrid_unittest.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/ut/ge/hybrid/ge_hybrid_unittest.cc b/tests/ut/ge/hybrid/ge_hybrid_unittest.cc index 3b5d19e6..e5669d15 100644 --- a/tests/ut/ge/hybrid/ge_hybrid_unittest.cc +++ b/tests/ut/ge/hybrid/ge_hybrid_unittest.cc @@ -51,7 +51,9 @@ class UtestGeHybrid : public testing::Test { protected: void SetUp() {} - void TearDown() {} + void TearDown() { + NpuMemoryAllocator::allocators_.clear(); + } }; static ge::OpDescPtr CreateOpDesc(string name = "", string type = "") { From d2ab17c3db136fbc3030a0afcf178cc5295211e4 Mon Sep 17 00:00:00 2001 From: chuxing Date: Tue, 23 Mar 2021 10:15:14 +0800 Subject: [PATCH 4/7] update --- .../compiledsubgraph/known_node_executor.cc | 14 ++------------ tests/ut/ge/hybrid/known_node_executor_unittest.cc | 3 --- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc index d79fcc86..bc0e5d93 100755 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc @@ -123,10 +123,9 @@ Status KnownNodeTask::Init(TaskContext &context) { davinci_model_->SetKnownShapeGlobalStep(global_step); } load_flag_ = true; - } else { - GE_CHK_STATUS_RET(ModelManager::GetInstance()->DestroyAicpuKernel(davinci_model_->GetSessionId(), - davinci_model_->Id(), davinci_model_->SubModelId()), "KnownNodeTask::Init destroy aicpu kernel failed."); } + GE_CHK_STATUS_RET(ModelManager::GetInstance()->DestroyAicpuKernel(davinci_model_->GetSessionId(), + davinci_model_->Id(), davinci_model_->SubModelId()), "KnownNodeTask::Init destroy aicpu kernel failed."); GELOGI("[%s] KnownNodeExecutor::Init success.", context.GetNodeName()); return SUCCESS; } @@ -134,15 +133,6 @@ Status KnownNodeTask::Init(TaskContext &context) { Status KnownNodeTask::InitDavinciModel() { davinci_model_->InitRuntimeParams(); GE_CHK_STATUS_RET(davinci_model_->InitVariableMem(), "init variable mem failed"); - auto mem_size = davinci_model_->TotalMemSize(); - if (mem_size > 0) { - auto buffer = TensorBuffer::Create(NpuMemoryAllocator::GetAllocator(), mem_size); - GE_CHECK_NOTNULL(buffer); - davinci_model_->UpdateMemBase(static_cast(buffer->GetData())); - GELOGI("KnownNodeTask::Init mem base is %p, size %lu.", - davinci_model_->GetRuntimeParam().mem_base, davinci_model_->GetRuntimeParam().mem_size); - } - int32_t device_id = 0; GE_CHK_RT_RET(rtGetDevice(&device_id)); davinci_model_->SetDeviceId(static_cast(device_id)); diff --git a/tests/ut/ge/hybrid/known_node_executor_unittest.cc b/tests/ut/ge/hybrid/known_node_executor_unittest.cc index 21b3faa9..5a3be8c3 100644 --- a/tests/ut/ge/hybrid/known_node_executor_unittest.cc +++ b/tests/ut/ge/hybrid/known_node_executor_unittest.cc @@ -51,8 +51,6 @@ TEST_F(UnknownNodeExecutorTest, test_init_davinci_model) { davinci_model->SetDeviceId(0); davinci_model->SetKnownNode(true); - MemManager::Instance().Initialize(std::vector({RT_MEMORY_HBM})); - auto ge_model = make_shared(); AttrUtils::SetInt(ge_model, ATTR_MODEL_VAR_SIZE, 0); AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, 1024); @@ -61,5 +59,4 @@ TEST_F(UnknownNodeExecutorTest, test_init_davinci_model) { KnownNodeTaskMock mock(davinci_model); EXPECT_CALL(mock, DoInitDavinciModel).WillOnce(::testing::Return(SUCCESS)); ASSERT_EQ(mock.InitDavinciModel(), SUCCESS); - MemManager::Instance().Finalize(); } \ No newline at end of file From c9b26a44c36dcc1932053521ae44d87138f82c25 Mon Sep 17 00:00:00 2001 From: chuxing Date: Tue, 23 Mar 2021 10:43:42 +0800 Subject: [PATCH 5/7] update --- .../node_executor/compiledsubgraph/known_node_executor.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc index bc0e5d93..45882343 100755 --- a/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc +++ b/ge/hybrid/node_executor/compiledsubgraph/known_node_executor.cc @@ -125,18 +125,21 @@ Status KnownNodeTask::Init(TaskContext &context) { load_flag_ = true; } GE_CHK_STATUS_RET(ModelManager::GetInstance()->DestroyAicpuKernel(davinci_model_->GetSessionId(), - davinci_model_->Id(), davinci_model_->SubModelId()), "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; } Status KnownNodeTask::InitDavinciModel() { + GELOGD("[Init][Model] start"); davinci_model_->InitRuntimeParams(); GE_CHK_STATUS_RET(davinci_model_->InitVariableMem(), "init variable mem failed"); int32_t device_id = 0; GE_CHK_RT_RET(rtGetDevice(&device_id)); davinci_model_->SetDeviceId(static_cast(device_id)); GE_CHK_STATUS_RET(DoInitDavinciModel(), "[Init][Model] Failed to init davinci model."); + GELOGD("[Init][Model] success"); return SUCCESS; } From c9ce876efcc649407713d8e1e1b64a216c26d8a5 Mon Sep 17 00:00:00 2001 From: chuxing Date: Tue, 23 Mar 2021 10:59:05 +0800 Subject: [PATCH 6/7] update --- .../ge/hybrid/known_node_executor_unittest.cc | 122 +++++++++--------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/tests/ut/ge/hybrid/known_node_executor_unittest.cc b/tests/ut/ge/hybrid/known_node_executor_unittest.cc index 5a3be8c3..67a8e323 100644 --- a/tests/ut/ge/hybrid/known_node_executor_unittest.cc +++ b/tests/ut/ge/hybrid/known_node_executor_unittest.cc @@ -1,62 +1,62 @@ -/** - * Copyright 2019-2021 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -#define protected public -#define private public -#include "hybrid/node_executor/compiledsubgraph/known_node_executor.h" -#undef private -#undef protected -#include "graph/manager/graph_mem_allocator.h" - -using namespace std; -using namespace testing; -using namespace ge; -using namespace hybrid; - -class UnknownNodeExecutorTest : public testing::Test { - protected: - void SetUp() {} - void TearDown() {} -}; - -namespace { -class KnownNodeTaskMock : public KnownNodeTask { - public: - KnownNodeTaskMock(std::shared_ptr davinci_model): KnownNodeTask(davinci_model) {}; - ~KnownNodeTaskMock() override = default; - MOCK_METHOD0(DoInitDavinciModel, Status()); -}; -} - -TEST_F(UnknownNodeExecutorTest, test_init_davinci_model) { - auto davinci_model = std::make_shared(0, nullptr); - davinci_model->SetDeviceId(0); - davinci_model->SetKnownNode(true); - - auto ge_model = make_shared(); - AttrUtils::SetInt(ge_model, ATTR_MODEL_VAR_SIZE, 0); - AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, 1024); - davinci_model->Assign(ge_model); - - KnownNodeTaskMock mock(davinci_model); - EXPECT_CALL(mock, DoInitDavinciModel).WillOnce(::testing::Return(SUCCESS)); - ASSERT_EQ(mock.InitDavinciModel(), SUCCESS); +/** + * Copyright 2019-2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#define protected public +#define private public +#include "hybrid/node_executor/compiledsubgraph/known_node_executor.h" +#undef private +#undef protected +#include "graph/manager/graph_mem_allocator.h" + +using namespace std; +using namespace testing; +using namespace ge; +using namespace hybrid; + +class UnknownNodeExecutorTest : public testing::Test { + protected: + void SetUp() {} + void TearDown() {} +}; + +namespace { +class KnownNodeTaskMock : public KnownNodeTask { + public: + KnownNodeTaskMock(std::shared_ptr davinci_model): KnownNodeTask(davinci_model) {}; + ~KnownNodeTaskMock() override = default; + MOCK_METHOD0(DoInitDavinciModel, Status()); +}; +} + +TEST_F(UnknownNodeExecutorTest, test_init_davinci_model) { + auto davinci_model = std::make_shared(0, nullptr); + davinci_model->SetDeviceId(0); + davinci_model->SetKnownNode(true); + + auto ge_model = make_shared(); + AttrUtils::SetInt(ge_model, ATTR_MODEL_VAR_SIZE, 0); + AttrUtils::SetInt(ge_model, ATTR_MODEL_MEMORY_SIZE, 1024); + davinci_model->Assign(ge_model); + + KnownNodeTaskMock mock(davinci_model); + EXPECT_CALL(mock, DoInitDavinciModel).WillOnce(::testing::Return(SUCCESS)); + ASSERT_EQ(mock.InitDavinciModel(), SUCCESS); } \ No newline at end of file From cd6ca2071b0adf47b2fb0b5f279e0513ad5adc54 Mon Sep 17 00:00:00 2001 From: chuxing Date: Thu, 25 Mar 2021 19:43:08 +0800 Subject: [PATCH 7/7] update --- ge/hybrid/model/hybrid_model_builder.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ge/hybrid/model/hybrid_model_builder.cc b/ge/hybrid/model/hybrid_model_builder.cc index a3b1da20..6bc3c35f 100755 --- a/ge/hybrid/model/hybrid_model_builder.cc +++ b/ge/hybrid/model/hybrid_model_builder.cc @@ -1077,7 +1077,12 @@ Status HybridModelBuilder::InitWeights() { Status HybridModelBuilder::LoadTasks() { GE_CHK_STATUS_RET(CheckAicpuOpList(), "Check Aicpu op failed."); + std::map ordered_node_items; for (auto &it : hybrid_model_.node_items_) { + auto &node_item = it.second; + ordered_node_items.emplace(node_item->node_id, node_item.get()); + } + for (auto &it : ordered_node_items) { auto &node_item = it.second; auto &node_ptr = node_item->node; if (node_item->node_type == NETOUTPUT) {