diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 55f99d388e..1601fbcc27 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -48,6 +48,7 @@ set(LITE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/common/string_util.cc ${CMAKE_CURRENT_SOURCE_DIR}/common/prim_util.cc ${CMAKE_CURRENT_SOURCE_DIR}/common/tensor_util.cc + ${CMAKE_CURRENT_SOURCE_DIR}/common/loader_util.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/allocator.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/runtime_api.cc ${CMAKE_CURRENT_SOURCE_DIR}/runtime/thread_pool.c @@ -277,6 +278,10 @@ if(DEFINED ARCHS) target_link_libraries(mindspore_lite) endif() +if(NOT WIN32) + target_link_libraries(mindspore-lite dl) +endif() + if(SUPPORT_TRAIN) set_target_properties(mindspore-lite PROPERTIES OUTPUT_NAME "mindspore-lite-train") set_target_properties(mindspore-lite_static PROPERTIES OUTPUT_NAME "mindspore-lite-train") diff --git a/mindspore/lite/src/common/loader_util.cc b/mindspore/lite/src/common/loader_util.cc new file mode 100644 index 0000000000..07e7038c37 --- /dev/null +++ b/mindspore/lite/src/common/loader_util.cc @@ -0,0 +1,60 @@ +/** + * Copyright 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 "src/common/loader_util.h" +#include +#include +#include "include/errorcode.h" +#include "src/common/log_util.h" + +#ifndef _WIN32 + +namespace mindspore { +namespace lite { +int SoLoader::Open(const char *SoPath, int mode) { + if ((strlen(SoPath)) >= PATH_MAX) { + MS_LOG(ERROR) << "path is too long"; + return RET_ERROR; + } + char resolved_path[PATH_MAX]; + auto resolve_res = realpath(SoPath, resolved_path); + if (resolve_res == nullptr) { + MS_LOG(ERROR) << "PATH NOT EXITS"; + return RET_ERROR; + } + handler_ = dlopen(SoPath, mode); + if (handler_ == nullptr) { + MS_LOG(ERROR) << "open path failed"; + return RET_ERROR; + } + return RET_OK; +} + +void *SoLoader::GetFunc(const char *FuncName) { return dlsym(handler_, FuncName); } + +int SoLoader::Close() { + auto close_res = dlclose(handler_); + if (close_res != 0) { + MS_LOG(ERROR) << "can not close handler"; + return RET_ERROR; + } + return RET_OK; +} + +} // namespace lite +} // namespace mindspore + +#endif diff --git a/mindspore/lite/src/common/loader_util.h b/mindspore/lite/src/common/loader_util.h new file mode 100644 index 0000000000..c784f2b07f --- /dev/null +++ b/mindspore/lite/src/common/loader_util.h @@ -0,0 +1,40 @@ +/** + * Copyright 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. + */ + +#ifndef MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_ +#define MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_ + +#ifndef _WIN32 +#include + +namespace mindspore { +namespace lite { + +class SoLoader { + public: + int Open(const char *SoPath, int mode = RTLD_LAZY); + void *GetFunc(const char *FuncName); + int Close(); + + private: + void *handler_; +}; + +} // namespace lite +} // namespace mindspore + +#endif +#endif diff --git a/mindspore/lite/test/CMakeLists.txt b/mindspore/lite/test/CMakeLists.txt index bcf6485e50..b898e98502 100644 --- a/mindspore/lite/test/CMakeLists.txt +++ b/mindspore/lite/test/CMakeLists.txt @@ -155,6 +155,7 @@ set(TEST_LITE_SRC ${LITE_DIR}/src/common/tensor_util.cc ${LITE_DIR}/src/common/file_utils.cc ${LITE_DIR}/src/common/utils.cc + ${LITE_DIR}/src/common/loader_util.cc ${LITE_DIR}/src/common/string_util.cc ${LITE_DIR}/tools/common/flag_parser.cc ${LITE_DIR}/tools/benchmark/benchmark.cc @@ -316,6 +317,7 @@ set(TEST_SRC ${TEST_DIR}/common/common_test.cc ${TEST_DIR}/ut/src/infer_test.cc ${TEST_DIR}/ut/src/utils_test.cc + ${TEST_DIR}/ut/src/loader_util_test.cc ${TEST_DIR}/ut/src/scheduler_test.cc ) diff --git a/mindspore/lite/test/models_onnx_fp16.cfg b/mindspore/lite/test/models_onnx_fp16.cfg index e5f8e424c6..80209d2750 100644 --- a/mindspore/lite/test/models_onnx_fp16.cfg +++ b/mindspore/lite/test/models_onnx_fp16.cfg @@ -55,6 +55,7 @@ ml_video_edit_imitate_filter.onnx 103 ml_facedetector.onnx 3 ml_ei_facedetection.onnx 2 #ml_video_edit_art_generate.onnx #mul operator overflows, not suitable for fp16 +#ml_voice_detect.onnx #conv operator overflows, not suitable for fp16 ml_location_lane_counter.onnx 6 ml_location_lane_counter0.onnx 0.5 #The encoder an decoder model are used in ml_asr scene, both have value overflow. Not suitable for fp16. diff --git a/mindspore/lite/test/ut/src/loader_util_test.cc b/mindspore/lite/test/ut/src/loader_util_test.cc new file mode 100644 index 0000000000..13c3d95531 --- /dev/null +++ b/mindspore/lite/test/ut/src/loader_util_test.cc @@ -0,0 +1,45 @@ +/** + * Copyright 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 "mindspore/lite/src/common/loader_util.h" +#include "common/common_test.h" + +namespace mindspore { +class LoaderUtilTest : public mindspore::CommonTest { + public: + LoaderUtilTest() {} +}; + +/* + in file add.cc, the code is: + int add(int a, int b) {return a + b;} + use this command to generate so file: + gcc add.cc -fPIC -shared -o libadd.so + use this command to see the symbol table: + nm -D libadd.so +*/ +TEST_F(LoaderUtilTest, TestAdd) { +#ifndef _WIN32 + lite::SoLoader loader; + loader.Open("./libadd.so"); + int (*add)(int a, int b); + add = (int (*)(int, int))loader.GetFunc("_Z3addii"); + int res = add(7, 8); + loader.Close(); + ASSERT_EQ(15, res); +#endif +} +} // namespace mindspore