{"id":2508,"date":"2026-07-31T18:16:50","date_gmt":"2026-07-31T10:16:50","guid":{"rendered":"https:\/\/www.o6s.net\/?p=2508"},"modified":"2026-07-31T18:20:48","modified_gmt":"2026-07-31T10:20:48","slug":"lama-cpp-%e5%b7%b2%e5%a4%84%e7%90%86%e5%86%85%e5%ae%b9%ef%bc%88kv-prompt%ef%bc%89%e7%bc%93%e5%ad%98%e5%ae%8c%e6%95%b4%e9%85%8d%e7%bd%ae%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.o6s.net\/index.php\/2026\/07\/31\/lama-cpp-%e5%b7%b2%e5%a4%84%e7%90%86%e5%86%85%e5%ae%b9%ef%bc%88kv-prompt%ef%bc%89%e7%bc%93%e5%ad%98%e5%ae%8c%e6%95%b4%e9%85%8d%e7%bd%ae%e6%8c%87%e5%8d%97\/","title":{"rendered":"lama.cpp \u5df2\u5904\u7406\u5185\u5bb9\uff08KV\/Prompt\uff09\u7f13\u5b58\u5b8c\u6574\u914d\u7f6e\u6307\u5357"},"content":{"rendered":"<p>llama.cpp \u91cc<strong>\u5df2\u5904\u7406\u6587\u672c\u7f13\u5b58\u672c\u8d28\u662f KV Cache<\/strong>\uff1a\u6a21\u578b\u524d\u7f6eprompt\u3001\u7cfb\u7edf\u63d0\u793a\u3001\u957f\u5bf9\u8bdd\u524d\u7f00\u8ba1\u7b97\u540e\u7684Key\/Value\u5f20\u91cf\uff0c\u7f13\u5b58\u540e\u91cd\u590d\u8bf7\u6c42\u65e0\u9700\u91cd\u65b0\u5168\u91cf\u9884\u586b\u5145\uff08prefill\uff09\uff0c\u901f\u5ea6\u63d0\u5347\u6570\u500d\u3002\u52064\u79cd\u7f13\u5b58\u65b9\u6848\uff0c\u540c\u65f6\u914d\u5957\u4f60\u4e4b\u524d\u5199\u7684Windows\u73af\u5883\u53d8\u91cf\u7f16\u8f91\u5668\u53ef\u4e00\u952e\u914d\u7f6e\u3002<\/p>\n<h2 id=\"_2\">\u4e00\u30014\u79cd\u7f13\u5b58\u673a\u5236\u533a\u5206\uff08\u6309\u9700\u9009\u7528\uff09<\/h2>\n<h3 id=\"_3\">1. \u5185\u5b58\u524d\u7f00\u81ea\u52a8\u590d\u7528\u7f13\u5b58\uff08\u9ed8\u8ba4\u5f00\u542f\uff0c\u6700\u5e38\u7528\uff09<\/h3>\n<p><strong>\u4f5c\u7528<\/strong>\uff1a\u540c\u6a21\u578b\u591a\u8bf7\u6c42\u5171\u4eab\u76f8\u540c\u524d\u7f00\uff08\u56fa\u5b9aSystem\u63d0\u793a\u8bcd\u3001\u901a\u7528\u6a21\u677f\uff09\uff0c\u81ea\u52a8\u5339\u914d\u590d\u7528KV\uff0c\u91cd\u542f\u7a0b\u5e8f\u4e22\u5931\u7f13\u5b58\u3002<\/p>\n<h4 id=\"_5\">\u5173\u952e\u53c2\u6570\/\u73af\u5883\u53d8\u91cf\uff08\u53ef\u5728\u4f60\u7684\u73af\u5883\u7f16\u8f91\u5668\u76f4\u63a5\u8bbe\u7f6e\uff09<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u5bf9\u5e94\u542f\u52a8\u53c2\u6570<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>LLAMA_ARG_CACHE_RAM<\/code><\/td>\n<td><code>--cache-ram N<\/code><\/td>\n<td>\u5185\u5b58\u7f13\u5b58\u6700\u5927\u5bb9\u91cf MiB\uff0c\u9ed8\u8ba48192\uff1b<code>0<\/code>\u5173\u95ed\uff0c<code>-1<\/code>\u65e0\u4e0a\u9650<\/td>\n<\/tr>\n<tr>\n<td><code>LLAMA_ARG_CACHE_REUSE<\/code><\/td>\n<td><code>--cache-reuse N<\/code><\/td>\n<td>\u53ef\u590d\u7528\u7f13\u5b58\u5757\u6700\u5c0ftoken\u957f\u5ea6\uff0c\u5efa\u8bae512\/1024\uff0c\u5f00\u542f\u524d\u7f00\u590d\u7528<\/td>\n<\/tr>\n<tr>\n<td><code>LLAMA_ARG_KV_UNIFIED<\/code><\/td>\n<td><code>--kv-unified<\/code><\/td>\n<td>\u7edf\u4e00KV\u7f13\u51b2\u533a\uff0c\u591a\u4f1a\u8bdd\u5171\u4eab\u7f13\u5b58\uff0c\u9ed8\u8ba4\u5f00\u542f<\/td>\n<\/tr>\n<tr>\n<td><code>LLAMA_ARG_CACHE_IDLE_SLOTS<\/code><\/td>\n<td><code>--cache-idle-slots<\/code><\/td>\n<td>\u4fdd\u7559\u7a7a\u95f2\u4f1a\u8bdd\u7f13\u5b58\u4e0d\u7acb\u5373\u91ca\u653e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u793a\u4f8b\u542f\u52a8\u547d\u4ee4\uff08main\/llama-server\u901a\u7528\uff09<\/strong><\/p>\n<pre><code class=\"language-bash hljs\"><span class=\"hljs-comment\"># Windows cmd\/PowerShell<\/span>\r\n<span class=\"hljs-built_in\">set<\/span> LLAMA_ARG_CACHE_RAM=16384\r\n<span class=\"hljs-built_in\">set<\/span> LLAMA_ARG_CACHE_REUSE=512\r\nllama-server -m qwen3-8b.gguf -c 8192 -ngl 35\r\n<\/code>\u200b<\/pre>\n<h3 id=\"_9\">2. \u6301\u4e45\u5316Prompt\u7f13\u5b58\uff08\u5355\u4f1a\u8bdd\u56fa\u5b9a\u524d\u7f00\uff0c\u4fdd\u5b58\u5230\u78c1\u76d8\u6587\u4ef6\uff09<\/h3>\n<p><strong>\u4f5c\u7528<\/strong>\uff1a\u628a\u56fa\u5b9a\u7cfb\u7edf\u63d0\u793a\u8bcd\u3001\u8d85\u957f\u524d\u7f6e\u6587\u6863\u4e00\u6b21\u6027\u8ba1\u7b97\uff0c\u4fdd\u5b58<code>.bin<\/code>\u7f13\u5b58\u6587\u4ef6\uff0c\u4e0b\u6b21\u542f\u52a8\u76f4\u63a5\u52a0\u8f7d\uff0c\u8df3\u8fc7prefill\u3002<\/p>\n<h4 id=\"_11\">\u53c2\u6570<\/h4>\n<ul>\n<li>\u542f\u52a8\u53c2\u6570\uff1a<code>--prompt-cache cache_sys.bin<\/code><\/li>\n<li>\u65e0\u72ec\u7acb\u73af\u5883\u53d8\u91cf\uff0c\u53ea\u80fd\u547d\u4ee4\u884c\u4f20\u5165<\/li>\n<\/ul>\n<p><strong>\u6d41\u7a0b<\/strong><\/p>\n<ol>\n<li>\u7b2c\u4e00\u6b21\u8fd0\u884c\u5e26\u5b8c\u6574\u7cfb\u7edfprompt\uff0c\u751f\u6210\u7f13\u5b58\u6587\u4ef6<\/li>\n<\/ol>\n<pre><code class=\"language-bash hljs\">llama-cli -m model.gguf -c 8192 --prompt-cache sys_cache.bin -f system_prompt.txt\r\n<\/code>\u200b<\/pre>\n<ol start=\"2\">\n<li>\u540e\u7eed\u76f4\u63a5\u52a0\u8f7d\u7f13\u5b58\uff0c\u65e0\u9700\u91cd\u590d\u5904\u7406\u7cfb\u7edf\u63d0\u793a<\/li>\n<\/ol>\n<pre><code class=\"language-bash hljs\">llama-cli -m model.gguf -c 8192 --prompt-cache sys_cache.bin\r\n<\/code>\u200b<\/pre>\n<h3 id=\"_18\">3. Server\u591a\u4f1a\u8bddSlot\u6301\u4e45\u5316\u7f13\u5b58\uff08\u957f\u5bf9\u8bdd\u65ad\u70b9\u7eed\u804a\uff09<\/h3>\n<p><strong>\u4f5c\u7528<\/strong>\uff1allama-server HTTP\u670d\u52a1\u6bcf\u4e2a\u5bf9\u8bdd\u72ec\u7acbslot\uff0c\u624b\u52a8\u4fdd\u5b58\/\u52a0\u8f7d\u5355\u6761\u5bf9\u8bdd\u5b8c\u6574KV\u7f13\u5b58\uff0c\u91cd\u542f\u670d\u52a1\u4ecd\u53ef\u6062\u590d\u804a\u5929\u4e0a\u4e0b\u6587\u3002<\/p>\n<h4 id=\"_20\">\u6838\u5fc3\u53c2\u6570\/\u73af\u5883\u53d8\u91cf<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u53c2\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>LLAMA_ARG_SLOT_SAVE_PATH<\/code><\/td>\n<td><code>--slot-save-path .\/kv_slots<\/code><\/td>\n<td>\u6307\u5b9a\u7f13\u5b58\u5b58\u653e\u76ee\u5f55\uff0c\u542f\u7528slot\u8bfb\u5199API<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u4f7f\u7528\u65b9\u5f0f<\/strong><\/p>\n<ol>\n<li>\u542f\u52a8\u670d\u52a1\u5f00\u542fslot\u6301\u4e45\u5316<\/li>\n<\/ol>\n<pre><code class=\"language-powershell hljs\"><span class=\"hljs-variable\">$env:LLAMA_ARG_SLOT_SAVE_PATH<\/span>=<span class=\"hljs-string\">\"D:\\llama_cache\\slots\"<\/span>\r\nllama<span class=\"hljs-literal\">-server<\/span> <span class=\"hljs-literal\">-m<\/span> model.gguf <span class=\"hljs-literal\">-c<\/span> <span class=\"hljs-number\">16384<\/span> <span class=\"hljs-literal\">-ngl<\/span> <span class=\"hljs-number\">40<\/span> <span class=\"hljs-literal\">--port<\/span> <span class=\"hljs-number\">8080<\/span>\r\n<\/code>\u200b<\/pre>\n<ol start=\"2\">\n<li>HTTP API \u4fdd\u5b58\/\u6062\u590d\u5bf9\u8bdd\u7f13\u5b58<\/li>\n<\/ol>\n<pre><code class=\"language-http hljs\"># \u4fdd\u5b58slot 1\u5230\u6587\u4ef6chat1.bin\r\nPOST http:\/\/127.0.0.1:8080\/slots\/1?action=save\r\n<span class=\"hljs-attribute\">Content-Type<\/span><span class=\"hljs-punctuation\">: <\/span>application\/json\r\n{\"filename\":\"chat1.bin\"}\r\n\r\n# \u91cd\u542f\u670d\u52a1\u540e\u6062\u590d\u5bf9\u8bdd\r\nPOST http:\/\/127.0.0.1:8080\/slots\/restore\r\n{\"slot_id\":1,\"filename\":\"chat1.bin\"}\r\n<\/code>\u200b<\/pre>\n<h3 id=\"_27\">4. SSD\u78c1\u76d8KV Offload\u7f13\u5b58\uff08\u4f4e\u663e\u5b58\/\u5185\u5b58\u8d85\u957f\u4e0a\u4e0b\u6587\uff09<\/h3>\n<p><strong>\u4f5c\u7528<\/strong>\uff1aKV\u7f13\u5b58\u653e\u4e0d\u4e0b\u5185\u5b58\u65f6\uff0c\u6ea2\u51fa\u5230SSD\u78c1\u76d8\uff0c\u652f\u6301\u767e\u4e07token\u8d85\u957f\u4e0a\u4e0b\u6587\uff0c\u9700\u8981\u7f16\u8bd1\u65f6\u5f00\u542f<code>LLAMA_SSD=1<\/code>\u3002<\/p>\n<h4 id=\"_29\">\u73af\u5883\u53d8\u91cf\/\u53c2\u6570<\/h4>\n<pre><code class=\"language-env hljs\">LLAMA_SSD=1 # \u7f16\u8bd1\u65f6\u5f00\u542f\r\nLLAMA_ARG_SSD_CACHE=1\r\nLLAMA_ARG_SSD_CACHE_PATH=D:\\llama_cache\\ssd_kv\r\n<\/code>\u200b<\/pre>\n<p>\u542f\u52a8\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-powershell hljs\"><span class=\"hljs-variable\">$env:LLAMA_ARG_SSD_CACHE<\/span>=<span class=\"hljs-number\">1<\/span>\r\n<span class=\"hljs-variable\">$env:LLAMA_ARG_SSD_CACHE_PATH<\/span>=<span class=\"hljs-string\">\"D:\\llama_cache\\ssd_kv\"<\/span>\r\nllama<span class=\"hljs-literal\">-server<\/span> <span class=\"hljs-literal\">-m<\/span> model.gguf <span class=\"hljs-literal\">-c<\/span> <span class=\"hljs-number\">131072<\/span>\r\n<\/code>\u200b<\/pre>\n<h2 id=\"_33\">\u4e8c\u3001KV\u7f13\u5b58\u538b\u7f29\uff08\u964d\u4f4e\u7f13\u5b58\u5185\u5b58\u5360\u7528\uff0c\u53ef\u914d\u5408\u6240\u6709\u7f13\u5b58\uff09<\/h2>\n<p>\u4fee\u6539KV\u5b58\u50a8\u7cbe\u5ea6\uff0c\u5927\u5e45\u51cf\u5c11\u7f13\u5b58\u4f53\u79ef\uff0c\u73af\u5883\u53d8\u91cf\u76f4\u63a5\u914d\u7f6e\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u53c2\u6570<\/th>\n<th>\u53ef\u9009\u503c<\/th>\n<th>\u6548\u679c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>LLAMA_ARG_CACHE_TYPE_K<\/code><\/td>\n<td><code>--cache-type-k<\/code><\/td>\n<td>f16\/q8_0\/q4_0<\/td>\n<td>K\u5411\u91cf\u91cf\u5316\u538b\u7f29<\/td>\n<\/tr>\n<tr>\n<td><code>LLAMA_ARG_CACHE_TYPE_V<\/code><\/td>\n<td><code>--cache-type-v<\/code><\/td>\n<td>f16\/q8_0\/q4_0<\/td>\n<td>V\u5411\u91cf\u91cf\u5316\u538b\u7f29<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u63a8\u8350\u914d\u7f6e\uff08\u5e73\u8861\u901f\u5ea6\/\u663e\u5b58\uff09\uff1a<\/p>\n<pre><code class=\"language-env hljs\">LLAMA_ARG_CACHE_TYPE_K=q8_0\r\nLLAMA_ARG_CACHE_TYPE_V=q8_0\r\n<\/code>\u200b<\/pre>\n<h2 id=\"_38\">\u4e09\u3001\u5b8c\u6574\u63a8\u8350\u751f\u4ea7\u7f13\u5b58\u914d\u7f6e\uff08\u73af\u5883\u53d8\u91cf\u4e00\u952e\u8bbe\u7f6e\uff09<\/h2>\n<h3 id=\"_43\">\u901a\u7528\u684c\u9762GPU\uff088G\u663e\u5b58\uff0c\u591a\u8bf7\u6c42\u5171\u4eabSystem\u63d0\u793a\uff09<\/h3>\n<pre><code class=\"language-env hljs\"># \u57fa\u7840\u63a8\u7406\r\nLLAMA_NUM_THREADS=12\r\nLLAMA_ARG_CTX_SIZE=8192\r\nLLAMA_ARG_N_GPU_LAYERS=35\r\n# \u7f13\u5b58\u6838\u5fc3\u914d\u7f6e\r\nLLAMA_ARG_CACHE_RAM=16384\r\nLLAMA_ARG_CACHE_REUSE=512\r\nLLAMA_ARG_KV_UNIFIED=1\r\nLLAMA_ARG_CACHE_IDLE_SLOTS=1\r\n# KV\u91cf\u5316\u7701\u663e\u5b58\r\nLLAMA_ARG_CACHE_TYPE_K=q8_0\r\nLLAMA_ARG_CACHE_TYPE_V=q8_0\r\n# \u5bf9\u8bdd\u6301\u4e45\u5316\u4fdd\u5b58\u76ee\u5f55\r\nLLAMA_ARG_SLOT_SAVE_PATH=D:\\AI\\llama_kv_slots\r\n<\/code>\u200b<\/pre>\n<h3 id=\"_45\">\u4f4e\u5185\u5b58CPU\u673a\u5668\uff08\u65e0\u72ec\u7acb\u663e\u5361\uff0cSSD\u6ea2\u51fa\u7f13\u5b58\uff09<\/h3>\n<pre><code class=\"language-env hljs\">LLAMA_ARG_CTX_SIZE=32768\r\nLLAMA_ARG_SSD_CACHE=1\r\nLLAMA_ARG_SSD_CACHE_PATH=D:\\AI\\llama_ssd_cache\r\nLLAMA_ARG_CACHE_RAM=4096\r\nLLAMA_ARG_CACHE_TYPE_K=q4_0\r\nLLAMA_ARG_CACHE_TYPE_V=q4_0\r\n<\/code>\u200b<\/pre>\n<h2 id=\"_47\">\u56db\u3001\u5e38\u89c1\u95ee\u9898<\/h2>\n<ol>\n<li><strong>\u7f13\u5b58\u4e0d\u751f\u6548\uff0c\u6bcf\u6b21\u90fd\u5b8c\u6574prefill<\/strong>\n<ul>\n<li>\u68c0\u67e5<code>LLAMA_ARG_CACHE_REUSE<\/code>\u5927\u4e8e0\uff0c\u5fc5\u987b\u5f00\u542f\u7edf\u4e00KV\u7f13\u51b2\u533a<code>--kv-unified<\/code><\/li>\n<li>\u524d\u7f00token\u957f\u5ea6\u5fc5\u987b\u2265<code>cache-reuse<\/code>\u8bbe\u5b9a\u503c\u624d\u4f1a\u590d\u7528<\/li>\n<\/ul>\n<\/li>\n<li><strong>slot\u4fdd\u5b58API\u627e\u4e0d\u5230<\/strong>\n<ul>\n<li>\u5fc5\u987b\u914d\u7f6e<code>LLAMA_ARG_SLOT_SAVE_PATH<\/code>\u76ee\u5f55\uff0c\u670d\u52a1\u542f\u52a8\u624d\u52a0\u8f7dslot\u63a5\u53e3<\/li>\n<\/ul>\n<\/li>\n<li><strong>SSD\u7f13\u5b58\u62a5\u9519<\/strong>\n<ul>\n<li>\u6e90\u7801\u7f16\u8bd1\u65f6\u52a0<code>LLAMA_SSD=1<\/code>\uff0c\u9884\u7f16\u8bd1\u4e8c\u8fdb\u5236\u5927\u591a\u672a\u5f00\u542fSSD\u7f13\u5b58<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5185\u5b58\u5360\u7528\u6301\u7eed\u4e0a\u6da8<\/strong>\n<ul>\n<li>\u8c03\u4f4e<code>LLAMA_ARG_CACHE_RAM<\/code>\u9650\u5236\u6700\u5927\u7f13\u5b58\uff0c\u6216\u5f00\u542f<code>cache-idle-slots<\/code>\u81ea\u52a8\u6e05\u7406\u95f2\u7f6eslot<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2 id=\"_49\">\u4e94\u3001\u7f13\u5b58\u751f\u6548\u9a8c\u8bc1<\/h2>\n<p>\u542f\u52a8llama-server\/llama-cli\u65f6\uff0c\u63a7\u5236\u53f0\u8f93\u51fa<code>prompt cache is enabled<\/code>\u5373\u4ee3\u8868\u81ea\u52a8\u524d\u7f00\u7f13\u5b58\u6b63\u5e38\u5de5\u4f5c\uff1b<br \/>\n\u590d\u7528\u7f13\u5b58\u65f6prefill\u8017\u65f6\u4f1a\u4ece\u51e0\u79d2\u964d\u5230\u51e0\u5341\u6beb\u79d2\uff0c\u65e5\u5fd7\u53ef\u89c1<code>cache hit<\/code>\u547d\u4e2d\u6807\u8bb0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>llama.cpp \u91cc\u5df2\u5904\u7406\u6587\u672c\u7f13\u5b58\u672c\u8d28\u662f KV Cache\uff1a\u6a21\u578b\u524d\u7f6eprompt\u3001\u7cfb\u7edf\u63d0\u793a\u3001\u957f\u5bf9\u8bdd\u524d\u7f00\u8ba1\u7b97\u540e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-2508","post","type-post","status-publish","format-standard","hentry","category-21"],"_links":{"self":[{"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/posts\/2508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/comments?post=2508"}],"version-history":[{"count":2,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2510,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/posts\/2508\/revisions\/2510"}],"wp:attachment":[{"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/tags?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}