MLC-LLM框架的安卓应用部署实战

借鉴的文章
另外借鉴的文章

主要的不同处在于:有些方式是不一样的,你得自己去看下载的项目里面的android.srt文件,看整个过程是啥样的 。基本上没有太多的变化。

先去GITHUB上拉取项目代码,git clone https://github.com/mlc-ai/mlc-llm.git --recursive吧子项目也拉去下来,接着就是安装。下载mlc-ai-nightly、mlc-llm-nightly的whell文件,安装的时候加上清华园比较好安装。舍弃使用命令:python3 -m mlc_llm.build 。。。。。。

查看命令mlc_llm --help是不是出现以下:

>>> mlc_llm --help
usage: MLC LLM Command Line Interface. [-h]
                                       {compile,convert_weight,gen_config,chat,serve,bench}

positional arguments:
  {compile,convert_weight,gen_config,chat,serve,bench}
                        Subcommand to to run. (choices: compile,
                        convert_weight, gen_config, chat, serve, bench)

options:
  -h, --help            show this help message and exit

接着下载模型
好,现在前置环境配置的工作终于完成了,现在可以进行模型的编译了,由于从huggingface上下载模型太慢,这里采用别人提供的国内云盘上的模型:

Llama2模型下载地址

Llama2-7B-Chat Hugging Face版本:https://pan.xunlei.com/s/VN_oaV4BpKFgKLto4KgOhBcaA1?pwd=ufir

接下来运行的命令

MODEL_NAME=Llama-2-7b-chat-hf
QUANTIZATION=q4f16_1
# convert weights
mlc_llm convert_weight ./dist/models/$MODEL_NAME/ --quantization $QUANTIZATION -o dist/$MODEL_NAME-$QUANTIZATION-MLC/

# create mlc-chat-config.json
mlc_llm gen_config ./dist/models/$MODEL_NAME/ --quantization $QUANTIZATION \
  --conv-template llama-2 --context-window-size 768 -o dist/${MODEL_NAME}-${QUANTIZATION}-MLC/

# 2. compile: compile model library with specification in mlc-chat-config.json
mlc_llm compile ./dist/${MODEL_NAME}-${QUANTIZATION}-MLC/mlc-chat-config.json \
    --device android -o ./dist/${MODEL_NAME}-${QUANTIZATION}-MLC/${MODEL_NAME}-${QUANTIZATION}-android.tar

链接上的命令,自己看下就可以了。需要将mlc-package-config.json改成自己的本地的模型config:

{
    "device": "android",
    "model_list": [

        {
            "model": "/home/xxx/mlc-llm/dist/Llama-2-7b-chat-hf-q4f16_1-MLC",
            "estimated_vram_bytes": 3790741504,
            "model_id": "Llama-2-7b-chat-hf-q4f16_1-MLC",
            "bundle_weight": true

        }

    ]
}

运行命令 mlc_llm package 得到一系列的文件放到安卓下面进行运行APP。接下来的和博客中记录的都差不多 注入权重 运行即可!

MODEL_NAME=Llama-2-7b-chat-hf
QUANTIZATION=q4f16_1
# convert weights
mlc_llm convert_weight ./Llama-2-7b-chat-hf/ --quantization q4f16_1 -o dist/Llama-2-7b-chat-hf-q4f16_1-MLC/

# create mlc-chat-config.json
mlc_llm gen_config ./Llama-2-7b-chat-hf/ --quantization q4f16_1  --conv-template llama-2 --context-window-size 768 -o dist/Llama-2-7b-chat-hf-q4f16_1-MLC/

# 2. compile: compile model library with specification in mlc-chat-config.json
mlc_llm compile ./dist/Llama-2-7b-chat-hf-q4f16_1-MLC/mlc-chat-config.json --device android -o ./dist/Llama-2-7b-chat-hf-q4f16_1-MLC/Llama-2-7b-chat-hf-q4f16_1-android.tar



# 将apk安装进你的android手机
adb install android/MLCChat/app/release/app-release.apk

# 将模型权重文件上传至android手机的临时文件夹
adb push dist/bundle/Llama-2-7b-chat-hf-q4f16_1-MLC /data/local/tmp/Llama-2-7b-chat-hf-q4f16_1-MLC/

# 在android手机上创建apk读取本地模型的文件夹路径
adb shell "mkdir -p /storage/emulated/0/Android/data/ai.mlc.mlcchat/files/"

# 将模型拷贝至apk读取文件夹路径
adb shell "mv /data/local/tmp/Llama-2-7b-chat-hf-q4f16_1-MLC /storage/emulated/0/Android/data/ai.mlc.mlcchat/files/"

随便问的问题

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值