ggml交叉编译安卓部署大模型

ubuntu 编译,推送.so文件和模型至安卓执行。

部分内容参考:

https://github.com/ggml-org/ggml?tab=readme-ov-file

注意下载模型时

复制代码
../examples/gpt-2/download-ggml-model.sh文件默认是huggingface,可以将网址替换为

https://hf-mirror.com

export HF_HUB_OFFLINE=0
export HF_ENDPOINT=https://hf-mirror.com
pip install -U huggingface_hub
复制代码
git clone https://github.com/ggml-org/ggml
cd ggml
conda create -n ggml python==3.10
# build the examples
mkdir build && cd build
cmake ..
cmake --build . --config Release -j 8
复制代码
# run the GPT-2 small 117M model
../examples/gpt-2/download-ggml-model.sh 117M


将models文件夹拷贝至build目录下

./bin/gpt-2-backend -m models/gpt-2-117M/ggml-model.bin -p "This is an example"

执行成功界面

交叉编译,推至安卓端执行

以下内容保存为.sh文件,执行该文件生成对应的安卓端执行文件

复制代码
#!/bin/bash

# 👇 这里改成你自己的 NDK 路径
NDK_PATH=/opt/android-ndk-r26c

# 编译目录
BUILD_DIR=build-android-arm64
rm -rf $BUILD_DIR
mkdir -p $BUILD_DIR
cd $BUILD_DIR

cmake .. \
  -DCMAKE_TOOLCHAIN_FILE=$NDK_PATH/build/cmake/android.toolchain.cmake \
  -DANDROID_ABI=arm64-v8a \
  -DANDROID_PLATFORM=android-24 \
  -DCMAKE_BUILD_TYPE=Release \
  -DBUILD_EXAMPLES=ON \
  -DBUILD_TESTS=OFF

make -j$(nproc)

生成以下内容在build-android-arm64目录下

编译好的文件在build-android-arm64目录下,注意查看libggml.so是否为安卓可执行的文件

复制代码
# create directories
adb shell 'mkdir /data/local/tmp/bin'
adb shell 'mkdir /data/local/tmp/models'

# push the compiled binaries to the folder
cd 
adb push build-android-arm64/bin/* /data/local/tmp/bin/

# push the ggml library
adb push build-android-arm64/src libggml-base.so libggml-cpu.so  libggml.so /data/local/tmp/

# push model files
adb push models/gpt-2-117M/ggml-model.bin /data/local/tmp/models/

adb shell
cd /data/local/tmp
export LD_LIBRARY_PATH=/data/local/tmp
./bin/gpt-2-backend -m models/ggml-model.bin -p "this is an example"

异常错误处理:/system/bin/sh: *** not executable: 64-bit ELF file

如果在安卓端执行以下命令时,出现/system/bin/sh: *** not executable: 64-bit ELF file错误,说明交叉编译生成.so库不是安卓端可执行的文件,需要重新进行编译

./bin/gpt-2-backend -m models/ggml-model.bin -p "this is an example"

相关推荐
普马萨特13 天前
Wi-Fi 扫描频率多层限制机制解析
网络协议·安卓
ᴀᴠɪᴄɪɪ ғᴏʀᴇᴠᴇʀ15 天前
WebHomeTV:把 Android 影音盒子变成一个可编程的网页应用平台
app·安卓·智能tv·tv box·webtv
百度搜知知学社16 天前
抖音双模块架构:兼容全安卓版本并支持登录
android·架构·安卓·登录·兼容性·抖音
花花少年17 天前
Ubuntu系统下交叉编译gpac
交叉编译·gpac
暗冰ཏོ18 天前
Flutter 从入门到项目实战:Dart 基础、跨平台开发、App 架构与上线发布完整指南
flutter·架构·app·安卓·应用开发
nnnnichijou20 天前
Qt 6.9 嵌入式 Linux 交叉编译全栈填坑指南(以树莓派5 AArch64 为例
qt·嵌入式·交叉编译·qml·树莓派5
UnicornDev20 天前
Android 开发入门教程(第三十五篇):Compose 中的 Material Design 3 —— 构建现代化 UI 的完整组件库
安卓
UnicornDev20 天前
Android 开发入门教程(第三十四篇):Compose 中的图像与图标 —— 从基础显示到性能优化
安卓
上天_去_做颗惺星 EVE_BLUE24 天前
Ubuntu Android 虚拟机安装使用教程
android·linux·测试工具·ubuntu·安卓
我命由我1234524 天前
Android 开发问题:Could not find com.github.PicnicSupermarket:FingerPaintView:1.2.
android·github·android studio·安卓·android jetpack·android-studio·android runtime