文章目录[隐藏]
llama-cpp-python可以用来对GGUF模型进行推理。如果只需要 纯CPU模式 进行推理,可以直接使用以下指令安装:
pip install llama-cpp-python
如果需要使用GPU加速推理,则需要在安装时添加对库的编译参数。
1.安装VS
只需勾选最新的MSVC就行了,Windows 11 SDK是之前安装的,所以不知道有没有使用到这个。

2. 安装CUDA
CUDA 工具包下载 |NVIDIA 开发人员 (https://developer.nvidia.com/cuda-downloads)
如果CUDA正常安装完成,那就不用操作下面步骤。 (也可以打开目录检查一下)
安装完成后打开以下路径可以找到四个文件 (红色字体改成自己版本)
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\extras\visual_studio_integration\MSBuildExtensions

3. 安装LLama-cpp-python
编译过程较久,请耐心等待。
$env:CMAKE_ARGS = "-DGGML_CUDA=ON"
pip install llama-cpp-python
先前装了CPU版本的也可以将上面的pip安装下面指令重新安装:
# 需要先清空pip缓存 直接安装会直接使用之前的编译结果
pip cache purge
pip install --upgrade --force-reinstall llama-cpp-python
如果安装过程中出现以下错误,那就需要手动指定CUDA目录(可能你的VS没有装在C盘)。CMAKE一般会检查Visual Studio下的路径,例如:
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations
...
-- Could not find nvcc, please set CUDAToolkit_ROOT.
CMake Error at vendor/llama.cpp/ggml/src/ggml-cuda/CMakeLists.txt:151 (message):
CUDA Toolkit not found
...
检查你实际CUDA安装目录,一般CUDA目录为:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.8\extras\visual_studio_integration\MSBuildExtensions
在 CMAKE_ARGS 添加CUDA路径参数,因此完整指令应该为:
# Windows
$env:CMAKE_ARGS = "-DGGML_CUDA=ON -DCUDAToolkit_ROOT='C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\BuildCustomizations'"
pip install llama-cpp-python
注意:
Getting Started - llama-cpp-python
以上官网的配置方案不是CUDA的!!用官方默认示例试了一个小时才发现不对劲..
具体的可以参考:llama.cpp/docs/build.md at master · ggml-org/llama.cpp · GitHub
4. 测试
以 qwen2.5-3b-instruct-q4_k_m.gguf 模型为标准,模仿一只猫娘给大家看~

可以看到模型所有层已经加载到GPU显存中了

发表回复