Skip to content

Commit

Permalink
Add more debug long for engine loading
Browse files Browse the repository at this point in the history
Change-Id: I17a68cbd1e16021d7452e3de267c06ba9c9865cc
  • Loading branch information
frankfliu committed Jun 17, 2020
1 parent eefb711 commit b4d31d9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 29 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/ai/djl/engine/Engine.java
Expand Up @@ -45,14 +45,18 @@ private static synchronized String initEngine() {
for (EngineProvider provider : loaders) {
Engine engine = provider.getEngine();
if (engine != null) {
logger.debug("Engine loaded from provider: {}", engine.getEngineName());
if (firstEngine == null) {
firstEngine = engine;
}
ALL_ENGINES.put(engine.getEngineName(), engine);
} else {
logger.warn("Failed to load engine from: {}", provider.getClass().getName());
}
}

if (firstEngine == null) {
logger.debug("No engine found from EngineProvider");
return null;
}

Expand Down
21 changes: 14 additions & 7 deletions mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/jna/LibUtils.java
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -102,27 +103,29 @@ private static String findOverrideLibrary() {
}

private static synchronized String findLibraryInClasspath() {
List<URL> urls;
Enumeration<URL> urls;
try {
urls =
Collections.list(
Thread.currentThread()
.getContextClassLoader()
.getResources("native/lib/mxnet.properties"));
Thread.currentThread()
.getContextClassLoader()
.getResources("native/lib/mxnet.properties");
} catch (IOException e) {
logger.warn("", e);
return null;
}

// No native jars
if (urls.isEmpty()) {
if (!urls.hasMoreElements()) {
logger.debug("mxnet.properties not found in class path.");
return null;
}

Platform systemPlatform = Platform.fromSystem();
try {
Platform matching = null;
Platform placeholder = null;
for (URL url : urls) {
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Platform platform = Platform.fromUrl(url);
if (platform.isPlaceholder()) {
placeholder = platform;
Expand Down Expand Up @@ -157,6 +160,8 @@ private static String loadLibraryFromClasspath(Platform platform) {
try {
String libName = System.mapLibraryName(LIB_NAME);
Path cacheFolder = getCacheDir();
logger.debug("Using cache dir: {}", cacheFolder);

Path dir = cacheFolder.resolve(platform.getVersion() + platform.getClassifier());
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand All @@ -167,6 +172,7 @@ private static String loadLibraryFromClasspath(Platform platform) {
for (String file : platform.getLibraries()) {
String libPath = "/native/lib/" + file;
try (InputStream is = LibUtils.class.getResourceAsStream(libPath)) {
logger.info("Extracting {} to cache ...", file);
Files.copy(is, tmp.resolve(file), StandardCopyOption.REPLACE_EXISTING);
}
}
Expand Down Expand Up @@ -262,6 +268,7 @@ private static String downloadMxnet(Platform platform) throws IOException {

String libName = System.mapLibraryName(LIB_NAME);
Path cacheFolder = getCacheDir();
logger.debug("Using cache dir: {}", cacheFolder);
Path dir = cacheFolder.resolve(version + flavor + '-' + classifier);
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand Down
Expand Up @@ -23,7 +23,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -64,13 +64,13 @@ public static void prepareLibrary() {
if (libPath == null) {
throw new IllegalStateException("ONNX Runtime Library now found!");
}
String nativeFileName = System.mapLibraryName(NATIVE_LIB_NAME);
String jniName = System.mapLibraryName(LIB_NAME);
System.setProperty(
"onnxruntime.native." + LIB_NAME + ".path", libPath.resolve(jniName).toString());
System.setProperty(
"onnxruntime.native." + NATIVE_LIB_NAME + ".path",
libPath.resolve(nativeFileName).toString());

String jniPath = libPath.resolve(System.mapLibraryName(LIB_NAME)).toString();
String nativePath = libPath.resolve(System.mapLibraryName(NATIVE_LIB_NAME)).toString();
System.setProperty("onnxruntime.native." + LIB_NAME + ".path", jniPath);
System.setProperty("onnxruntime.native." + NATIVE_LIB_NAME + ".path", nativePath);
logger.debug("Loading onnxruntime JNI from: {}", jniPath);
logger.debug("Loading onnxruntime native library from: {}", nativePath);
}

private static Path findOverrideLibrary() {
Expand Down Expand Up @@ -111,26 +111,28 @@ private static Path findLibraryInPath(String libPath) {
}

private static synchronized Path findNativeLibrary() {
List<URL> urls;
Enumeration<URL> urls;
try {
urls =
Collections.list(
Thread.currentThread()
.getContextClassLoader()
.getResources("native/lib/onnxruntime.properties"));
Thread.currentThread()
.getContextClassLoader()
.getResources("native/lib/onnxruntime.properties");
} catch (IOException e) {
logger.warn("", e);
return null;
}
// No native jars
if (urls.isEmpty()) {
if (!urls.hasMoreElements()) {
logger.debug("onnxruntime.properties not found in class path.");
return null;
}

Platform systemPlatform = Platform.fromSystem();
try {
Platform matching = null;
Platform placeholder = null;
for (URL url : urls) {
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Platform platform = Platform.fromUrl(url);
if (platform.isPlaceholder()) {
placeholder = platform;
Expand Down Expand Up @@ -168,6 +170,7 @@ private static Path copyNativeLibraryFromClasspath(Platform platform) {
try {
String libName = System.mapLibraryName(NATIVE_LIB_NAME);
Path cacheDir = getCacheDir();
logger.debug("Using cache dir: {}", cacheDir);
Path dir = cacheDir.resolve(version + flavor + '-' + classifier);
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand Down Expand Up @@ -205,6 +208,7 @@ private static Path downloadOnnxRuntime(Platform platform) throws IOException {

String libName = System.mapLibraryName(NATIVE_LIB_NAME);
Path cacheDir = getCacheDir();
logger.debug("Using cache dir: {}", cacheDir);
Path dir = cacheDir.resolve(version + flavor + '-' + classifier);
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -191,26 +192,27 @@ private static String copyJniLibraryFromClasspath(Path nativeDir) {
}

private static synchronized String findNativeLibrary() {
List<URL> urls;
Enumeration<URL> urls;
try {
urls =
Collections.list(
Thread.currentThread()
.getContextClassLoader()
.getResources("native/lib/pytorch.properties"));
Thread.currentThread()
.getContextClassLoader()
.getResources("native/lib/pytorch.properties");
} catch (IOException e) {
logger.warn("", e);
return null;
}
// No native jars
if (urls.isEmpty()) {
if (!urls.hasMoreElements()) {
return null;
}

Platform systemPlatform = Platform.fromSystem();
try {
Platform matching = null;
Platform placeholder = null;
for (URL url : urls) {
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Platform platform = Platform.fromUrl(url);
if (platform.isPlaceholder()) {
placeholder = platform;
Expand Down Expand Up @@ -247,6 +249,7 @@ private static String copyNativeLibraryFromClasspath(Platform platform) {
try {
String libName = System.mapLibraryName(NATIVE_LIB_NAME);
Path cacheDir = getCacheDir();
logger.debug("Using cache dir: {}", cacheDir);
Path dir = cacheDir.resolve(version + flavor + '-' + classifier);
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand Down Expand Up @@ -284,6 +287,7 @@ private static String downloadPyTorch(Platform platform) throws IOException {

String libName = System.mapLibraryName(NATIVE_LIB_NAME);
Path cacheDir = getCacheDir();
logger.debug("Using cache dir: {}", cacheDir);
Path dir = cacheDir.resolve(version + flavor + '-' + classifier);
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand Down
Expand Up @@ -59,6 +59,7 @@ private static String getTensorFlowLib() {
.getResource("native/lib/tensorflow.properties");
if (url == null) {
// defer to tensorflow-core-api to handle loading native library.
logger.debug("tensorflow.properties not found in class path.");
return null;
}

Expand Down Expand Up @@ -88,6 +89,7 @@ private static String downloadTensorFlow(Platform platform) throws IOException {

String libName = System.mapLibraryName(LIB_NAME);
Path cacheDir = getCacheDir();
logger.debug("Using cache dir: {}", cacheDir);
Path dir = cacheDir.resolve(version + '-' + flavor + '-' + classifier);
Path path = dir.resolve(libName);
if (Files.exists(path)) {
Expand Down

0 comments on commit b4d31d9

Please sign in to comment.