Skip to content
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

feat(android): build for x86_64 platform #15

Merged
merged 2 commits into from
Sep 4, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ iOS:
- It's also not supported in iOS simulator due to [this limitation](https://developer.apple.com/documentation/metal/developing_metal_apps_that_run_in_simulator#3241609), we used constant buffers more than 14.

Android:
- Currently only supported arm64-v8a platform, this means you can't initialize a context on another platforms.
- Currently only supported arm64-v8a / x86_64 platform, this means you can't initialize a context on another platforms. The 64-bit platform are recommended because it can allocate more memory for the model.

## Contributing

Expand Down
13 changes: 10 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["RNLlama_" + name]).toInteger()
}

def reactNativeArchitectures() {
def value = project.getProperties().get("reactNativeArchitectures")
def archs = value ? value.split(",") : ["x86_64", "arm64-v8a"]
return archs.findAll { it != "armeabi-v7a" && it != "x86" } // Not building for 32-bit architectures
}

android {
ndkVersion getExtOrDefault("ndkVersion")
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
Expand All @@ -38,9 +44,10 @@ android {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()

ndk {
abiFilters 'arm64-v8a' // , 'armeabi-v7a'
externalNativeBuild {
cmake {
abiFilters (*reactNativeArchitectures())
}
}
}
externalNativeBuild {
Expand Down
10 changes: 3 additions & 7 deletions android/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@ set(
${CMAKE_SOURCE_DIR}/jni.cpp
)

if (${ANDROID_ABI} STREQUAL "arm64-v8a")
set(RNLLAMA_LIBRARY_NAME rnllama_arm64)
endif ()
set(RNLLAMA_LIBRARY_NAME rnllama)

add_library(
${RNLLAMA_LIBRARY_NAME}
SHARED
${SOURCE_FILES}
)

if (${ANDROID_ABI} STREQUAL "arm64-v8a")
# target_compile_options(${RNLLAMA_LIBRARY_NAME} PRIVATE -mcpu=native)
endif ()
find_library(LOG_LIB log)

target_link_libraries(${RNLLAMA_LIBRARY_NAME} log android)
target_link_libraries(${RNLLAMA_LIBRARY_NAME} ${LOG_LIB} android)
include_directories(${RNLLAMA_LIB_DIR})

target_compile_options(${RNLLAMA_LIBRARY_NAME} PRIVATE -DLM_GGML_USE_K_QUANTS -pthread)
Expand Down
16 changes: 9 additions & 7 deletions android/src/main/java/com/rnllama/LlamaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class LlamaContext {
private DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter;

public LlamaContext(int id, ReactApplicationContext reactContext, ReadableMap params) {
if (LlamaContext.isArmeabiV8a() == false) {
throw new IllegalStateException("Only arm64-v8a is supported");
if (LlamaContext.isArm64V8a() == false || LlamaContext.isX86_64() == false) {
throw new IllegalStateException("Only 64-bit architectures are supported");
}
if (!params.hasKey("model")) {
throw new IllegalArgumentException("Missing required parameter: model");
Expand Down Expand Up @@ -191,17 +191,19 @@ public void release() {
}

static {
Log.d(NAME, "Primary ABI: " + Build.SUPPORTED_ABIS[0]);
if (isArmeabiV8a()) {
Log.d(NAME, "Loading librnllama_arm64.so");
System.loadLibrary("rnllama_arm64");
if (LlamaContext.isArm64V8a() == true || LlamaContext.isX86_64() == true) {
System.loadLibrary("rnllama");
}
}

private static boolean isArmeabiV8a() {
private static boolean isArm64V8a() {
return Build.SUPPORTED_ABIS[0].equals("arm64-v8a");
}

private static boolean isX86_64() {
return Build.SUPPORTED_ABIS[0].equals("x86_64");
}

private static String cpuInfo() {
File file = new File("/proc/cpuinfo");
StringBuilder stringBuilder = new StringBuilder();
Expand Down
Loading