环境
操作系统

CPU

内存

生成量化版本模型
转换出q4_0 q4_k q6_k q8_0模型
cd ~/Downloads/ai/llama.cpp
sourvce venv/bin/activate
~/Downloads/ai/llama.cpp/quantize /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16.gguf /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_0.bin q4_0
~/Downloads/ai/llama.cpp/quantize /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16.gguf /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_k.bin q4_k
~/Downloads/ai/llama.cpp/quantize /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16.gguf /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q6_k.bin q6_k
~/Downloads/ai/llama.cpp/quantize /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16.gguf /home/yeqiang/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q8_0.bin q8_0

CPU版本测试
编译
make clean
make -j6
创建prompt.txt,内容太长,放到后面参考部分。
q4_0
bash
./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_0.bin -f prompt.txt -c 2048 -ngl 1


q4_k
./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_k.bin -f prompt.txt -c 2048 -ngl 1

q6_k
./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q6_k.bin -f prompt.txt -c 2048 -ngl 1

q8_0
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q8_0.bin -f prompt.txt -c 2048 -ngl 1

GPU版本测试
编译
bash
make clean
make LLAMA_CUBLAS=1 -j6
推理
q4_0 ngl 1
bash
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_0.bin -f prompt.txt -c 2048 -ngl 1


q4_0 ngl 10
放更多的数据到显存
bash
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_0.bin -f prompt.txt -c 2048 -ngl 1


q4_0 ngl 20
bash
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_0.bin -f prompt.txt -c 2048 -ngl 20

q4_0 ngl 24

最高做到24个layer,继续放大参数,显存不足


与20个layer差异不大,后面都将采用20个layer做本次测试
q4_k ngl 20
bash
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q4_k.bin -f prompt.txt -c 2048 -ngl 20

q6_k ngl 20 加载不上(显存太低了)

q6_k ngl 16
bash
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q6_k.bin -f prompt.txt -c 2048 -ngl 16


q8_0 ngl 13
bash
time ./perplexity -m ~/Downloads/ai/chinese-alpaca-2-7b/ggml-model-f16-q8_0.bin -f prompt.txt -c 2048 -ngl 13

数据汇总(由于显存低,不严谨,取 eval time 的ms per token值)
|-----|---------------------------------------------------------|--------------|--------------|--------------|
| | q4_0 | q4_k | q6_k | q8_0 |
| CPU | 116 | 90.47 | 110.13 | 104.95 |
| GPU | 6.19(ngl=1) 4.50(ngl=10) 2.63(ngl=20) 2.68(ngl=24) 显存不足 | 2.73(ngl=20) | 3.99(ngl=16) | 4.51(ngl=13) |
官方建议q6_k模型,精度与q8_0差不多,速度快。

CPU版本的ngl参数应该是无意义的。
chat.sh性能疑问

7b q6_k模型,智能加载到14个layers,速度良好,约2秒就开始输出。观察GPU显示显存暂用到达极限,GPU负载不高(main根本就没消耗GPU算力),所有计算压力在CPU。
签名的测试可以确认perplexity程序能够使用GPU算力极大提高推理速度,这个chat.sh封装了main程序,虽然可以看到显存占用,但是显卡负载一点都没变。问题在哪里?
重新编译CPU版本

响应速度慢了一大截,说明GPU还是起了很大作用,就是没搞明白GPU版本为什么显卡负载几乎没有。
再次分析GPU版本,调整参数 -t 1,参考下图

从这个观察看,CPU的负载降低到100%,交互速度没有任何影响,说明还是GPU在干活。CPU可能是在轮训GPU的数据导致负载高。
参考
【AI】RTX2060 6G Ubuntu 22.04.1 LTS (Jammy Jellyfish) 部署Chinese-LLaMA-Alpaca-2 【2】启用GPU支持-CSDN博客
prompt.txt内容:
bash
与LangChain进行集成
什么是LangChain?
LangChain是一个用于开发由LLM驱动的应用程序的框架,旨在帮助开发人员使用LLM构建端到端的应用程序。
借助LangChain提供的组件和接口,开发人员可以方便地设计与搭建诸如问答、摘要、聊天机器人、代码理解、信息提取等多种基于LLM能力的应用程序。
如何在LangChain中使用Chinese-Alpaca-2?
以下文档通过两个示例,分别介绍在LangChain中如何使用Chinese-Alpaca-2实现
检索式问答
摘要生成
例子中的超参、prompt模版均未调优,仅供演示参考用。关于LangChain的更详细的使用说明,请参见其官方文档。
准备工作
环境准备
pip install langchain
pip install sentence_transformers==2.2.2
pip install pydantic==1.10.8
pip install faiss-gpu==1.7.2
模型准备
下载完整版权重,或者参照模型合并与转换将LoRA权重与原版Llama-2合并得到完整版权重,并将模型保存至本地。
在检索式问答中,LangChain通过问句与文档内容的相似性匹配,来选取文档中与问句最相关的部分作为上下文,与问题组合生成LLM的输入。因此,需要准备一个合适的embedding model用于匹配过程中的文本/问题向量化。本文以GanymedeNil/text2vec-large-chinese为例进行说明(实际上,也可以根据实际需要选择其他合适的embedding model)。
检索式问答
该任务使用LLM完成针对特定文档的自动问答,流程包括:文本读取、文本分割、文本/问题向量化、文本-问题匹配、将匹配文本作为上下文和问题组合生成对应Prompt中作为LLM的输入、生成回答。
cd scripts/langchain
python langchain_qa.py \
--embedding_path text2vec-large-chinese \
--model_path chinese-alpaca-2-7b \
--file_path doc.txt \
--chain_type refine
参数说明:
--embedding_path: 下载至本地的embedding model所在目录(如text2vec-large-chinese)或HuggingFace模型名(如GanymedeNil/text2vec-large-chinese)
--model_path: 合并后的Alpaca模型所在目录
--file_path: 待进行检索与提问的文档
--chain_type: 可以为refine(默认)或stuff,为两种不同的chain,详细解释见这里。简单来说,stuff适用于较短的篇章,而refine适用于较长的篇章。
--gpu_id: 指定使用的GPU设备编号,默认为0。目前仅支持单GPU推理。
运行示例:
> python langchain_qa.py --embedding_path text2vec-large-chinese --model_path chinese-alpaca-2-7b --file_path doc.txt --chain_type refine
# 中间输出信息省略
> 请输入问题:李白的诗是什么风格?
> 李白的诗歌风格是浪漫主义。
摘要生成
该任务使用LLM完成给定文档的摘要生成,以帮助提炼文档中的核心信息。
cd scripts/langchain
python langchain_sum.py \
--model_path chinese-alpaca-2-7b \
--file_path doc.txt \
--chain_type refine
参数说明:
--model_path: 合并后的Alpaca模型所在目录
--file_path: 待进行摘要的文档
--chain_type: 可以为refine(默认)或stuff,为两种不同的chain,详细解释见这里。简单来说,stuff适用于较短的篇章,而refine适用于较长的篇章。
--gpu_id: 指定使用的GPU设备编号,默认为0。目前仅支持单GPU推理。
运行示例:
> python langchain_sum.py --model_path chinese-alpaca-2-7b --file_path doc.txt
# 中间输出信息省略
> 李白(701年5月19日-762年11月30日),字太白,号青莲居士,唐代著名诗人。他在少年时代就展现出了非凡的才华,但由于缺乏正规教育,他放弃了学业并开始漫游生涯,以写作诗歌为主要职业。尽管经历了许多困难和挫折,他始终坚持自己的理想,努力追求卓越。在盛唐时期,他活跃于文学界,成为了当时最杰出的浪漫主义诗人之一。他的诗歌充满着想象力和创造力,经常使用夸张和比喻来表达深刻的思想感情。他的作品至今仍是中国古典文学的重要组成部分。
手动模型合并与转换
以下介绍了手动将LoRA与原版Llama-2合并得到完整模型的流程。如网络带宽充足,建议直接下载完整版模型。
准备工作
运行前确保拉取仓库最新版代码:git pull
确保机器有足够的内存加载完整模型(例如7B模型需要13-15G)以进行合并模型操作
安装依赖库(项目根目录requirements.txt):
$ pip install -r requirements.txt
Step 1: 获取原版Llama-2-hf模型
原版Llama-2-hf地址:https://huggingface.co/meta-llama/Llama-2-7b-hf
HF格式模型相关文件(可以不用下载safetensors格式模型权重):
config.json
generation_config.json
pytorch_model-00001-of-00002.bin
pytorch_model-00002-of-00002.bin
pytorch_model.bin.index.json
special_tokens_map.json
tokenizer_config.json
tokenizer.json
tokenizer.model
Step 2: 合并LoRA权重,生成全量模型权重
这一步骤会合并LoRA权重,生成全量模型权重。此处可以选择输出PyTorch版本权重(.pth文件)或者输出HuggingFace版本权重(.bin文件)。执行以下命令:
$ python scripts/merge_llama2_with_chinese_lora_low_mem.py \
--base_model path_to_original_llama2_hf_dir \
--lora_model path_to_chinese_llama2_or_alpaca2_lora \
--output_type huggingface \
--output_dir path_to_output_dir
参数说明:
--base_model:存放HF格式的Llama-2模型权重和配置文件的目录
--lora_model:中文LLaMA-2/Alpaca-2 LoRA解压后文件所在目录,也可使用🤗Model Hub模型调用名称(会自动下载)
--output_type:指定输出格式,可为pth或huggingface。若不指定,默认为huggingface
--output_dir:指定保存全量模型权重的目录,默认为./