Skip to content

Commit df90bdd

Browse files
committed
Get rid of NativePeer.java
Adds an additional layer without clear usage
1 parent 2ede762 commit df90bdd

File tree

5 files changed

+43
-118
lines changed

5 files changed

+43
-118
lines changed

examples/demo-apps/android/LlamaDemo/docs/delegates/xnnpack_README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ This is running the shell script which configures the required core ExecuTorch,
172172
```
173173
bash examples/demo-apps/android/LlamaDemo/download_prebuilt_lib.sh
174174
```
175-
The prebuilt AAR library contains the Java library and the JNI binding for NativePeer.java and ExecuTorch native library, including core ExecuTorch runtime libraries, XNNPACK backend, Portable kernels, Optimized kernels, and Quantized kernels. It comes with two ABI variants, arm64-v8a and x86_64.
175+
The prebuilt AAR library contains the Java library and the JNI binding for Module.java and ExecuTorch native library, including core ExecuTorch runtime libraries, XNNPACK backend, Portable kernels, Optimized kernels, and Quantized kernels. It comes with two ABI variants, arm64-v8a and x86_64.
176176
If you need to use other dependencies (like tokenizer), please build from the local machine option.
177177

178178
## Run the Android Demo App

extension/android/BUCK

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ non_fbcode_target(_kind = fb_android_library,
99
"executorch_android/src/main/java/org/pytorch/executorch/DType.java",
1010
"executorch_android/src/main/java/org/pytorch/executorch/EValue.java",
1111
"executorch_android/src/main/java/org/pytorch/executorch/Module.java",
12-
"executorch_android/src/main/java/org/pytorch/executorch/NativePeer.java",
1312
"executorch_android/src/main/java/org/pytorch/executorch/Tensor.java",
1413
"executorch_android/src/main/java/org/pytorch/executorch/annotations/Experimental.java",
1514
],

extension/android/executorch_android/src/main/java/org/pytorch/executorch/Module.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
package org.pytorch.executorch;
1010

1111
import android.util.Log;
12+
13+
import com.facebook.jni.HybridData;
14+
import com.facebook.jni.annotations.DoNotStrip;
1215
import com.facebook.soloader.nativeloader.NativeLoader;
1316
import com.facebook.soloader.nativeloader.SystemDelegate;
1417
import java.io.File;
@@ -24,6 +27,14 @@
2427
@Experimental
2528
public class Module {
2629

30+
static {
31+
if (!NativeLoader.isInitialized()) {
32+
NativeLoader.init(new SystemDelegate());
33+
}
34+
// Loads libexecutorch.so from jniLibs
35+
NativeLoader.loadLibrary("executorch");
36+
}
37+
2738
/** Load mode for the module. Load the whole file as a buffer. */
2839
public static final int LOAD_MODE_FILE = 0;
2940

@@ -36,10 +47,16 @@ public class Module {
3647
/** Load mode for the module. Use memory locking and ignore errors. */
3748
public static final int LOAD_MODE_MMAP_USE_MLOCK_IGNORE_ERRORS = 3;
3849

39-
/** Reference to the NativePeer object of this module. */
40-
private NativePeer mNativePeer;
50+
private final HybridData mHybridData;
51+
52+
@DoNotStrip
53+
private static native HybridData initHybrid(String moduleAbsolutePath, int loadMode);
4154

42-
/** Lock protecting the non-thread safe methods in NativePeer. */
55+
private Module(String moduleAbsolutePath, int loadMode) {
56+
mHybridData = initHybrid(moduleAbsolutePath, loadMode);
57+
}
58+
59+
/** Lock protecting the non-thread safe methods in mHybridData. */
4360
private Lock mLock = new ReentrantLock();
4461

4562
/**
@@ -50,14 +67,11 @@ public class Module {
5067
* @return new {@link org.pytorch.executorch.Module} object which owns the model module.
5168
*/
5269
public static Module load(final String modelPath, int loadMode) {
53-
if (!NativeLoader.isInitialized()) {
54-
NativeLoader.init(new SystemDelegate());
55-
}
5670
File modelFile = new File(modelPath);
5771
if (!modelFile.canRead() || !modelFile.isFile()) {
5872
throw new RuntimeException("Cannot load model path " + modelPath);
5973
}
60-
return new Module(new NativePeer(modelPath, loadMode));
74+
return new Module(modelPath, loadMode);
6175
}
6276

6377
/**
@@ -70,10 +84,6 @@ public static Module load(final String modelPath) {
7084
return load(modelPath, LOAD_MODE_FILE);
7185
}
7286

73-
Module(NativePeer nativePeer) {
74-
this.mNativePeer = nativePeer;
75-
}
76-
7787
/**
7888
* Runs the 'forward' method of this module with the specified arguments.
7989
*
@@ -83,16 +93,7 @@ public static Module load(final String modelPath) {
8393
* @return return value from the 'forward' method.
8494
*/
8595
public EValue[] forward(EValue... inputs) {
86-
try {
87-
mLock.lock();
88-
if (mNativePeer == null) {
89-
Log.e("ExecuTorch", "Attempt to use a destroyed module");
90-
return new EValue[0];
91-
}
92-
return mNativePeer.forward(inputs);
93-
} finally {
94-
mLock.unlock();
95-
}
96+
return execute("forward", inputs);
9697
}
9798

9899
/**
@@ -105,16 +106,19 @@ public EValue[] forward(EValue... inputs) {
105106
public EValue[] execute(String methodName, EValue... inputs) {
106107
try {
107108
mLock.lock();
108-
if (mNativePeer == null) {
109+
if (!mHybridData.isValid()) {
109110
Log.e("ExecuTorch", "Attempt to use a destroyed module");
110111
return new EValue[0];
111112
}
112-
return mNativePeer.execute(methodName, inputs);
113+
return executeNative(methodName, inputs);
113114
} finally {
114115
mLock.unlock();
115116
}
116117
}
117118

119+
@DoNotStrip
120+
private native EValue[] executeNative(String methodName, EValue... inputs);
121+
118122
/**
119123
* Load a method on this module. This might help with the first time inference performance,
120124
* because otherwise the method is loaded lazily when it's execute. Note: this function is
@@ -127,30 +131,31 @@ public EValue[] execute(String methodName, EValue... inputs) {
127131
public int loadMethod(String methodName) {
128132
try {
129133
mLock.lock();
130-
if (mNativePeer == null) {
134+
if (!mHybridData.isValid()) {
131135
Log.e("ExecuTorch", "Attempt to use a destroyed module");
132136
return 0x2; // InvalidState
133137
}
134-
return mNativePeer.loadMethod(methodName);
138+
return loadMethodNative(methodName);
135139
} finally {
136140
mLock.unlock();
137141
}
138142
}
139143

144+
@DoNotStrip
145+
private native int loadMethodNative(String methodName);
146+
140147
/**
141148
* Returns the names of the methods in a certain method.
142149
*
143150
* @param methodName method name to query
144151
* @return an array of backend name
145152
*/
146-
public String[] getUsedBackends(String methodName) {
147-
return mNativePeer.getUsedBackends(methodName);
148-
}
153+
@DoNotStrip
154+
public native String[] getUsedBackends(String methodName);
149155

150156
/** Retrieve the in-memory log buffer, containing the most recent ExecuTorch log entries. */
151-
public String[] readLogBuffer() {
152-
return mNativePeer.readLogBuffer();
153-
}
157+
@DoNotStrip
158+
public native String[] readLogBuffer();
154159

155160
/**
156161
* Dump the ExecuTorch ETRecord file to /data/local/tmp/result.etdump.
@@ -160,9 +165,8 @@ public String[] readLogBuffer() {
160165
* @return true if the etdump was successfully written, false otherwise.
161166
*/
162167
@Experimental
163-
public boolean etdump() {
164-
return mNativePeer.etdump();
165-
}
168+
@DoNotStrip
169+
public native boolean etdump();
166170

167171
/**
168172
* Explicitly destroys the native Module object. Calling this method is not required, as the
@@ -173,13 +177,11 @@ public boolean etdump() {
173177
public void destroy() {
174178
if (mLock.tryLock()) {
175179
try {
176-
mNativePeer.resetNative();
180+
mHybridData.resetNative();
177181
} finally {
178-
mNativePeer = null;
179182
mLock.unlock();
180183
}
181184
} else {
182-
mNativePeer = null;
183185
Log.w(
184186
"ExecuTorch",
185187
"Destroy was called while the module was in use. Resources will not be immediately"

extension/android/executorch_android/src/main/java/org/pytorch/executorch/NativePeer.java

Lines changed: 0 additions & 68 deletions
This file was deleted.

extension/android/jni/jni_layer.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
223223
std::unique_ptr<Module> module_;
224224

225225
public:
226-
constexpr static auto kJavaDescriptor = "Lorg/pytorch/executorch/NativePeer;";
226+
constexpr static auto kJavaDescriptor = "Lorg/pytorch/executorch/Module;";
227227

228228
static facebook::jni::local_ref<jhybriddata> initHybrid(
229229
facebook::jni::alias_ref<jclass>,
@@ -271,13 +271,6 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
271271
#endif
272272
}
273273

274-
facebook::jni::local_ref<facebook::jni::JArrayClass<JEValue>> forward(
275-
facebook::jni::alias_ref<
276-
facebook::jni::JArrayClass<JEValue::javaobject>::javaobject>
277-
jinputs) {
278-
return execute_method("forward", jinputs);
279-
}
280-
281274
facebook::jni::local_ref<facebook::jni::JArrayClass<JEValue>> execute(
282275
facebook::jni::alias_ref<jstring> methodName,
283276
facebook::jni::alias_ref<
@@ -459,9 +452,8 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
459452
static void registerNatives() {
460453
registerHybrid({
461454
makeNativeMethod("initHybrid", ExecuTorchJni::initHybrid),
462-
makeNativeMethod("forward", ExecuTorchJni::forward),
463-
makeNativeMethod("execute", ExecuTorchJni::execute),
464-
makeNativeMethod("loadMethod", ExecuTorchJni::load_method),
455+
makeNativeMethod("executeNative", ExecuTorchJni::execute),
456+
makeNativeMethod("loadMethodNative", ExecuTorchJni::load_method),
465457
makeNativeMethod("readLogBuffer", ExecuTorchJni::readLogBuffer),
466458
makeNativeMethod("etdump", ExecuTorchJni::etdump),
467459
makeNativeMethod("getUsedBackends", ExecuTorchJni::getUsedBackends),

0 commit comments

Comments
 (0)