Try   HackMD

以原生方式建置llama.cpp for Windows

前言

本篇為「Homemade version of "AI in a Box"」專案的系列文章,關於該專案的整體描述請參閱這篇內容。
前面一篇文章「使用llama.cpp來執行Taiwan-LLM大語言模型」撰寫當下因不熟悉CMake的使用方式而選擇了使用GCC與Makefile方式來建置llama.cpp專案。但在Windows平台上更為原生的建置方式應是使用CMake,並且搭配微軟的C/C++編譯器(MSVC,附帶於Visual Studio內);但llama.cpp專案的README.md對於這方面的說明語焉不詳,自我摸索後完成驗證並將過程整理在此。
由於是延續前一篇文章「使用llama.cpp來執行Taiwan-LLM大語言模型」,因此接下來不會完整的講述所有過程。只會將建置或執行的差異項目條列說明。

準備工作

與前一篇文章不同處是不再使用w64devkit編譯環境。首先安裝CMake(下載Windows x64 Installer後進行安裝,安裝過程中選擇"Add CMake to the system path for the current user"),接著安裝Visual Studio Community。並在Visual Studio Community工作負載中選擇安裝「使用C++的桌面開發」:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

完成安裝程序後,執行開始功能表的「x64 Native Tools Command Prompt for VS 2022」進入命令提示字元;做為專案建置與執行之用。

建置llama.cpp

參照官方說明進行建置,這些動作都是在「x64 Native Tools Command Prompt for VS 2022」視窗中進行:
> mkdir build
> cd build
> cmake ..
> cmake --build . --config Release

量化模型

在「x64 Native Tools Command Prompt for VS 2022」視窗中,用以下命令進行模型量化:
> .\bin\Release\quantize.exe ..\models\Taiwan-LLM-7B\ggml-model-f16.gguf ..\models\Taiwan-LLM-7B\ggml-model-q4_0.gguf q4_0

執行對話應用程序

在「x64 Native Tools Command Prompt for VS 2022」視窗中,執行以下命令執行應用程序並進行互動:
> .\bin\Release\main.exe -m ..\models\Taiwan-LLM-7B\ggml-model-q4_0.gguf -n -1 --repeat_penalty 1.0 --color --interactive-first -r "User:" -f ..\chat-with-ai.txt

結語

之所以改用CMake搭配MSVC方式來建置llama.cpp專案,是因為後續要進行的whisper.cpp專案只能以CMake方式進行;而選擇使用MSVC則是考量後續與Windows應用程式框架進行整合。