{"id":102,"date":"2025-12-30T14:49:49","date_gmt":"2025-12-30T06:49:49","guid":{"rendered":"http:\/\/blog.thirdbody.cn\/?p=102"},"modified":"2025-12-30T14:49:49","modified_gmt":"2025-12-30T06:49:49","slug":"%e4%bd%bf%e7%94%a8llama-cpp%e5%9c%a8%e7%ba%afcpu%e7%8e%af%e5%a2%83%e4%b8%ad%e8%bf%9b%e8%a1%8c%e5%a4%a7%e6%a8%a1%e5%9e%8b%e9%87%8f%e5%8c%96%e5%92%8c%e9%83%a8%e7%bd%b2","status":"publish","type":"post","link":"http:\/\/blog.thirdbody.cn\/index.php\/2025\/12\/30\/%e4%bd%bf%e7%94%a8llama-cpp%e5%9c%a8%e7%ba%afcpu%e7%8e%af%e5%a2%83%e4%b8%ad%e8%bf%9b%e8%a1%8c%e5%a4%a7%e6%a8%a1%e5%9e%8b%e9%87%8f%e5%8c%96%e5%92%8c%e9%83%a8%e7%bd%b2\/","title":{"rendered":"\u4f7f\u7528llama.cpp\u5728\u7eafCPU\u73af\u5883\u4e2d\u8fdb\u884c\u5927\u6a21\u578b\u91cf\u5316\u548c\u90e8\u7f72"},"content":{"rendered":"<h2>\u4f7f\u7528llama.cpp\u5728\u7eafCPU\u73af\u5883\u4e2d\u8fdb\u884c\u5927\u6a21\u578b\u91cf\u5316\u548c\u90e8\u7f72<\/h2>\n<h3>1. llama.cpp\u73af\u5883\u7684\u5b89\u88c5<\/h3>\n<ul>\n<li>\n<p>\u514b\u9686\u8fdc\u7a0bllama.cpp\u9879\u76ee<\/p>\n<pre><code class=\"language-shell\">git clone https:\/\/gitclone.com\/github.com\/ggerganov\/llama.cpp\ncd .\/llama.cpp<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-2.png\" alt=\"llamacpp\u9879\u76ee\u514b\u9686\" \/><\/p>\n<p>\u82e5\u7f51\u7edc\u73af\u5883\u4e0d\u540c\u6216\u60f3\u7528\u672c\u6587\u4e00\u81f4\u7684llamacpp\u7248\u672c\uff0c\u53ef\u4ee5\u5728\u6b64\u5904\u76f4\u63a5\u4e0b\u8f7d<code>llamacpp<\/code>\u5de5\u7a0b:  <a href=\"http:\/\/ftp.thirdbody.cn\/study\/llamacpp.tar.gz\">llamacpp<\/a><\/p>\n<\/li>\n<li>\n<p>\u5b89\u88c5Python\u73af\u5883\u4f9d\u8d56<\/p>\n<ul>\n<li>\u4f7f\u7528conda\u521b\u5efaPython\u865a\u62df\u73af\u5883(Python &gt;= 3.10)<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">conda create --name llamacpp python=3.11 -y<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-1.png\" alt=\"\u521b\u5efaPython\u865a\u62df\u73af\u5883\" \/><\/p>\n<ul>\n<li>\u5b89\u88c5\u4f9d\u8d56\u5305<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">conda activate llamacpp\npip install -r .\/requirements.txt<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-7.png\" alt=\"\u5b89\u88c5Python\u4f9d\u8d56\" \/><\/p>\n<\/li>\n<li>\n<p>\u6784\u5efa\u9879\u76ee<\/p>\n<ul>\n<li>\u521b\u5efa\u6784\u5efa\u76ee\u5f55<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">mkdir build\ncd build<\/code><\/pre>\n<ul>\n<li>\u914d\u7f6e\uff08\u7eaf CPU\uff0c\u65e0 CUDA\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">cmake .. -DLLAMA_CUDA=OFF -DLLAMA_METAL=OFF -DLLAMA_HIPBLAS=OFF -DCMAKE_BUILD_TYPE=Release<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-4.png\" alt=\"\" \/><\/p>\n<ul>\n<li>\u7f16\u8bd1\uff08\u4f7f\u7528 8 \u7ebf\u7a0b\u52a0\u901f\uff09<\/li>\n<\/ul>\n<pre><code class=\"language-shell\">cmake --build . --config Release -j8<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-5.png\" alt=\"llamacpp\u9879\u76ee\u7f16\u8bd1\" \/><\/p>\n<p>\u7f16\u8bd1\u6210\u529f\u540e<code>.\/build\/bin<\/code>\u76ee\u5f55\u4e0b\u6709<code>llama-quantize<\/code>\u548c<code>llama-server<\/code>\u7684\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-6.png\" alt=\"llama-quantize\u548cllama-server\u53ef\u6267\u884c\u6587\u4ef6\" \/><\/p>\n<\/li>\n<\/ul>\n<h3>2. \u57fa\u4e8ellama.cpp\u7684\u5927\u6a21\u578b\u91cf\u5316(\u4ee5<code>Qwen3-30B-A3B<\/code>\u6a21\u578b\u4e3a\u4f8b)<\/h3>\n<ul>\n<li>\n<p>\u4e0b\u8f7d\u6a21\u578b<\/p>\n<p>\u82e5\u6ca1\u6709\u8be5\u6a21\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528modelscope\u547d\u4ee4\u884c\u5de5\u5177\u4ecemodelscope\u5b98\u7f51\u8fdb\u884c\u4e0b\u8f7d\uff1a<\/p>\n<pre><code class=\"language-shell\">conda activate modelscope # modelscope\u4e3aconda\u4e2d\u5b89\u88c5\u4e86modelscope\u5305\u7684Python\u865a\u62df\u73af\u5883\nmodelscope download Qwen\/Qwen3-30B-A3B --local_dir .\/Qwen3-30B-A3B\/<\/code><\/pre>\n<\/li>\n<li>\n<p>\u82e5\u539f\u59cb\u6a21\u578b\u4e0d\u662f<code>gguf<\/code>\u683c\u5f0f\uff0c\u5219\u9700\u8981\u5148\u8f6c\u6362\u6210<code>gguf<\/code>\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"language-shell\"># \u57fa\u7840\u8f6c\u6362\u547d\u4ee4\npython convert_hf_to_gguf.py .\/your-model-path --outfile .\/output-model.gguf\n# \u793a\u4f8b\npython convert_hf_to_gguf.py \/home\/qll\/models\/Qwen3-30B-A3B\/ --outfile .\/Qwen3-30B-A3B_f16.gguf<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-8.png\" alt=\"gguf\u683c\u5f0f\u8f6c\u63621\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-9.png\" alt=\"gguf\u683c\u5f0f\u8f6c\u63622\" \/><\/p>\n<\/li>\n<li>\n<p>\u6a21\u578b\u91cf\u5316<\/p>\n<pre><code class=\"language-shell\"># 4bit\u91cf\u5316\u547d\u4ee4\n.\/build\/bin\/llama-quantize .\/output-model.gguf .\/output-model-q4_k_m.gguf Q4_K_M\n# \u793a\u4f8b\n.\/build\/bin\/llama-quantize .\/Qwen3-30B-A3B_f16.gguf .\/Qwen-30B-A3B-q4_k_m.gguf Q4_K_M<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-10.png\" alt=\"\u6a21\u578b\u91cf\u53161\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-11.png\" alt=\"\u6a21\u578b\u91cf\u53162\" \/><\/p>\n<p>\u91cf\u5316\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li><code>Q4_0<\/code>\uff1a\u57fa\u78404bit\u91cf\u5316<\/li>\n<li><code>Q4_K_S<\/code>\uff1a4bit\u5c0f\u6a21\u578b\u4f18\u5316<\/li>\n<li><code>Q4_K_M<\/code>\uff1a4bit\u4e2d\u7b49\u6a21\u578b\u4f18\u5316\uff08\u63a8\u8350\uff09\uff0c\u5e73\u8861\u6027\u80fd\u548c\u7cbe\u5ea6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3. \u57fa\u4e8ellama.cpp\u7684\u6a21\u578b\u90e8\u7f72<\/h3>\n<pre><code class=\"language-shell\"># \u652f\u6301openai api\u517c\u5bb9\u7684\u65b9\u5f0f\u8fdb\u884c\n.\/build\/bin\/llama-server -m qwen3-32b-a3b-q4_k_m.gguf --host 0.0.0.0 --port 8080 -c 4096 --threads 8 --cont-batching<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-12.png\" alt=\"\u6a21\u578b\u90e8\u7f721\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-13.png\" alt=\"\u6a21\u578b\u90e8\u7f722\" \/><\/p>\n<h3>4. \u6d4b\u8bd5<\/h3>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>\/v1\/models<\/code>\u63a5\u53e3\u83b7\u53d6\u53ef\u7528\u7684\u6a21\u578b<\/p>\n<pre><code class=\"language-shell\">curl -X GET http:\/\/10.0.14.60:8080\/v1\/models<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-14.png\" alt=\"\u83b7\u53d6\u53ef\u7528\u6a21\u578b\" \/><\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528<code>\/v1\/chat\/completions<\/code>\u63a5\u53e3\u8fdb\u884c\u4f1a\u8bdd<\/p>\n<pre><code class=\"language-shell\">curl -X POST http:\/\/10.0.14.60:8080\/v1\/chat\/completions -H \"Content-Type: application\/json\" -d '{\"model\": \"Qwen-30B-A3B-q4_k_m.gguf\", \"messages\": [{\"role\": \"user\", \"content\": \"\/no_think\\n\u4f60\u597d\uff0c\u4f60\u662f\u8c01\"}], \"stream\": false}'<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/cdn.thirdbody.cn\/blog\/llamacpp-15.png\" alt=\"\u8c03\u7528\u4f1a\u8bdd\u63a5\u53e3\" \/><\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528llama.cpp\u5728\u7eafCPU\u73af\u5883\u4e2d\u8fdb\u884c\u5927\u6a21\u578b\u91cf\u5316\u548c\u90e8\u7f72 1. llama.cpp\u73af\u5883\u7684\u5b89\u88c5 \u514b\u9686\u8fdc\u7a0blla [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":103,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[58,28],"class_list":["post-102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-model_deployment","tag-llama-cpp","tag-28"],"_links":{"self":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":1,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":104,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/104"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media\/103"}],"wp:attachment":[{"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.thirdbody.cn\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}