python系列&deep_study系列:LLM大模型2_量化
LLM大模型2_量化
LLM大模型2_量化
⚠️内存消耗提示(确保刷出来的机器RAM大于以下要求):
-
7B模型:15G+
-
13B模型:18G+
-
33B模型:22G+
安装相关依赖
# python3.9
pip install peft==0.3.0 transformers==4.31.0 sentencepiece==0.1.97 bitsandbytes==0.39.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
合并模型(以LLaMA-2-7B为例)
合并LoRA
,生成全量模型权重。可以直接指定🤗模型库的地址,也可以是本地存放地址。
-
基模型:
meta-llama/Llama-2-7b-hf
(注意需要官方授权)- 这里使用一个平替(
SHA256一致
)做演示:daryl149/llama-2-7b-hf
- 这里使用一个平替(
-
LoRA模型:
ziqingyang/chinese-llama-2-lora-7b
-
输出格式:可选
pth
或者huggingface
,这里选择huggingface
-
Llama-2
的config
中途有一次更新。因为本教程里使用的是第三方的权重,并没有及时更新对应的config.json文件
。 -
请手动打开
llama-2-7b-combined文件夹
下的config.json
(可直接双击打开),将max_position_embeddings字段
由2048
改为4096
git clone https://github.com/ymcui/Chinese-LLaMA-Alpaca-2.git
git clone https://github.com/ggerganov/llama.cpp
python ./Chinese-LLaMA-Alpaca-2/scripts/merge_llama2_with_chinese_lora_low_mem.py \
--base_model ./chinese-alpaca-2-7b/ \
--lora_model ./chinese-alpaca-2-lora-7b/ \
--output_type huggingface\
--output_dir llama-2-7b-combined
output_type : pth或huggingface
比对SHA256
完整值:https://github.com/ymcui/Chinese-LLaMA-Alpaca/blob/main/SHA256.md
其中本示例生成的Alpaca-7B的标准SHA256:
fbfccc91183169842aac8d093379f0a449b5a26c5ee7a298baf0d556f1499b90
使用下述命令评测后发现两者相同,合并无误。
# macOS
shasum -a 256 <文件路径>
# Liunx
sha256sum <文件路径>
# win
CertUtil -hashfile "**\alpaca-combined\*-consolidated.*.pth" SHA256
量化模型
接下来我们使用llama.cpp工具对上一步生成的全量版本权重进行转换,生成4-bit量化模型。
这一步,我们将模型转换为ggml格式
(FP16
)。
在这之前需要把alpaca-combined目录
挪个位置,把模型文件放到llama.cpp/zh-models/7B
下,把tokenizer.model
放到llama.cpp/zh-models
# 首先对llama.cpp工具进行编译。
# M1芯片 LLAMA_METAL=1 make
# 使用NVIDIA显卡 make LLAMA_CUBLAS=1
!cd llama.cpp && make
# 转换为ggml格式(FP16)
!cd llama.cpp && mkdir zh-models && mv ../alpaca-combined zh-models/7B
!mv llama.cpp/zh-models/7B/tokenizer.model llama.cpp/zh-models/
!ls llama.cpp/zh-models/
!cd llama.cpp && python convert.py zh-models/7B/
# 将FP16模型转换为4-bit量化模型,此处选择的是新版Q6_K方法
!cd llama.cpp && ./quantize ./zh-models/7B/ggml-model-f16.gguf ./zh-models/7B/ggml-model-q6_K.gguf q6_K
# 测试量化模型解码
!cd llama.cpp && ./main -m ./zh-models/7B/ggml-model-q6_K.bin --color -p "详细介绍一下北京的名胜古迹:" -n 128
运行
#!/bin/bash
# temporary script to chat with Chinese Alpaca-2 model
# usage: ./chat.sh alpaca2-ggml-model-path your-first-instruction
SYSTEM_PROMPT='You are a helpful assistant. 你是一个乐于助人的助手。'
# SYSTEM_PROMPT='You are a helpful assistant. 你是一个乐于助人的助手。请你提供专业、有逻辑、内容真实、有价值的详细回复。' # Try this one, if you prefer longer response.
MODEL_PATH=$1
FIRST_INSTRUCTION=$2
./main -m "$MODEL_PATH" \
--color -i -c 4096 -t 8 --temp 0.5 --top_k 40 --top_p 0.9 --repeat_penalty 1.1 \
--in-prefix-bos --in-prefix ' [INST] ' --in-suffix ' [/INST]' -p \
"[INST] <<SYS>>
$SYSTEM_PROMPT
<</SYS>>
$FIRST_INSTRUCTION [/INST]"
调用API
#!/bin/bash
# NOTE: start the server first before running this script.
# usage: ./server_curl_example.sh your-instruction
SYSTEM_PROMPT='You are a helpful assistant. 你是一个乐于助人的助手。'
# SYSTEM_PROMPT='You are a helpful assistant. 你是一个乐于助人的助手。请你提供专业、有逻辑、内容真实、有价值的详细回复。' # Try this one, if you prefer longer response.
INSTRUCTION=$1
ALL_PROMPT="[INST] <<SYS>>\n$SYSTEM_PROMPT\n<</SYS>>\n\n$INSTRUCTION [/INST]"
CURL_DATA="{\"prompt\": \"$ALL_PROMPT\",\"n_predict\": 128}"
curl --request POST \
--url http://192.168.1.3:8080/completion \
--header "Content-Type: application/json" \
--data "$CURL_DATA"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)