Skip to content

Commit 3083980

Browse files
authored
Add ExecuTorchRuntime.java skeleton
Differential Revision: D75180081 Pull Request resolved: #11058
1 parent 3f19793 commit 3083980

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

extension/android/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ non_fbcode_target(_kind = fb_android_library,
88
srcs = [
99
"executorch_android/src/main/java/org/pytorch/executorch/DType.java",
1010
"executorch_android/src/main/java/org/pytorch/executorch/EValue.java",
11+
"executorch_android/src/main/java/org/pytorch/executorch/ExecuTorchRuntime.java",
1112
"executorch_android/src/main/java/org/pytorch/executorch/MethodMetadata.java",
1213
"executorch_android/src/main/java/org/pytorch/executorch/Module.java",
1314
"executorch_android/src/main/java/org/pytorch/executorch/Tensor.java",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
package org.pytorch.executorch;
10+
11+
import com.facebook.soloader.nativeloader.NativeLoader;
12+
import com.facebook.soloader.nativeloader.SystemDelegate;
13+
14+
/** Class for entire ExecuTorch Runtime related functions. */
15+
public class ExecuTorchRuntime {
16+
17+
static {
18+
if (!NativeLoader.isInitialized()) {
19+
NativeLoader.init(new SystemDelegate());
20+
}
21+
// Loads libexecutorch.so from jniLibs
22+
NativeLoader.loadLibrary("executorch");
23+
}
24+
25+
private static final ExecuTorchRuntime sInstance = new ExecuTorchRuntime();
26+
27+
private ExecuTorchRuntime() {}
28+
29+
/** Get the runtime instance. */
30+
public static ExecuTorchRuntime getRuntime() {
31+
return sInstance;
32+
}
33+
}

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import android.util.Log;
1212
import com.facebook.jni.HybridData;
1313
import com.facebook.jni.annotations.DoNotStrip;
14-
import com.facebook.soloader.nativeloader.NativeLoader;
15-
import com.facebook.soloader.nativeloader.SystemDelegate;
1614
import java.io.File;
1715
import java.util.HashMap;
1816
import java.util.Map;
@@ -28,14 +26,6 @@
2826
@Experimental
2927
public class Module {
3028

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

@@ -57,6 +47,8 @@ private static native HybridData initHybrid(
5747
String moduleAbsolutePath, int loadMode, int initHybrid);
5848

5949
private Module(String moduleAbsolutePath, int loadMode, int numThreads) {
50+
ExecuTorchRuntime runtime = ExecuTorchRuntime.getRuntime();
51+
6052
mHybridData = initHybrid(moduleAbsolutePath, loadMode, numThreads);
6153

6254
mMethodMetadata = populateMethodMeta();

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
import com.facebook.jni.HybridData;
1212
import com.facebook.jni.annotations.DoNotStrip;
13-
import com.facebook.soloader.nativeloader.NativeLoader;
14-
import com.facebook.soloader.nativeloader.SystemDelegate;
1513
import java.io.File;
14+
15+
import org.pytorch.executorch.ExecuTorchRuntime;
1616
import org.pytorch.executorch.annotations.Experimental;
1717

1818
/**
@@ -27,13 +27,6 @@ public class LlmModule {
2727
public static final int MODEL_TYPE_TEXT = 1;
2828
public static final int MODEL_TYPE_TEXT_VISION = 2;
2929

30-
static {
31-
if (!NativeLoader.isInitialized()) {
32-
NativeLoader.init(new SystemDelegate());
33-
}
34-
NativeLoader.loadLibrary("executorch");
35-
}
36-
3730
private final HybridData mHybridData;
3831
private static final int DEFAULT_SEQ_LEN = 128;
3932
private static final boolean DEFAULT_ECHO = true;
@@ -48,6 +41,8 @@ private static native HybridData initHybrid(
4841
*/
4942
public LlmModule(
5043
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath) {
44+
ExecuTorchRuntime runtime = ExecuTorchRuntime.getRuntime();
45+
5146
File modelFile = new File(modulePath);
5247
if (!modelFile.canRead() || !modelFile.isFile()) {
5348
throw new RuntimeException("Cannot load model path " + modulePath);

0 commit comments

Comments
 (0)