From 43fab0ee0cd261f3d2e93867e6d994aa2226e96d Mon Sep 17 00:00:00 2001 From: zhujingxuan Date: Wed, 17 Mar 2021 11:13:05 +0800 Subject: [PATCH] format cli && add support for relative path --- mindspore/lite/micro/CMakeLists.txt | 5 +++ mindspore/lite/micro/cmake/file_list.cmake | 43 ------------------- mindspore/lite/micro/coder/CMakeLists.txt | 1 - mindspore/lite/micro/coder/coder.cc | 6 +-- .../component/const_blocks/cmake_lists.cc | 19 +++++++- mindspore/lite/micro/coder/session.cc | 5 ++- 6 files changed, 30 insertions(+), 49 deletions(-) diff --git a/mindspore/lite/micro/CMakeLists.txt b/mindspore/lite/micro/CMakeLists.txt index 27be697802c..785d49d6eb1 100644 --- a/mindspore/lite/micro/CMakeLists.txt +++ b/mindspore/lite/micro/CMakeLists.txt @@ -45,6 +45,11 @@ else() -fstack-protector-all -Wno-attributes -Wno-deprecated-declarations \ -Wno-missing-braces -Wno-overloaded-virtual ${CMAKE_CXX_FLAGS}") endif() + +if(NOT WIN32) + set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,relro,-z,now -Wl,-z,noexecstack -fPIC -pie ${CMAKE_EXE_LINKER_FLAGS}") +endif() + add_subdirectory(coder) if(BUILD_TESTCASES AND NOT PLATFORM_ARM64 AND NOT PLATFORM_ARM32) add_subdirectory(test) diff --git a/mindspore/lite/micro/cmake/file_list.cmake b/mindspore/lite/micro/cmake/file_list.cmake index 8a40bd8ed18..cbbb7ef4336 100644 --- a/mindspore/lite/micro/cmake/file_list.cmake +++ b/mindspore/lite/micro/cmake/file_list.cmake @@ -201,10 +201,8 @@ set(LITE_KERNEL_SRC ${LITE_DIR}/nnacl/infer/arithmetic_compare_infer.c ${LITE_DIR}/nnacl/infer/arithmetic_grad_infer.c ${LITE_DIR}/nnacl/infer/arithmetic_infer.c - ${LITE_DIR}/nnacl/infer/assert_op_infer.c ${LITE_DIR}/nnacl/infer/assign_add_infer.c ${LITE_DIR}/nnacl/infer/assign_infer.c - ${LITE_DIR}/nnacl/infer/audio_spectrogram_infer.c ${LITE_DIR}/nnacl/infer/batch_to_space_infer.c ${LITE_DIR}/nnacl/infer/bias_grad_infer.c ${LITE_DIR}/nnacl/infer/binary_cross_entropy_infer.c @@ -217,67 +215,36 @@ set(LITE_KERNEL_SRC ${LITE_DIR}/nnacl/infer/conv2d_grad_filter_infer.c ${LITE_DIR}/nnacl/infer/conv2d_grad_input_infer.c ${LITE_DIR}/nnacl/infer/conv2d_infer.c - ${LITE_DIR}/nnacl/infer/crop_and_resize_infer.c - ${LITE_DIR}/nnacl/infer/crop_infer.c - ${LITE_DIR}/nnacl/infer/custom_extract_features_infer.c - ${LITE_DIR}/nnacl/infer/custom_normalize_infer.c - ${LITE_DIR}/nnacl/infer/custom_predict_infer.c ${LITE_DIR}/nnacl/infer/deconv2d_infer.c ${LITE_DIR}/nnacl/infer/dedepthwise_conv2d_infer.c - ${LITE_DIR}/nnacl/infer/depth_to_space_infer.c ${LITE_DIR}/nnacl/infer/depthwise_conv2d_infer.c ${LITE_DIR}/nnacl/infer/detection_post_process_infer.c - ${LITE_DIR}/nnacl/infer/dropout_grad_infer.c - ${LITE_DIR}/nnacl/infer/dropout_infer.c - ${LITE_DIR}/nnacl/infer/embedding_lookup_infer.c ${LITE_DIR}/nnacl/infer/expand_dims_infer.c - ${LITE_DIR}/nnacl/infer/fft_imag_infer.c - ${LITE_DIR}/nnacl/infer/fft_real_infer.c ${LITE_DIR}/nnacl/infer/fill_infer.c - ${LITE_DIR}/nnacl/infer/flatten_grad_infer.c - ${LITE_DIR}/nnacl/infer/flatten_infer.c ${LITE_DIR}/nnacl/infer/full_connection_infer.c ${LITE_DIR}/nnacl/infer/fused_batchnorm_infer.c ${LITE_DIR}/nnacl/infer/gather_infer.c ${LITE_DIR}/nnacl/infer/gather_nd_infer.c ${LITE_DIR}/nnacl/infer/group_conv2d_grad_input_infer.c - ${LITE_DIR}/nnacl/infer/gru_infer.c - ${LITE_DIR}/nnacl/infer/hashtable_lookup_infer.c ${LITE_DIR}/nnacl/infer/infer_register.c - ${LITE_DIR}/nnacl/infer/invert_permutation_infer.c - ${LITE_DIR}/nnacl/infer/layer_norm_infer.c - ${LITE_DIR}/nnacl/infer/layer_norm_grad_infer.c - ${LITE_DIR}/nnacl/infer/lin_space_infer.c ${LITE_DIR}/nnacl/infer/lsh_projection_infer.c ${LITE_DIR}/nnacl/infer/lstm_infer.c ${LITE_DIR}/nnacl/infer/matmul_infer.c ${LITE_DIR}/nnacl/infer/maximum_grad_infer.c ${LITE_DIR}/nnacl/infer/mean_infer.c - ${LITE_DIR}/nnacl/infer/merge_infer.c - ${LITE_DIR}/nnacl/infer/mfcc_infer.c - ${LITE_DIR}/nnacl/infer/non_max_suppression_infer.c - ${LITE_DIR}/nnacl/infer/one_hot_infer.c - ${LITE_DIR}/nnacl/infer/pad_infer.c - ${LITE_DIR}/nnacl/infer/partial_infer.c ${LITE_DIR}/nnacl/infer/pooling_grad_infer.c ${LITE_DIR}/nnacl/infer/pooling_infer.c ${LITE_DIR}/nnacl/infer/power_infer.c - ${LITE_DIR}/nnacl/infer/prior_box_infer.c ${LITE_DIR}/nnacl/infer/quant_dtype_cast_infer.c - ${LITE_DIR}/nnacl/infer/random_standard_normal_infer.c ${LITE_DIR}/nnacl/infer/range_infer.c ${LITE_DIR}/nnacl/infer/rank_infer.c ${LITE_DIR}/nnacl/infer/reduce_infer.c ${LITE_DIR}/nnacl/infer/reshape_infer.c ${LITE_DIR}/nnacl/infer/resize_infer.c - ${LITE_DIR}/nnacl/infer/rfft_infer.c ${LITE_DIR}/nnacl/infer/roi_pooling_infer.c - ${LITE_DIR}/nnacl/infer/scatter_nd_infer.c ${LITE_DIR}/nnacl/infer/select_infer.c ${LITE_DIR}/nnacl/infer/sgd_infer.c ${LITE_DIR}/nnacl/infer/shape_infer.c - ${LITE_DIR}/nnacl/infer/size_infer.c - ${LITE_DIR}/nnacl/infer/skip_gram_infer.c ${LITE_DIR}/nnacl/infer/slice_infer.c ${LITE_DIR}/nnacl/infer/softmax_cross_entropy_infer.c ${LITE_DIR}/nnacl/infer/softmax_infer.c @@ -288,23 +255,13 @@ set(LITE_KERNEL_SRC ${LITE_DIR}/nnacl/infer/sparse_to_dense_infer.c ${LITE_DIR}/nnacl/infer/split_infer.c ${LITE_DIR}/nnacl/infer/squeeze_infer.c - ${LITE_DIR}/nnacl/infer/stack_infer.c ${LITE_DIR}/nnacl/infer/strided_slice_grad_infer.c ${LITE_DIR}/nnacl/infer/strided_slice_infer.c - ${LITE_DIR}/nnacl/infer/switch_infer.c - ${LITE_DIR}/nnacl/infer/tensorlist_fromtensor_infer.c - ${LITE_DIR}/nnacl/infer/tensorlist_getitem_infer.c - ${LITE_DIR}/nnacl/infer/tensorlist_reserve_infer.c - ${LITE_DIR}/nnacl/infer/tensorlist_setitem_infer.c - ${LITE_DIR}/nnacl/infer/tensorlist_stack_infer.c ${LITE_DIR}/nnacl/infer/tile_infer.c ${LITE_DIR}/nnacl/infer/topk_infer.c ${LITE_DIR}/nnacl/infer/transpose_infer.c - ${LITE_DIR}/nnacl/infer/uniform_real_infer.c - ${LITE_DIR}/nnacl/infer/unique_infer.c ${LITE_DIR}/nnacl/infer/unsorted_segment_sum_infer.c ${LITE_DIR}/nnacl/infer/unsqueeze_infer.c - ${LITE_DIR}/nnacl/infer/unstack_infer.c ${LITE_DIR}/nnacl/infer/where_infer.c ${LITE_DIR}/nnacl/infer/while_infer.c ${LITE_DIR}/nnacl/infer/splice_infer.c diff --git a/mindspore/lite/micro/coder/CMakeLists.txt b/mindspore/lite/micro/coder/CMakeLists.txt index 9941db38034..efa732d194e 100644 --- a/mindspore/lite/micro/coder/CMakeLists.txt +++ b/mindspore/lite/micro/coder/CMakeLists.txt @@ -2,7 +2,6 @@ if(NOT PLATFORM_ARM64 AND NOT PLATFORM_ARM32) add_definitions(-DUSE_GLOG) endif() -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections ") set(3RD_DIR ${TOP_DIR}/third_party) set(LITE_DIR ${TOP_DIR}/mindspore/lite) set(MICRO_DIR ${LITE_DIR}/micro) diff --git a/mindspore/lite/micro/coder/coder.cc b/mindspore/lite/micro/coder/coder.cc index 81a5ce6c7dc..f3c7c70ca5d 100644 --- a/mindspore/lite/micro/coder/coder.cc +++ b/mindspore/lite/micro/coder/coder.cc @@ -121,7 +121,7 @@ int Coder::Init(const CoderFlags &flags) const { parsers.emplace_back([&flags, config]() -> bool { if (!FileExists(flags.model_path_)) { - MS_LOG(ERROR) << "code_gen model_path " << flags.model_path_ << " is not valid"; + MS_LOG(ERROR) << "model_path \"" << flags.model_path_ << "\" is not valid"; return false; } if (flags.code_module_name_.empty() || isdigit(flags.code_module_name_.at(0))) { @@ -194,12 +194,12 @@ int RunCoder(int argc, const char **argv) { Coder code_gen; STATUS status = code_gen.Init(flags); if (status != RET_OK) { - MS_LOG(ERROR) << "Coder init Error : " << status; + MS_LOG(ERROR) << "Coder init Error"; return status; } status = code_gen.Run(flags.model_path_); if (status != RET_OK) { - MS_LOG(ERROR) << "Run Coder Error : " << status; + MS_LOG(ERROR) << "Coder Run Error."; return status; } MS_LOG(INFO) << "end of Coder"; diff --git a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc index 595d186f329..ed3c140b63a 100644 --- a/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc +++ b/mindspore/lite/micro/coder/generator/component/const_blocks/cmake_lists.cc @@ -22,7 +22,11 @@ const char *bench_cmake_lists_txt = R"RAW( cmake_minimum_required(VERSION 3.14) project(benchmark) -message("project name: ${MODEL_LIB_PATH}") +if(NOT DEFINED MODEL_LIB) + message(FATAL_ERROR "MODEL_LIB not set") +endif() + +get_filename_component(MODEL_LIB ${MODEL_LIB} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}) function(parse_lib_info lib_full_path lib_name lib_path) string(FIND "${lib_full_path}" "/" POS REVERSE) @@ -35,6 +39,8 @@ endfunction(parse_lib_info) parse_lib_info(${MODEL_LIB} MODEL_LIB_NAME MODEL_LIB_PATH) +message("project name: ${MODEL_LIB_NAME}") + option(MICRO_BUILD_ARM64 "build android arm64" OFF) option(MICRO_BUILD_ARM32A "build android arm32" OFF) @@ -74,6 +80,17 @@ const char *src_cmake_lists_txt = R"RAW( cmake_minimum_required(VERSION 3.14) project(net) +if(NOT DEFINED OP_LIB) + message(FATAL_ERROR "OP_LIB not set") +endif() + +if(NOT DEFINED OP_HEADER_PATH) + message(FATAL_ERROR "OP_HEADER_PATH not set") +endif() + +get_filename_component(OP_LIB ${OP_LIB} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}) +get_filename_component(OP_HEADER_PATH ${OP_HEADER_PATH} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}) + message("operator lib path: ${OP_LIB}") message("operator header path: ${OP_HEADER_PATH}") diff --git a/mindspore/lite/micro/coder/session.cc b/mindspore/lite/micro/coder/session.cc index f4fdc5084e8..398dba95474 100644 --- a/mindspore/lite/micro/coder/session.cc +++ b/mindspore/lite/micro/coder/session.cc @@ -173,7 +173,10 @@ int CoderSession::Init(const std::string &model_path) { MS_LOG(DEBUG) << "start reading model file"; size_t size = 0; char *graph_buf = ReadFile(model_path.c_str(), &size); - MS_CHECK_PTR(graph_buf); + if (graph_buf == nullptr) { + MS_LOG(ERROR) << "read model file from path \"" << model_path << "\" failed."; + return RET_ERROR; + } // new a context for session if (size >= UINT_MAX) { MS_LOG(ERROR) << "the size is invalid";