diff --git a/README.md b/README.md index 24ad152..ad350b1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/android/build.gradle b/android/build.gradle index 9904ddf..d0b42ac 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -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") @@ -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 { diff --git a/android/src/main/CMakeLists.txt b/android/src/main/CMakeLists.txt index 6e3d843..7662d91 100644 --- a/android/src/main/CMakeLists.txt +++ b/android/src/main/CMakeLists.txt @@ -17,9 +17,7 @@ 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} @@ -27,11 +25,9 @@ add_library( ${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) diff --git a/android/src/main/java/com/rnllama/LlamaContext.java b/android/src/main/java/com/rnllama/LlamaContext.java index 945c0e4..f2fba43 100644 --- a/android/src/main/java/com/rnllama/LlamaContext.java +++ b/android/src/main/java/com/rnllama/LlamaContext.java @@ -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"); @@ -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();