Skip to content

[executorch][android] Add Runtime.java to centralize native library l… #10672

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.pytorch.executorch;

import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
import com.facebook.jni.annotations.DoNotStrip;

public class Runtime {
private static boolean initialized = false;

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

public static boolean isInitialized() {
return initialized;
}

@DoNotStrip
public static native String[] getRegisteredOps();

@DoNotStrip
public static native String[] getRegisteredBackends();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.facebook.soloader.nativeloader.NativeLoader;
import com.facebook.soloader.nativeloader.SystemDelegate;
import org.pytorch.executorch.annotations.Experimental;
import org.pytorch.executorch.Runtime;

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

static {
if (!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
if (!Runtime.isInitialized()) {
throw new IllegalStateException("ExecuTorch runtime not initialized.");
}
NativeLoader.loadLibrary("executorch");
}

private final HybridData mHybridData;
private static final int DEFAULT_SEQ_LEN = 128;
Expand Down
Loading