Convert GGUF
Contents
下载 llama.cpp 项目,需要用到其 Python 部分
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
llama.cpp 下创建虚拟环境及安装依赖
本来想用 uv init
创建虚拟环境,但是本地已经有 pyproject.toml 文件了。venv
python 自带的。
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# 查看是否使用的 .venv 目录下的 python, pip
which python
which pip
确保安装无报错。我因为安装不完整,执行 convert 脚本报错了,一度以为 llama.cpp 还不支持 Qwen,很惊讶。
FileNotFoundError: File not found: Qwen3-0.6B/tokenizer.model
下载模型到本地
前序工作,lfs 需要额外安装。以下是 ubuntu 下的指令。一次性工作。
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
git lfs install
huggingface 上下载模型。
git clone https://huggingface.co/Qwen/Qwen3-0.6B
如果在 lfs 安装之前 clone 了模型,需要使用 lfs 再次拉取模型。
cd Qwen3-0.6B
git lfs pull
Convert
Qwen3-0.6B 为模型目录
python convert_hf_to_gguf.py Qwen3-0.6B --outfile /home/sagemaker-user/exports/Qwen3-0.6B.gguf
python convert_hf_to_gguf.py /home/sagemaker-user/saves/Qwen3-0.6B-Instruct/lora/train_2025-06-12-21-05-00/output-001 --outfile /home/sagemaker-user/exports/Qwen3-0.6B-Translator.gguf
编译 llama.cpp
可以使用 llama.cpp 对生成的 gguf 文件做测试。
CUDA 编译很慢。
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
cmake -B build -DGGML_CUDA=ON
cmake --build build --config Release -j 8
使用 llama-cli 与模型聊天
./build/bin/llama-cli -m /home/sagemaker-user/exports/Qwen3-0.6B.gguf
> hi /no_think
<think>
</think>
Hi! How can I assist you today? 😊
./build/bin/llama-cli -m /home/sagemaker-user/exports/Qwen3-0.6B-Translator.gguf
> One person sets a goal: become a best-selling author. Another imposes a constraint: write every day, but never write what bores me. The first may spend years pitching, networking, contorting themselves into marketable shapes. The second may accidentally build a following simply because the work sustains itself.
<think>
</think>
有一个人制定目标,成为畅销作者;另一个人施加约束条件:每天写,但不要写让我痛苦的东西。第一个可能花费数年时间做广告、参加活动、把想法塑造成符合畅销产品的形状。第二个可能无意间就因为作品持续成功而拥有了一个庞大的粉丝。