在LFS中CPU架构LLaMA.cpp安装

我的物理主机使用了 LFS(Linux from scratch) / BLFS(Beyond Linux from scratch) 构建,力图对系统进行精简和性能优化。由于 HPE ProLiant DL380 Gen9服务器 满配了 768GB 物理能够,能够满血 本地化部署DeepSeek-R1 CPU架构 ,所以本文尝试在 LFS(Linux from scratch) / BLFS(Beyond Linux from scratch) 编译安装 LLaMA

编译准备

LFS(Linux from scratch) 部署中没有 cmake ,所以在 BLFS(Beyond Linux from scratch) 补全安装 CMake

编译安装

本地化部署DeepSeek-R1 CPU架构 准备,本地编译 llama.cpp

  • 下载 llama.cpp 源代码:

下载 llama.cpp 源代码
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp

准备阶段

  • 针对CPU架构编译准备:

针对CPU架构编译
# NOT USE -DGGML_CUDA=ON, ONLY support CPU
cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DLLAMA_CURL=ON

报错 gmake :

缺少 gmake 命令
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - broken
CMake Error at /usr/share/cmake-3.30/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/usr/bin/cc"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/huggingface.co/llama.cpp/build/CMakeFiles/CMakeScratch/TryCompile-2FKnKy'
    
    Run Build Command(s): /usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_f3c55/fast
    no such file or directory
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!

这是因为没有构建 gmake 软链接,参考 Debian 环境:

创建 gmake 软链接
cd /usr/bin
ln -s make gmake

编译阶段

  • 针对CPU架构编译:

针对CPU架构编译
# -j 40 for system wich 48 cpu core
cmake --build llama.cpp/build --config Release --clean-first -j 40
#cp llama.cpp/build/bin/llama-* llama.cpp

编译报错:

针对CPU架构编译错误1
...
[ 12%] Built target ggml
gmake[2]: *** No rule to make target '/usr/lib/gcc/x86_64-linux-gnu/12/libgomp.so', needed by 'bin/llama-gguf-hash'.  Stop.
gmake[2]: *** Waiting for unfinished jobs....
gmake[2]: *** No rule to make target '/usr/lib/gcc/x86_64-linux-gnu/12/libgomp.so', needed by 'bin/llama-gguf'.  Stop.
gmake[2]: *** Waiting for unfinished jobs....
...
[ 22%] Building CXX object src/CMakeFiles/llama.dir/unicode.cpp.o
gmake[1]: *** [CMakeFiles/Makefile2:2797: examples/gguf/CMakeFiles/llama-gguf.dir/all] Error 2
gmake[1]: *** Waiting for unfinished jobs....
gmake[1]: *** [CMakeFiles/Makefile2:2660: examples/gguf-hash/CMakeFiles/llama-gguf-hash.dir/all] Error 2
[ 23%] Linking CXX static library libllama.a
[ 23%] Built target llama
gmake: *** [Makefile:146: all] Error 2

这个报错表明系统可能缺少 libgomp.cpp (参考 error while loading shared libraries: libgomp.so.1: , wrong GCC version? ),依赖库 libgomp ?

我在 Debian 系统中可以找到 /usr/lib/x86_64-linux-gnu/libgomp.so.1 ,但是我的 LFS(Linux from scratch) 系统中缺少。所以寻找 libgomp (根据 Debian 系统查询 libgomp.so.1 属于 libgomp1 包)

备注

libgomp : GNU Offloading and Multi Processing Runtime Library

gomp :

The GOMP project consists of implementation of OpenMP and OpenACC to permit annotating the source code to permit running it concurrently with thread parallelization and on offloading devices (accelerators such as GPUs), including the associated run-time library and API routines. Both OpenMP and OpenACC are supported with GCC's C, C++ and Fortran compilers.

注意到这个 libgomp 是跟随 GCC 提供的: 参考 libgomp > Enabling OpenMP : 要激活 C/C++和Fortran的 OpenMP 扩展,需要在编译时使用 -fopenmp

我忽然发现一个问题,报错信息中提示:

No rule to make target '/usr/lib/gcc/x86_64-linux-gnu/12/libgomp.so', needed by 'bin/llama-gguf-hash'.

但是,只有 Debian 系统目前使用的是 gcc-12 ,而我在 LFS(Linux from scratch) 中使用的是 gcc-14.2 ,目前在我的 LFS(Linux from scratch) 系统中, /usr/lib/libgomp.so 是存在的。看起来像是之前在 Debian 12 中编译的配置残留导致的。

所以,我删除源代码目录下的 build 子目录重新编译:

删除 build 目录重新配置编译, 成功
rm -rf llama.cpp/build

cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DLLAMA_CURL=ON

cmake --build llama.cpp/build --config Release --clean-first -j 40

终于成功了

运行

参考