From 13a744ec3fa7e18de195744d37a909a381667008 Mon Sep 17 00:00:00 2001 From: chenping38 Date: Thu, 14 Apr 2022 18:00:29 +0800 Subject: [PATCH] DVPP method adapt and lite whl package bugfix. --- include/api/types.h | 1 + mindspore/lite/src/CMakeLists.txt | 10 ++++--- mindspore/lite/src/common/tensor_util.cc | 22 ++++++++++++++++ .../lite/src/cxx_api/model/model_impl.cc | 7 +++++ mindspore/lite/src/ms_tensor.cc | 19 +++++++++++++- .../kernel/ascend/src/model_process.cc | 26 ++++++++++++++++--- mindspore/lite/tools/converter/converter.cc | 9 +++++++ 7 files changed, 86 insertions(+), 8 deletions(-) diff --git a/include/api/types.h b/include/api/types.h index 816c4398ad0..41b2bfac498 100644 --- a/include/api/types.h +++ b/include/api/types.h @@ -140,6 +140,7 @@ class MS_API MSTensor { MSTensor(); explicit MSTensor(const std::shared_ptr &impl); + // if malloc data, user need to free after constructing MSTensor, else memory leak. inline MSTensor(const std::string &name, DataType type, const std::vector &shape, const void *data, size_t data_len); explicit MSTensor(std::nullptr_t); diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 68fdb4dba15..936ce5df8e4 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -480,14 +480,18 @@ if(MSLITE_ENABLE_RUNTIME_CONVERT) quantizer_mid fusion_mid proto_mid graph_pass_mid preprocess_mid cpu_kernel_mid ccsrc_src_mid converter_src_mid anf_exporter_mid config_parser_mid mslite_converter_plugin mindspore_core coder_mid - ccsrc_debug_common_mid_ mindir_proto_mid _mindspore_transform_express_ir_obj mindir_serializer_mid mindspore::protobuf ${SECUREC_LIBRARY}) target_link_libraries(mindspore-lite_static quantizer_mid fusion_mid proto_mid graph_pass_mid preprocess_mid cpu_kernel_mid ccsrc_src_mid converter_src_mid anf_exporter_mid config_parser_mid mslite_converter_plugin mindspore_core coder_mid - ccsrc_debug_common_mid_ mindir_proto_mid _mindspore_transform_express_ir_obj mindir_serializer_mid mindspore::protobuf ${SECUREC_LIBRARY}) + if(NOT ENABLE_CLOUD_AND_LITE) + target_link_libraries(mindspore-lite + ccsrc_debug_common_mid_ mindir_proto_mid _mindspore_transform_express_ir_obj) + target_link_libraries(mindspore-lite_static + ccsrc_debug_common_mid_ mindir_proto_mid _mindspore_transform_express_ir_obj) + endif() if(MSLITE_ENABLE_ACL) target_link_libraries(mindspore-lite lite_acl_mid mindspore_shared_lib) @@ -552,4 +556,4 @@ endif() if(ENABLE_CLOUD_AND_LITE) set_target_properties(mindspore-lite PROPERTIES INSTALL_RPATH $ORIGIN) -endif() \ No newline at end of file +endif() diff --git a/mindspore/lite/src/common/tensor_util.cc b/mindspore/lite/src/common/tensor_util.cc index c1bce578534..916d6c77148 100644 --- a/mindspore/lite/src/common/tensor_util.cc +++ b/mindspore/lite/src/common/tensor_util.cc @@ -266,6 +266,20 @@ int CheckTensorsInvalid(const std::vector &tensors) { return RET_OK; } +std::string ShapeToString(const std::vector &shape) { + std::string result = "["; + int max_size = 40; + result.reserve(max_size); + for (size_t i = 0; i < shape.size(); ++i) { + result += std::to_string(shape[i]); + if (i + 1 < shape.size()) { + result += ", "; + } + } + result += "]"; + return result; +} + int CheckGraphInputShapes(const std::vector &inputs, const std::unordered_map> &input_shape_map) { for (const auto input : inputs) { @@ -275,9 +289,17 @@ int CheckGraphInputShapes(const std::vector &inputs, return RET_ERROR; } if (!input_shape_map.at(input).empty() && input_shape_map.at(input) != input->shape()) { +#ifndef ENABLE_LITE_ACL MS_LOG(ERROR) << "graph input:" << input->tensor_name() << " shape has been illegally modified, please modify the input shape with method Resize()."; return RET_ERROR; +#else + MS_LOG(WARNING) << "Please check graph input " << input->tensor_name() + << " shape:" << ShapeToString(input->shape()) + << " has been modified by DVPP method to shape:" << ShapeToString(input_shape_map.at(input)) + << "." + << "If not, the modification is illegal, please modify the input shape with method Resize()."; +#endif } } return RET_OK; diff --git a/mindspore/lite/src/cxx_api/model/model_impl.cc b/mindspore/lite/src/cxx_api/model/model_impl.cc index 87e3034dcb0..c767c26b90c 100644 --- a/mindspore/lite/src/cxx_api/model/model_impl.cc +++ b/mindspore/lite/src/cxx_api/model/model_impl.cc @@ -302,8 +302,15 @@ Status ModelImpl::Predict(const std::vector &inputs, std::vectordata()) { if (input->Size() != user_input.DataSize()) { ResetTensorData(old_data, input_tensors); +#ifndef ENABLE_LITE_ACL MS_LOG(ERROR) << "Tensor " << user_input.Name() << " has wrong data size."; return kLiteInputTensorError; +#else + MS_LOG(WARNING) << "Please check tensor " << user_input.Name() + << " has been modified data size by DVPP method."; + std::vector truncate_shape = {static_cast(user_input.DataSize())}; + input->set_shape(truncate_shape); +#endif } input->set_data(user_input.MutableData()); } diff --git a/mindspore/lite/src/ms_tensor.cc b/mindspore/lite/src/ms_tensor.cc index 75e9fa27168..a09d88d7e04 100644 --- a/mindspore/lite/src/ms_tensor.cc +++ b/mindspore/lite/src/ms_tensor.cc @@ -104,7 +104,24 @@ tensor::MSTensor *tensor::MSTensor::CreateTensorByDeepCopy(const std::string &na tensor->set_data(const_cast(new_data)); } - tensor->set_shape(shape); + size_t shape_size = 1; + if (shape.empty()) { + shape_size = 0; + } else { + for (size_t i = 0; i < shape.size(); ++i) { + if (shape[i] < 0) { + delete tensor; + return nullptr; + } + shape_size *= static_cast(shape[i]); + } + } + if (data_len != shape_size * data_type_size) { + std::vector truncate_shape = {static_cast(data_len)}; + tensor->set_shape(truncate_shape); + } else { + tensor->set_shape(shape); + } tensor->set_tensor_name(name); tensor->set_data_type(type); return tensor; diff --git a/mindspore/lite/src/runtime/kernel/ascend/src/model_process.cc b/mindspore/lite/src/runtime/kernel/ascend/src/model_process.cc index 11a80f05548..6fab80e0891 100644 --- a/mindspore/lite/src/runtime/kernel/ascend/src/model_process.cc +++ b/mindspore/lite/src/runtime/kernel/ascend/src/model_process.cc @@ -15,6 +15,7 @@ */ #include "src/runtime/kernel/ascend/src/model_process.h" +#include #include #include #include @@ -426,9 +427,9 @@ STATUS ModelProcess::CheckTensorByTensorInfo(const std::vector &inp DestroyInputsDataset(); return ret; // forward status error } - aclError acl_ret = aclmdlExecute(model_id_, inputs_, outputs_); + + aclError acl_ret; + auto env = std::getenv("GLOG_v"); + if (env != nullptr && env[0] == '1') { + struct timeval start_time; + struct timeval end_time; + (void)gettimeofday(&start_time, nullptr); + acl_ret = aclmdlExecute(model_id_, inputs_, outputs_); + (void)gettimeofday(&end_time, nullptr); + constexpr uint64_t kUSecondInSecond = 1000000; + uint64_t cost = + (kUSecondInSecond * static_cast(end_time.tv_sec) + static_cast(end_time.tv_usec)) - + (kUSecondInSecond * static_cast(start_time.tv_sec) + static_cast(start_time.tv_usec)); + MS_LOG(INFO) << "Model execute in " << cost << " us"; + } else { + acl_ret = aclmdlExecute(model_id_, inputs_, outputs_); + } + DestroyInputsDataset(); if (acl_ret != ACL_ERROR_NONE) { MS_LOG(ERROR) << "Execute Model Failed, ret = " << acl_ret; diff --git a/mindspore/lite/tools/converter/converter.cc b/mindspore/lite/tools/converter/converter.cc index 519873c60fd..b8673eac2ff 100644 --- a/mindspore/lite/tools/converter/converter.cc +++ b/mindspore/lite/tools/converter/converter.cc @@ -119,6 +119,15 @@ schema::MetaGraphT *Converter::Convert(const std::unique_ptr & MS_LOG(ERROR) << "Parser/Import model return nullptr"; return nullptr; } + MS_CHECK_TRUE_MSG(funcgraph_transform_ != nullptr, nullptr, "funcgraph_transform init failed."); + // funcgraph_transform + graph = funcgraph_transform_->Transform(graph, flag.get()); + MS_CHECK_TRUE_MSG(graph != nullptr, nullptr, "Transform anf graph return nullptr."); + // export protobuf + auto status = MindIRSerialize(flag, graph); + if (status != RET_OK) { + MS_LOG(WARNING) << "Export to mindir proto return nullptr."; + } return TransferFuncGraph(flag, graph); }