本地部署大模型——Safetensors 格式

张开发
2026/6/22 11:05:09 15 分钟阅读
本地部署大模型——Safetensors 格式
偶然看到这样一篇公众号文章Baichuan-M2百川的医疗答卷模型解读于是一时兴起决定拿来作为自己第一个本地部署大模型的练手项目。环境和模型来源本机环境是 4*2080ti 共 88G 显存关于模型运行的硬件配置要求可以参考 这篇文章 以及模型自身的参考文档。模型下载链接Baichuan-M2-32B · HuggingFace当然也可以选择国内的魔搭社区Baichuan-M2-32B · ModelScope。此外还需要本机配置好 Ollama尽管 Ollama 现在已经拥有了自己的客户端界面但你仍然可以选择接入到例如 CherryStudio 中以使用更多功能。关于软件的配置非常简单这里提供一个参考这里包括了如何将 Ollama 中的模型导入 CherryStudio 中虽然笔者觉得后者在使用上已经足够浅显了DeepSeek R1OllamaCherry Studio实现本地知识库的搭建-阿里云开发者社区。部署过程由于从模型库下载得到的模型文件为 Safetensors 格式所以本次部署采用的方法是先使用 llama.cpp 工具将模型文件转换为 GGUF 格式事实上 Ollama 可以直接导入 Safetensors 格式的模型文件但是笔者只是一个什么也不懂的小白实在是不想去处理五花八门的配置文件所以选择了这样一个貌似更傻瓜式的处理方式——殊不知为了避开麻烦只会遇到更多麻烦。如果顺利的话参考以下教程可以非常简单地完成从模型下载到格式转换最后成功部署本文对于大多数教程都会呈现的步骤不予重复只记录个人实践中存在的问题。Ollama部署本地大模型DeepSeek-R1-Distill-Llama-70B-CSDN博客编译问题令人头疼的 Cmake在笔者的实操过程中最费时间的就是对 llama.cpp 进行编译安装的这一步时不时弹出的环境和模型文件下载报错也是件糟心的事情但这通常是网络的问题只能姑且受着也可以采取换源等方法进行解决这里不做阐述由于笔者是在 Windows 操作系统上进行部署并不具备 Linux 的天然优势所以必须自行配置好 Cmake 环境。下载安装比较简易的方法是下载 Visual Studio注意不是 Visual Studio Code这两个有本质上的区别在安装时勾选“C 桌面开发”Desktop development with C并确保”Windows 10/11 SDK”被选中通常默认包含。它会自动包含nmakeWindows 的 make 工具和其他必要的编译工具。也可以选择自行配置 MinGWGCC 在 Windows 上的移植版——对于嫌麻烦的笔者来说选择上一种方法。这里两种方法的教程都附上在 Windows 上安装和编译 llama.cpp-CSDN博客。需要注意的是执行类似于cmake -G MinGW Makefiles ..时需要确保build文件夹中是空的否则可能会出现这样的报错(base) PS E:\llama.cpp-master\build cmake -G MinGW Makefiles .. CMake Error: Error: generator : MinGW Makefiles Does not match the generator used previously: NMake Makefiles Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.安装了但是执行cmake命令还是报错问题当然还没有结束笔者又遇到了下面这样的报错(base) PS E:\llama.cpp-master\build cmake -G MinGW Makefiles .. -- The C compiler identification is GNU 15.2.0 -- The CXX compiler identification is GNU 15.2.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/mingw64/bin/cc.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/mingw64/bin/c.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Could NOT find Git (missing: GIT_EXECUTABLE) -- Could NOT find Git (missing: GIT_EXECUTABLE) CMake Warning at cmake/build-info.cmake:14 (message): Git not found. Build info will not be accurate. Call Stack (most recent call first): CMakeLists.txt:89 (include) -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed -- Check if compiler accepts -pthread -- Found Threads: TRUE -- Warning: ccache not found - consider installing it for faster compilation or disable this warning with GGML_CCACHEOFF -- CMAKE_SYSTEM_PROCESSOR: AMD64 -- GGML_SYSTEM_ARCH: x86 -- Including CPU backend -- Found OpenMP_C: -fopenmp (found version 4.5) -- Found OpenMP_CXX: -fopenmp (found version 4.5) -- Found OpenMP: TRUE (found version 4.5) -- x86 detected -- Adding CPU backend variant ggml-cpu: -marchnative -- ggml version: 0.0.0 -- ggml commit: unknown -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - found CMake Warning at common/CMakeLists.txt:32 (message): Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository. -- Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) CMake Error at common/CMakeLists.txt:85 (message): Could NOT find CURL. Hint: to disable this feature, set -DLLAMA_CURLOFF -- Configuring incomplete, errors occurred!根本看不懂这个时候明智的选择是丢给 AI它告诉我这个新的错误信息非常清晰指出了两个主要问题缺少 Git这是一个警告不会导致配置失败但会影响版本信息。缺少 CURL 库这是一个错误并且错误信息明确告诉你如何解决它。关于 curl 包的安装之后的流程基本是参考 AI 给出的回答然后逐个尝试看哪一个能解决问题最后笔者的解决方法大致包含以下两步以下回答来自 GLM-4.5curl 安装之 vcpkg 管理依赖vcpkg 是一个由微软维护的 C 包管理器可以非常方便地为 Visual Studio 下载和配置像 CURL 这样的库。安装 vcpkg:如果你还没有安装 vcpkg请先从它的 GitHub 仓库下载https://github.com/microsoft/vcpkg解压到一个你喜欢的位置例如C:\dev\vcpkg。集成 vcpkg 到 Visual Studio:在 PowerShell 中运行以下命令请将路径替换为你自己的 vcpkg 解压路径cd C:\dev\vcpkg vcpkg integrate install这个命令会修改你的 Visual Studio 安装使其在编译项目时自动使用 vcpkg 提供的库。使用 vcpkg 安装 CURL:现在在同一个 vcpkg 目录下运行vcpkg install curl这会下载并编译适用于 Visual Studio 的 CURL 库。重新配置项目:回到你的E:\llama.cpp-master\build目录删除旧的 CMake 缓存文件CMakeCache.txt和CMakeFiles文件夹然后重新运行 CMakecd E:\llama.cpp-master\build cmake …这次vcpkg 会告诉 CMake 在哪里找到 CURL配置应该会成功。安装 curl 的过程可能还会遇到(base) PS D:\vcpkg vcpkg integrate install Applied user-wide integration for this vcpkg root. CMake projects should use: -DCMAKE_TOOLCHAIN_FILED:/vcpkg/scripts/buildsystems/vcpkg.cmake All MSBuild C projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available. (base) PS D:\vcpkg vcpkg install curl Computing installation plan... The following packages are already installed: curl[core,sspi,ssl,non-http]:x64-windows8.15.0#1 Total install time: 532 us curl is compatible with built-in CMake targets: find_package(CURL REQUIRED) target_link_libraries(main PRIVATE CURL::libcurl) All requested installations completed successfully in: 532 us (base) PS E:\llama.cpp\build cmake .. -- Building for: Visual Studio 17 2022 -- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.19045. -- The C compiler identification is MSVC 19.44.35214.0 -- The CXX compiler identification is MSVC 19.44.35214.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/Visual Studio/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/Visual Studio/VC/Tools/MSVC/14.44.35207/bin/Hostx64/x64/cl.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Could NOT find Git (missing: GIT_EXECUTABLE) -- Could NOT find Git (missing: GIT_EXECUTABLE) CMake Warning at cmake/build-info.cmake:14 (message): Git not found. Build info will not be accurate. Call Stack (most recent call first): CMakeLists.txt:89 (include) -- Performing Test CMAKE_HAVE_LIBC_PTHREAD -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed -- Looking for pthread_create in pthreads -- Looking for pthread_create in pthreads - not found -- Looking for pthread_create in pthread -- Looking for pthread_create in pthread - not found -- Found Threads: TRUE -- Warning: ccache not found - consider installing it for faster compilation or disable this warning with GGML_CCACHEOFF -- CMAKE_SYSTEM_PROCESSOR: AMD64 -- CMAKE_GENERATOR_PLATFORM: -- GGML_SYSTEM_ARCH: x86 -- Including CPU backend -- Found OpenMP_C: -openmp (found version 2.0) -- Found OpenMP_CXX: -openmp (found version 2.0) -- Found OpenMP: TRUE (found version 2.0) -- x86 detected -- Performing Test HAS_AVX_1 -- Performing Test HAS_AVX_1 - Success -- Performing Test HAS_AVX2_1 -- Performing Test HAS_AVX2_1 - Success -- Performing Test HAS_FMA_1 -- Performing Test HAS_FMA_1 - Success -- Performing Test HAS_AVX512_1 -- Performing Test HAS_AVX512_1 - Failed -- Performing Test HAS_AVX512_2 -- Performing Test HAS_AVX512_2 - Failed -- Adding CPU backend variant ggml-cpu: /arch:AVX2 GGML_AVX2;GGML_FMA;GGML_F16C -- ggml version: 0.0.0 -- ggml commit: unknown CMake Warning at common/CMakeLists.txt:32 (message): Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository. -- Could NOT find CURL (missing: CURL_LIBRARY CURL_INCLUDE_DIR) CMake Error at common/CMakeLists.txt:85 (message): Could NOT find CURL. Hint: to disable this feature, set -DLLAMA_CURLOFF -- Configuring incomplete, errors occurred!一长串的其实只说了一件事curl 装了但是没找着。AI 提供的分析如下笔者已实践可行错误原因vcpkg 安装成功:vcpkg install curl命令已经成功将curl库安装到了D:\vcpkg目录中。缺少工具链文件: CMake 本身不知道 vcpkg 的存在。你需要通过一个特殊的“工具链文件”Toolchain File来告诉 CMake“当你需要找库的时候请先去D:\vcpkg里看看”。CMAKE_TOOLCHAIN_FILE: 这个文件就是D:/vcpkg/scripts/buildsystems/vcpkg.cmake。vcpkg 在integrate install时已经提示过你了。解决方案你需要重新运行 CMake并加上-DCMAKE_TOOLCHAIN_FILE参数。清理旧的配置: 首先确保删除掉之前失败的配置文件以免它们干扰新的配置。# 在 E:\llama.cpp\build 目录下执行 Remove-Item CMakeCache.txt -ErrorAction SilentlyContinue Remove-Item CMakeFiles -Recurse -ErrorAction SilentlyContinue重新运行 CMake (关键步骤): 现在在E:\llama.cpp\build目录下使用以下命令重新配置项目。注意DCMAKE_TOOLCHAIN_FILE参数。看到Found CURL这一行就说明配置成功了cmake -G Visual Studio 17 2022 -A x64 -DCMAKE_TOOLCHAIN_FILED:/vcpkg/scripts/buildsystems/vcpkg.cmake ..命令解释:G Visual Studio 17 2022: 指定使用 Visual Studio 2022 作为生成器。A x64: 强烈建议添加。这会告诉 CMake 生成一个 64位 的解决方案。对于拥有4张 RTX 2080 Ti 的系统64位是必须的可以充分利用大内存。DCMAKE_TOOLCHAIN_FILED:/vcpkg/scripts/buildsystems/vcpkg.cmake: 这是核心。它告诉 CMake 使用 vcpkg 的工具链文件从而能自动找到curl等库。运行这个命令后你应该会看到类似这样的输出-- Using vcpkg installed libraries -- Found CURL: D:/vcpkg/installed/x64-windows/lib/libcurl.lib (found version 8.15.0) ...编译项目一旦 CMake 配置成功下一步就是编译。 在E:\llama.cpp\build目录下运行以下命令cmake --build . --config Release命令解释:cmake --build .: 告诉 CMake 使用当前目录即build目录中的项目文件进行构建。-config Release: 指定构建Release发布版本这是优化过的、性能最好的版本。 编译过程可能需要几分钟。完成后你可以在E:\llama.cpp\build\Release目录下找到所有编译好的可执行文件比如llama-cli.exe、llama-server.exe等。可能还有问题——心得在这之后的转换格式阶段可能还会遇到有关 python 包的安装问题或者环境冲突问题这些问题的处理就比较简单了笔者的建议是遇到长串报错自己完全无法判断就交给 AI 分析只要有错就一股脑地丢给它也不失为一种省力的方法AI 提供的方法针对简单问题往往是有效的但是遇到复杂问题则有可能不知所云这比较依赖不同模型自己的能力。所以笔者认为借助 AI 分析问题是快捷的但知晓问题之后不必死磕 AI 让其提供解决方案搜索引擎依旧是最强大的第二大脑。最后附上一张笔者部署成功的截图以作留念。

更多文章