Win10 上 mingw64 编译 llama.cpp (纯cpu版本)
mingw64 命令行打开。切换到工作目录:
$ cd /d/huggingface
下载
$ git clone https://github.com/ggml-org/llama.cpp.git
编译
$ cd llama.cpp
$ mkdir build-win10
$ cd build-win10
$ cmake .. -G "MinGW Makefiles" \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=ON \
-DLLAMA_BUILD_TESTS=OFF \
-DLLAMA_BUILD_EXAMPLES=ON \
-DLLAMA_BUILD_SERVER=ON \
-DCMAKE_CXX_FLAGS="-D_WIN32_WINNT=0x0A00"
写在一行:
$ cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=ON -DLLAMA_BUILD_SERVER=ON -DCMAKE_CXX_FLAGS="-D_WIN32_WINNT=0x0A00"
$ mingw32-make -j 8
...
100% Linking CXX static library libllama-cli-impl.a
100% Built target llama-cli-impl
100% Building CXX object tools/cli/CMakeFiles/llama-cli.dir/main.cpp.obj
100% Linking CXX executable ...\bin\llama-cli.exe
100% Built target llama-cli
100% Linking CXX static library libllama-server-impl.a
100% Built target llama-server-impl
100% Building CXX object tools/server/CMakeFiles/llama-server.dir/main.cpp.obj
100% Building CXX object app/CMakeFiles/llama-app.dir/llama.cpp.obj
100% Building CXX object app/CMakeFiles/llama-app.dir/__/license.cpp.obj
100% Linking CXX executable ...\bin\llama-server.exe
100% Linking CXX executable ...\bin\llama.exe
100% Built target llama-server
100% Built target llama-app
编译成功。生成的文件在 bin/ 目录下。这样就可以用 llama.cpp 实验模型推理了。二次开发,可以使用 ggml 的动态库。
模型转换
这里演示把 DeepSeek-R1-Distill-Qwen-7B 转换为 gguf 格式(单文件):
$ cd /d/huggingface/
$ mkdir llama-gguf
$ source pytorch.env
$ python llama.cpp/convert_hf_to_gguf.py \
hub/models--deepseek-ai--DeepSeek-R1-Distill-Qwen-7B/snapshots/916b56a44061fd5cd7d6a8fb632557ed4f724f60 \
--outfile llama-gguf/deepseek-r1-7b-q8_0.gguf \
--outtype q8_0
注:
- 输入目录 .../916b56a44061fd5cd7d6a8fb632557ed4f724f60 是 config.json 文件所在的目录
- q8_0 是量化精度最高版本。
- 务必使用 hub 下的模型(F16)或者直接拉取网上的 gguf 模型。
- 转换的过程需要 pytorch 环境。请参考我的文章:
win10 笔记本电脑安装 pytorch+cuda+gpu 大模型开发环境过程记录
pytorch.env 内容如下:
bash
#!/bin/bash
# 2025-05-19, zhangliang
# source pytorch_env.sh
#
export HF_HUB_DISABLE_SYMLINKS_WARNING=1
export HF_ENDPOINT="https://hf-mirror.com"
# Windows 平台自动转为: D:/huggingface
export HF_HOME=/d/huggingface
export HF_MODELS=${HF_HOME}/models
source "$ANACONDA3_ROOT_BASH/etc/profile.d/conda.sh"
conda activate pytorch_env`
和大模型聊天
加载模型,交互式聊天:
$ cd /d/huggingface/
$ llama.cpp/build-win10/bin/llama-cli.exe -m llama-gguf/deepseek-r1-7b-q8_0.gguf -cnv
Loading model...
build : b9692-f3e182816
model : deepseek-r1-7b-q8_0.gguf
modalities : text
available commands:
/exit or Ctrl+C stop or exit
/regen regenerate the last response
/clear clear the chat history
/read add a text file
/glob add text files using globbing pattern
你好
Start thinking
你好!欢迎来到深度求索(DeepSeek)。我是一个由深度求索公司独立开发的智能助手DeepSeek-R1-Lite-Preview,很高兴为您提供服务!
End thinking
你好!欢迎来到深度求索(DeepSeek)。我是一个由深度求索公司独立开发的智能助手DeepSeek-R1-Lite-Preview,很高兴为您提供服务!
Prompt: 12.3 t/s \| Generation: 3.3 t/s
注:运行时不需要 python,纯 CPU,作为边缘推理极佳。