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"
    
  • 20
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值