[MSLITE] enable runtime convert

This commit is contained in:
ling 2021-10-19 20:54:17 +08:00
parent f62f494d75
commit ca8b2c0bf2
12 changed files with 159 additions and 20 deletions

View File

@ -473,7 +473,22 @@ else()
if(MSLITE_ENABLE_RUNTIME_GLOG)
install(DIRECTORY ${glog_LIBPATH}/../include/glog/ DESTINATION ${RUNTIME_INC_DIR}/third_party/glog
COMPONENT ${RUNTIME_COMPONENT_NAME} FILES_MATCHING PATTERN "*.h")
install(FILES ${glog_LIBPATH}/libglog.so.0.4.0 DESTINATION ${GLOG_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME})
install(FILES ${glog_LIBPATH}/libglog.so.0.4.0 DESTINATION ${GLOG_DIR} RENAME libglog.so.0
COMPONENT ${RUNTIME_COMPONENT_NAME})
endif()
if(MSLITE_ENABLE_RUNTIME_CONVERT)
install(FILES ${TOP_DIR}/mindspore/lite/build/tools/converter/registry/libmslite_converter_plugin.so
DESTINATION ${RUNTIME_LIB_DIR} COMPONENT ${RUNTIME_COMPONENT_NAME})
install(FILES ${opencv_LIBPATH}/libopencv_core.so.4.5.1
DESTINATION ${RUNTIME_LIB_DIR} RENAME libopencv_core.so.4.5
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(FILES ${opencv_LIBPATH}/libopencv_imgcodecs.so.4.5.1
DESTINATION ${RUNTIME_LIB_DIR} RENAME libopencv_imgcodecs.so.4.5
COMPONENT ${RUNTIME_COMPONENT_NAME})
install(FILES ${opencv_LIBPATH}/libopencv_imgproc.so.4.5.1
DESTINATION ${RUNTIME_LIB_DIR} RENAME libopencv_imgproc.so.4.5
COMPONENT ${RUNTIME_COMPONENT_NAME})
endif()
__install_micro_wrapper()
__install_micro_codegen()

View File

@ -174,10 +174,13 @@ else()
set(MSLITE_ENABLE_NPU off)
endif()
if(MSLITE_ENABLE_SSE OR MSLITE_ENABLE_AVX OR WIN32)
set(MSLITE_ENABLE_RUNTIME_CONVERT off)
endif()
if(MSLITE_ENABLE_RUNTIME_CONVERT)
set(MSLITE_ENABLE_RUNTIME_GLOG on)
set(MSLITE_ENABLE_CONVERTER on)
set(MSLITE_ENABLE_V0 off)
endif()
if(MSLITE_ENABLE_TRAIN)
@ -433,12 +436,15 @@ if(ENABLE_MODEL_OBF)
set(OBF_LIBS libmsdeobfuscator-lite.so)
endif()
if(MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_RUNTIME_GLOG)
include(${TOP_DIR}/cmake/external_libs/glog.cmake)
endif()
if(MSLITE_ENABLE_CONVERTER)
include(${TOP_DIR}/cmake/external_libs/opencv.cmake)
include_directories(${PYTHON_INCLUDE_DIRS})
include(${TOP_DIR}/cmake/external_libs/eigen.cmake)
include(${TOP_DIR}/cmake/external_libs/protobuf.cmake)
include(${TOP_DIR}/cmake/external_libs/glog.cmake)
if(MSLITE_ENABLE_MODEL_ENCRYPTION)
include(${TOP_DIR}/cmake/external_libs/openssl.cmake)
endif()

View File

@ -110,8 +110,6 @@ set(LITE_SRC
${CMAKE_CURRENT_SOURCE_DIR}/lite_session.cc
${CMAKE_CURRENT_SOURCE_DIR}/errorcode.cc
${CMAKE_CURRENT_SOURCE_DIR}/cpu_info.cc
${CMAKE_CURRENT_SOURCE_DIR}/ops/ops_def.cc
${CMAKE_CURRENT_SOURCE_DIR}/ops/ops_utils.cc
)
if(MSLITE_ENABLE_RUNTIME_GLOG)
@ -119,12 +117,20 @@ if(MSLITE_ENABLE_RUNTIME_GLOG)
add_definitions(-DUSE_GLOG)
string(REPLACE "-fno-rtti" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
string(REPLACE "-fno-rtti" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
if(NOT MSLITE_ENABLE_RUNTIME_CONVERT)
set(LITE_SRC ${LITE_SRC}
${CORE_DIR}/utils/log_adapter.cc
${CORE_DIR}/gvar/logging_level.cc
${CORE_DIR}/gvar/log_adapter_common.cc)
endif()
endif()
if(MSLITE_ENABLE_RUNTIME_CONVERT)
add_definitions(-DRUNTIME_CONVERT)
file(GLOB RUNTIME_CONVERT_SRC
${CMAKE_CURRENT_SOURCE_DIR}/ops/ops_def.cc
${CMAKE_CURRENT_SOURCE_DIR}/ops/ops_utils.cc
${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_convert.cc)
set(LITE_SRC ${LITE_SRC} ${RUNTIME_CONVERT_SRC})
@ -373,24 +379,24 @@ if(PLATFORM_ARM)
endif()
endif()
if(MSLITE_ENABLE_RUNTIME_GLOG)
target_link_libraries(mindspore-lite mindspore::glog mindspore_core)
target_link_libraries(mindspore-lite_static mindspore::glog mindspore_core)
endif()
if(MSLITE_ENABLE_RUNTIME_CONVERT)
target_link_libraries(mindspore-lite
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
config_parser_mid mslite_converter_plugin mindspore_core
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
config_parser_mid mslite_converter_plugin mindspore_core
mindspore::protobuf ${SECUREC_LIBRARY})
endif()
if(MSLITE_ENABLE_RUNTIME_GLOG)
target_link_libraries(mindspore-lite mindspore::glog)
target_link_libraries(mindspore-lite_static mindspore::glog)
endif()
if(DEFINED ARCHS)
set_target_properties(mindspore-lite_static PROPERTIES FRAMEWORK TRUE)
set_target_properties(mindspore-lite_static PROPERTIES

View File

@ -1325,7 +1325,7 @@ char *lite::LiteSession::LoadModelByPath(const char *file, size_t *size) {
#ifdef RUNTIME_CONVERT
return RuntimeConvert(file, size);
#endif
MS_LOG(ERROR) << "Enable runtime convert.";
MS_LOG(ERROR) << "Please enable runtime convert.";
return nullptr;
}

View File

@ -16,8 +16,77 @@
#ifdef RUNTIME_CONVERT
#include "src/runtime/runtime_convert.h"
#include "tools/common/graph_util.h"
#include "tools/converter/converter_flags.h"
#include "tools/converter/anf_transform.h"
#include "tools/anf_exporter/anf_exporter.h"
#include "tools/converter/graphdef_transform.h"
#include "tools/converter/import/mindspore_importer.h"
namespace mindspore::lite {
char *RuntimeConvert(const char *file_path, size_t *size) { return nullptr; }
char *RuntimeConvert(const char *file_path, size_t *size) {
void *model_buf = nullptr;
converter::Flags flag;
flag.fmk = converter::kFmkTypeMs;
flag.modelFile = file_path;
flag.inputDataType = kTypeUnknown;
flag.outputDataType = kTypeUnknown;
flag.saveFP16 = false;
flag.trainModel = false;
MindsporeImporter ms_import;
FuncGraphPtr func_graph = ms_import.ImportMindIR(flag);
if (func_graph == nullptr) {
return nullptr;
}
if (UpdateFuncGraphInputsAndOutputsDtype(func_graph) != RET_OK) {
MS_LOG(ERROR) << "Update graph inputs and outputs dtype failed.";
return nullptr;
}
// funcgraph compile
AnfTransform funcgraph_transform;
func_graph = funcgraph_transform.Transform(func_graph, &flag);
if (func_graph == nullptr) {
MS_LOG(ERROR) << "Transform anf graph return nullptr";
return nullptr;
}
// protobuf -> flatbuffer
auto meta_graph = Export(func_graph, false, false, false);
if (meta_graph == nullptr) {
MS_LOG(ERROR) << "Export to meta graph return nullptr";
return nullptr;
}
// metagraph compile
GraphDefTransform metagraph_transform;
metagraph_transform.SetGraphDef(meta_graph);
auto status = metagraph_transform.Transform(flag);
if (status != RET_OK) {
MS_LOG(ERROR) << "Transform meta graph failed " << status;
delete meta_graph;
return nullptr;
}
status = UpdateGraphOutputName(meta_graph);
if (status != RET_OK) {
MS_LOG(ERROR) << "UpdateGraphOutputName failed.";
delete meta_graph;
return nullptr;
}
meta_graph->version = Version();
status = TransferMetaGraph(*meta_graph, &model_buf, size);
if (status != RET_OK) {
MS_LOG(ERROR) << "Transfer model failed.";
delete meta_graph;
return nullptr;
}
delete meta_graph;
return reinterpret_cast<char *>(model_buf);
}
} // namespace mindspore::lite
#endif

View File

@ -21,7 +21,6 @@ file(GLOB_RECURSE TEST_UT_SRC
${TEST_DIR}/ut/src/registry/registry_test.cc
${TEST_DIR}/ut/src/registry/registry_custom_op_test.cc
${TEST_DIR}/ut/src/runtime/runtime_pass_tests.cc
${TEST_DIR}/ut/src/runtime/runtime_convert_tests.cc
${TEST_DIR}/st/multiple_device_test.cc
${TEST_DIR}/st/mindrt_parallel_runtime_test.cc
${TEST_DIR}/st/mix_data_type_test.cc
@ -32,6 +31,10 @@ file(GLOB_RECURSE TEST_UT_SRC
${TEST_DIR}/ut/src/runtime/kernel/arm/string/*.cc
)
if(MSLITE_ENABLE_RUNTIME_CONVERT)
list(APPEND TEST_UT_SRC ${TEST_DIR}/ut/src/runtime/runtime_convert_tests.cc)
endif()
if(MSLITE_ENABLE_TRAIN)
file(GLOB_RECURSE TEST_TRAIN_UT_SRC
${TEST_DIR}/ut/src/runtime/kernel/arm/fp32_grad/*.cc

View File

@ -100,8 +100,8 @@ echo 'run custom delegate st test'
echo 'runtime pass'
./lite-test --gtest_filter="RuntimePass.*"
#echo 'runtime convert'
#./lite-test --gtest_filter="RuntimeConvert.*"
echo 'runtime convert'
./lite-test --gtest_filter="RuntimeConvert.*"
echo 'Optimize Allocator'
./lite-test --gtest_filter="OptAllocator.*"

View File

@ -24,7 +24,7 @@ function Run_Converter() {
function Run_x86_asan() {
echo 'cd '${x86_path}'/mindspore-lite-'${version}'-linux-x64' >> "${run_x86_asan_log_file}"
cd ${x86_path}/mindspore-lite-${version}-linux-x64 || return 1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./runtime/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./runtime/lib:./runtime/third_party/glog
cp tools/benchmark/benchmark ./ || exit 1
# Prepare the config file list
local asan_cfg_file_list=("$models_asan_config")

View File

@ -67,7 +67,7 @@ function Run_x86() {
# $1:framework;
echo 'cd '${x86_path}'/mindspore-lite-'${version}'-linux-x64' >> "${run_x86_log_file}"
cd ${x86_path}/mindspore-lite-${version}-linux-x64 || exit 1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./runtime/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./runtime/lib:./runtime/third_party/glog
cp tools/benchmark/benchmark ./ || exit 1
# Run converted models:
# $1:cfgFileList; $2:modelPath; $3:dataPath; $4:logFile; $5:resultFile; $6:platform; $7:processor; $8:phoneId;
@ -144,7 +144,7 @@ function Run_x86_parallel_split() {
rm -rf parallel_split
mkdir parallel_split
cd parallel_split || exit 1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../runtime/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../runtime/lib::../runtime/third_party/glog
cp ../tools/benchmark/benchmark ./ || exit 1
# Run tflite parallel split converted models:

View File

@ -39,6 +39,9 @@ int RunBenchmark(int argc, const char **argv) {
if (flags.config_file_ != "") {
run_old_api = false;
}
if (IsCharEndWith(flags.model_file_.c_str(), MINDIR_POSTFIX)) {
run_old_api = false;
}
if (run_old_api) {
benchmark = new Benchmark(&flags);
} else {

View File

@ -476,5 +476,39 @@ STATUS UpdateGraphOutputName(schema::MetaGraphT *meta_graph) {
}
return RET_OK;
}
int TransferMetaGraph(const schema::MetaGraphT &graph, void **model_buf, size_t *size) {
if (model_buf == nullptr) {
MS_LOG(ERROR) << "input model_buf invalid";
return RET_ERROR;
}
if (size == nullptr) {
MS_LOG(ERROR) << "input size invalid";
return RET_ERROR;
}
/* model_buf malloc here, free outside */
if (*model_buf != nullptr) {
MS_LOG(ERROR) << "input model_buf must be nullptr";
return RET_ERROR;
}
flatbuffers::FlatBufferBuilder builder(MAX_GRAPH_SIZE);
auto offset = schema::MetaGraph::Pack(builder, &graph);
builder.Finish(offset);
schema::FinishMetaGraphBuffer(builder, offset);
*size = builder.GetSize();
auto content = builder.GetBufferPointer();
if (content == nullptr) {
MS_LOG(ERROR) << "GetBufferPointer nullptr";
return RET_ERROR;
}
*model_buf = malloc(*size);
if (*model_buf == nullptr) {
MS_LOG(ERROR) << "malloc model_buf failed";
return RET_ERROR;
}
(void)memcpy(*model_buf, content, *size);
return RET_OK;
}
} // namespace lite
} // namespace mindspore

View File

@ -38,6 +38,7 @@
namespace mindspore {
namespace lite {
#define MAX_GRAPH_SIZE 1024
using STATUS = int;
enum InsertPlace { kBefore, kAfter };
@ -89,6 +90,8 @@ STATUS UpdateFuncGraphInputsAndOutputsDtype(const FuncGraphPtr &func_graph);
STATUS UpdateGraphOutputName(schema::MetaGraphT *meta_graph);
int TransferMetaGraph(const schema::MetaGraphT &graph, void **model_buf, size_t *size);
template <typename T>
bool IndexingCompress(const std::set<T> &quant_data_set, const std::map<T, size_t> &unique_value_index_map,
size_t unique_value_bit, size_t unique_value_cnt, size_t pack_repetition_size_in_byte,