Skip to content

Add ExecuTorchRuntime.java skeleton #11058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extension/android/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ non_fbcode_target(_kind = fb_android_library,
srcs = [
"executorch_android/src/main/java/org/pytorch/executorch/DType.java",
"executorch_android/src/main/java/org/pytorch/executorch/EValue.java",
"executorch_android/src/main/java/org/pytorch/executorch/ExecuTorchRuntime.java",
"executorch_android/src/main/java/org/pytorch/executorch/MethodMetadata.java",
"executorch_android/src/main/java/org/pytorch/executorch/Module.java",
"executorch_android/src/main/java/org/pytorch/executorch/Tensor.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

package org.pytorch.executorch;

import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;

/** Class for entire ExecuTorch Runtime related functions. */
public class ExecuTorchRuntime {

static {
if (!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
}
// Loads libexecutorch.so from jniLibs
NativeLoader.loadLibrary("executorch");
}

private static final ExecuTorchRuntime sInstance = new ExecuTorchRuntime();

private ExecuTorchRuntime() {}

/** Get the runtime instance. */
public static ExecuTorchRuntime getRuntime() {
return sInstance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import android.util.Log;
import com.facebook.jni.HybridData;
import com.facebook.jni.annotations.DoNotStrip;
import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -28,14 +26,6 @@
@Experimental
public class Module {

static {
if (!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
}
// Loads libexecutorch.so from jniLibs
NativeLoader.loadLibrary("executorch");
}

/** Load mode for the module. Load the whole file as a buffer. */
public static final int LOAD_MODE_FILE = 0;

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

private Module(String moduleAbsolutePath, int loadMode, int numThreads) {
ExecuTorchRuntime runtime = ExecuTorchRuntime.getRuntime();

mHybridData = initHybrid(moduleAbsolutePath, loadMode, numThreads);

mMethodMetadata = populateMethodMeta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

import com.facebook.jni.HybridData;
import com.facebook.jni.annotations.DoNotStrip;
import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
import java.io.File;

import org.pytorch.executorch.ExecuTorchRuntime;
import org.pytorch.executorch.annotations.Experimental;

/**
Expand All @@ -27,13 +27,6 @@ public class LlmModule {
public static final int MODEL_TYPE_TEXT = 1;
public static final int MODEL_TYPE_TEXT_VISION = 2;

static {
if (!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
}
NativeLoader.loadLibrary("executorch");
}

private final HybridData mHybridData;
private static final int DEFAULT_SEQ_LEN = 128;
private static final boolean DEFAULT_ECHO = true;
Expand All @@ -48,6 +41,8 @@ private static native HybridData initHybrid(
*/
public LlmModule(
int modelType, String modulePath, String tokenizerPath, float temperature, String dataPath) {
ExecuTorchRuntime runtime = ExecuTorchRuntime.getRuntime();

File modelFile = new File(modulePath);
if (!modelFile.canRead() || !modelFile.isFile()) {
throw new RuntimeException("Cannot load model path " + modulePath);
Expand Down
Loading