> 技术文档 > 【后知后觉】llama.cpp-cpu-跑deepseek:671b_llamacpp纯cpu推理

【后知后觉】llama.cpp-cpu-跑deepseek:671b_llamacpp纯cpu推理

摘要:只是想试试,cpu跑deepseek:671b。

1.编译llama.cpp

参考资料:DeepSeek R1 671b 满血版部署笔记。

原味摘抄:“如果是纯 CPU 推理,考虑到我希望有 -march=native 来达到最大运算速度采用了本地编译,这里 llama.cpp 会帮我们加上 -mrach=native。”

git clone https://github.com/ggerganov/llama.cppcd llama.cppmkdir build && cd build#确认是否安装sudo apt install -y cmake gcc g++ libcurl4-openssl-dev #编译cmake .. -DCMAKE_BUILD_TYPE=Release -DLLAMA_CURL=ON -DCMAKE_INSTALL_PREFIX=~/LLAMAmake all -j install

必需将LLAMA/lib 加入系统搜寻库,不然无法运行

vim ~/.bashrcexport LD_LIBRARY_PATH=~/LLAMA/lib:$LD_LIBRARY_PATHexport PATH=$PATH:~/LLAMA/bin#保存退出后,执行source ~/.bashrc

2.使用huggingface-cli 下载模型

2.1安装huggingface-cli

安装python3、pip不在叙述,请自行安装

pip install huggingface_hub#在~/.bashrc加入bin路径vim ~/.bashrcexport PATH=$PATH:~/.local/bin#执行source ~/.bashrc

2.2 huggingface-cli命令解释

仅对使用到的参数做出说明,原始说明进入该网站查看:https://huggingface.co/docs/huggingface_hub/main/en/guides/cli

2.2.1下载多个文件

您还可以使用单个命令从存储库下载文件的子集。这可以通过两种方式完成。如果您已经有要下载的文件的精确列表,则只需按顺序提供它们即可:

huggingface-cli download gpt2 config.json model.safetensors

另一种方法是提供模式来过滤要使用 和 下载的文件。例如,如果要从 stabilityai/stable-diffusion-xl-base-1.0 下载所有 safetensors 文件,但 FP16 精度的文件除外:--include--exclude

huggingface-cli download stabilityai/stable-diffusion-xl-base-1.0 --include \"*.safetensors\" --exclude \"*.fp16.*\"*

2.2.2下载到本地文件夹

从 Hub 下载文件的推荐(也是默认)方法是使用 cache-system。但是,在某些情况下,您希望下载文件并将它们移动到特定文件夹。这对于使工作流程更接近 git 命令提供的内容很有用。您可以使用选项来执行此作。--local-dir

将在本地目录的根目录下创建一个文件夹,其中包含有关已下载文件的元数据。如果文件已经是最新的,则可以防止重新下载文件。如果元数据已更改,则会下载新的文件版本。这使得 针对仅提取最新更改进行了优化。.cache/huggingface/local-dir

huggingface-cli download adept/fuyu-8b model-00001-of-00002.safetensors --local-dir fuyu

    2.3官网下载

    首先在官网创建access Tokens

    在shell中,登录和下载

    huggingface-cli login#输入创建的access Tokens#理论命令,但是allow-patterns会报错,不加这个又不知道怎么指定模型huggingface-cli download unsloth/deepseek-r1-gguf --local-dir \"本地路径\" --repo-type model --allow-patterns \"*r1-q4_k_m*\"

    2.4镜像下载

    从官网几乎不可能下载下来。

    注:从镜像源下载,不需要登录也不需要access token。

    从上述官网或者镜像网站找到要下载的版本,取红字里面的表示文字。

    整个下载模型,以DeepSeek-R1:671b 1.25bit量化版本为例

    #指定镜像网站入口 export HF_ENDPOINT=https://hf-mirror.com #使用要下载的版本,替换下述粗体部分即可 huggingface-cli download unsloth/DeepSeek-R1-GGUF --include \"*IQ1_M*\" --local-dir ~/deepseek/unsloth

    指定单个文件下载,以DeepSeek-R1:671b 4bit量化版本为例

    huggingface-cli download unsloth/DeepSeek-R1-GGUF DeepSeek-R1-Q4_K_M/DeepSeek-R1-Q4_K_M-00008-of-00009.gguf --local-dir /home/hank8s/deepseek/unsloth

    大火的Qwen_QwQ-32B

    export HF_ENDPOINT=https://hf-mirror.comhuggingface-cli download bartowski/Qwen_QwQ-32B-GGUF Qwen_QwQ-32B-Q4_0.gguf --local-dir /home/hank8s/deepseek/bartowski

    3.加载模型

    1. GGUF 分片文件的命名需遵循固定格式:模型名-分片编号.gguf(如 DeepSeek-R1-Q4_K_M-00001-of-00009.gguf)。所有分片文件必须位于同一目录,且文件名中的编号需连续、完整。

    2. 直接指定主模型路径llama.cpp 中,只需指定 任意一个分片文件路径基础模型名(不带编号的部分),程序会自动加载所有分片。例如:

    3.2使用llama-cli交互运行

    #加载Q4量化版本llama-cli -t 48 -m unsloth/DeepSeek-R1-Q4_K_M/DeepSeek-R1-Q4_K_M-00001-of-00009.gguf -c 4096#--no-mmap 的话会直接把模型拉到内存里#-t 48 使用 48 个线程#-c 4096 context 设置为 4k#通过 --n-gpu-layers 参数限制 GPU 加速层数(若支持 GPU)

    备用

    #加载Q1量化版本llama-cli -t 48 -m unsloth/DeepSeek-R1-UD-IQ1_M/DeepSeek-R1-UD-IQ1_M-00001-of-00004.gguf -c 4096 --no-mmap#加载Q8量化本本llama-cli -t 48 -m /nfsdata/deepseek/unsloth/DeepSeek-R1-Q8_0/DeepSeek-R1.Q8_0-00001-of-00015.gguf -c 4096 --no-mmap

    备用

    #加载QwQ模型llama-cli -t 48 -m ~/deepseek/bartowski/Qwen_QwQ-32B-Q8_0.gguf -c 4096 --no-mmapllama-cli -t 12 -m Qwen_QwQ-32B-Q8_0.gguf -c 4096 --no-mmap --n-gpu-layers 10

    3.3使用llama-server运行模型

    llama-server -t 48 -m ./DeepSeek-R1-Q8_0/DeepSeek-R1.Q8_0-00001-of-00015.gguf \\ -c 4096 --no-mmap \\ --host 0.0.0.0 --port 9090

    3.4评测模型速度

    指令

    #Q4量化llama-bench --model unsloth/DeepSeek-R1-Q4_K_M/DeepSeek-R1-Q4_K_M-00001-of-00009.gguf --n-gen 128 --batch-size 2048#Q8量化llama-bench --model /nfsdata/deepseek/unsloth/DeepSeek-R1-Q8_0/DeepSeek-R1.Q8_0-00001-of-00015.gguf --n-gen 128 --batch-size 2048#Q1.25量化llama-bench --model unsloth/DeepSeek-R1-UD-IQ1_M/DeepSeek-R1-UD-IQ1_M-00001-of-00004.gguf --n-gen 128 --batch-size 2048#Qwen_QwQ-32Bllama-bench --model ~/deepseek/bartowski/Qwen_QwQ-32B-Q4_0.gguf --n-gen 128 --batch-size 2048 

    备用测试指令

    llama-bench --model ~/deepseek/bartowski/Qwen_QwQ-32B-Q4_0.gguf \\--n-gen 128 --batch-size 512 --ubatch-size 128 --threads 48 --numa isolate --mmap 1 --cpu-strict 1

    参数

    • CPU严格模式: 启用 (--cpu-strict 1)
    • 批处理大小 (batch size): 512
    • 子批处理大小 (ubatch size): 128
    • NUMA 隔离: 启用 (--numa isolate)
    • 内存映射 (mmap): 启用 (--mmap 1)

    3.4.1 DeepSeek-R1:671b-Q4量化 对比分析 (与 Qwen2 32B Q4_0 模型)

    模型 推理性能 (pp512) 生成性能 (tg128) Qwen2 32B Q4_0 71.26 tokens/s 13.98 tokens/s DeepSeek2 671B Q4_K 31.85 tokens/s 9.08 tokens/s

    结果

    备注:使用--n-gen 128 --batch-size 2048测试,生成性能为8点多tokens/s。

    3.4.2 DeepSeek-R1:671b-Q1.25量化

    测试结果与 Q4使用--n-gen 128 --batch-size 2048测试,生成性能为8点多tokens/s。

    这个ai解释说是因为cpu浮点-整数性能以及整数不同bit的性能是差不多的,所以结果是接近的,甚至Q1.25还不如Q4。

    3.4.3Qwen_QwQ-32B-Q4测试结果

    测试场景 吞吐量 (tokens/s) pp512 (推理性能) 71.26 ± 0.08 tg128 (生成性能) 13.98 ± 0.01

    4.后记

    4.1后记1-Ollama跑deepseek:671b

    问题:回答第二个问题,肯定崩溃,希望Ollama好好优化下。

    4.2后记2

    windows下使用LM Studio运行即可,因为上述下载的是gguf模型,与LM Studio运要求的模型格式一致。