Skip to content

Commit

Permalink
Merge pull request #19 from InfiniTensor/android
Browse files Browse the repository at this point in the history
添加 android crate
  • Loading branch information
YdrMaster authored Aug 11, 2024
2 parents 8798e56 + abcc97c commit 5ffd316
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 18 deletions.
171 changes: 157 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"chat-template",
"service",
"web-api",
"android",
"xtask",

"devices/common",
Expand Down
17 changes: 17 additions & 0 deletions android/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "android"
version = "0.0.0"
edition = "2021"
authors = ["YdrMaster <[email protected]>"]

[dependencies]
service = { path = "../service" }
llama-cpu = { path = "../models/llama/common-cpu" }
tokio.workspace = true
log.workspace = true
android_logger = "0.14"
jni = "0.21"

[lib]
name = "infinilm_chat"
crate-type = ["cdylib"]
42 changes: 42 additions & 0 deletions android/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 安卓对话支持

使用 [cargo-ndk](https://crates.io/crates/cargo-ndk) 编译:

```shell
cargo ndk -t armeabi-v7a -t arm64-v8a -t x86_64 -o target/jniLibs build --package android --release
```

> **NOTICE** 需要按安装 cargo-ndk 并按文档描述配置 NDK 和安卓工具链。
> **NOTICE** 如果需要支持的目标硬件较少,可以减少 `-t xxx` 选项。
> **NOTICE** 必须使用 `--release` 编译。gemm simd 内联汇编在 debug 模式下无法编译。
将在 target 目录生成 jniLibs 目录,将这个目录拷贝到安卓项目的 *app/src/main* 目录中。并添 Native.java:

```java
package org.infinitensor.lm;

public class Native {
// 加载模型并启动推理服务,必须最先调用。
public native static void init(String model_path);
// 开始对话。
public native static void start(String prompt);
// 终止对话。
public native static void abort();
// 解码模型反馈。
public native static String decode();
}
```

调用这些代码后:

```kotlin
try {
System.loadLibrary("infinilm_chat")
} catch (e: UnsatisfiedLinkError) {
throw RuntimeException("Native library not found", e)
}
```

即可使用 `Native` 类进行推理。
Loading

0 comments on commit 5ffd316

Please sign in to comment.