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: enable ggml-rpc #81

Open
wants to merge 5 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
4 changes: 3 additions & 1 deletion android/src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(
${RNLLAMA_LIB_DIR}/ggml-alloc.c
${RNLLAMA_LIB_DIR}/ggml-backend.cpp
${RNLLAMA_LIB_DIR}/ggml.c
${RNLLAMA_LIB_DIR}/ggml-rpc.cpp
${RNLLAMA_LIB_DIR}/ggml-quants.c
${RNLLAMA_LIB_DIR}/common.cpp
${RNLLAMA_LIB_DIR}/json.hpp
Expand Down Expand Up @@ -55,6 +56,7 @@ function(build_library target_name cpu_flags)
target_compile_options(${target_name} PRIVATE -O3 -DNDEBUG)
target_compile_options(${target_name} PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden)
target_compile_options(${target_name} PRIVATE -ffunction-sections -fdata-sections)
target_compile_options(${target_name} PRIVATE -DLM_GGML_USE_RPC)

target_link_options(${target_name} PRIVATE -Wl,--gc-sections)
target_link_options(${target_name} PRIVATE -Wl,--exclude-libs,ALL)
Expand All @@ -77,7 +79,7 @@ if (${ANDROID_ABI} STREQUAL "arm64-v8a")

# https://github.com/ggerganov/llama.cpp/blob/master/docs/android.md#cross-compile-using-android-ndk
# llama.cpp will deal with the cpu features
# build_library("rnllama_v8_7" "-march=armv8.7-a")
# build_library("rnllama_v8_7" "-march=armv8.7-a")
# TODO: Add support runtime check for cpu features
# At the moment runtime check is failing.

Expand Down
9 changes: 8 additions & 1 deletion android/src/main/java/com/rnllama/LlamaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class LlamaContext {
public static final String NAME = "RNLlamaContext";
Expand Down Expand Up @@ -64,8 +66,12 @@ public LlamaContext(int id, ReactApplicationContext reactContext, ReadableMap pa
params.hasKey("lora_scaled") ? (float) params.getDouble("lora_scaled") : 1.0f,
// float rope_freq_base,
params.hasKey("rope_freq_base") ? (float) params.getDouble("rope_freq_base") : 0.0f,
// float rope_freq_scale
// float rope_freq_scale,
params.hasKey("rope_freq_scale") ? (float) params.getDouble("rope_freq_scale") : 0.0f,
// String rpc_servers
params.hasKey("rpc_servers") ?
String.join(",", Arrays.asList(params.getArray("rpc_servers").toArrayList().toArray(new String[0]))) :
"",
// LoadProgressCallback load_progress_callback
params.hasKey("use_progress_callback") ? new LoadProgressCallback(this) : null
);
Expand Down Expand Up @@ -372,6 +378,7 @@ protected static native long initContext(
float lora_scaled,
float rope_freq_base,
float rope_freq_scale,
String rpc_servers,
LoadProgressCallback load_progress_callback
);
protected static native void interruptLoad(long contextPtr);
Expand Down
7 changes: 7 additions & 0 deletions android/src/main/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Java_com_rnllama_LlamaContext_initContext(
jfloat lora_scaled,
jfloat rope_freq_base,
jfloat rope_freq_scale,
jstring rpc_servers,
jobject load_progress_callback
) {
UNUSED(thiz);
Expand Down Expand Up @@ -195,6 +196,11 @@ Java_com_rnllama_LlamaContext_initContext(
defaultParams.rope_freq_base = rope_freq_base;
defaultParams.rope_freq_scale = rope_freq_scale;

const char *rpc_servers_chars = env->GetStringUTFChars(rpc_servers, nullptr);
if (rpc_servers_chars != nullptr && rpc_servers_chars[0] != '\0') {
defaultParams.rpc_servers = rpc_servers_chars;
}

auto llama = new rnllama::llama_rn_context();
llama->is_load_interrupted = false;
llama->loading_progress = 0;
Expand Down Expand Up @@ -233,6 +239,7 @@ Java_com_rnllama_LlamaContext_initContext(

env->ReleaseStringUTFChars(model_path_str, model_path_chars);
env->ReleaseStringUTFChars(lora_str, lora_chars);
env->ReleaseStringUTFChars(rpc_servers, rpc_servers_chars);

return reinterpret_cast<jlong>(llama->ctx);
}
Expand Down
Loading