Skip to content

Commit 1ac9393

Browse files
author
Lefteris Gilmaz
committed
[executorch][android] Add Runtime.java to centralize native library loading
1 parent b504ba2 commit 1ac9393

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.pytorch.executorch;
2+
3+
import com.facebook.soloader.nativeloader.NativeLoader;
4+
import com.facebook.soloader.nativeloader.SystemDelegate;
5+
import com.facebook.jni.annotations.DoNotStrip;
6+
7+
public class Runtime {
8+
private static boolean initialized = false;
9+
10+
static {
11+
if (!NativeLoader.isInitialized()) {
12+
NativeLoader.init(new SystemDelegate());
13+
}
14+
// Loads libexecutorch.so from jniLibs
15+
NativeLoader.loadLibrary("executorch");
16+
initialized = true;
17+
}
18+
19+
public static boolean isInitialized() {
20+
return initialized;
21+
}
22+
23+
@DoNotStrip
24+
public static native String[] getRegisteredOps();
25+
26+
@DoNotStrip
27+
public static native String[] getRegisteredBackends();
28+
}

extension/android/executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModule.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.facebook.soloader.nativeloader.NativeLoader;
1414
import com.facebook.soloader.nativeloader.SystemDelegate;
1515
import org.pytorch.executorch.annotations.Experimental;
16+
import org.pytorch.executorch.Runtime;
1617

1718
/**
1819
* LlmModule is a wrapper around the Executorch LLM. It provides a simple interface to generate text
@@ -27,11 +28,9 @@ public class LlmModule {
2728
public static final int MODEL_TYPE_TEXT_VISION = 2;
2829

2930
static {
30-
if (!NativeLoader.isInitialized()) {
31-
NativeLoader.init(new SystemDelegate());
31+
if (!Runtime.isInitialized()) {
32+
throw new IllegalStateException("ExecuTorch runtime not initialized.");
3233
}
33-
NativeLoader.loadLibrary("executorch");
34-
}
3534

3635
private final HybridData mHybridData;
3736
private static final int DEFAULT_SEQ_LEN = 128;

0 commit comments

Comments
 (0)