-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from InfiniTensor/android
添加 android crate
- Loading branch information
Showing
7 changed files
with
427 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ members = [ | |
"chat-template", | ||
"service", | ||
"web-api", | ||
"android", | ||
"xtask", | ||
|
||
"devices/common", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` 类进行推理。 |
Oops, something went wrong.