From 6feb35d50dd107aeb225d4894ee97c4e02f9a7a6 Mon Sep 17 00:00:00 2001 From: sunsuodong Date: Sat, 7 May 2022 09:35:52 -0700 Subject: [PATCH] optimizing python package build and release --- mindspore/lite/CMakeLists.txt | 2 +- mindspore/lite/build_lite.sh | 9 +++++++-- mindspore/lite/python/CMakeLists.txt | 30 ++++++++++++++++------------ mindspore/lite/python/api/tensor.py | 1 + scripts/lite_release_package.sh | 10 +++++----- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index 8d47a00289f..e0faa2ed11b 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -774,7 +774,7 @@ if(NOT APPLE AND NOT ENABLE_CLOUD_AND_LITE) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/file_list.cmake) include(${TOP_DIR}/cmake/package_lite.cmake) - if(PLATFORM_X86_64) + if(PLATFORM_X86_64 OR MACHINE_LINUX_ARM64) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/python) endif() endif() diff --git a/mindspore/lite/build_lite.sh b/mindspore/lite/build_lite.sh index 89f5fe532d3..0df095ceaa2 100755 --- a/mindspore/lite/build_lite.sh +++ b/mindspore/lite/build_lite.sh @@ -241,9 +241,13 @@ build_lite_aarch64_jni_and_jar() { } build_python_wheel_package() { - local python_version=`python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1}'` + local python_version=`python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1}'` if [[ "${python_version}" == "3" ]]; then cd ${BASEPATH}/mindspore/lite/build/ + local lite_wrapper_so=`ls python/*.so` + if [ ! -f "${lite_wrapper_so}" ]; then + return 0 + fi mkdir -pv package/mindspore_lite/lib/ cp ../python/api/* package/mindspore_lite/ cp src/*.so package/mindspore_lite/lib/ @@ -264,7 +268,7 @@ build_python_wheel_package() { export TOP_DIR=${BASEPATH} cd package python setup.py bdist_wheel - local minor_version=`python3 -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $2}'` + local minor_version=`python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $2}'` local py_tags="cp${python_version}${minor_version}-cp${python_version}${minor_version}" if [[ "${minor_version}" == "7" ]]; then py_tags="cp37-cp37m" @@ -448,6 +452,7 @@ build_lite() { echo -e "\e[31mIf you want to compile the JAR package, please set $JAVA_HOME. For example: export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 \e[0m" fi elif [[ "${local_lite_platform}" == "arm64" ]] && [[ "${machine}" == "aarch64" ]]; then + build_python_wheel_package "aarch64" if [ "${JAVA_HOME}" ]; then echo -e "\e[31mJAVA_HOME=$JAVA_HOME \e[0m" build_lite_aarch64_jni_and_jar "${CMAKE_ARGS}" diff --git a/mindspore/lite/python/CMakeLists.txt b/mindspore/lite/python/CMakeLists.txt index 58c9885ff33..c31652a65f8 100644 --- a/mindspore/lite/python/CMakeLists.txt +++ b/mindspore/lite/python/CMakeLists.txt @@ -10,22 +10,26 @@ find_package(Python3 COMPONENTS Interpreter Development) if(Python3_FOUND) find_package(Python3 COMPONENTS NumPy Development) - include_directories(${Python3_INCLUDE_DIRS}) - include_directories(${Python3_NumPy_INCLUDE_DIRS}) - include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../) - include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../core/) + if(Python3_NumPy_FOUND) + include_directories(${Python3_INCLUDE_DIRS}) + include_directories(${Python3_NumPy_INCLUDE_DIRS}) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../) + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../core/) - if(NOT ENABLE_CLOUD_AND_LITE) - include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/utils.cmake) - include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/external_libs/pybind11.cmake) - endif() + if(NOT ENABLE_CLOUD_AND_LITE) + include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/utils.cmake) + include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/external_libs/pybind11.cmake) + endif() - file(GLOB_RECURSE PY_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc") - pybind11_add_module(_c_lite_wrapper NO_EXTRAS ${PY_SRC_LIST}) + file(GLOB_RECURSE PY_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc") + pybind11_add_module(_c_lite_wrapper NO_EXTRAS ${PY_SRC_LIST}) - target_link_libraries(_c_lite_wrapper PRIVATE -Wl,--whole-archive mindspore-lite_static -Wl,--no-whole-archive) - if(MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_RUNTIME_GLOG) - target_link_libraries(_c_lite_wrapper PRIVATE mindspore::glog) + target_link_libraries(_c_lite_wrapper PRIVATE -Wl,--whole-archive mindspore-lite_static -Wl,--no-whole-archive) + if(MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_RUNTIME_GLOG) + target_link_libraries(_c_lite_wrapper PRIVATE mindspore::glog) + endif() + else() + message(WARNING "NumPy not found, so Python API will not be compiled.") endif() else() message(WARNING "Python3 not found, so Python API will not be compiled.") diff --git a/mindspore/lite/python/api/tensor.py b/mindspore/lite/python/api/tensor.py index ca33be4c8e9..7c64c930df6 100644 --- a/mindspore/lite/python/api/tensor.py +++ b/mindspore/lite/python/api/tensor.py @@ -348,6 +348,7 @@ class Tensor: if numpy_obj.nbytes != self.get_data_size(): raise RuntimeError( f"data size not equal! Numpy size: {numpy_obj.nbytes}, Tensor size: {self.get_data_size()}") + numpy_obj.flatten() self._tensor.set_data_from_numpy(numpy_obj) self._numpy_obj = numpy_obj # keep reference count of numpy objects diff --git a/scripts/lite_release_package.sh b/scripts/lite_release_package.sh index 2f508ca4cfa..8531df85c45 100644 --- a/scripts/lite_release_package.sh +++ b/scripts/lite_release_package.sh @@ -52,11 +52,11 @@ function linux_release_package() mkdir -p ${output_path}/release/linux/x86_64/server/ mkdir -p ${output_path}/release/linux/aarch64/server/ - cp ${input_path}/centos_x86/avx/*.tar.gz* ${output_path}/release/linux/x86_64/ - cp ${input_path}/linux_aarch64/*.tar.gz* ${output_path}/release/linux/aarch64/ - cp ${input_path}/centos_x86/ascend/*.tar.gz* ${output_path}/release/linux/x86_64/ascend/ - cp ${input_path}/linux_aarch64/ascend/*.tar.gz* ${output_path}/release/linux/aarch64/ascend/ - cp ${input_path}/centos_x86/tensorrt/*.tar.gz* ${output_path}/release/linux/x86_64/tensorrt/ + cp ${input_path}/centos_x86/avx/mindspore-lite-* ${output_path}/release/linux/x86_64/ + cp ${input_path}/linux_aarch64/mindspore-lite-* ${output_path}/release/linux/aarch64/ + cp ${input_path}/centos_x86/ascend/mindspore-lite-* ${output_path}/release/linux/x86_64/ascend/ + cp ${input_path}/linux_aarch64/ascend/mindspore-lite-* ${output_path}/release/linux/aarch64/ascend/ + cp ${input_path}/centos_x86/tensorrt/mindspore-lite-* ${output_path}/release/linux/x86_64/tensorrt/ cp -r ${input_path}/centos_x86/server/* ${output_path}/release/linux/x86_64/server/ cp -r ${input_path}/linux_aarch64/server/* ${output_path}/release/linux/aarch64/server/