forked from mindspore-Ecosystem/mindspore
modify java native lib
This commit is contained in:
parent
0e06275442
commit
bcd1fa1f7a
|
@ -153,46 +153,7 @@ endif()
|
|||
if(DEFINED ENV{MSLITE_ENABLE_SERVER_INFERENCE})
|
||||
set(MSLITE_ENABLE_SERVER_INFERENCE $ENV{MSLITE_ENABLE_SERVER_INFERENCE})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_SERVER_INFERENCE)
|
||||
set(MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE on)
|
||||
set(MSLITE_ENABLE_BFC_MEMORY on)
|
||||
set(MSLITE_ENABLE_PARALLEL_INFERENCE on)
|
||||
set(MSLITE_ENABLE_SHARING_MODEL_WEIGHT on)
|
||||
set(MSLITE_ENABLE_RUNTIME_GLOG off)
|
||||
add_compile_definitions(SERVER_INFERENCE)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_GLOG})
|
||||
set(MSLITE_ENABLE_RUNTIME_GLOG $ENV{MSLITE_ENABLE_RUNTIME_GLOG})
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE})
|
||||
set(MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE $ENV{MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE)
|
||||
add_compile_definitions(DYNAMIC_THREAD_DISTRIBUTE)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_BFC_MEMORY})
|
||||
set(MSLITE_ENABLE_BFC_MEMORY $ENV{MSLITE_ENABLE_BFC_MEMORY})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_BFC_MEMORY)
|
||||
add_compile_definitions(BFC_MEMORY)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_PARALLEL_INFERENCE})
|
||||
set(MSLITE_ENABLE_PARALLEL_INFERENCE $ENV{MSLITE_ENABLE_PARALLEL_INFERENCE})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_PARALLEL_INFERENCE)
|
||||
add_compile_definitions(PARALLEL_INFERENCE)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_SHARING_MODEL_WEIGHT})
|
||||
set(MSLITE_ENABLE_SHARING_MODEL_WEIGHT $ENV{MSLITE_ENABLE_SHARING_MODEL_WEIGHT})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_SHARING_MODEL_WEIGHT)
|
||||
add_compile_definitions(SHARING_MODEL_WEIGHT)
|
||||
endif()
|
||||
if(DEFINED ENV{MSLITE_ENABLE_SERVING})
|
||||
set(MSLITE_ENABLE_SERVING $ENV{MSLITE_ENABLE_SERVING})
|
||||
endif()
|
||||
|
@ -279,6 +240,47 @@ else()
|
|||
set(MSLITE_ENABLE_NPU off)
|
||||
endif()
|
||||
|
||||
if(MSLITE_ENABLE_SERVER_INFERENCE)
|
||||
set(MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE on)
|
||||
set(MSLITE_ENABLE_BFC_MEMORY on)
|
||||
set(MSLITE_ENABLE_PARALLEL_INFERENCE on)
|
||||
set(MSLITE_ENABLE_SHARING_MODEL_WEIGHT on)
|
||||
set(MSLITE_ENABLE_RUNTIME_GLOG on)
|
||||
add_compile_definitions(SERVER_INFERENCE)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_GLOG})
|
||||
set(MSLITE_ENABLE_RUNTIME_GLOG $ENV{MSLITE_ENABLE_RUNTIME_GLOG})
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE})
|
||||
set(MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE $ENV{MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_DYNAMIC_THREAD_DISTRIBUTE)
|
||||
add_compile_definitions(DYNAMIC_THREAD_DISTRIBUTE)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_BFC_MEMORY})
|
||||
set(MSLITE_ENABLE_BFC_MEMORY $ENV{MSLITE_ENABLE_BFC_MEMORY})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_BFC_MEMORY)
|
||||
add_compile_definitions(BFC_MEMORY)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_PARALLEL_INFERENCE})
|
||||
set(MSLITE_ENABLE_PARALLEL_INFERENCE $ENV{MSLITE_ENABLE_PARALLEL_INFERENCE})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_PARALLEL_INFERENCE)
|
||||
add_compile_definitions(PARALLEL_INFERENCE)
|
||||
endif()
|
||||
|
||||
if(DEFINED ENV{MSLITE_ENABLE_SHARING_MODEL_WEIGHT})
|
||||
set(MSLITE_ENABLE_SHARING_MODEL_WEIGHT $ENV{MSLITE_ENABLE_SHARING_MODEL_WEIGHT})
|
||||
endif()
|
||||
if(MSLITE_ENABLE_SHARING_MODEL_WEIGHT)
|
||||
add_compile_definitions(SHARING_MODEL_WEIGHT)
|
||||
endif()
|
||||
|
||||
if(MSLITE_ENABLE_SSE OR MSLITE_ENABLE_AVX OR MSLITE_ENABLE_AVX512 OR WIN32)
|
||||
set(MSLITE_ENABLE_RUNTIME_CONVERT off)
|
||||
endif()
|
||||
|
|
|
@ -80,11 +80,15 @@ build_lite_x86_64_jni_and_jar() {
|
|||
echo "---------------- mindspore lite: build jni x86_64 failed----------------"
|
||||
exit 1
|
||||
fi
|
||||
rm -f ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_x86_64/*.so*
|
||||
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/java/linux_x86/libs/
|
||||
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/native/libs/linux_x86/
|
||||
cp ./libmindspore-lite-jni.so ${INSTALL_PREFIX}/${pkg_name}/runtime/lib/
|
||||
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_x86_64/
|
||||
cp ${BASEPATH}/output/tmp/${pkg_name}/runtime/lib/libmindspore-lite.so ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_x86_64/
|
||||
if [[ "${MSLITE_ENABLE_SERVER_INFERENCE}" == "ON" || "${MSLITE_ENABLE_SERVER_INFERENCE}" == "on" ]] ; then
|
||||
cp ${BASEPATH}/output/tmp/${pkg_name}/runtime/third_party/glog/libglog.so* ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_x86_64/libglog.so
|
||||
fi
|
||||
if [[ "X$is_train" = "Xon" ]]; then
|
||||
cp ./libmindspore-lite-train-jni.so ${LITE_JAVA_PATH}/java/linux_x86/libs/
|
||||
cp ./libmindspore-lite-train-jni.so ${LITE_JAVA_PATH}/native/libs/linux_x86/
|
||||
|
@ -120,13 +124,13 @@ build_lite_x86_64_jni_and_jar() {
|
|||
# build jar
|
||||
${gradle_command} clean -p ${LITE_JAVA_PATH}/
|
||||
if [[ "${ENABLE_ASAN}" == "ON" || "${ENABLE_ASAN}" == "on" ]] ; then
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test --info
|
||||
else
|
||||
if [[ "${MSLITE_ENABLE_TESTCASES}" == "ON" || "${MSLITE_ENABLE_TESTCASES}" == "on" ]] ; then
|
||||
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${LITE_JAVA_PATH}/native/libs/linux_x86/
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ --info
|
||||
else
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test --info
|
||||
fi
|
||||
fi
|
||||
cp ${LITE_JAVA_PATH}/build/lib/jar/*.jar ${INSTALL_PREFIX}/${pkg_name}/runtime/lib/
|
||||
|
@ -174,11 +178,15 @@ build_lite_aarch64_jni_and_jar() {
|
|||
echo "---------------- mindspore lite: build jni arm64 failed----------------"
|
||||
exit 1
|
||||
fi
|
||||
rm -f ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_aarch64/*.so*
|
||||
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/java/linux_aarch64/libs/
|
||||
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/native/libs/linux_aarch64/
|
||||
cp ./libmindspore-lite-jni.so ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_aarch64/
|
||||
cp ./libmindspore-lite-jni.so ${BASEPATH}/output/tmp/${pkg_name}/runtime/lib/
|
||||
cp ${BASEPATH}/output/tmp/${pkg_name}/runtime/lib/libmindspore-lite.so ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_aarch64/
|
||||
if [[ "${MSLITE_ENABLE_SERVER_INFERENCE}" == "ON" || "${MSLITE_ENABLE_SERVER_INFERENCE}" == "on" ]] ; then
|
||||
cp ${BASEPATH}/output/tmp/${pkg_name}/runtime/third_party/glog/libglog.so* ${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite/linux_aarch64/libglog.so
|
||||
fi
|
||||
if [[ "X$is_train" = "Xon" ]]; then
|
||||
cp ./libmindspore-lite-train-jni.so ${LITE_JAVA_PATH}/java/linux_aarch64/libs/
|
||||
cp ./libmindspore-lite-train-jni.so ${LITE_JAVA_PATH}/native/libs/linux_aarch64/
|
||||
|
@ -214,13 +222,13 @@ build_lite_aarch64_jni_and_jar() {
|
|||
# build jar
|
||||
${gradle_command} clean -p ${LITE_JAVA_PATH}/
|
||||
if [[ "${ENABLE_ASAN}" == "ON" || "${ENABLE_ASAN}" == "on" ]] ; then
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test --info
|
||||
else
|
||||
if [[ "${MSLITE_ENABLE_TESTCASES}" == "ON" || "${MSLITE_ENABLE_TESTCASES}" == "on" ]] ; then
|
||||
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${LITE_JAVA_PATH}/native/libs/linux_aarch64/
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ --info
|
||||
else
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test --info
|
||||
fi
|
||||
fi
|
||||
cp ${LITE_JAVA_PATH}/build/lib/jar/*.jar ${BASEPATH}/output/tmp/${pkg_name}/runtime/lib/
|
||||
|
@ -668,11 +676,19 @@ build_lite_x86_64_aarch64_jar()
|
|||
|
||||
LITE_JAVA_PATH=${LITE_BASEPATH}/java
|
||||
local LITE_JAVA_NATIVE_RESOURCE_PATH=${LITE_JAVA_PATH}/src/main/resources/com/mindspore/lite
|
||||
rm -f ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_x86_64/*.so
|
||||
rm -f ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_aarch64/*.so
|
||||
cp ${x86_64_base_path}/tmp/${x86_64_package_name}/runtime/lib/*.so ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_x86_64/
|
||||
cp ${aarch64_base_path}/tmp/${aarch64_package_name}/runtime/lib/*.so ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_aarch64/
|
||||
echo "unzip tar success."
|
||||
rm -f ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_x86_64/*.so*
|
||||
rm -f ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_aarch64/*.so*
|
||||
cp ${x86_64_base_path}/tmp/${x86_64_package_name}/runtime/lib/libmindspore-lite.so ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_x86_64/
|
||||
cp ${x86_64_base_path}/tmp/${x86_64_package_name}/runtime/lib/libmindspore-lite-jni.so ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_x86_64/
|
||||
cp ${aarch64_base_path}/tmp/${aarch64_package_name}/runtime/lib/libmindspore-lite.so ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_aarch64/
|
||||
cp ${aarch64_base_path}/tmp/${aarch64_package_name}/runtime/lib/libmindspore-lite-jni.so ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_aarch64/
|
||||
|
||||
if [[ "${MSLITE_ENABLE_SERVER_INFERENCE}" == "ON" || "${MSLITE_ENABLE_SERVER_INFERENCE}" == "on" ]] ; then
|
||||
cp ${x86_64_base_path}/tmp/${x86_64_package_name}/runtime/third_party/glog/libglog.so* ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_x86_64/libglog.so
|
||||
cp ${aarch64_base_path}/tmp/${aarch64_package_name}/runtime/third_party/glog/libglog.so* ${LITE_JAVA_NATIVE_RESOURCE_PATH}/linux_aarch64/libglog.so
|
||||
else
|
||||
echo -e "\e[31mMSLITE_ENABLE_SERVER_INFERENCE is not set, so glog libs will not be compiled \e[0m"
|
||||
fi
|
||||
|
||||
# compile jar package
|
||||
rm -rf ${LITE_JAVA_PATH}/build
|
||||
|
@ -688,13 +704,13 @@ build_lite_x86_64_aarch64_jar()
|
|||
|
||||
${gradle_command} clean -p ${LITE_JAVA_PATH}/
|
||||
if [[ "${ENABLE_ASAN}" == "ON" || "${ENABLE_ASAN}" == "on" ]] ; then
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test --info
|
||||
else
|
||||
if [[ "${MSLITE_ENABLE_TESTCASES}" == "ON" || "${MSLITE_ENABLE_TESTCASES}" == "on" ]] ; then
|
||||
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${LITE_JAVA_PATH}/native/libs/linux_x86/
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ --info
|
||||
else
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test
|
||||
${gradle_command} releaseJar -p ${LITE_JAVA_PATH}/ -x test --info
|
||||
fi
|
||||
fi
|
||||
echo "compile jar success."
|
||||
|
|
|
@ -20,16 +20,6 @@
|
|||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/lib/mindspore-lite-java.jar</systemPath>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.huawei.mindspore</groupId>
|
||||
<artifactId>lite</artifactId>
|
||||
<version>1.6.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -19,10 +19,13 @@ allprojects {
|
|||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
test {
|
||||
testLogging.showStandardStreams = true
|
||||
testLogging.exceptionFormat = 'full'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation 'junit:junit:4.13'
|
||||
implementation project(':common')
|
||||
|
||||
implementation fileTree(dir: "libs", include: ["*.jar"])
|
||||
implementation project(':linux_x86')
|
||||
|
|
|
@ -4,9 +4,6 @@ plugins {
|
|||
|
||||
archivesBaseName = 'mindspore-lite-java-common'
|
||||
|
||||
dependencies {
|
||||
implementation group: 'commons-io', name: 'commons-io', version: '2.5'
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
|
|
@ -1,97 +0,0 @@
|
|||
package com.mindspore.lite;
|
||||
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
public class NativeLibrary {
|
||||
private static final Logger LOGGER = Logger.getLogger(NativeLibrary.class.toString());
|
||||
|
||||
private static final String MINDSPORE_LITE_LIBNAME = "mindspore-lite";
|
||||
private static final String MINDSPORE_LITE_JNI_LIBNAME = "mindspore-lite-jni";
|
||||
|
||||
private static final String MINDSPORE_LITE_LIBS = "mindspore_lite_libs";
|
||||
|
||||
public static void load() {
|
||||
if (isLoaded() || tryLoadLibrary()) {
|
||||
return;
|
||||
}
|
||||
loadLib(makeResourceDir(MINDSPORE_LITE_LIBS), makeResourceName("lib" + MINDSPORE_LITE_LIBNAME + ".so"));
|
||||
loadLib(makeResourceDir(MINDSPORE_LITE_LIBS), makeResourceName("lib" + MINDSPORE_LITE_JNI_LIBNAME + ".so"));
|
||||
}
|
||||
|
||||
private static boolean tryLoadLibrary() {
|
||||
try {
|
||||
System.loadLibrary(MINDSPORE_LITE_LIBNAME);
|
||||
System.loadLibrary(MINDSPORE_LITE_JNI_LIBNAME);
|
||||
LOGGER.info("LoadLibrary: success");
|
||||
return true;
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
LOGGER.warning("tryLoadLibraryFailed: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isLoaded() {
|
||||
try {
|
||||
Version.version();
|
||||
LOGGER.info("isLoaded: true");
|
||||
return true;
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLib(String jniResourceDir, String jniResourceName) {
|
||||
final Integer BUFFER_SIZE = 8024;
|
||||
final String TMPDIR_PROPERTY = "java.io.tmpdir";
|
||||
try {
|
||||
InputStream in = NativeLibrary.class.getClassLoader().getResourceAsStream(jniResourceName);
|
||||
String tmpPath = System.getProperty(TMPDIR_PROPERTY) + "/" + jniResourceDir;
|
||||
File fileOutDir = new File(tmpPath);
|
||||
if (!fileOutDir.exists()) {
|
||||
fileOutDir.mkdirs();
|
||||
}
|
||||
File fileOut = new File(tmpPath + jniResourceName.substring(jniResourceName.lastIndexOf("/") + 1));
|
||||
if (!fileOut.exists()) {
|
||||
fileOut.createNewFile();
|
||||
}
|
||||
|
||||
OutputStream out = new FileOutputStream(fileOut);
|
||||
IOUtils.copy(in, out, BUFFER_SIZE);
|
||||
in.close();
|
||||
out.close();
|
||||
System.load(fileOut.getAbsolutePath());
|
||||
LOGGER.info(String.format("load file: %s success.", fileOut.getAbsolutePath()));
|
||||
} catch (IOException e) {
|
||||
throw new UnsatisfiedLinkError(String.format(
|
||||
"Unable to extract native library into a temporary file (%s)", e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get native lib dir, eg. temp/linux_x86_64
|
||||
* @param dir
|
||||
* @return
|
||||
*/
|
||||
private static String makeResourceDir(String dir) {
|
||||
return dir + String.format("linux_%s/", architecture());
|
||||
}
|
||||
|
||||
/**
|
||||
* get native lib file name, eg. com/mindspore/lite/linux_x86_64/libmindspore-lite.so
|
||||
* @param basename
|
||||
* @return
|
||||
*/
|
||||
private static String makeResourceName(String basename) {
|
||||
return "com/mindspore/lite/" + String.format("linux_%s/", architecture()) + basename;
|
||||
}
|
||||
|
||||
private static String architecture() {
|
||||
final String arch = System.getProperty("os.arch").toLowerCase();
|
||||
return (arch.equals("amd64")) ? "x86_64" : arch;
|
||||
}
|
||||
}
|
|
@ -23,15 +23,8 @@ package com.mindspore.lite;
|
|||
*/
|
||||
public class Version {
|
||||
static {
|
||||
try {
|
||||
NativeLibrary.load();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load MindSporLite native library.");
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
System.loadLibrary("mindspore-lite-jni");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get MindSpore Lite version info.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
* Copyright 2022 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.
|
||||
|
@ -16,7 +16,6 @@
|
|||
|
||||
package com.mindspore.lite.config;
|
||||
|
||||
import com.mindspore.lite.NativeLibrary;
|
||||
|
||||
/**
|
||||
* MSConfig is defined for holding environment variables during runtime.
|
||||
|
@ -25,13 +24,7 @@ import com.mindspore.lite.NativeLibrary;
|
|||
*/
|
||||
public class MSConfig {
|
||||
static {
|
||||
try {
|
||||
NativeLibrary.load();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load MindSporLite native library.");
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
System.loadLibrary("mindspore-lite-jni");
|
||||
}
|
||||
|
||||
private long msConfigPtr;
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package com.mindspore.config;
|
||||
|
||||
import com.mindspore.lite.NativeLibrary;
|
||||
|
||||
public class TrainCfg {
|
||||
// depend "mindspore-lite-train-jni"
|
||||
static {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2021 Huawei Technologies Co., Ltd
|
||||
* Copyright 2022 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.
|
||||
|
@ -26,7 +26,7 @@ import com.mindspore.lite.NativeLibrary;
|
|||
public class Version {
|
||||
static {
|
||||
try {
|
||||
NativeLibrary.load();
|
||||
NativeLibrary.loadLibs();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load MindSporLite native library.");
|
||||
e.printStackTrace();
|
||||
|
|
|
@ -0,0 +1,117 @@
|
|||
package com.mindspore.lite;
|
||||
|
||||
import com.mindspore.config.Version;
|
||||
import java.io.*;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class NativeLibrary {
|
||||
private static final Logger LOGGER = Logger.getLogger(NativeLibrary.class.toString());
|
||||
|
||||
private static final String GLOG_LIBNAME = "glog";
|
||||
private static final String MINDSPORE_LITE_LIBNAME = "mindspore-lite";
|
||||
private static final String MINDSPORE_LITE_JNI_LIBNAME = "mindspore-lite-jni";
|
||||
|
||||
public static void load() {
|
||||
if (isLibLoaded() || loadLibrary()) {
|
||||
LOGGER.info("Native lib has been loaded.");
|
||||
return;
|
||||
}
|
||||
loadLibs();
|
||||
}
|
||||
|
||||
public static void loadLibs() {
|
||||
loadLib(makeResourceName("lib" + GLOG_LIBNAME + ".so"));
|
||||
loadLib(makeResourceName("lib" + MINDSPORE_LITE_LIBNAME + ".so"));
|
||||
loadLib(makeResourceName("lib" + MINDSPORE_LITE_JNI_LIBNAME + ".so"));
|
||||
}
|
||||
|
||||
private static boolean isLibLoaded() {
|
||||
try {
|
||||
Version.version();
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean loadLibrary() {
|
||||
try {
|
||||
System.loadLibrary(GLOG_LIBNAME);
|
||||
System.loadLibrary(MINDSPORE_LITE_LIBNAME);
|
||||
System.loadLibrary(MINDSPORE_LITE_JNI_LIBNAME);
|
||||
LOGGER.info("loadLibrary: success");
|
||||
return true;
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
LOGGER.warning("tryLoadLibraryFailed: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void loadLib(String libResourceName) {
|
||||
LOGGER.info("start load libResourceName: " + libResourceName);
|
||||
final InputStream libResource = NativeLibrary.class.getClassLoader().getResourceAsStream(libResourceName);
|
||||
if (libResource == null) {
|
||||
LOGGER.warning(String.format("lib file: %s not exist.", libResourceName));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final File tmpDir = mkTmpDir();
|
||||
String libName = libResourceName.substring(libResourceName.lastIndexOf("/") + 1);
|
||||
tmpDir.deleteOnExit();
|
||||
|
||||
//copy file to tmpFile
|
||||
final File tmpFile = new File(tmpDir.getCanonicalPath(), libName);
|
||||
tmpFile.deleteOnExit();
|
||||
LOGGER.info(String.format("extract %d bytes to %s", copyLib(libResource, tmpFile), tmpFile));
|
||||
System.load(tmpFile.toString());
|
||||
} catch (IOException e) {
|
||||
throw new UnsatisfiedLinkError(
|
||||
String.format("extract library into tmp file (%s) failed.", e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static long copyLib(InputStream libResource, File tmpFile) throws IOException{
|
||||
try (FileOutputStream outputStream = new FileOutputStream(tmpFile);) {
|
||||
// 1MB
|
||||
byte[] buffer = new byte[1 << 20];
|
||||
long byteCnt = 0;
|
||||
int n = 0;
|
||||
while ((n = libResource.read(buffer)) >= 0) {
|
||||
outputStream.write(buffer, 0, n);
|
||||
byteCnt += n;
|
||||
}
|
||||
return byteCnt;
|
||||
} finally {
|
||||
libResource.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static File mkTmpDir() {
|
||||
final String MINDSPORE_LITE_LIBS = "mindspore_lite_libs-";
|
||||
Long timestamp = System.currentTimeMillis();
|
||||
String dirName = MINDSPORE_LITE_LIBS + timestamp + "-";
|
||||
for (int i = 0; i < 10; i++) {
|
||||
File tmpDir = new File(new File(System.getProperty("java.io.tmpdir")), dirName + i);
|
||||
if (tmpDir.mkdir()) {
|
||||
return tmpDir;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("create tmp dir failed, dirName: " + dirName);
|
||||
}
|
||||
|
||||
/**
|
||||
* get native lib file name, eg. com/mindspore/lite/linux_x86_64/libmindspore-lite.so
|
||||
*
|
||||
* @param basename
|
||||
* @return
|
||||
*/
|
||||
private static String makeResourceName(String basename) {
|
||||
return "com/mindspore/lite/" + String.format("linux_%s/", architecture()) + basename;
|
||||
}
|
||||
|
||||
private static String architecture() {
|
||||
final String arch = System.getProperty("os.arch").toLowerCase();
|
||||
return (arch.equals("amd64")) ? "x86_64" : arch;
|
||||
}
|
||||
}
|
|
@ -16,10 +16,8 @@
|
|||
|
||||
package com.mindspore;
|
||||
|
||||
import com.mindspore.config.DataType;
|
||||
import com.mindspore.config.DeviceType;
|
||||
import com.mindspore.config.MSContext;
|
||||
import com.mindspore.config.TrainCfg;
|
||||
import com.mindspore.config.*;
|
||||
import com.mindspore.lite.NativeLibrary;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
@ -44,6 +42,14 @@ public class ModelTest {
|
|||
|
||||
@Test
|
||||
public void testBuildByGraphSuccess() {
|
||||
try {
|
||||
NativeLibrary.load();
|
||||
System.err.println("System: NativeLibrary load success.");
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load MindSporLite native library.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(Version.version());
|
||||
Graph g = new Graph();
|
||||
assertTrue(g.load("../test/ut/src/runtime/kernel/arm/test_data/nets/lenet_train.ms"));
|
||||
MSContext context = new MSContext();
|
||||
|
|
Loading…
Reference in New Issue