测试:Ollama 如何利用英特尔 CPU 的性能核心和高效核心
Ollama 在 Intel CPU 上的效率核心与性能核心对比
目录
我有一个理论要测试——如果利用所有Intel CPU核心是否能提高LLMs的速度?。
困扰我的是,新的gemma3 27位模型(gemma3:27b,17GB在ollama上)无法适应我GPU的16GB显存,部分运行在CPU上。
更准确地说:
ollama ps
显示:
gemma3:27b a418f5838eaf 22 GB 29%/71% CPU/GPU
虽然看起来不那么糟糕,但它是分层运行的。实际负载是:GPU:28%,CPU: 560%。是的,多个核心被使用了。
这里有一个想法:
如果我们推动Ollama使用所有Intel CPU核心——包括性能型和高效型核心呢?
OLLAMA_NUM_THREADS配置参数
Ollama有一个环境变量配置参数OLLAMA_NUM_THREADS,它应该告诉Ollama应该使用多少线程和核心。
我首先尝试限制为3个核心:
sudo xed /etc/systemd/system/ollama.service
# 添加 OLLAMA_NUM_THREADS=3 作为
# Environment="OLLAMA_NUM_THREADS=3"
sudo systemctl daemon-reload
sudo systemctl restart ollama
但没起作用。
运行Gemma 3 27B LLM时,Ollama仍然使用了约560%的CPU。
运气不好。
num_thread调用选项
让我们尝试调用:
curl http://localhost:11434/api/generate -d '
{
"model": "gemma3:27b",
"prompt": "Why is the blue sky blue?",
"stream": false,
"options":{
"num_thread": 8
}
}' | jq .
结果:
- CPU使用率:585%
- GPU使用率:25%
- GPU功耗:67w
- 性能评估:6.5 tokens/sec
现在我们尝试双倍核心,告诉Ollama使用性能型和高效型核心的混合:
curl http://localhost:11434/api/generate -d '
{
"model": "gemma3:27b",
"prompt": "Why is the blue sky blue?",
"stream": false,
"options":{
"num_thread": 16
}
}' | jq .
结果:
- CPU使用率:1030%
- GPU使用率:26%
- GPU功耗:70w
- 性能评估:7.4 t/s
很好!性能提高了约14%!
现在我们来极端一点!使用所有物理核心:
curl http://localhost:11434/api/generate -d '
{
"model": "gemma3:27b",
"prompt": "Why is the blue sky blue?",
"stream": false,
"options":{
"num_thread": 20
}
}' | jq .
结果:
- CPU使用率:1250%
- GPU使用率:10-26%(不稳定)
- GPU功耗:67w
- 性能评估:6.9 t/s
现在我们看到一些性能下降。 让我们尝试使用8个性能型+4个高效型:
curl http://localhost:11434/api/generate -d '
{
"model": "gemma3:27b",
"prompt": "Why is the blue sky blue?",
"stream": false,
"options":{
"num_thread": 12
}
}' | jq .
结果:
- CPU使用率:801%
- GPU使用率:27%(不稳定)
- GPU功耗:70w
- 性能评估:7.1 t/s
这里那里。
为了比较,运行Gemma 3 14b,它不如Gemma 27b聪明,但可以很好地适应GPU显存。
curl http://localhost:11434/api/generate -d '
{
"model": "gemma3:12b-it-qat",
"prompt": "Why is the blue sky blue?",
"stream": false
}' | jq .
结果:
- CPU使用率:106%
- GPU使用率:94%(不稳定)
- GPU功耗:225w
- 性能评估:61.1 t/s
这就是我们所说的性能。 虽然Gemma 3 27b比14b更聪明,但并不是10倍!
结论
如果LLM无法适应GPU显存,并且Ollama将一些层卸载到CPU上:
- 通过提供
num_thread
参数,可以将LLM性能提高10-14%。 - 由于卸载导致的性能下降更高,且无法通过这种提升来弥补。
- 最好拥有更强大的GPU和更多显存。虽然RTX 3090比RTX 5080更好,但我不拥有任何这些…