| @@ -123,6 +123,7 @@ checkopts() | |||
| DEVICE="" | |||
| ENABLE_NPU="off" | |||
| ENABLE_HIDDEN="on" | |||
| LITE_ENABLE_GPU="" | |||
| # Process the options | |||
| while getopts 'drvj:c:t:hsb:a:g:p:ie:m:l:I:LRP:D:zM:V:K:B:En:T:A:C:o:S:k:W:H:' opt | |||
| do | |||
| @@ -553,15 +554,33 @@ build_lite() | |||
| { | |||
| get_version | |||
| echo "============ Start building MindSpore Lite ${VERSION_STR} ============" | |||
| local LOCAL_LITE_PLATFORM=${LITE_PLATFORM} | |||
| local LOCAL_INC_BUILD=${INC_BUILD} | |||
| local LOCAL_LITE_ENABLE_GPU=${LITE_ENABLE_GPU} | |||
| if [[ "${LITE_LANGUAGE}" == "java" ]]; then | |||
| if [[ "X$1" != "X" ]]; then | |||
| LOCAL_LITE_PLATFORM=$1 | |||
| else | |||
| LOCAL_LITE_PLATFORM="" | |||
| fi | |||
| if [[ "X$2" != "X" ]]; then | |||
| LOCAL_INC_BUILD=$2 | |||
| else | |||
| LOCAL_INC_BUILD="" | |||
| fi | |||
| if [[ "X$3" != "X" ]]; then | |||
| LOCAL_LITE_ENABLE_GPU=$3 | |||
| else | |||
| LOCAL_LITE_ENABLE_GPU="" | |||
| fi | |||
| fi | |||
| LITE_ENABLE_NPU=${ENABLE_NPU} | |||
| if [[ "${DEVICE}" == "" && "${LITE_PLATFORM}" == "arm64" ]]; then | |||
| LITE_ENABLE_GPU="opencl" | |||
| if [[ "${LITE_LANGUAGE}" == "cpp" && "${DEVICE}" == "" && "${LOCAL_LITE_PLATFORM}" == "arm64" ]]; then | |||
| LOCAL_LITE_ENABLE_GPU="opencl" | |||
| LITE_ENABLE_NPU="on" | |||
| fi | |||
| if [ "${LITE_ENABLE_NPU}" == "on" ]; then | |||
| if [ "${LITE_PLATFORM}" == "arm64" ]; then | |||
| if [ "${LOCAL_LITE_PLATFORM}" == "arm64" ]; then | |||
| checkddk | |||
| else | |||
| echo "NPU only support platform arm64." | |||
| @@ -570,7 +589,7 @@ build_lite() | |||
| fi | |||
| cd "${BASEPATH}/mindspore/lite" | |||
| if [[ "${INC_BUILD}" == "off" || $2 == "off" ]]; then | |||
| if [[ "${LOCAL_INC_BUILD}" == "off" ]]; then | |||
| rm -rf build | |||
| fi | |||
| mkdir -pv build | |||
| @@ -581,26 +600,26 @@ build_lite() | |||
| BUILD_TYPE="Debug" | |||
| fi | |||
| if [[ "${LITE_PLATFORM}" == "arm64" || $1 == "arm64" ]]; then | |||
| if [[ "${LOCAL_LITE_PLATFORM}" == "arm64" ]]; then | |||
| checkndk | |||
| cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" -DANDROID_NATIVE_API_LEVEL="19" \ | |||
| -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="arm64-v8a" -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" \ | |||
| -DANDROID_STL=${ANDROID_STL} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DSUPPORT_TRAIN=${SUPPORT_TRAIN} \ | |||
| -DPLATFORM_ARM64=on -DENABLE_NEON=on -DENABLE_FP16="on" \ | |||
| -DENABLE_TOOLS=${ENABLE_TOOLS} -DENABLE_CONVERTER=${ENABLE_CONVERTER} -DBUILD_TESTCASES=${RUN_TESTCASES} \ | |||
| -DSUPPORT_GPU=${LITE_ENABLE_GPU} -DSUPPORT_NPU=${LITE_ENABLE_NPU} -DENABLE_V0=on \ | |||
| -DSUPPORT_GPU=${LOCAL_LITE_ENABLE_GPU} -DSUPPORT_NPU=${LITE_ENABLE_NPU} -DENABLE_V0=on \ | |||
| -DOFFLINE_COMPILE=${OPENCL_OFFLINE_COMPILE} -DBUILD_MINDDATA=${COMPILE_MINDDATA_LITE} \ | |||
| -DCMAKE_INSTALL_PREFIX=${BASEPATH}/output/tmp -DMS_VERSION_MAJOR=${VERSION_MAJOR} \ | |||
| -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} -DENABLE_VERBOSE=${ENABLE_VERBOSE} \ | |||
| "${BASEPATH}/mindspore/lite" | |||
| elif [[ "${LITE_PLATFORM}" == "arm32" || $1 == "arm32" ]]; then | |||
| elif [[ "${LOCAL_LITE_PLATFORM}" == "arm32" ]]; then | |||
| checkndk | |||
| cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" -DANDROID_NATIVE_API_LEVEL="19" \ | |||
| -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="armeabi-v7a" -DANDROID_TOOLCHAIN_NAME="clang" \ | |||
| -DANDROID_STL=${ANDROID_STL} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ | |||
| -DPLATFORM_ARM32=on -DENABLE_NEON=on -DSUPPORT_TRAIN=${SUPPORT_TRAIN} \ | |||
| -DENABLE_TOOLS=${ENABLE_TOOLS} -DENABLE_CONVERTER=${ENABLE_CONVERTER} -DBUILD_TESTCASES=${RUN_TESTCASES} \ | |||
| -DSUPPORT_GPU=${LITE_ENABLE_GPU} -DSUPPORT_NPU=${ENABLE_NPU} -DENABLE_V0=on \ | |||
| -DSUPPORT_GPU=${LOCAL_LITE_ENABLE_GPU} -DSUPPORT_NPU=${ENABLE_NPU} -DENABLE_V0=on \ | |||
| -DOFFLINE_COMPILE=${OPENCL_OFFLINE_COMPILE} -DBUILD_MINDDATA=${COMPILE_MINDDATA_LITE} \ | |||
| -DCMAKE_INSTALL_PREFIX=${BASEPATH}/output/tmp -DMS_VERSION_MAJOR=${VERSION_MAJOR} \ | |||
| -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} -DENABLE_VERBOSE=${ENABLE_VERBOSE} \ | |||
| @@ -608,7 +627,7 @@ build_lite() | |||
| else | |||
| cmake -DPLATFORM_ARM64=off -DSUPPORT_TRAIN=${SUPPORT_TRAIN} \ | |||
| -DENABLE_TOOLS=${ENABLE_TOOLS} -DENABLE_CONVERTER=${ENABLE_CONVERTER} -DBUILD_TESTCASES=${RUN_TESTCASES} \ | |||
| -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DSUPPORT_GPU=${LITE_ENABLE_GPU} -DSUPPORT_NPU=${ENABLE_NPU} \ | |||
| -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DSUPPORT_GPU=${LOCAL_LITE_ENABLE_GPU} -DSUPPORT_NPU=${ENABLE_NPU} \ | |||
| -DBUILD_MINDDATA=${COMPILE_MINDDATA_LITE} -DENABLE_V0=on \ | |||
| -DOFFLINE_COMPILE=${OPENCL_OFFLINE_COMPILE} -DCMAKE_INSTALL_PREFIX=${BASEPATH}/output/tmp \ | |||
| -DMS_VERSION_MAJOR=${VERSION_MAJOR} -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} \ | |||
| @@ -631,12 +650,19 @@ build_lite() | |||
| build_lite_java_arm64() { | |||
| # build mindspore-lite arm64 | |||
| JTARBALL=mindspore-lite-${VERSION_STR}-inference-android-aarch64 | |||
| local JTARBALL=mindspore-lite-${VERSION_STR}-inference-android-aarch64 | |||
| if [[ "X$SUPPORT_TRAIN" = "Xon" ]]; then | |||
| JTARBALL=mindspore-lite-${VERSION_STR}-train-android-aarch64 | |||
| fi | |||
| if [[ "X$INC_BUILD" = "Xoff" ]] || [[ ! -f "${BASEPATH}/output/${JTARBALL}.tar.gz" ]]; then | |||
| build_lite "arm64" "off" | |||
| if [[ "X${DEVICE}" == "Xcpu" ]]; then | |||
| build_lite "arm64" "off" "" | |||
| elif [[ "X${DEVICE}" == "Xnpu" ]]; then | |||
| echo "NPU only support c++." | |||
| exit 1 | |||
| else | |||
| build_lite "arm64" "off" "opencl" | |||
| fi | |||
| fi | |||
| # copy arm64 so | |||
| cd ${BASEPATH}/output/ | |||
| @@ -645,17 +671,19 @@ build_lite_java_arm64() { | |||
| [ -n "${JAVA_PATH}" ] && rm -rf ${JAVA_PATH}/java/app/libs/arm64-v8a/ | |||
| mkdir -p ${JAVA_PATH}/java/app/libs/arm64-v8a/ | |||
| cp ${BASEPATH}/output/${JTARBALL}/lib/libmindspore-lite.so ${JAVA_PATH}/java/app/libs/arm64-v8a/ | |||
| mkdir -p ${JAVA_PATH}/native/libs/arm64-v8a/ | |||
| cp ${BASEPATH}/output/${JTARBALL}/lib/libmindspore-lite.so ${JAVA_PATH}/native/libs/arm64-v8a/ | |||
| [ -n "${VERSION_STR}" ] && rm -rf ${JTARBALL} | |||
| } | |||
| build_lite_java_arm32() { | |||
| # build mindspore-lite arm32 | |||
| JTARBALL=mindspore-lite-${VERSION_STR}-inference-android-aarch32 | |||
| local JTARBALL=mindspore-lite-${VERSION_STR}-inference-android-aarch32 | |||
| if [[ "X$SUPPORT_TRAIN" = "Xon" ]]; then | |||
| JTARBALL=mindspore-lite-${VERSION_STR}-train-android-aarch32 | |||
| fi | |||
| if [[ "X$INC_BUILD" = "Xoff" ]] || [[ ! -f "${BASEPATH}/output/${JTARBALL}.tar.gz" ]]; then | |||
| build_lite "arm32" "off" | |||
| build_lite "arm32" "off" "" | |||
| fi | |||
| # copy arm32 so | |||
| cd ${BASEPATH}/output/ | |||
| @@ -664,9 +692,28 @@ build_lite_java_arm32() { | |||
| [ -n "${JAVA_PATH}" ] && rm -rf ${JAVA_PATH}/java/app/libs/armeabi-v7a/ | |||
| mkdir -p ${JAVA_PATH}/java/app/libs/armeabi-v7a/ | |||
| cp ${BASEPATH}/output/${JTARBALL}/lib/libmindspore-lite.so ${JAVA_PATH}/java/app/libs/armeabi-v7a/ | |||
| mkdir -p ${JAVA_PATH}/native/libs/armeabi-v7a/ | |||
| cp ${BASEPATH}/output/${JTARBALL}/lib/libmindspore-lite.so ${JAVA_PATH}/native/libs/armeabi-v7a/ | |||
| [ -n "${VERSION_STR}" ] && rm -rf ${JTARBALL} | |||
| } | |||
| build_lite_java_x86() { | |||
| # build mindspore-lite x86 | |||
| local JTARBALL=mindspore-lite-${VERSION_STR}-inference-linux-x64 | |||
| if [[ "X$INC_BUILD" = "Xoff" ]] || [[ ! -f "${BASEPATH}/output/${JTARBALL}.tar.gz" ]]; then | |||
| build_lite "x86_64" "off" "" | |||
| fi | |||
| # copy x86 so | |||
| cd ${BASEPATH}/output/ | |||
| rm -rf ${JTARBALL} | |||
| tar -zxvf ${JTARBALL}.tar.gz | |||
| [ -n "${JAVA_PATH}" ] && rm -rf ${JAVA_PATH}/java/linux_x86/libs/ | |||
| mkdir -p ${JAVA_PATH}/java/linux_x86/libs/ | |||
| cp ${BASEPATH}/output/${JTARBALL}/lib/libmindspore-lite.so ${JAVA_PATH}/java/linux_x86/libs/ | |||
| mkdir -p ${JAVA_PATH}/native/libs/linux_x86/ | |||
| cp ${BASEPATH}/output/${JTARBALL}/lib/libmindspore-lite.so ${JAVA_PATH}/native/libs/linux_x86/ | |||
| } | |||
| build_jni_arm64() { | |||
| # build jni so | |||
| cd "${BASEPATH}/mindspore/lite/build" | |||
| @@ -677,7 +724,7 @@ build_jni_arm64() { | |||
| -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="arm64-v8a" -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" \ | |||
| -DMS_VERSION_MAJOR=${VERSION_MAJOR} -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} \ | |||
| -DANDROID_STL="c++_static" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DENABLE_VERBOSE=${ENABLE_VERBOSE} \ | |||
| -DSUPPORT_TRAIN=${SUPPORT_TRAIN} -DPLATFORM_ARM64=on "${JAVA_PATH}/java/app/src/main/native" | |||
| -DSUPPORT_TRAIN=${SUPPORT_TRAIN} -DPLATFORM_ARM64=on "${JAVA_PATH}/native/" | |||
| make -j$THREAD_NUM | |||
| if [[ $? -ne 0 ]]; then | |||
| echo "---------------- mindspore lite: build jni arm64 failed----------------" | |||
| @@ -685,6 +732,8 @@ build_jni_arm64() { | |||
| fi | |||
| mkdir -p ${JAVA_PATH}/java/app/libs/arm64-v8a/ | |||
| cp ${BASEPATH}/mindspore/lite/build/java/libmindspore-lite-jni.so ${JAVA_PATH}/java/app/libs/arm64-v8a/ | |||
| mkdir -p ${JAVA_PATH}/native/libs/arm64-v8a/ | |||
| cp ${BASEPATH}/mindspore/lite/build/java/libmindspore-lite-jni.so ${JAVA_PATH}/native/libs/arm64-v8a/ | |||
| } | |||
| build_jni_arm32() { | |||
| @@ -697,7 +746,7 @@ build_jni_arm32() { | |||
| -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="armeabi-v7a" -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" \ | |||
| -DMS_VERSION_MAJOR=${VERSION_MAJOR} -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} \ | |||
| -DANDROID_STL="c++_static" -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DENABLE_VERBOSE=${ENABLE_VERBOSE} \ | |||
| -DSUPPORT_TRAIN=${SUPPORT_TRAIN} -DPLATFORM_ARM32=on "${JAVA_PATH}/java/app/src/main/native" | |||
| -DSUPPORT_TRAIN=${SUPPORT_TRAIN} -DPLATFORM_ARM32=on "${JAVA_PATH}/native" | |||
| make -j$THREAD_NUM | |||
| if [[ $? -ne 0 ]]; then | |||
| echo "---------------- mindspore lite: build jni arm32 failed----------------" | |||
| @@ -705,32 +754,85 @@ build_jni_arm32() { | |||
| fi | |||
| mkdir -p ${JAVA_PATH}/java/app/libs/armeabi-v7a/ | |||
| cp ${BASEPATH}/mindspore/lite/build/java/libmindspore-lite-jni.so ${JAVA_PATH}/java/app/libs/armeabi-v7a/ | |||
| mkdir -p ${JAVA_PATH}/native/libs/armeabi-v7a/ | |||
| cp ${BASEPATH}/mindspore/lite/build/java/libmindspore-lite-jni.so ${JAVA_PATH}/native/libs/armeabi-v7a/ | |||
| } | |||
| build_jni_x86_64() { | |||
| # build jni so | |||
| cd "${BASEPATH}/mindspore/lite/build" | |||
| rm -rf java | |||
| mkdir -pv java | |||
| cd java | |||
| cmake -DMS_VERSION_MAJOR=${VERSION_MAJOR} -DMS_VERSION_MINOR=${VERSION_MINOR} -DMS_VERSION_REVISION=${VERSION_REVISION} \ | |||
| -DENABLE_VERBOSE=${ENABLE_VERBOSE} "${JAVA_PATH}/native/" | |||
| make -j$THREAD_NUM | |||
| if [[ $? -ne 0 ]]; then | |||
| echo "---------------- mindspore lite: build jni x86_64 failed----------------" | |||
| exit 1 | |||
| fi | |||
| mkdir -p ${JAVA_PATH}/java/linux_x86/libs/ | |||
| cp ${BASEPATH}/mindspore/lite/build/java/libmindspore-lite-jni.so ${JAVA_PATH}/java/linux_x86/libs/ | |||
| mkdir -p ${JAVA_PATH}/native/libs/linux_x86/ | |||
| cp ${BASEPATH}/mindspore/lite/build/java/libmindspore-lite-jni.so ${JAVA_PATH}/native/libs/linux_x86/ | |||
| } | |||
| check_java_home() { | |||
| if [ "${JAVA_PATH}" ]; then | |||
| echo -e "\e[31mJAVA_HOME=$JAVA_HOME \e[0m" | |||
| else | |||
| echo -e "\e[31mplease set $JAVA_HOME in environment variable for example: export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64 \e[0m" | |||
| exit 1 | |||
| fi | |||
| } | |||
| build_java() { | |||
| JAVA_PATH=${BASEPATH}/mindspore/lite/java | |||
| get_version | |||
| build_lite_java_arm64 | |||
| build_lite_java_arm32 | |||
| build_jni_arm64 | |||
| build_jni_arm32 | |||
| # build aar | |||
| ## check sdk gradle | |||
| cd ${JAVA_PATH}/java | |||
| rm -rf .gradle build gradle gradlew gradlew.bat build app/build | |||
| gradle init | |||
| gradle wrapper | |||
| ./gradlew build | |||
| gradle publish -PLITE_VERSION=${VERSION_STR} | |||
| cd ${JAVA_PATH}/java/app/build | |||
| zip -r mindspore-lite-maven-${VERSION_STR}.zip mindspore | |||
| # copy output | |||
| cp mindspore-lite-maven-${VERSION_STR}.zip ${BASEPATH}/output/ | |||
| exit 0 | |||
| JAVA_PATH=${BASEPATH}/mindspore/lite/java | |||
| get_version | |||
| # build common module | |||
| cd ${JAVA_PATH}/java/common | |||
| gradle clean | |||
| gradle build | |||
| # build aar | |||
| build_lite_java_arm64 | |||
| build_jni_arm64 | |||
| build_lite_java_arm32 | |||
| build_jni_arm32 | |||
| mkdir -p ${JAVA_PATH}/java/linux_x86/libs | |||
| cp ${JAVA_PATH}/java/common/build/libs/mindspore-lite-java-common.jar ${JAVA_PATH}/java/app/libs | |||
| cd ${JAVA_PATH}/java/app | |||
| gradle clean | |||
| gradle build | |||
| gradle publish -PLITE_VERSION=${VERSION_STR} | |||
| cd ${JAVA_PATH}/java/app/build | |||
| zip -r mindspore-lite-maven-${VERSION_STR}.zip mindspore | |||
| # copy output | |||
| cp mindspore-lite-maven-${VERSION_STR}.zip ${BASEPATH}/output/ | |||
| # build linux x86 jar | |||
| check_java_home | |||
| build_lite_java_x86 | |||
| build_jni_x86_64 | |||
| mkdir -p ${JAVA_PATH}/java/linux_x86/libs | |||
| cp ${JAVA_PATH}/java/common/build/libs/mindspore-lite-java-common.jar ${JAVA_PATH}/java/linux_x86/libs/ | |||
| # build java | |||
| cd ${JAVA_PATH}/java/linux_x86/ | |||
| gradle clean | |||
| gradle releaseJar | |||
| # install and package | |||
| mkdir -p ${JAVA_PATH}/java/linux_x86/build/lib | |||
| cp ${JAVA_PATH}/java/linux_x86/libs/*.so ${JAVA_PATH}/java/linux_x86/build/lib/jar | |||
| cp -r ${JAVA_PATH}/java/linux_x86/build/lib/jar ${BASEPATH}/output/mindspore-lite-${VERSION_STR}-inference-linux-x64/lib/ | |||
| cd ${BASEPATH}/output | |||
| tar czf mindspore-lite-${VERSION_STR}-inference-linux-x64.tar.gz mindspore-lite-${VERSION_STR}-inference-linux-x64 | |||
| # copy output | |||
| [ -n "${VERSION_STR}" ] && rm -rf mindspore-lite-${VERSION_STR}-inference-linux-x64 | |||
| exit 0 | |||
| } | |||
| make_clean() | |||
| @@ -5,17 +5,26 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_L | |||
| message(FATAL_ERROR "GCC version ${CMAKE_CXX_COMPILER_VERSION} must not be less than 7.3.0") | |||
| endif() | |||
| # Add directory to include search path | |||
| include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | |||
| file(GLOB_RECURSE QUICK_START_CXX ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) | |||
| add_library(mindspore-lite STATIC IMPORTED) | |||
| set_target_properties(mindspore-lite PROPERTIES IMPORTED_LOCATION | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/lib/libmindspore-lite.a) | |||
| # Add directory to linker search path | |||
| link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib) | |||
| file(GLOB_RECURSE QUICK_START_CXX ${CMAKE_CURRENT_SOURCE_DIR}/*.cc) | |||
| add_executable(mindspore_quick_start_cpp ${QUICK_START_CXX}) | |||
| target_link_libraries( | |||
| mindspore_quick_start_cpp | |||
| -Wl,--whole-archive mindspore-lite -Wl,--no-whole-archive | |||
| pthread | |||
| ) | |||
| ) | |||
| # Due to the increased compilation options for stack protection, | |||
| # it is necessary to target link ssp library when Use the static library in Windows. | |||
| if(WIN32) | |||
| target_link_libraries( | |||
| mindspore_quick_start_cpp | |||
| ssp | |||
| ) | |||
| endif() | |||
| @@ -0,0 +1,26 @@ | |||
| @rem Copyright 2020 Huawei Technologies Co., Ltd | |||
| @rem | |||
| @rem Licensed under the Apache License, Version 2.0 (the "License"); | |||
| @rem you may not use this file except in compliance with the License. | |||
| @rem You may obtain a copy of the License at | |||
| @rem | |||
| @rem http://www.apache.org/licenses/LICENSE-2.0 | |||
| @rem | |||
| @rem Unless required by applicable law or agreed to in writing, software | |||
| @rem distributed under the License is distributed on an "AS IS" BASIS, | |||
| @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| @rem See the License for the specific language governing permissions and | |||
| @rem limitations under the License. | |||
| @rem ============================================================================ | |||
| @echo off | |||
| @title mindspore_lite_quick_start_cpp_demo_build | |||
| SET BASEPATH=%CD% | |||
| IF NOT EXIST "%BASEPATH%/build" ( | |||
| md build | |||
| ) | |||
| cd %BASEPATH%/build | |||
| cmake -G "CodeBlocks - MinGW Makefiles" %BASEPATH% | |||
| cmake --build . | |||
| @@ -22,7 +22,7 @@ get_version() { | |||
| VERSION_STR=${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION} | |||
| } | |||
| get_version | |||
| MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_openimage_lite/1.1/mobilenetv2.ms" | |||
| MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_imagenet/mobilenetv2.ms" | |||
| MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/linux/mindspore-lite-${VERSION_STR}-inference-linux-x64.tar.gz" | |||
| mkdir -p build | |||
| @@ -25,16 +25,16 @@ | |||
| #include "include/lite_session.h" | |||
| std::string RealPath(const char *path) { | |||
| size_t PATH_MAX = 4096; | |||
| size_t max = 4096; | |||
| if (path == nullptr) { | |||
| std::cerr << "path is nullptr" << std::endl; | |||
| return ""; | |||
| } | |||
| if ((strlen(path)) >= PATH_MAX) { | |||
| if ((strlen(path)) >= max) { | |||
| std::cerr << "path is too long" << std::endl; | |||
| return ""; | |||
| } | |||
| auto resolved_path = std::make_unique<char[]>(PATH_MAX); | |||
| auto resolved_path = std::make_unique<char[]>(max); | |||
| if (resolved_path == nullptr) { | |||
| std::cerr << "new resolved_path failed" << std::endl; | |||
| return ""; | |||
| @@ -0,0 +1,55 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
| <modelVersion>4.0.0</modelVersion> | |||
| <groupId>com.mindspore.lite.demo</groupId> | |||
| <artifactId>quick_start_java</artifactId> | |||
| <version>1.0</version> | |||
| <properties> | |||
| <maven.compiler.source>8</maven.compiler.source> | |||
| <maven.compiler.target>8</maven.compiler.target> | |||
| </properties> | |||
| <dependencies> | |||
| <dependency> | |||
| <groupId>com.mindspore.lite</groupId> | |||
| <artifactId>mindspore-lite-java</artifactId> | |||
| <version>1.0</version> | |||
| <scope>system</scope> | |||
| <systemPath>${project.basedir}/lib/mindspore-lite-java.jar</systemPath> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| <finalName>${project.name}</finalName> | |||
| <plugins> | |||
| <plugin> | |||
| <groupId>org.apache.maven.plugins</groupId> | |||
| <artifactId>maven-assembly-plugin</artifactId> | |||
| <configuration> | |||
| <archive> | |||
| <manifest> | |||
| <mainClass>com.mindspore.lite.demo.Main</mainClass> | |||
| </manifest> | |||
| </archive> | |||
| <descriptorRefs> | |||
| <descriptorRef>jar-with-dependencies</descriptorRef> | |||
| </descriptorRefs> | |||
| </configuration> | |||
| <executions> | |||
| <execution> | |||
| <id>make-assemble</id> | |||
| <phase>package</phase> | |||
| <goals> | |||
| <goal>single</goal> | |||
| </goals> | |||
| </execution> | |||
| </executions> | |||
| </plugin> | |||
| </plugins> | |||
| </build> | |||
| </project> | |||
| @@ -0,0 +1,150 @@ | |||
| package com.mindspore.lite.demo; | |||
| import com.mindspore.lite.LiteSession; | |||
| import com.mindspore.lite.MSTensor; | |||
| import com.mindspore.lite.Model; | |||
| import com.mindspore.lite.DataType; | |||
| import com.mindspore.lite.Version; | |||
| import com.mindspore.lite.config.MSConfig; | |||
| import java.nio.ByteBuffer; | |||
| import java.nio.ByteOrder; | |||
| import java.nio.FloatBuffer; | |||
| import java.util.Random; | |||
| public class Main { | |||
| private static Model model; | |||
| private static LiteSession session; | |||
| public static float[] generateArray(int len) { | |||
| Random rand = new Random(); | |||
| float[] arr = new float[len]; | |||
| for (int i = 0; i < arr.length; i++) { | |||
| arr[i] = rand.nextFloat(); | |||
| } | |||
| return arr; | |||
| } | |||
| private static ByteBuffer floatArrayToByteBuffer(float[] floats) { | |||
| if (floats == null) { | |||
| return null; | |||
| } | |||
| ByteBuffer buffer = ByteBuffer.allocateDirect(floats.length * Float.BYTES); | |||
| buffer.order(ByteOrder.LITTLE_ENDIAN); | |||
| FloatBuffer floatBuffer = buffer.asFloatBuffer(); | |||
| floatBuffer.put(floats); | |||
| return buffer; | |||
| } | |||
| private static boolean compile() { | |||
| MSConfig msConfig = new MSConfig(DeviceType.DT_CPU, 2); | |||
| boolean ret = msConfig.init(); | |||
| if (!ret) { | |||
| System.err.println("Init context failed"); | |||
| return false; | |||
| } | |||
| // Create the MindSpore lite session. | |||
| session = new LiteSession(); | |||
| ret = session.init(msConfig); | |||
| msConfig.free(); | |||
| if (!ret) { | |||
| System.err.println("Create session failed"); | |||
| model.free(); | |||
| return false; | |||
| } | |||
| // Compile graph. | |||
| ret = session.compileGraph(model); | |||
| if (!ret) { | |||
| System.err.println("Compile graph failed"); | |||
| model.free(); | |||
| return false; | |||
| } | |||
| return true; | |||
| } | |||
| private static boolean run() { | |||
| MSTensor inputTensor = session.getInputsByTensorName("2031_2030_1_construct_wrapper:x"); | |||
| if (inputTensor.getDataType() != DataType.kNumberTypeFloat32) { | |||
| System.err.println("Input tensor shape do not float, the data type is " + inputTensor.getDataType()); | |||
| return false; | |||
| } | |||
| // Generator Random Data. | |||
| int elementNums = inputTensor.elementsNum(); | |||
| float[] randomData = generateArray(elementNums); | |||
| ByteBuffer inputData = floatArrayToByteBuffer(randomData); | |||
| // Set Input Data. | |||
| inputTensor.setData(inputData); | |||
| // Run Inference. | |||
| boolean ret = session.runGraph(); | |||
| if (!ret) { | |||
| System.err.println("MindSpore Lite run failed."); | |||
| return false; | |||
| } | |||
| // Get Output Tensor Data. | |||
| MSTensor outTensor = session.getOutputByTensorName("Default/head-MobileNetV2Head/Softmax-op204"); | |||
| // Print out Tensor Data. | |||
| StringBuilder msgSb = new StringBuilder(); | |||
| msgSb.append("out tensor shape: ["); | |||
| int[] shape = outTensor.getShape(); | |||
| for (int dim : shape) { | |||
| msgSb.append(dim).append(","); | |||
| } | |||
| msgSb.append("]"); | |||
| if (outTensor.getDataType() != DataType.kNumberTypeFloat32) { | |||
| System.err.println("output tensor shape do not float, the data type is " + outTensor.getDataType()); | |||
| return false; | |||
| } | |||
| float[] result = outTensor.getFloatData(); | |||
| if (result == null) { | |||
| System.err.println("decodeBytes return null"); | |||
| return false; | |||
| } | |||
| msgSb.append(" and out data:"); | |||
| for (int i = 0; i < 10 && i < outTensor.elementsNum(); i++) { | |||
| msgSb.append(" ").append(result[i]); | |||
| } | |||
| System.out.println(msgSb.toString()); | |||
| return true; | |||
| } | |||
| private static void freeBuffer() { | |||
| session.free(); | |||
| model.free(); | |||
| } | |||
| public static void main(String[] args) { | |||
| System.out.println(Version.version()); | |||
| if (args.length < 1) { | |||
| System.err.println("The model path parameter must be passed."); | |||
| return; | |||
| } | |||
| String modelPath = args[0]; | |||
| model = new Model(); | |||
| boolean ret = model.loadModel(modelPath); | |||
| if (!ret) { | |||
| System.err.println("Load model failed, model path is " + modelPath); | |||
| return; | |||
| } | |||
| ret = compile(); | |||
| if (!ret) { | |||
| System.err.println("MindSpore Lite compile failed."); | |||
| return; | |||
| } | |||
| ret = run(); | |||
| if (!ret) { | |||
| System.err.println("MindSpore Lite run failed."); | |||
| freeBuffer(); | |||
| return; | |||
| } | |||
| freeBuffer(); | |||
| } | |||
| } | |||
| @@ -25,7 +25,7 @@ get_version() { | |||
| VERSION_STR=${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION} | |||
| } | |||
| get_version | |||
| MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_openimage_lite/1.1/mobilenetv2.ms" | |||
| MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_imagenet/mobilenetv2.ms" | |||
| MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/android/mindspore-lite-${VERSION_STR}-inference-android.tar.gz" | |||
| mkdir -p build | |||
| @@ -167,7 +167,7 @@ int GetInputsAndSetData(mindspore::session::LiteSession *session) { | |||
| } | |||
| int GetInputsByTensorNameAndSetData(mindspore::session::LiteSession *session) { | |||
| auto in_tensor = session->GetInputsByTensorName("2029_2028_1_construct_wrapper:x"); | |||
| auto in_tensor = session->GetInputsByTensorName("2031_2030_1_construct_wrapper:x"); | |||
| if (in_tensor == nullptr) { | |||
| std::cerr << "Input tensor is nullptr" << std::endl; | |||
| return -1; | |||
| @@ -183,7 +183,7 @@ int GetInputsByTensorNameAndSetData(mindspore::session::LiteSession *session) { | |||
| void GetOutputsByNodeName(mindspore::session::LiteSession *session) { | |||
| // model has a output node named output_node_name_0. | |||
| auto output_vec = session->GetOutputsByNodeName("Default/Sigmoid-op204"); | |||
| auto output_vec = session->GetOutputsByNodeName("Default/head-MobileNetV2Head/Softmax-op204"); | |||
| // output node named output_node_name_0 has only one output tensor. | |||
| auto out_tensor = output_vec.front(); | |||
| if (out_tensor == nullptr) { | |||
| @@ -0,0 +1,45 @@ | |||
| plugins { | |||
| id 'com.android.application' | |||
| } | |||
| android { | |||
| compileSdkVersion 30 | |||
| buildToolsVersion "30.0.2" | |||
| defaultConfig { | |||
| applicationId "com.mindspore.lite.demo" | |||
| minSdkVersion 19 | |||
| targetSdkVersion 30 | |||
| versionCode 1 | |||
| versionName "1.0" | |||
| } | |||
| buildTypes { | |||
| release { | |||
| minifyEnabled false | |||
| proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | |||
| } | |||
| } | |||
| compileOptions { | |||
| sourceCompatibility JavaVersion.VERSION_1_8 | |||
| targetCompatibility JavaVersion.VERSION_1_8 | |||
| } | |||
| aaptOptions { | |||
| noCompress = ['.so', '.ms', '.bin', '.jpg'] | |||
| } | |||
| } | |||
| repositories { | |||
| flatDir { | |||
| dirs 'libs' | |||
| } | |||
| } | |||
| apply from: 'download.gradle' | |||
| dependencies { | |||
| implementation fileTree(dir: "libs", include: ['*.aar', '*.jar']) | |||
| implementation 'androidx.appcompat:appcompat:1.2.0' | |||
| implementation 'com.google.android.material:material:1.3.0' | |||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | |||
| } | |||
| @@ -0,0 +1,112 @@ | |||
| /** | |||
| * To download necessary library from HuaWei server. | |||
| * Including mindspore-lite .so file, minddata-lite .so file and model file. | |||
| * The libraries can be downloaded manually. | |||
| */ | |||
| def targetModelFile = "src/main/assets/mobilenetv2.ms" | |||
| def modelDownloadUrl = "https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_imagenet/mobilenetv2.ms" | |||
| def mindsporeLite_Version = "mindspore-lite-1.1.0-inference-android" | |||
| def mindsporeLite_Version_AAR = "mindspore-lite-maven-1.1.0" | |||
| def mindsporeLiteDownloadUrl = "https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.1.0/MindSpore/lite/release/android/${mindsporeLite_Version}.tar.gz" | |||
| def targetRoot = "libs/" | |||
| def mindSporeLibrary = "${targetRoot}${mindsporeLite_Version}.tar.gz" | |||
| def mindSporeLibraryAAR = "${targetRoot}${mindsporeLite_Version}/${mindsporeLite_Version_AAR}.zip" | |||
| def cleantargetMindSporeInclude = "${targetRoot}${mindsporeLite_Version}" | |||
| def cleantargetMindSporeIncludeAAR = "${targetRoot}mindspore" | |||
| task downloadModelFile(type: DownloadUrlTask) { | |||
| doFirst { | |||
| println "Downloading ${modelDownloadUrl}" | |||
| } | |||
| sourceUrl = "${modelDownloadUrl}" | |||
| target = file("${targetModelFile}") | |||
| } | |||
| task downloadMindSporeLibrary(type: DownloadUrlTask) { | |||
| doFirst { | |||
| println "Downloading ${mindsporeLiteDownloadUrl}" | |||
| } | |||
| sourceUrl = "${mindsporeLiteDownloadUrl}" | |||
| target = file("${mindSporeLibrary}") | |||
| } | |||
| task unzipMindSporeInclude(type: Copy, dependsOn: ['downloadMindSporeLibrary']) { | |||
| doFirst { | |||
| println "Unzipping ${mindSporeLibrary}" | |||
| } | |||
| from tarTree(resources.gzip("${mindSporeLibrary}")) | |||
| into "${targetRoot}" | |||
| } | |||
| task unzipMindSporeIncludeAAR(type: Copy, dependsOn: ['unzipMindSporeInclude']) { | |||
| doFirst { | |||
| println "Unzipping ${mindSporeLibraryAAR}" | |||
| } | |||
| from zipTree("${mindSporeLibraryAAR}") | |||
| into "${targetRoot}" | |||
| } | |||
| task copyAARToRoot(type: Copy, dependsOn: ['unzipMindSporeIncludeAAR']) { | |||
| from('libs/mindspore/mindspore-lite/1.1.0/mindspore-lite-1.1.0.aar') | |||
| into "${targetRoot}" | |||
| } | |||
| task cleanUnusedmindsporeFiles(type: Delete, dependsOn: ['copyAARToRoot']) { | |||
| delete fileTree("${targetRoot}").matching { | |||
| include "*.tar.gz" | |||
| } | |||
| } | |||
| task cleanUnuseFiles(type: Delete, dependsOn: ['cleanUnusedmindsporeFiles']) { | |||
| delete("${cleantargetMindSporeInclude}") | |||
| } | |||
| task cleanUnuseFileAAR(type: Delete, dependsOn: ['cleanUnuseFiles']) { | |||
| delete("${cleantargetMindSporeIncludeAAR}") | |||
| } | |||
| if (file("libs/mindspore-lite-1.1.0.aar").exists()) { | |||
| downloadMindSporeLibrary.enabled = false | |||
| unzipMindSporeInclude.enabled = false | |||
| unzipMindSporeIncludeAAR.enabled = false | |||
| cleanUnuseFiles.enabled = false | |||
| cleanUnuseFileAAR.enabled = false | |||
| cleanUnusedmindsporeFiles.enabled = false | |||
| copyAARToRoot.enabled = false | |||
| } | |||
| if (file("src/main/assets/mobilenetv2.ms").exists()) { | |||
| downloadModelFile.enabled = false | |||
| } | |||
| preBuild.dependsOn downloadModelFile | |||
| preBuild.dependsOn downloadMindSporeLibrary | |||
| preBuild.dependsOn unzipMindSporeInclude | |||
| preBuild.dependsOn unzipMindSporeIncludeAAR | |||
| preBuild.dependsOn copyAARToRoot | |||
| preBuild.dependsOn cleanUnusedmindsporeFiles | |||
| preBuild.dependsOn cleanUnuseFiles | |||
| preBuild.dependsOn cleanUnuseFileAAR | |||
| class DownloadUrlTask extends DefaultTask { | |||
| @Input | |||
| String sourceUrl | |||
| @OutputFile | |||
| File target | |||
| @TaskAction | |||
| void download() { | |||
| ant.get(src: sourceUrl, dest: target) | |||
| } | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| # Add project specific ProGuard rules here. | |||
| # You can control the set of applied configuration files using the | |||
| # proguardFiles setting in build.gradle. | |||
| # | |||
| # For more details, see | |||
| # http://developer.android.com/guide/developing/tools/proguard.html | |||
| # If your project uses WebView with JS, uncomment the following | |||
| # and specify the fully qualified class name to the JavaScript interface | |||
| # class: | |||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | |||
| # public *; | |||
| #} | |||
| # Uncomment this to preserve the line number information for | |||
| # debugging stack traces. | |||
| #-keepattributes SourceFile,LineNumberTable | |||
| # If you keep the line number information, uncomment this to | |||
| # hide the original source file name. | |||
| #-renamesourcefileattribute SourceFile | |||
| @@ -0,0 +1,21 @@ | |||
| <?xml version="1.0" encoding="utf-8"?> | |||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |||
| package="com.mindspore.lite.demo"> | |||
| <application | |||
| android:allowBackup="true" | |||
| android:icon="@mipmap/ic_launcher" | |||
| android:label="@string/app_name" | |||
| android:roundIcon="@mipmap/ic_launcher_round" | |||
| android:supportsRtl="true" | |||
| android:theme="@style/Theme.Runtime"> | |||
| <activity android:name=".MainActivity"> | |||
| <intent-filter> | |||
| <action android:name="android.intent.action.MAIN" /> | |||
| <category android:name="android.intent.category.LAUNCHER" /> | |||
| </intent-filter> | |||
| </activity> | |||
| </application> | |||
| </manifest> | |||
| @@ -0,0 +1,305 @@ | |||
| package com.mindspore.lite.demo; | |||
| import androidx.appcompat.app.AppCompatActivity; | |||
| import android.os.Bundle; | |||
| import android.util.Log; | |||
| import android.view.View; | |||
| import android.widget.TextView; | |||
| import android.widget.Toast; | |||
| import com.mindspore.lite.DataType; | |||
| import com.mindspore.lite.LiteSession; | |||
| import com.mindspore.lite.MSTensor; | |||
| import com.mindspore.lite.Model; | |||
| import com.mindspore.lite.config.CpuBindMode; | |||
| import com.mindspore.lite.config.DeviceType; | |||
| import com.mindspore.lite.config.MSConfig; | |||
| import com.mindspore.lite.Version; | |||
| import java.nio.ByteBuffer; | |||
| import java.nio.ByteOrder; | |||
| import java.nio.FloatBuffer; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Random; | |||
| public class MainActivity extends AppCompatActivity { | |||
| private String TAG = "MS_LITE"; | |||
| private Model model; | |||
| private LiteSession session1; | |||
| private LiteSession session2; | |||
| private boolean session1Finish = true; | |||
| private boolean session2Finish = true; | |||
| private boolean session1Compile = false; | |||
| private boolean session2Compile = false; | |||
| public float[] generateArray(int len) { | |||
| Random rand = new Random(); | |||
| float[] arr = new float[len]; | |||
| for (int i = 0; i < arr.length; i++) { | |||
| arr[i] = rand.nextFloat(); | |||
| } | |||
| return arr; | |||
| } | |||
| private byte[] floatArrayToByteArray(float[] floats) { | |||
| if (floats == null) { | |||
| return null; | |||
| } | |||
| ByteBuffer buffer = ByteBuffer.allocate(floats.length * Float.BYTES); | |||
| buffer.order(ByteOrder.nativeOrder()); | |||
| FloatBuffer floatBuffer = buffer.asFloatBuffer(); | |||
| floatBuffer.put(floats); | |||
| return buffer.array(); | |||
| } | |||
| private MSConfig createCPUConfig() { | |||
| MSConfig msConfig = new MSConfig(); | |||
| boolean ret = msConfig.init(DeviceType.DT_CPU, 2, CpuBindMode.HIGHER_CPU, true); | |||
| if (!ret) { | |||
| Log.e(TAG, "Create CPU Config failed."); | |||
| return null; | |||
| } | |||
| return msConfig; | |||
| } | |||
| private MSConfig createGPUConfig() { | |||
| MSConfig msConfig = new MSConfig(); | |||
| boolean ret = msConfig.init(DeviceType.DT_GPU, 2, CpuBindMode.MID_CPU, true); | |||
| if (!ret) { | |||
| return null; | |||
| } | |||
| return msConfig; | |||
| } | |||
| private LiteSession createLiteSession(boolean isResize) { | |||
| MSConfig msConfig = createCPUConfig(); | |||
| if (msConfig == null) { | |||
| Log.e(TAG, "Init context failed"); | |||
| return null; | |||
| } | |||
| // Create the MindSpore lite session. | |||
| LiteSession session = new LiteSession(); | |||
| boolean ret = session.init(msConfig); | |||
| msConfig.free(); | |||
| if (!ret) { | |||
| Log.e(TAG, "Create session failed"); | |||
| return null; | |||
| } | |||
| // Compile graph. | |||
| ret = session.compileGraph(model); | |||
| if (!ret) { | |||
| session.free(); | |||
| Log.e(TAG, "Compile graph failed"); | |||
| return null; | |||
| } | |||
| if (isResize) { | |||
| List<MSTensor> inputs = session.getInputs(); | |||
| int[][] dims = {{1, 300, 300, 3}}; | |||
| ret = session.resize(inputs, dims); | |||
| if (!ret) { | |||
| Log.e(TAG, "Resize failed"); | |||
| session.free(); | |||
| return null; | |||
| } | |||
| StringBuilder msgSb = new StringBuilder(); | |||
| msgSb.append("in tensor shape: ["); | |||
| int[] shape = session.getInputs().get(0).getShape(); | |||
| for (int dim : shape) { | |||
| msgSb.append(dim).append(","); | |||
| } | |||
| msgSb.append("]"); | |||
| Log.i(TAG, msgSb.toString()); | |||
| } | |||
| return session; | |||
| } | |||
| private boolean printTensorData(MSTensor outTensor) { | |||
| int[] shape = outTensor.getShape(); | |||
| StringBuilder msgSb = new StringBuilder(); | |||
| msgSb.append("out tensor shape: ["); | |||
| for (int dim : shape) { | |||
| msgSb.append(dim).append(","); | |||
| } | |||
| msgSb.append("]"); | |||
| if (outTensor.getDataType() != DataType.kNumberTypeFloat32) { | |||
| Log.e(TAG, "output tensor shape do not float, the data type is " + outTensor.getDataType()); | |||
| return false; | |||
| } | |||
| float[] result = outTensor.getFloatData(); | |||
| if (result == null) { | |||
| Log.e(TAG, "decodeBytes return null"); | |||
| return false; | |||
| } | |||
| msgSb.append(" and out data:"); | |||
| for (int i = 0; i < 10 && i < outTensor.elementsNum(); i++) { | |||
| msgSb.append(" ").append(result[i]); | |||
| } | |||
| Log.i(TAG, msgSb.toString()); | |||
| return true; | |||
| } | |||
| private boolean runInference(LiteSession session) { | |||
| Log.i(TAG, "runInference: "); | |||
| MSTensor inputTensor = session.getInputsByTensorName("2031_2030_1_construct_wrapper:x"); | |||
| if (inputTensor.getDataType() != DataType.kNumberTypeFloat32) { | |||
| Log.e(TAG, "Input tensor shape do not float, the data type is " + inputTensor.getDataType()); | |||
| return false; | |||
| } | |||
| // Generator Random Data. | |||
| int elementNums = inputTensor.elementsNum(); | |||
| float[] randomData = generateArray(elementNums); | |||
| byte[] inputData = floatArrayToByteArray(randomData); | |||
| // Set Input Data. | |||
| inputTensor.setData(inputData); | |||
| session.bindThread(true); | |||
| // Run Inference. | |||
| boolean ret = session.runGraph(); | |||
| session.bindThread(false); | |||
| if (!ret) { | |||
| Log.e(TAG, "MindSpore Lite run failed."); | |||
| return false; | |||
| } | |||
| // Get Output Tensor Data. | |||
| MSTensor outTensor = session.getOutputByTensorName("Default/head-MobileNetV2Head/Softmax-op204"); | |||
| // Print out Tensor Data. | |||
| ret = printTensorData(outTensor); | |||
| if (!ret) { | |||
| return false; | |||
| } | |||
| outTensor = session.getOutputsByNodeName("Default/head-MobileNetV2Head/Softmax-op204").get(0); | |||
| ret = printTensorData(outTensor); | |||
| if (!ret) { | |||
| return false; | |||
| } | |||
| Map<String, MSTensor> outTensors = session.getOutputMapByTensor(); | |||
| Iterator<Map.Entry<String, MSTensor>> entries = outTensors.entrySet().iterator(); | |||
| while (entries.hasNext()) { | |||
| Map.Entry<String, MSTensor> entry = entries.next(); | |||
| Log.i(TAG, "Tensor name is:" + entry.getKey()); | |||
| ret = printTensorData(entry.getValue()); | |||
| if (!ret) { | |||
| return false; | |||
| } | |||
| } | |||
| return true; | |||
| } | |||
| private void freeBuffer() { | |||
| session1.free(); | |||
| session2.free(); | |||
| model.free(); | |||
| } | |||
| @Override | |||
| protected void onCreate(Bundle savedInstanceState) { | |||
| super.onCreate(savedInstanceState); | |||
| setContentView(R.layout.activity_main); | |||
| String version = Version.version(); | |||
| Log.i(TAG, version); | |||
| model = new Model(); | |||
| String modelPath = "mobilenetv2.ms"; | |||
| boolean ret = model.loadModel(this.getApplicationContext(), modelPath); | |||
| if (!ret) { | |||
| Log.e(TAG, "Load model failed, model is " + modelPath); | |||
| } else { | |||
| session1 = createLiteSession(false); | |||
| if (session1 != null) { | |||
| session1Compile = true; | |||
| } else { | |||
| Toast.makeText(getApplicationContext(), "session1 Compile Failed.", | |||
| Toast.LENGTH_SHORT).show(); | |||
| } | |||
| session2 = createLiteSession(true); | |||
| if (session2 != null) { | |||
| session2Compile = true; | |||
| } else { | |||
| Toast.makeText(getApplicationContext(), "session2 Compile Failed.", | |||
| Toast.LENGTH_SHORT).show(); | |||
| } | |||
| } | |||
| if (model != null) { | |||
| // Note: when use model.freeBuffer(), the model can not be compiled again. | |||
| model.freeBuffer(); | |||
| } | |||
| TextView btn_run = findViewById(R.id.btn_run); | |||
| btn_run.setOnClickListener(new View.OnClickListener() { | |||
| @Override | |||
| public void onClick(View v) { | |||
| if (session1Finish && session1Compile) { | |||
| new Thread(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| session1Finish = false; | |||
| runInference(session1); | |||
| session1Finish = true; | |||
| } | |||
| }).start(); | |||
| } else { | |||
| Toast.makeText(getApplicationContext(), "MindSpore Lite is running...", | |||
| Toast.LENGTH_SHORT).show(); | |||
| } | |||
| } | |||
| }); | |||
| TextView btn_run_multi_thread = findViewById(R.id.btn_run_multi_thread); | |||
| btn_run_multi_thread.setOnClickListener( | |||
| new View.OnClickListener() { | |||
| @Override | |||
| public void onClick(View v) { | |||
| if (session1Finish && session1Compile) { | |||
| new Thread(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| session1Finish = false; | |||
| runInference(session1); | |||
| session1Finish = true; | |||
| } | |||
| }).start(); | |||
| } | |||
| if (session2Finish && session2Compile) { | |||
| new Thread(new Runnable() { | |||
| @Override | |||
| public void run() { | |||
| session2Finish = false; | |||
| runInference(session2); | |||
| session2Finish = true; | |||
| } | |||
| }).start(); | |||
| } | |||
| if (!session2Finish && !session2Finish) { | |||
| Toast.makeText(getApplicationContext(), "MindSpore Lite is running...", | |||
| Toast.LENGTH_SHORT).show(); | |||
| } | |||
| } | |||
| } | |||
| ); | |||
| } | |||
| @Override | |||
| protected void onDestroy() { | |||
| super.onDestroy(); | |||
| freeBuffer(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,24 @@ | |||
| // Top-level build file where you can add configuration options common to all sub-projects/modules. | |||
| buildscript { | |||
| repositories { | |||
| google() | |||
| jcenter() | |||
| } | |||
| dependencies { | |||
| classpath "com.android.tools.build:gradle:4.1.2" | |||
| // NOTE: Do not place your application dependencies here; they belong | |||
| // in the individual module build.gradle files | |||
| } | |||
| } | |||
| allprojects { | |||
| repositories { | |||
| google() | |||
| jcenter() | |||
| } | |||
| } | |||
| task clean(type: Delete) { | |||
| delete rootProject.buildDir | |||
| } | |||
| @@ -0,0 +1,2 @@ | |||
| include ':app' | |||
| rootProject.name = "Runtime" | |||
| @@ -9,17 +9,6 @@ android { | |||
| targetSdkVersion 30 | |||
| versionCode 1 | |||
| versionName project.hasProperty("LITE_VERSION") ? LITE_VERSION : "" | |||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | |||
| externalNativeBuild { | |||
| cmake { | |||
| cppFlags "-std=c++17" | |||
| } | |||
| } | |||
| ndk { | |||
| abiFilters 'arm64-v8a' | |||
| abiFilters 'armeabi-v7a' | |||
| } | |||
| } | |||
| buildTypes { | |||
| @@ -45,7 +34,6 @@ android { | |||
| dependencies { | |||
| implementation fileTree(dir: "libs", include: ["*.jar"]) | |||
| testImplementation 'junit:junit:4.12' | |||
| } | |||
| publishing { | |||
| @@ -0,0 +1,9 @@ | |||
| plugins { | |||
| id 'java' | |||
| } | |||
| archivesBaseName = 'mindspore-lite-java-common' | |||
| repositories { | |||
| mavenCentral() | |||
| } | |||
| @@ -16,8 +16,6 @@ | |||
| package com.mindspore.lite; | |||
| import android.util.Log; | |||
| import java.nio.ByteBuffer; | |||
| public class MSTensor { | |||
| @@ -0,0 +1,16 @@ | |||
| apply plugin: 'java' | |||
| dependencies { | |||
| implementation fileTree(dir: "libs", include: ["*.jar"]) | |||
| } | |||
| archivesBaseName = 'mindspore-lite-java' | |||
| task releaseJar(type: Jar) { | |||
| from('build/classes/java/main') | |||
| from('../common/build/classes/java/main') | |||
| destinationDirectory = file('build/lib/jar') | |||
| } | |||
| releaseJar.dependsOn(build) | |||
| @@ -0,0 +1,148 @@ | |||
| /** | |||
| * Copyright 2020 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. | |||
| */ | |||
| package com.mindspore.lite; | |||
| import java.util.ArrayList; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Set; | |||
| import com.mindspore.lite.config.MSConfig; | |||
| public class LiteSession { | |||
| static { | |||
| System.loadLibrary("mindspore-lite-jni"); | |||
| } | |||
| private long sessionPtr; | |||
| public LiteSession() { | |||
| this.sessionPtr = 0; | |||
| } | |||
| public boolean init(MSConfig config) { | |||
| this.sessionPtr = createSession(config.getMSConfigPtr()); | |||
| return this.sessionPtr != 0; | |||
| } | |||
| public long getSessionPtr() { | |||
| return sessionPtr; | |||
| } | |||
| public void bindThread(boolean if_bind) { | |||
| this.bindThread(this.sessionPtr, if_bind); | |||
| } | |||
| public boolean compileGraph(Model model) { | |||
| return this.compileGraph(this.sessionPtr, model.getModelPtr()); | |||
| } | |||
| public boolean runGraph() { | |||
| return this.runGraph(this.sessionPtr); | |||
| } | |||
| public List<MSTensor> getInputs() { | |||
| List<Long> ret = this.getInputs(this.sessionPtr); | |||
| ArrayList<MSTensor> tensors = new ArrayList<MSTensor>(); | |||
| for (Long msTensorAddr : ret) { | |||
| MSTensor msTensor = new MSTensor(msTensorAddr); | |||
| tensors.add(msTensor); | |||
| } | |||
| return tensors; | |||
| } | |||
| public MSTensor getInputsByTensorName(String tensorName) { | |||
| Long tensorAddr = this.getInputsByTensorName(this.sessionPtr, tensorName); | |||
| if (tensorAddr == null) { | |||
| return null; | |||
| } | |||
| MSTensor msTensor = new MSTensor(tensorAddr); | |||
| return msTensor; | |||
| } | |||
| public List<MSTensor> getOutputsByNodeName(String nodeName) { | |||
| List<Long> ret = this.getOutputsByNodeName(this.sessionPtr, nodeName); | |||
| ArrayList<MSTensor> tensors = new ArrayList<>(); | |||
| for (Long msTensorAddr : ret) { | |||
| MSTensor msTensor = new MSTensor(msTensorAddr); | |||
| tensors.add(msTensor); | |||
| } | |||
| return tensors; | |||
| } | |||
| public Map<String, MSTensor> getOutputMapByTensor() { | |||
| Map<String, Long> ret = this.getOutputMapByTensor(this.sessionPtr); | |||
| Map<String, MSTensor> tensorMap = new HashMap<>(); | |||
| Set<Map.Entry<String, Long>> entrySet = ret.entrySet(); | |||
| for (Map.Entry<String, Long> entry : entrySet) { | |||
| String name = entry.getKey(); | |||
| Long msTensorAddr = entry.getValue(); | |||
| tensorMap.put(name, new MSTensor(msTensorAddr)); | |||
| } | |||
| return tensorMap; | |||
| } | |||
| public List<String> getOutputTensorNames() { | |||
| return getOutputTensorNames(this.sessionPtr); | |||
| } | |||
| public MSTensor getOutputByTensorName(String tensorName) { | |||
| Long tensorAddr = getOutputByTensorName(this.sessionPtr, tensorName); | |||
| if (tensorAddr == null) { | |||
| return null; | |||
| } | |||
| return new MSTensor(tensorAddr); | |||
| } | |||
| public void free() { | |||
| this.free(this.sessionPtr); | |||
| this.sessionPtr = 0; | |||
| } | |||
| public boolean resize(List<MSTensor> inputs, int[][] dims) { | |||
| long[] inputsArray = new long[inputs.size()]; | |||
| for (int i = 0; i < inputs.size(); i++) { | |||
| inputsArray[i] = inputs.get(i).getMSTensorPtr(); | |||
| } | |||
| return this.resize(this.sessionPtr, inputsArray, dims); | |||
| } | |||
| private native long createSession(long msConfigPtr); | |||
| private native boolean compileGraph(long sessionPtr, long modelPtr); | |||
| private native void bindThread(long sessionPtr, boolean if_bind); | |||
| private native boolean runGraph(long sessionPtr); | |||
| private native List<Long> getInputs(long sessionPtr); | |||
| private native long getInputsByTensorName(long sessionPtr, String tensorName); | |||
| private native List<Long> getOutputsByNodeName(long sessionPtr, String nodeName); | |||
| private native Map<String, Long> getOutputMapByTensor(long sessionPtr); | |||
| private native List<String> getOutputTensorNames(long sessionPtr); | |||
| private native long getOutputByTensorName(long sessionPtr, String tensorName); | |||
| private native void free(long sessionPtr); | |||
| private native boolean resize(long sessionPtr, long[] inputs, int[][] dims); | |||
| } | |||
| @@ -0,0 +1,60 @@ | |||
| /** | |||
| * Copyright 2020 Huawei Technologies Co., Ltd | |||
| * <p> | |||
| * 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 | |||
| * <p> | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * <p> | |||
| * 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. | |||
| */ | |||
| package com.mindspore.lite; | |||
| import java.io.FileInputStream; | |||
| import java.io.IOException; | |||
| import java.nio.MappedByteBuffer; | |||
| import java.nio.channels.FileChannel; | |||
| public class Model { | |||
| static { | |||
| System.loadLibrary("mindspore-lite-jni"); | |||
| } | |||
| private long modelPtr; | |||
| public Model() { | |||
| this.modelPtr = 0; | |||
| } | |||
| public boolean loadModel(String modelPath) { | |||
| this.modelPtr = loadModelByPath(modelPath); | |||
| return this.modelPtr != 0; | |||
| } | |||
| public void free() { | |||
| this.free(this.modelPtr); | |||
| this.modelPtr = 0; | |||
| } | |||
| public void freeBuffer() { | |||
| this.freeBuffer(this.modelPtr); | |||
| } | |||
| protected long getModelPtr() { | |||
| return modelPtr; | |||
| } | |||
| private native long loadModel(MappedByteBuffer buffer); | |||
| private native long loadModelByPath(String modelPath); | |||
| private native void free(long modelPtr); | |||
| private native void freeBuffer(long modelPtr); | |||
| } | |||
| @@ -8,4 +8,6 @@ | |||
| */ | |||
| include ':app' | |||
| include ':common' | |||
| include ':linux_x86' | |||
| rootProject.name = 'mindspore-lite-aar' | |||
| @@ -2,7 +2,11 @@ cmake_minimum_required(VERSION 3.10) | |||
| project(Lite-java) | |||
| set(BUILD_LITE "on") | |||
| set(PLATFORM_ARM "on") | |||
| if(PLATFORM_ARM32 OR PLATFORM_ARM32) | |||
| set(PLATFORM_ARM "on") | |||
| add_compile_definitions(PLATFORM_ARM) | |||
| endif() | |||
| set(MS_VERSION_MAJOR ${MS_VERSION_MAJOR}) | |||
| set(MS_VERSION_MINOR ${MS_VERSION_MINOR}) | |||
| @@ -29,7 +33,7 @@ if(PLATFORM_ARM64) | |||
| add_compile_definitions(ENABLE_ARM64) | |||
| endif() | |||
| set(TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../..) | |||
| set(TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) | |||
| set(LITE_DIR ${TOP_DIR}/mindspore/lite) | |||
| include_directories(${CMAKE_CURRENT_SOURCE_DIR}) | |||
| @@ -37,16 +41,21 @@ include_directories(${LITE_DIR}) ## lite include | |||
| include_directories(${TOP_DIR}) ## api include | |||
| include_directories(${TOP_DIR}/mindspore/core/) ## core include | |||
| include_directories(${LITE_DIR}/build) ## flatbuffers | |||
| link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${ANDROID_ABI}/) | |||
| if(PLATFORM_ARM64 OR PLATFORM_ARM32) | |||
| link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/) | |||
| else() | |||
| link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/linux_x86/) | |||
| set(JAVA_HOME "$ENV{JAVA_HOME}") | |||
| include_directories(${JAVA_HOME}/include/) ## jni | |||
| include_directories(${JAVA_HOME}/include/linux) ## jni | |||
| endif() | |||
| set(JNI_SRC | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/common/jni_utils.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/model.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/version.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/ms_config.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/ms_tensor.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/lite_session.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/common/jni_utils.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/model.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/version.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/ms_config.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/ms_tensor.cpp | |||
| ${CMAKE_CURRENT_SOURCE_DIR}/runtime/lite_session.cpp | |||
| ) | |||
| if(SUPPORT_TRAIN) | |||
| @@ -58,7 +67,9 @@ endif() | |||
| add_library(mindspore-lite-jni SHARED ${JNI_SRC}) | |||
| find_library(log-lib log) | |||
| target_link_libraries(mindspore-lite-jni mindspore-lite ${log-lib}) | |||
| if(PLATFORM_ARM64 OR PLATFORM_ARM32) | |||
| find_library(log-lib log) | |||
| target_link_libraries(mindspore-lite-jni mindspore-lite ${log-lib}) | |||
| else() | |||
| target_link_libraries(mindspore-lite-jni mindspore-lite) | |||
| endif() | |||
| @@ -17,11 +17,12 @@ | |||
| #ifndef MINDSPORE_LITE_JAVA_SRC_COMMON_MS_LOG_H | |||
| #define MINDSPORE_LITE_JAVA_SRC_COMMON_MS_LOG_H | |||
| #define TAG "MS_LITE" | |||
| #ifdef PLATFORM_ARM | |||
| #include <android/log.h> | |||
| #include <unistd.h> | |||
| #define TAG "MS_LITE" | |||
| #define MS_LOGD(fmt, args...) \ | |||
| { __android_log_print(ANDROID_LOG_DEBUG, TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, __LINE__, ##args); } | |||
| @@ -30,5 +31,15 @@ | |||
| #define MS_LOGI(fmt, args...) \ | |||
| { __android_log_print(ANDROID_LOG_INFO, TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, __LINE__, ##args); } | |||
| #else | |||
| #define MS_LOGD(fmt, args...) \ | |||
| { printf("[DEBUG] %s|%s|%s[%d]|: " #fmt "\r\n", TAG, __FILE__, __func__, __LINE__, ##args); } | |||
| #define MS_LOGE(fmt, args...) \ | |||
| { printf("[ERROR] %s|%s|%s[%d]|: " #fmt "\r\n", TAG, __FILE__, __func__, __LINE__, ##args); } | |||
| #define MS_LOGI(fmt, args...) \ | |||
| { printf("[INFO] %s|%s|%s[%d]|: " #fmt "\r\n", TAG, __FILE__, __func__, __LINE__, ##args); } | |||
| #endif | |||
| #endif // MINDSPORE_LITE_JAVA_SRC_COMMON_MS_LOG_H | |||
| @@ -21,7 +21,6 @@ | |||
| #include "include/model.h" | |||
| extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEnv *env, jobject thiz, jobject buffer) { | |||
| MS_LOGD("Start getting buffer from java"); | |||
| if (buffer == nullptr) { | |||
| MS_LOGE("Buffer from java is nullptr"); | |||
| return reinterpret_cast<jlong>(nullptr); | |||
| @@ -29,7 +28,6 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEn | |||
| jlong buffer_len = env->GetDirectBufferCapacity(buffer); | |||
| auto *model_buffer = static_cast<char *>(env->GetDirectBufferAddress(buffer)); | |||
| MS_LOGD("Start Loading model"); | |||
| auto model = mindspore::lite::Model::Import(model_buffer, buffer_len); | |||
| if (model == nullptr) { | |||
| MS_LOGE("Import model failed"); | |||
| @@ -69,7 +67,6 @@ extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModelByPath | |||
| ifs.read(buf.get(), size); | |||
| ifs.close(); | |||
| delete[](model_path_char); | |||
| MS_LOGD("Start Loading model"); | |||
| auto model = mindspore::lite::Model::Import(buf.get(), size); | |||
| if (model == nullptr) { | |||
| MS_LOGE("Import model failed"); | |||
| @@ -15,6 +15,7 @@ | |||
| */ | |||
| #include <jni.h> | |||
| #include <cstring> | |||
| #include "common/ms_log.h" | |||
| #include "include/ms_tensor.h" | |||
| #include "ir/dtype/type_id.h" | |||
| @@ -34,8 +35,8 @@ extern "C" JNIEXPORT jintArray JNICALL Java_com_mindspore_lite_MSTensor_getShape | |||
| for (size_t i = 0; i < shape_size; i++) { | |||
| tmp[i] = local_shape.at(i); | |||
| } | |||
| delete[](tmp); | |||
| env->SetIntArrayRegion(shape, 0, shape_size, tmp); | |||
| delete[](tmp); | |||
| return shape; | |||
| } | |||
| @@ -181,14 +182,12 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_MSTensor_setData(J | |||
| extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_MSTensor_setByteBufferData(JNIEnv *env, jobject thiz, | |||
| jlong tensor_ptr, | |||
| jobject buffer) { | |||
| jbyte *p_data = reinterpret_cast<jbyte *>(env->GetDirectBufferAddress(buffer)); // get buffer poiter | |||
| auto *p_data = reinterpret_cast<jbyte *>(env->GetDirectBufferAddress(buffer)); // get buffer pointer | |||
| jlong data_len = env->GetDirectBufferCapacity(buffer); // get buffer capacity | |||
| if (p_data == nullptr) { | |||
| MS_LOGE("GetDirectBufferAddress return null"); | |||
| return false; | |||
| } | |||
| jbyteArray data = env->NewByteArray(data_len); // create byte[] | |||
| env->SetByteArrayRegion(data, 0, data_len, p_data); // copy data to byte[] | |||
| auto *pointer = reinterpret_cast<void *>(tensor_ptr); | |||
| if (pointer == nullptr) { | |||
| @@ -205,11 +204,8 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_mindspore_lite_MSTensor_setByteBu | |||
| #endif | |||
| return static_cast<jboolean>(false); | |||
| } | |||
| jboolean is_copy = false; | |||
| auto *data_arr = env->GetByteArrayElements(data, &is_copy); | |||
| auto *local_data = ms_tensor_ptr->MutableData(); | |||
| memcpy(local_data, data_arr, data_len); | |||
| env->ReleaseByteArrayElements(data, data_arr, JNI_ABORT); | |||
| memcpy(local_data, p_data, data_len); | |||
| return static_cast<jboolean>(true); | |||
| } | |||