无需GPU无需网络“本地部署chatGPT”(更新vicuna模型)

编程知识 更新时间:2023-05-02 03:00:15

想当初图像生成从DELL到stable diffusion再到苹果的移动部署过了两三年吧
聊天bot才发展几个月就可以边缘部署了,如果苹果更新silicon,npu和运存翻倍,争取apple watch也能本地内置,最快ios18 mac、ipad、iPhone能内置吧
又是一个平民百姓都高兴的开源项目,chatGPT这种级别的模型甚至能部署到树莓派上运行,然后在操作的过程中也遇到一些问题,这篇就是记录步数的这个过程。
已经为最新版的github更新了(2023.4.23),可以放心食用,实测运行速度比4.7快了很多。
然后配合下面的模型百度云链接,大家应该就可以自己搭建语言模型了。

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

下载及生成

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

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

#对于Windows和CMake,使用下面的方法构建:
cd <path_to_llama_folder>
mkdir build
cd build
cmake ..
cmake --build . --config Release

模型下载

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

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

好吧我直接给百度云
链接: https://pan.baidu/s/1ZC2SCG9X8jZ-GysavQl29Q 提取码: 4ret
–来自百度网盘超级会员v6的分享

然后安装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 ./models/7B/ggml-model-f16.bin ./models/7B/ggml-model-q4_0.bin 2

推理

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

想和chatGPT一样对话的话用下面这个指令,-n 控制回复生成的最大长度, --color是区分ai和人类的颜色,-i 作为参数在交互模式下运行, -r 是一种反向提示,-f 是一整段提示, --repeat_penalty 控制生成回复中对重复文本的惩罚力度,–temp 温度系数,值越低回复的随机性越小,反之越大。
更新了之后速度快了很多。

./main -m ./models/7B/ggml-model-q4_0.bin -n 256 --repeat_penalty 1.0 --color -i -r "User:" -f prompts/chat-with-bob.txt

让我们打开prompts/chat-with-bob.txt来看一下。

我们可以看到这相当于给了ai模型一个场景话题,然后你和ai之间就可以接着这个话题聊天。

我英文名叫zale,然后我把这个机器人叫作kangaroo,这样的身份和他聊天,你可以按自己的喜欢自己修改下面的代码。

./main -m ./models/7B/ggml-model-q4_0.bin -n 256 --repeat_penalty 1.0 --color -i -r "Zale:" \

写一个txt文件

"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:"

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

分享一段有意思的对话

中文部署

哈工大的github
https://github/ymcui/Chinese-LLaMA-Alpaca

git clone https://github/ymcui/Chinese-LLaMA-Alpaca.git

下载中文模型,但这不是llama.cpp要输入的模型,官方的说明是llama的中文补丁模型,需要和原版的llama/alpaca模型合并才能使用。

安装依赖

pip install git+https://github/huggingface/transformers
pip install sentencepiece
pip install peft

为了方便起见,我把llama原文件也放到了这里

还有一些注意事项

查看sha256,每个平台查看方式略微不同,可以上网搜一下如何查看sha256

整理一下llama原文件的路径

我是将transformers下载到conda里了,路径有点长。你就是找到你的convert_llama_weights_to_hf.py文件的路径就好。

python /Users/kangaroo/miniconda3/envs/pytorch/lib/python3.10/site-packages/transformers/models/llama/convert_llama_weights_to_hf.py \
    --input_dir ./llama_7b \
    --model_size 7B \      
    --output_dir ./llama_hf

合并模型

python scripts/merge_llama_with_chinese_lora.py \
    --base_model ./llama_hf \                   
    --lora_model ./chinese_llama_lora_7b \             
    --output_dir ./cn_llama 

再把这个文件夹复制到llama.cpp/models 中

回到llama.cpp里重新量化

python convert-pth-to-ggml.py models/cn_llama/ 1

./quantize ./models/cn_llama/ggml-model-f16.bin ./models/cn_llama/ggml-model-q4_0.bin 2

有点话痨,我直接掐掉了,之后再看看

./main -m ./models/cn_llama/ggml-model-q4_0.bin -n 48 --repeat_penalty 1.0 --color -i -r "Zale:" -f prompts/chat-with-zale.txt

./main -m models/cn_llama/ggml-model-q4_0.bin --color -f ./prompts/alpaca.txt -ins -c 2048 --temp 0.2 -n 256 --repeat_penalty 1.3

Vicuna-7B

把原始的llama模型转换成huggleface的格式

python3 ~/anaconda/envs/pytorch2/lib/python3.10/site-packages/transformers/models/llama/convert_lama_weights_to_hf.py \
	--input_dir ./llama-7b \
	--model_size 7B \
	--output_dir ./llama-7b-hf

这样生成的文件放在llama-7b-hf之中。

下载vicuna7b的权重文件,也可以理解为补丁。
注意融合vicuna7b模型需要30个G的内存,我特意买128G内存和4090的电脑,
如果有需要,可以私信我我看到的话直接把生成的模型发给你。
还有就是现在的模型是v1.1的版本,必须搭配使用transformers>=4.28.0 and fschat >= 0.2.0

python3 -m fastchat .model.apply_delta \
	--base-model-path ./llama- 7b-hf/ \
	--target-model-path ./vicuna-7b/ \
	--delta-path ./vicuna-7b-delta-v1.1/

这样融合的模型就在vicuna-7b的文件夹下。

可以直接用fastchat用测试一下,速度好快哦!!!确实fast

python3 -m fastchat.serve.cli --model-path ./vicuna-7b

回到llama.cpp之中,老三样

python3 convert-pth-to-ggml.py models/vicuna-7b/ 1

./quantize ./models/vicuna-7b/ggml-model-f16.bin ./models/vicuna-7b/ggml-model-q4_0.bin 2

./main -m ./models/vicuna-7b/ggml-model-q4_0.bin -n 256 --repeat_penalty 1.0 --color -i -r "User:" -f prompts/chat-with-bob.txt

实测理解能力和中文水平我认为都是目前最佳的边缘部署的模型,我觉得我可以把哈工大的中文模型部分给删了。目前使用下来不错,很有chatGPT那味。
我又试了一下i9-13900KF,速度是快了一些。

来点好玩的例子。

更多推荐

无需GPU无需网络“本地部署chatGPT”(更新vicuna模型)

本文发布于:2023-04-25 09:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/be3e69ed3d8a184e331096243a731e0c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   网络   GPU   vicuna   chatGPT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 102873文章数
  • 26169阅读数
  • 0评论数