想当初图像生成从DELL到stable diffusion再到苹果的移动部署过了两三年吧
聊天bot才发展几个月就可以边缘部署了,如果苹果更新silicon,npu和运存翻倍,争取apple watch也能本地内置,最快ios18 mac、ipad、iPhone能内置吧
又是一个平民百姓都高兴的开源项目,chatGPT这种级别的模型甚至能部署到树莓派上运行,然后在操作的过程中也遇到一些问题,这篇就是记录步数的这个过程。

大佬的网址:https://github/ggerganov/llama.cpp

下载及生成

打开命令行输入下面的指令

git clone https://github/ggerganov/llama.cpp
cd llama.cpp
make
mkdir models


模型下载

我觉得模型下载是最麻烦的,还好有别人给了

git clone https://huggingface.co/nyanko7/LLaMA-7B

然后安装python依赖,然后转换模型到FP16格式。然后第一个小bug会出现。

python3 -m pip install torch numpy sentencepiece

# convert the 7B model to ggml FP16 format
python3 convert-pth-to-ggml.py models/7B/ 1

他会报找不到文件。

打开convert-pth-to-ggml.py文件,修改"/tokenizer.model"的路径,再运行python3 convert-pth-to-gaml.py ./models/7B 1,我顺便名字也改了。

文件找到了,然后出现第二个bug。。。。。

我一开始找不出问题,后来对比原网址和7B文件夹里的文件,才发现文件大小根本都不一样,我说几十个G的东西怎么git这么。
打开网站下图这个网址,点红色框的那两个下载。替换掉7B文件夹里的那两个文件。

将模型再转换成4位格式

# quantize the model to 4-bits
./quantize.sh 7B

推理

# run the inference
./main -m ./models/7B/ggml-model-q4_0.bin -t 8 -n 128

他会自己随便说一些,如果你想问他问题,-p就是你要问的东西

./main -m ./models/7B/ggml-model-q4_0.bin -t 8 -n 256 -p "Building a website can be done in 10 simple steps:"

要是想像chatGPT这样你一句我一句的聊,用下面的聊。
我英文名叫zale,然后我把这个机器人叫作kangaroo,这样的身份和他聊天,你可以按自己的喜欢自己修改下面的代码。
在-p这加上一点对话好让模型知道要以对话的形式交流,否则它很喜欢自己精分成两个人,然后你一句我一句的对答。

./main -m ./models/7B/ggml-model-q4_0.bin -t 8 -n 256 --repeat_penalty 1.0 --color -i -r "Zale:" \
                                           -p \
"Transcript of a dialog, where the Zale interacts with an Assistant named Kangaroo. Kangaroo is helpful, kind, honest, good at writing, and never fails to answer the Zale's requests immediately and with precision.

Zale: Hello, Kangaroo.
Kangaroo: Hello. How may I help you today?
Zale: Please tell me the largest city in Europe.
Kangaroo: Sure. The largest city in Europe is Moscow, the capital of Russia.
Zale:"

有点呆呆的,不过也算边缘部署的巨大进步了!
一个蛮有意思的发现,明明看得懂中文却跟我说不懂中文。。。。。

更多推荐

无需GPU无需网络“本地部署chatGPT”