diff --git a/ge/graph/build/graph_builder.cc b/ge/graph/build/graph_builder.cc index 0883d895..a185ee0e 100644 --- a/ge/graph/build/graph_builder.cc +++ b/ge/graph/build/graph_builder.cc @@ -400,6 +400,10 @@ static Status InsertMemcpyNode(const ComputeGraphPtr &graph, const OutDataAnchor } static Status GenerateTaskForConstant(const std::shared_ptr &graph) { + if (graph->GetGraphUnknownFlag()) { + GELOGI("Graph %s is unknown graph, ignore gen_task for constant.", graph->GetName().c_str()); + return SUCCESS; + } for (auto &node : graph->GetDirectNode()) { // CONSTANT not generate task, so insert IDENTITY between CONSTANT and NETOUTPUT auto op_desc = node->GetOpDesc(); diff --git a/ge/graph/load/model_manager/model_manager.cc b/ge/graph/load/model_manager/model_manager.cc index e46bef88..0273b77e 100755 --- a/ge/graph/load/model_manager/model_manager.cc +++ b/ge/graph/load/model_manager/model_manager.cc @@ -298,10 +298,8 @@ Status ModelManager::LoadModelOnline(uint32_t &model_id, const shared_ptrCheckIsUnknownShape(is_shape_unknown), "CheckIsUnknownShape failed, model id:%u", - model_id); + bool is_shape_unknown = ge_root_model->GetRootGraph()->GetGraphUnknownFlag(); if (is_shape_unknown || GetContext().GetHostExecFlag()) { return DoLoadHybridModelOnline(model_id, model_name, ge_root_model, listener); } diff --git a/ge/host_kernels/concat_offset_kernel.cc b/ge/host_kernels/concat_offset_kernel.cc index ff597873..b6940eb4 100644 --- a/ge/host_kernels/concat_offset_kernel.cc +++ b/ge/host_kernels/concat_offset_kernel.cc @@ -33,7 +33,7 @@ const int kNumOne = 1; } // namespace Status ConcatOffsetKernel::Compute(const OpDescPtr op_desc_ptr, const vector &input, vector &v_output) { - GELOGI("ConcatOffsetKernel in."); + GELOGD("ConcatOffsetKernel in"); if (op_desc_ptr == nullptr) { GELOGE(PARAM_INVALID, "input opdesc is nullptr."); return PARAM_INVALID; @@ -41,7 +41,7 @@ Status ConcatOffsetKernel::Compute(const OpDescPtr op_desc_ptr, const vector(reinterpret_cast(input_0->GetData().data()))); // validate inputs if ((static_cast(input.size()) != (N + kNumOne)) || (input.size() <= kConcatOffsetInputIndexOne)) { - GELOGW("The number of input for concat offset must be equal to %d, and must be more than one.", (N + kNumOne)); + GELOGW("The number of input for concat offset must be equal to %d, and must be more than one", (N + kNumOne)); return NOT_CHANGED; } @@ -61,7 +61,7 @@ Status ConcatOffsetKernel::Compute(const OpDescPtr op_desc_ptr, const vectorMutableTensorDesc().SetShape(output_shape); GE_IF_BOOL_EXEC(output_ptr->SetData(reinterpret_cast(buf.get()), static_cast(sizeof(DT_INT32) * output_size)) != GRAPH_SUCCESS, - GELOGW("set data failed"); + GELOGW("set data failed."); return NOT_CHANGED); v_output.push_back(output_ptr); // caculate offset @@ -99,7 +99,7 @@ Status ConcatOffsetKernel::Compute(const OpDescPtr op_desc_ptr, const vector(reinterpret_cast(indices_tensor_ptr->GetData().data())); for (int64_t i = 0; i < indices_shape.GetShapeSize(); i++) { if (*(indices_ptr + i) < 0 || *(indices_ptr + i) >= x_shape.GetDim(axis)) { - GELOGW("indices %ld value is not in range [0, %ld)", i, x_shape.GetDim(axis)); + GELOGW("indices %ld value is not in range [0, %ld).", i, x_shape.GetDim(axis)); return NOT_CHANGED; } indicates_.push_back(*(indices_ptr + i)); @@ -288,7 +288,7 @@ Status GatherV2Kernel::SaveIndicesByDataType(ConstGeTensorPtr indices_tensor_ptr auto indices_ptr = const_cast(reinterpret_cast(indices_tensor_ptr->GetData().data())); for (int64_t i = 0; i < indices_shape.GetShapeSize(); i++) { if (*(indices_ptr + i) < 0 || *(indices_ptr + i) >= x_shape.GetDim(axis)) { - GELOGW("indices %ld value is not in range [0, %ld)", i, x_shape.GetDim(axis)); + GELOGW("indices %ld value is not in range [0, %ld).", i, x_shape.GetDim(axis)); return NOT_CHANGED; } indicates_.push_back(*(indices_ptr + i)); @@ -344,42 +344,42 @@ Status GatherV2Kernel::Check(const OpDescPtr &op_desc_ptr, const vectorGetTensorDesc().GetDataType(); bool is_valid_indices_data_type = indices_data_type == DT_INT32 || indices_data_type == DT_INT64; if (!is_valid_indices_data_type) { - GELOGW("indices datatype must be DT_INT32 or DT_INT64"); + GELOGW("indices datatype must be DT_INT32 or DT_INT64."); return NOT_CHANGED; } if (indices_shape.GetDimNum() > kMaxIndicatesDims) { - GELOGW("indices input only support 0 or 1 dims"); + GELOGW("indices input only support 0 or 1 dims."); return NOT_CHANGED; } return SUCCESS; } void GatherV2Kernel::DebugPrint(int64_t axis, const GeShape &x_shape, const GeShape &indices_shape, const std::vector &y_shape) { - GELOGD("GatherV2Kernel axis:%ld x_shape:%zu indices_shape:%zu y_shape:%zu", axis, x_shape.GetDimNum(), + GELOGD("GatherV2Kernel axis:%ld x_shape:%zu indices_shape:%zu y_shape:%zu.", axis, x_shape.GetDimNum(), indices_shape.GetDimNum(), y_shape.size()); for (size_t i = 0; i < x_shape.GetDimNum(); i++) { - GELOGD("GatherV2Kernel x_shape[%zu]: %ld", i, x_shape.GetDim(i)); + GELOGD("GatherV2Kernel x_shape[%zu]: %ld.", i, x_shape.GetDim(i)); } for (size_t i = 0; i < indices_shape.GetDimNum(); i++) { - GELOGD("GatherV2Kernel indices_shape[%zu]: %ld", i, indices_shape.GetDim(i)); + GELOGD("GatherV2Kernel indices_shape[%zu]: %ld.", i, indices_shape.GetDim(i)); } for (size_t i = 0; i < y_shape.size(); i++) { - GELOGD("GatherV2Kernel y_shape[%zu]: %ld", i, y_shape[i]); + GELOGD("GatherV2Kernel y_shape[%zu]: %ld.", i, y_shape[i]); } for (auto ele : indicates_) { - GELOGD("GatherV2Kernel indices:%ld", ele); + GELOGD("GatherV2Kernel indices:%ld.", ele); } } Status GatherV2Kernel::Compute(const OpDescPtr op_desc_ptr, const vector &input, vector &v_output) { - GELOGI("Enter GatherV2Kernel Process."); + GELOGI("Enter GatherV2Kernel Process"); Status ret = Check(op_desc_ptr, input, v_output); if (ret != SUCCESS) { - GELOGW("param check failed."); + GELOGW("param check failed"); return NOT_CHANGED; } - GELOGI("GatherV2Kernel[%s] start Process.", op_desc_ptr->GetName().c_str()); + GELOGI("GatherV2Kernel[%s] start Process", op_desc_ptr->GetName().c_str()); ConstGeTensorPtr tensor0 = input.at(kGatherV2InputIndexZero); ConstGeTensorPtr tensor1 = input.at(kGatherV2InputIndexOne); ConstGeTensorPtr tensor2 = input.at(kGatherV2InputIndexTwo); @@ -394,7 +394,7 @@ Status GatherV2Kernel::Compute(const OpDescPtr op_desc_ptr, const vector= 0 ? axis : axis + x_shape.GetDimNum(); // check axis value if (axis < 0 || (axis + 1) > static_cast(x_shape.GetDimNum())) { - GELOGW("axis is invalid"); + GELOGW("axis is invalid!"); return NOT_CHANGED; } auto indices_data_type = tensor1->GetTensorDesc().GetDataType(); @@ -407,7 +407,7 @@ Status GatherV2Kernel::Compute(const OpDescPtr op_desc_ptr, const vectorGetTensorDesc().GetDataType(); if (supported_type.find(x_data_type) == supported_type.end()) { - GELOGI("GatherV2Kernel does not support this Data type:%s", TypeUtils::DataTypeToSerialString(x_data_type).c_str()); + GELOGI("GatherV2Kernel does not support this Data type:%s.", TypeUtils::DataTypeToSerialString(x_data_type).c_str()); return NOT_CHANGED; } // calc output shape diff --git a/ge/host_kernels/identity_kernel.cc b/ge/host_kernels/identity_kernel.cc index 702f5c93..ef1446a8 100644 --- a/ge/host_kernels/identity_kernel.cc +++ b/ge/host_kernels/identity_kernel.cc @@ -61,4 +61,5 @@ Status IdentityKernel::Compute(const ge::OpDescPtr op_desc, const std::vector &input, vector &input, vector &v_output) { - GELOGD("StridedSliceKernel in."); + GELOGD("StridedSliceKernel in"); // 1.Check input and attrs if (CheckAndGetAttr(attr) != SUCCESS) { - GELOGW("Check and get attrs failed.Ignore kernel."); + GELOGW("Check and get attrs failed.Ignore kernel"); return NOT_CHANGED; } if (CheckInputParam(input) != SUCCESS) { - GELOGW("Check input params failed.Ignore kernel."); + GELOGW("Check input params failed.Ignore kernel"); return NOT_CHANGED; } // 2.Init param with mask attrs. @@ -120,7 +120,7 @@ Status StridedSliceKernel::Compute(const ge::OpDescPtr attr, const std::vector(data_size), data_type, input_dims, begin_vec, output_dims, output_ptr.get(), stride_vec); if (ret != SUCCESS) { - GELOGE(INTERNAL_ERROR, "SetOutputSliceData failed."); + GELOGE(INTERNAL_ERROR, "SetOutputSliceData failed"); return NOT_CHANGED; } @@ -133,7 +133,7 @@ Status StridedSliceKernel::Compute(const ge::OpDescPtr attr, const std::vector & return PARAM_INVALID; } if (kIndexNumberType.find(begin_tensor_desc.GetDataType()) == kIndexNumberType.end()) { - GELOGW("Data type of StridedSlice OP(begin,end,strides) must be int32 or int64."); + GELOGW("Data type of StridedSlice OP(begin,end,strides) must be int32 or int64"); return PARAM_INVALID; } @@ -250,7 +250,7 @@ Status StridedSliceKernel::InitParamWithAttrs(const std::vector weight_buffer_; + std::map> weight_buffer_map_; RuntimeParam root_runtime_param_; string om_name_; }; diff --git a/ge/hybrid/model/hybrid_model_builder.cc b/ge/hybrid/model/hybrid_model_builder.cc index a349210d..f5cb5f7e 100755 --- a/ge/hybrid/model/hybrid_model_builder.cc +++ b/ge/hybrid/model/hybrid_model_builder.cc @@ -1012,70 +1012,65 @@ Status HybridModelBuilder::InitVariableTensors() { Status HybridModelBuilder::InitWeights() { // For constant in root graph - const auto &root_graph = ge_root_model_->GetRootGraph(); - const auto &subgraph_models = ge_root_model_->GetSubgraphInstanceNameToModel(); - auto iter = subgraph_models.find(root_graph->GetName()); - if (iter == subgraph_models.end()) { - GELOGD("Root graph model not found"); - return SUCCESS; - } + for (const auto &subgraph_model : ge_root_model_->GetSubgraphInstanceNameToModel()) { + const auto &weight_buffer = subgraph_model.second->GetWeight(); + if (weight_buffer.GetSize() == 0) { + GELOGD("weight is empty"); + return SUCCESS; + } - auto &root_model = iter->second; - const auto &weight_buffer = root_model->GetWeight(); - if (weight_buffer.GetSize() == 0) { - GELOGD("weight is empty"); - return SUCCESS; - } + auto allocator = NpuMemoryAllocator::GetAllocator(); + GE_CHECK_NOTNULL(allocator); + auto sub_weight_buffer = TensorBuffer::Create(allocator, weight_buffer.size()); + GE_CHECK_NOTNULL(sub_weight_buffer); + auto weight_base = reinterpret_cast(sub_weight_buffer->GetData()); + GE_CHK_RT_RET(rtMemcpy(weight_base, + sub_weight_buffer->GetSize(), + weight_buffer.GetData(), + weight_buffer.GetSize(), + RT_MEMCPY_HOST_TO_DEVICE)); + + GELOGI("Init weight mem successfully, weight base %p, weight size = %zu", + weight_base, + sub_weight_buffer->GetSize()); + auto root_graph = GraphUtils::GetComputeGraph(subgraph_model.second->GetGraph()); + hybrid_model_.weight_buffer_map_.emplace(root_graph->GetName(),std::move(sub_weight_buffer)); + for (auto &node : root_graph->GetDirectNode()) { + if (node->GetType() != CONSTANT) { + continue; + } - auto allocator = NpuMemoryAllocator::GetAllocator(); - GE_CHECK_NOTNULL(allocator); - hybrid_model_.weight_buffer_ = TensorBuffer::Create(allocator, weight_buffer.size()); - GE_CHECK_NOTNULL(hybrid_model_.weight_buffer_); - auto weight_base = reinterpret_cast(hybrid_model_.weight_buffer_->GetData()); - GE_CHK_RT_RET(rtMemcpy(weight_base, - hybrid_model_.weight_buffer_->GetSize(), - weight_buffer.GetData(), - weight_buffer.GetSize(), - RT_MEMCPY_HOST_TO_DEVICE)); - - GELOGI("Init weight mem successfully, weight base %p, weight size = %zu", - weight_base, - hybrid_model_.weight_buffer_->GetSize()); - for (auto &node : root_graph->GetDirectNode()) { - if (node->GetType() != CONSTANT) { - continue; - } + auto op_desc = node->GetOpDesc(); + auto v_weights = ModelUtils::GetWeights(op_desc); + if (v_weights.empty()) { + GELOGE(INTERNAL_ERROR, "[%s] Constant has no value", node->GetName().c_str()); + return INTERNAL_ERROR; + } + auto *ge_tensor = const_cast(v_weights[0].get()); + GE_CHECK_NOTNULL(ge_tensor); + const GeTensorDesc &tensor_desc = ge_tensor->GetTensorDesc(); + int64_t tensor_size = 0; + GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetSize(*op_desc->MutableOutputDesc(0), tensor_size), + "[%s] Failed to get tensor size", + node->GetName().c_str()); + int64_t data_offset = 0; + GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetDataOffset(tensor_desc, data_offset), + "[%s] Failed to get data offset", + node->GetName().c_str()); + GELOGD("[%s] Start to init Constant node [%s], size = %ld, offset = %ld", + GetGraphName(), + node->GetName().c_str(), + tensor_size, + data_offset); - auto op_desc = node->GetOpDesc(); - auto v_weights = ModelUtils::GetWeights(op_desc); - if (v_weights.empty()) { - GELOGE(INTERNAL_ERROR, "[%s] Constant has no value", node->GetName().c_str()); - return INTERNAL_ERROR; + auto tensor_buffer = TensorBuffer::Create(weight_base + data_offset, tensor_size); + GE_CHECK_NOTNULL(tensor_buffer); + std::unique_ptr constant_tensor(new (std::nothrow)TensorValue(std::move(tensor_buffer))); + GE_CHECK_NOTNULL(constant_tensor); + constant_tensor->SetName("Constant_" + op_desc->GetName()); + hybrid_model_.constant_tensors_.emplace(node, std::move(constant_tensor)); + GELOGD("[%s] Constant node [%s] added, size = %ld", GetGraphName(), node->GetName().c_str(), tensor_size); } - auto *ge_tensor = const_cast(v_weights[0].get()); - GE_CHECK_NOTNULL(ge_tensor); - const GeTensorDesc &tensor_desc = ge_tensor->GetTensorDesc(); - int64_t tensor_size = 0; - GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetSize(*op_desc->MutableOutputDesc(0), tensor_size), - "[%s] Failed to get tensor size", - node->GetName().c_str()); - int64_t data_offset = 0; - GE_CHK_GRAPH_STATUS_RET(TensorUtils::GetDataOffset(tensor_desc, data_offset), - "[%s] Failed to get data offset", - node->GetName().c_str()); - GELOGD("[%s] Start to init Constant node [%s], size = %ld, offset = %ld", - GetGraphName(), - node->GetName().c_str(), - tensor_size, - data_offset); - - auto tensor_buffer = TensorBuffer::Create(weight_base + data_offset, tensor_size); - GE_CHECK_NOTNULL(tensor_buffer); - std::unique_ptr constant_tensor(new (std::nothrow)TensorValue(std::move(tensor_buffer))); - GE_CHECK_NOTNULL(constant_tensor); - constant_tensor->SetName("Constant_" + op_desc->GetName()); - hybrid_model_.constant_tensors_.emplace(node, std::move(constant_tensor)); - GELOGD("[%s] Constant node [%s] added, size = %ld", GetGraphName(), node->GetName().c_str(), tensor_size); } return SUCCESS; } diff --git a/ge/hybrid/node_executor/task_context.cc b/ge/hybrid/node_executor/task_context.cc index 84dd8fd8..f4271551 100644 --- a/ge/hybrid/node_executor/task_context.cc +++ b/ge/hybrid/node_executor/task_context.cc @@ -236,7 +236,7 @@ Status TaskContext::AllocateOutput(int index, ref_node->GetName().c_str(), ref_node->GetType().c_str()); - TensorValue *ref_tensor = execution_context_->model->GetVariable(ref_node->GetName()); + TensorValue *ref_tensor = execution_context_->model->GetTensor(ref_node); GE_CHECK_NOTNULL(ref_tensor); outputs_start_[index] = *ref_tensor; } else { diff --git a/tests/ut/ge/hybrid/ge_hybrid_unittest.cc b/tests/ut/ge/hybrid/ge_hybrid_unittest.cc index 286186de..d7116dbc 100644 --- a/tests/ut/ge/hybrid/ge_hybrid_unittest.cc +++ b/tests/ut/ge/hybrid/ge_hybrid_unittest.cc @@ -15,8 +15,8 @@ */ #include +#include #include - #include "runtime/rt.h" #define protected public @@ -25,7 +25,6 @@ #include "hybrid/model/hybrid_model.h" #include "model/ge_model.h" #include "model/ge_root_model.h" - #include "hybrid/node_executor/aicore/aicore_op_task.h" #include "framework/common/taskdown_common.h" #include "framework/common/debug/log.h" @@ -33,7 +32,10 @@ #include "hybrid/executor/hybrid_execution_context.h" #include "hybrid/node_executor/aicore/aicore_task_builder.h" #include "graph/load/model_manager/tbe_handle_store.h" +#include "graph/manager/graph_mem_allocator.h" +#include "hybrid/common/npu_memory_allocator.h" #include "graph/types.h" +#include "graph/utils/tensor_utils.h" #undef private #undef protected @@ -43,6 +45,7 @@ using namespace testing; using namespace ge; using namespace hybrid; + class UtestGeHybrid : public testing::Test { protected: void SetUp() {} @@ -204,4 +207,39 @@ TEST_F(UtestGeHybrid, index_taskdefs_success) { HybridModelBuilder hybrid_model_builder(hybrid_model); ASSERT_EQ(hybrid_model_builder.IndexTaskDefs(graph, ge_model), SUCCESS); +} + +TEST_F(UtestGeHybrid, init_weight_success) { + NpuMemoryAllocator::allocators_.emplace(make_pair(0, nullptr)); + // make graph with sub_graph + ComputeGraphPtr graph = std::make_shared("root_graph"); + OpDescPtr op_desc = CreateOpDesc("if", IF); + NodePtr node = graph->AddNode(op_desc); + // make sub graph + ComputeGraphPtr sub_graph = std::make_shared("if_sub_graph"); + OpDescPtr const_op_desc = CreateOpDesc("const", CONSTANT); + vector dims_vec_0 = {2, 1, 4, 1, 2}; + vector data_vec_0 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; + GeTensorDesc tensor_desc_0(GeShape(dims_vec_0), FORMAT_NCHW, DT_INT32); + (void)TensorUtils::SetRealDimCnt(tensor_desc_0, dims_vec_0.size()); + ConstGeTensorPtr constTensor_0 = + std::make_shared(tensor_desc_0, (uint8_t *)&data_vec_0[0], data_vec_0.size() * sizeof(int32_t)); + AttrUtils::SetTensor(const_op_desc, ge::ATTR_NAME_WEIGHTS, constTensor_0); + const_op_desc->AddOutputDesc(tensor_desc_0); + NodePtr const_node = sub_graph->AddNode(const_op_desc); + graph->AddSubgraph("sub", sub_graph); + + GeRootModelPtr ge_root_model = make_shared(graph); + GeModelPtr ge_sub_model = make_shared(); + //Buffer weight_buffer = Buffer(128,0); + //ge_sub_model->SetWeight(weight_buffer); + ge_root_model->SetSubgraphInstanceNameToModel("sub",ge_sub_model); + HybridModel hybrid_model(ge_root_model); + HybridModelBuilder hybrid_model_builder(hybrid_model); + auto ret = hybrid_model_builder.InitWeights(); + ASSERT_EQ(ret,SUCCESS); + Buffer weight_buffer = Buffer(128,0); + ge_sub_model->SetWeight(weight_buffer); + ret = hybrid_model_builder.InitWeights(); + ASSERT_EQ(ret,PARAM_INVALID); } \ No newline at end of file