制作并量化GGUF模型上传到HuggingFace和ModelScope

0 / 66

llama.cppOllamaLMStudio 和其他很多热门项目的底层实现,也是 GPUStack 所支持的推理引擎之一,它提供了 GGUF 模型文件格式。GGUF (General Gaussian U-Net Format) 是一种用于存储模型以进行推理的文件格式,旨在针对推理进行优化,可以快速加载和运行模型。

llama.cpp 还支持量化模型,在保持较高的模型精度的同时,减少模型的存储和计算需求,使大模型能够在桌面端、嵌入式设备和资源受限的环境中高效部署,并提高推理速度。

今天带来一篇介绍如何制作并量化 GGUF 模型,将模型上传到 HuggingFaceModelScope 模型仓库的操作教程。

注册与配置 HuggingFace 和 ModelScope

  • 注册 HuggingFace

访问 https://huggingface.co/join 注册 HuggingFace 账号(需要某上网条件)

  • 配置 HuggingFace SSH 公钥

将本地环境的 SSH 公钥添加到 HuggingFace,查看本地环境的 SSH 公钥(如果没有可以用 ssh-keygen -t rsa -b 4096 命令生成):

cat ~/.ssh/id_rsa.pub

在 HuggingFace 的右上角点击头像,选择 Settings - SSH and GPG Keys,添加上面的公钥,用于后面上传模型时的认证。

  • 注册 ModelScope

访问 https://www.modelscope.cn/register?back=%2Fhome 注册 ModelScope 账号

  • 获取 ModelScope Token

访问 https://www.modelscope.cn/my/myaccesstoken,将 Git 访问令牌复制保存,用于后面上传模型时的认证。

image-20241106162218513

准备 llama.cpp 环境

创建并激活 Conda 环境(没有安装的参考 Miniconda 安装:https://docs.anaconda.com/miniconda/):

conda create -n llama-cpp python=3.12 -y
conda activate llama-cpp
which python
pip -V

克隆 llama.cpp 的最新分支代码,编译量化所需的二进制文件:

cd ~
git clone -b b4034 https://github.com/ggerganov/llama.cpp.git
cd llama.cpp/
pip install -r requirements.txt
brew install cmake
make

image-20241106120151035

编译完成后,可以运行以下命令确认量化所需要的二进制文件 llama-quantize 是否可用:

./llama-quantize --help

image-20241106120420296

下载原始模型

下载需要转换为 GGUF 格式并量化的原始模型。

从 HuggingFace 下载模型,通过 HuggingFace 提供的 huggingface-cli 命令下载,首先安装依赖:

pip install -U huggingface_hub

国内网络配置下载镜像源:

export HF_ENDPOINT=https://hf-mirror.com

这里下载 meta-llama/Llama-3.2-3B-Instruct 模型,该模型是 Gated model,需要在 HuggingFace 填写申请并确认获得访问授权:

image-20241106141024313

在 HuggingFace 的右上角点击头像,选择 Access Tokens,创建一个 Read 权限的 Token,保存下来:

image-20241106131544469

下载 meta-llama/Llama-3.2-3B-Instruct 模型,--local-dir 指定保存到当前目录,--token 指定上面创建的访问 Token:

mkdir ~/huggingface.co
cd ~/huggingface.co/
huggingface-cli download meta-llama/Llama-3.2-3B-Instruct --local-dir Llama-3.2-3B-Instruct --token hf_abcdefghijklmnopqrstuvwxyz

转换为 GGUF 格式与量化模型

创建 GGUF 格式与量化模型的脚本:

cd ~/huggingface.co/
vim quantize.sh

填入以下脚本内容,并把 llama.cpphuggingface.co 的目录路径修改为当前环境的实际路径,需要为绝对路径,将 d 变量中的 gpustack 修改为 HuggingFace 用户名:

#!/usr/bin/env bash

llama_cpp="/Users/gpustack/llama.cpp"
b="/Users/gpustack/huggingface.co"

export PATH="$PATH:${llama_cpp}"

s="$1"
n="$(echo "${s}" | cut -d'/' -f2)"
d="gpustack/${n}-GGUF"

# prepare

mkdir -p ${b}/${d} 1>/dev/null 2>&1
pushd ${b}/${d} 1>/dev/null 2>&1
git init . 1>/dev/null 2>&1

if [[ ! -f .gitattributes ]]; then
    cp -f ${b}/${s}/.gitattributes . 1>/dev/null 2>&1 || true
    echo "*.gguf filter=lfs diff=lfs merge=lfs -text" >> .gitattributes
fi
if [[ ! -d assets ]]; then
    cp -rf ${b}/${s}/assets . 1>/dev/null 2>&1 || true
fi
if [[ ! -d images ]]; then
    cp -rf ${b}/${s}/images . 1>/dev/null 2>&1 || true
fi
if [[ ! -d imgs ]]; then
    cp -rf ${b}/${s}/imgs . 1>/dev/null 2>&1 || true
fi
if [[ ! -f README.md ]]; then
    cp -f ${b}/${s}/README.md . 1>/dev/null 2>&1 || true
fi

set -e

pushd ${llama_cpp} 1>/dev/null 2>&1

# convert

[[ -f venv/bin/activate ]] && source venv/bin/activate
echo "#### convert_hf_to_gguf.py ${b}/${s} --outfile ${b}/${d}/${n}-FP16.gguf"
python3 convert_hf_to_gguf.py ${b}/${s} --outfile ${b}/${d}/${n}-FP16.gguf

# quantize

qs=(
  "Q8_0"
  "Q6_K"
  "Q5_K_M"
  "Q5_0"
  "Q4_K_M"
  "Q4_0"
  "Q3_K"
  "Q2_K"
)
for q in "${qs[@]}"; do
    echo "#### llama-quantize ${b}/${d}/${n}-FP16.gguf ${b}/${d}/${n}-${q}.gguf ${q}"
    llama-quantize ${b}/${d}/${n}-FP16.gguf ${b}/${d}/${n}-${q}.gguf ${q}
    ls -lth ${b}/${d}
    sleep 3
done

popd 1>/dev/null 2>&1

set +e

开始将模型转换为 FP16 精度的 GGUF 模型,并分别用 Q8_0Q6_KQ5_K_MQ5_0Q4_K_MQ4_0Q3_KQ2_K 方法来量化模型:

bash quantize.sh Llama-3.2-3B-Instruct

脚本执行完后,确认成功转换为 FP16 精度的 GGUF 模型和量化后的 GGUF 模型:

image-20241106154934731

模型被存储在对应用户名的目录下:

ll gpustack/Llama-3.2-3B-Instruct-GGUF/

image-20241106171759142

上传模型到 HuggingFace

在 HuggingFace 右上角点击头像,选择 New Model 创建同名的模型仓库,格式为 原始模型名-GGUF

image-20241106171458202

更新模型的 README: