{"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-10-19T03:56:15","modified_gmt":"2026-10-18T19:56:15","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\u201c\u5df2\u5904\u7406\u6587\u672c\u7f13\u5b58\u201d\u7684\u672c\u8d28\u662f <strong>KV Cache<\/strong>\uff1a\u6a21\u578b\u628a\u7cfb\u7edf\u63d0\u793a\u3001\u957f\u5bf9\u8bdd\u524d\u7f00\u8ba1\u7b97\u540e\u7684 Key\/Value \u5f20\u91cf\u5b58\u4e0b\u6765\uff0c\u91cd\u590d\u8bf7\u6c42\u65e0\u9700\u91cd\u65b0\u5168\u91cf\u9884\u586b\u5145\uff08prefill\uff09\uff0c\u901f\u5ea6\u53ef\u63d0\u5347\u6570\u500d\u3002\u4e0b\u9762\u6309\u4f7f\u7528\u573a\u666f\u628a\u56db\u79cd\u673a\u5236\u62c6\u5f00\u8bb2\uff0c\u5e76\u7ed9\u51fa\u53ef\u76f4\u63a5\u5957\u7528\u7684\u751f\u4ea7\u914d\u7f6e\u3002<\/p>\n<div class=\"note\">\n  \ud83d\udd27 \u672c\u6587\u6240\u6709\u53c2\u6570\u90fd\u652f\u6301\u4e24\u79cd\u5199\u6cd5\uff1a\u547d\u4ee4\u884c\u957f\u9009\u9879\uff08\u5982 <code>--cache-ram<\/code>\uff09\u6216\u73af\u5883\u53d8\u91cf\uff08<code>LLAMA_ARG_CACHE_RAM<\/code>\uff09\u3002\u73af\u5883\u53d8\u91cf\u89c4\u5219\u4e3a\u300c\u957f\u9009\u9879\u540d\u5168\u5927\u5199\u3001\u8fde\u5b57\u7b26\u53d8\u4e0b\u5212\u7ebf\u3001\u52a0 <code>LLAMA_ARG_<\/code> \u524d\u7f00\u300d\uff1b\u5e03\u5c14\u53d6\u53cd\u7528 <code>LLAMA_ARG_NO_<\/code> \u524d\u7f00\u3002\u53ef\u914d\u5408 Windows \u73af\u5883\u53d8\u91cf\u7f16\u8f91\u5668\u4e00\u952e\u914d\u7f6e\u3002\n<\/div>\n<h2>\u4e00\u3001\u5185\u5b58\u524d\u7f00\u81ea\u52a8\u590d\u7528\uff08server \u9ed8\u8ba4\u5f00\u542f\uff0c\u6700\u5e38\u7528\uff09<\/h2>\n<p>\u8fd9\u662f\u540c\u6a21\u578b\u591a\u8bf7\u6c42\u5171\u4eab\u76f8\u540c\u524d\u7f00\uff08\u56fa\u5b9a System \u63d0\u793a\u3001\u901a\u7528\u6a21\u677f\uff09\u65f6\u7684\u9996\u9009\u65b9\u6848\u3002llama-server \u9ed8\u8ba4\u5f00\u542f <strong>prompt caching<\/strong>\uff08<code>--cache-prompt<\/code>\uff09\uff0c\u914d\u5408 <strong>unified KV<\/strong>\uff08slot \u6570\u4e3a auto \u65f6\u9ed8\u8ba4\u542f\u7528\uff09\uff0c\u76f8\u540c\u524d\u7f00\u5728\u5e76\u53d1\u8bf7\u6c42 \/ \u591a\u4f1a\u8bdd\u95f4\u76f4\u63a5\u590d\u7528 KV \u5f20\u91cf\uff0c\u8df3\u8fc7\u91cd\u590d prefill\uff1b<strong>\u8fdb\u7a0b\u91cd\u542f\u7f13\u5b58\u4f1a\u4e22\u5931<\/strong>\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u522b\u540d<\/th>\n<th>\u9ed8\u8ba4\u503c<\/th>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>--cache-prompt<\/code><\/td>\n<td><code>--no-cache-prompt<\/code><\/td>\n<td><span class=\"tag ok\">\u5f00\u542f<\/span><\/td>\n<td><code>LLAMA_ARG_CACHE_PROMPT<\/code><\/td>\n<td>\u524d\u7f00\u590d\u7528\u4e3b\u5f00\u5173\uff08server-only\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>--cache-ram N<\/code><\/td>\n<td><code>-cram<\/code><\/td>\n<td><strong>8192<\/strong> MiB<br \/>\uff08-1=\u65e0\u9650\u5236\uff0c0=\u7981\u7528\uff09<\/td>\n<td><code>LLAMA_ARG_CACHE_RAM<\/code><\/td>\n<td>\u5185\u5b58\u7f13\u5b58\u5bb9\u91cf\u4e0a\u9650\uff1bdecision model \u9ed8\u8ba4\u7981\u7528\uff0c\u9700\u663e\u5f0f\u8bbe N<\/td>\n<\/tr>\n<tr>\n<td><code>--kv-unified<\/code><\/td>\n<td><code>--no-kv-unified<\/code><\/td>\n<td>slot \u6570\u4e3a auto \u65f6\u5f00\u542f<\/td>\n<td><code>LLAMA_ARG_KV_UNIFIED<\/code><\/td>\n<td>\u6240\u6709\u5e8f\u5217\u5171\u4eab\u540c\u4e00\u4efd KV \u7f13\u51b2<\/td>\n<\/tr>\n<tr>\n<td><code>--kv-unified-per-slot N<\/code><\/td>\n<td>\u2014<\/td>\n<td>0\uff08\u4e0d\u9650\u5236\uff09<\/td>\n<td><code>LLAMA_ARG_KV_UNIFIED_PER_SLOT<\/code><\/td>\n<td>\u6bcf slot \u4e0a\u4e0b\u6587\u4e0a\u9650\uff1b\u4e0d\u8bbe <code>-c<\/code> \u65f6\u5171\u4eab\u6c60 = n_parallel\u00d7N<\/td>\n<\/tr>\n<tr>\n<td><code>--cache-idle-slots<\/code><\/td>\n<td><code>--no-cache-idle-slots<\/code><\/td>\n<td><span class=\"tag ok\">\u5f00\u542f<\/span><\/td>\n<td><code>LLAMA_ARG_CACHE_IDLE_SLOTS<\/code><\/td>\n<td>\u65b0\u4efb\u52a1\u5f00\u59cb\u65f6\u628a\u7a7a\u95f2 slot \u7684 KV \u5199\u5165 prompt cache \u6587\u4ef6\uff1b\u4f7f\u7528 unified KV \u65f6\u6e05\u7a7a\uff08\u9700 <code>--cache-ram<\/code>\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>--cache-reuse N<\/code><\/td>\n<td>\u2014<\/td>\n<td><strong>0<\/strong>\uff08\u5173\u95ed\uff09<\/td>\n<td><code>LLAMA_ARG_CACHE_REUSE<\/code><\/td>\n<td>\u901a\u8fc7 KV shifting \u590d\u7528\u7684\u6700\u5c0f chunk \u5c3a\u5bf8\uff0c\u9700 prompt caching \u5f00\u542f\uff1b\u90e8\u5206\u6a21\u5f0f\uff08\u591a\u6a21\u6001\/\u7279\u5b9a\u7ec4\u5408\uff09\u4e0b\u4f1a\u88ab\u5ffd\u7565<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"warn\">\n  \u26a0\ufe0f <strong>\u6982\u5ff5\u6f84\u6e05<\/strong>\uff1a\u524d\u7f00\u590d\u7528\u7684\u4e3b\u5f00\u5173\u662f <code>--cache-prompt<\/code>\uff08server \u9ed8\u8ba4\u5df2\u5f00\uff09\uff0c<strong>\u4e0d\u662f<\/strong> <code>--cache-reuse<\/code>\u3002<code>--cache-reuse N<\/code> \u9ed8\u8ba4\u503c\u4e3a 0\uff08\u5173\u95ed\uff09\uff0c\u5b83\u53ea\u662f\u201cKV shifting \u590d\u7528\u7684\u6700\u5c0f chunk \u5c3a\u5bf8\u201d\uff0c\u8bbe\u4e3a 512\/1024 \u53ef\u542f\u7528\u4f46\u975e\u5fc5\u9700\uff0c\u4e14\u5728\u67d0\u4e9b\u6a21\u5f0f\u4e0b\u4f1a\u88ab\u5ffd\u7565\u3002\n<\/div>\n<h3>\u73af\u5883\u53d8\u91cf\u542f\u52a8\uff08PowerShell\uff09<\/h3>\n<pre><code class=\"language-powershell\">$env:LLAMA_ARG_CACHE_RAM=16384\r\n$env:LLAMA_ARG_CACHE_REUSE=512\r\nllama-server -m qwen3-8b.gguf -c 8192 -ngl 35<\/code><\/pre>\n<h3>\u7b49\u4ef7\u547d\u4ee4\u884c\uff08cmd\uff09<\/h3>\n<pre><code class=\"language-bat\">set LLAMA_ARG_CACHE_RAM=16384\r\nset LLAMA_ARG_CACHE_REUSE=512\r\nllama-server -m qwen3-8b.gguf -c 8192 -ngl 35 --cache-ram 16384 --cache-reuse 512<\/code><\/pre>\n<h2>\u4e8c\u3001\u6301\u4e45\u5316 Prompt Cache \u6587\u4ef6\uff08\u4ec5 CLI\uff09<\/h2>\n<p>\u628a\u56fa\u5b9a\u7cfb\u7edf\u63d0\u793a\u8bcd\u3001\u8d85\u957f\u524d\u7f6e\u6587\u6863\u4e00\u6b21\u6027\u8ba1\u7b97\u540e\uff0c\u4fdd\u5b58\u4e3a <code>.bin<\/code> \u7f13\u5b58\u6587\u4ef6\uff0c\u4e0b\u6b21\u542f\u52a8\u76f4\u63a5\u52a0\u8f7d\uff0c\u8df3\u8fc7 prefill\u3002<strong>\u6ce8\u610f\uff1a\u6b64\u529f\u80fd\u4ec5 <code>llama-cli<\/code> \/ perplexity \/ completion \u793a\u4f8b\u53ef\u7528\uff0cserver \u65e0\u6b64\u53c2\u6570\u3001\u4e5f\u65e0\u5bf9\u5e94\u73af\u5883\u53d8\u91cf\u3002<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>--prompt-cache FNAME<\/code><\/td>\n<td>\u628a prompt \u72b6\u6001\u7f13\u5b58\u5230\u6587\u4ef6\uff0c\u4e0b\u6b21\u542f\u52a8\u8df3\u8fc7 prefill\uff08\u9ed8\u8ba4 none\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>--prompt-cache-all<\/code><\/td>\n<td>\u628a\u7528\u6237\u8f93\u5165\u4e0e\u751f\u6210\u4e5f\u5199\u5165\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td><code>--prompt-cache-ro<\/code><\/td>\n<td>\u53ea\u8bfb\u4f7f\u7528\u7f13\u5b58\u3001\u4e0d\u66f4\u65b0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-bash\"># \u7b2c\u4e00\u6b21\uff1a\u5e26\u5b8c\u6574 system prompt \u751f\u6210\u7f13\u5b58\r\nllama-cli -m model.gguf -c 8192 --prompt-cache sys_cache.bin -f system_prompt.txt\r\n# \u4e4b\u540e\uff1a\u76f4\u63a5\u52a0\u8f7d\uff0c\u8df3\u8fc7 prefill\r\nllama-cli -m model.gguf -c 8192 --prompt-cache sys_cache.bin<\/code><\/pre>\n<div class=\"note\">\n  server \u7684\u201c\u524d\u7f00\u590d\u7528\u201d\u7528\u7684\u662f <code>--cache-prompt<\/code>\uff08\u5185\u5b58\u7ea7\uff09\uff0c\u4e0e\u8fd9\u91cc\u7684\u78c1\u76d8\u6587\u4ef6 <code>--prompt-cache<\/code> \u4e0d\u662f\u4e00\u56de\u4e8b\uff0c\u522b\u6df7\u7528\u3002\n<\/div>\n<h2>\u4e09\u3001Server \u591a slot \u6301\u4e45\u5316\uff08\u65ad\u70b9\u7eed\u804a\uff09<\/h2>\n<p>llama-server \u6bcf\u4e2a\u5bf9\u8bdd\u662f\u72ec\u7acb\u7684 slot\u3002\u624b\u52a8\u4fdd\u5b58 \/ \u52a0\u8f7d\u5355\u6761\u5bf9\u8bdd\u7684\u5b8c\u6574 KV \u7f13\u5b58\uff0c\u91cd\u542f\u670d\u52a1\u540e\u4ecd\u53ef\u6062\u590d\u804a\u5929\u4e0a\u4e0b\u6587\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u9ed8\u8ba4\u503c<\/th>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>--slot-save-path PATH<\/code><\/td>\n<td>disabled<\/td>\n<td><code>LLAMA_ARG_SLOT_SAVE_PATH<\/code><\/td>\n<td>\u5f00\u542f slot save\/restore API\uff1b<strong>\u76ee\u5f55\u5fc5\u987b\u9884\u5148\u5b58\u5728\uff0c\u5426\u5219\u542f\u52a8\u76f4\u63a5\u62a5\u9519<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>--slots<\/code> \/ <code>--no-slots<\/code><\/td>\n<td><span class=\"tag ok\">\u5f00\u542f<\/span><\/td>\n<td><code>LLAMA_ARG_ENDPOINT_SLOTS<\/code><\/td>\n<td>\u66b4\u9732 <code>GET \/slots<\/code> \u76d1\u63a7\u7aef\u70b9<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>HTTP API\uff08\u7aef\u70b9\u9ed8\u8ba4\u5df2\u5f00\u542f\uff09<\/h3>\n<pre><code class=\"language-http\"># \u67e5\u770b\u5404 slot \u72b6\u6001\uff08\u9a8c\u8bc1\u547d\u4e2d\u7528\uff09\r\nGET  http:\/\/127.0.0.1:8080\/slots\r\n\r\n# \u4fdd\u5b58\u67d0\u4e2a slot \u7684 KV \u7f13\u5b58\u5230 slot-save-path \u76ee\u5f55\r\nPOST http:\/\/127.0.0.1:8080\/slots\/1?action=save\r\nContent-Type: application\/json\r\n{ \"filename\": \"chat1.bin\" }\r\n\r\n# \u6062\u590d\uff08\u540c\u4e00\u7aef\u70b9\uff0caction=restore\uff0c\u4e0d\u662f \/slots\/restore\uff09\r\nPOST http:\/\/127.0.0.1:8080\/slots\/1?action=restore\r\nContent-Type: application\/json\r\n{ \"filename\": \"chat1.bin\" }\r\n\r\n# \u6e05\u9664\u8be5 slot \u7684 prompt cache\r\nPOST http:\/\/127.0.0.1:8080\/slots\/1?action=erase<\/code><\/pre>\n<p>\u8fd4\u56de\u793a\u4f8b\uff08save\uff09\uff1a<code>{ \"id_slot\":0, \"filename\":\"x.bin\", \"n_saved\":1745, \"n_written\":14309796, \"timings\":{\"save_ms\":49.86} }<\/code><\/p>\n<div class=\"danger\">\n  \u274c \u672a\u52a0 <code>--slot-save-path<\/code> \u5c31\u8c03\u7528\u4f1a\u8fd4\u56de\uff1a<code>\"This server does not support slots action. Start it with --slot-save-path\"<\/code>\u3002\n<\/div>\n<h3>\u542f\u52a8\uff08PowerShell\uff09<\/h3>\n<pre><code class=\"language-powershell\"># \u76ee\u5f55\u8981\u5148\u5efa\u597d\uff1aNew-Item -ItemType Directory -Path D:\\llama_cache\\slots\r\n$env:LLAMA_ARG_SLOT_SAVE_PATH=\"D:\\llama_cache\\slots\"\r\nllama-server -m model.gguf -c 16384 -ngl 40 --port 8080<\/code><\/pre>\n<h2>\u56db\u3001\u957f\u4e0a\u4e0b\u6587 \/ \u4f4e\u8d44\u6e90\uff1aKV \u91cf\u5316 + KV \u5378\u8f7d<\/h2>\n<div class=\"danger\">\n  \u274c <strong>\u4e0d\u5b58\u5728\u7684\u201cSSD \u78c1\u76d8 KV Offload\u201d<\/strong>\uff1a\u4e0a\u6e38 llama.cpp \u6ca1\u6709 <code>LLAMA_SSD=1<\/code> \u7f16\u8bd1\u5f00\u5173\uff0c<code>src\/llama.cpp<\/code> \u65e0\u78c1\u76d8\u6ea2\u51fa\u903b\u8f91\uff0c\u4e5f\u65e0 <code>LLAMA_ARG_SSD_CACHE*<\/code> \u53c2\u6570\u3002\u4efb\u4f55\u4e2d\u6587\u535a\u5ba2\u8bb2\u7684\u201c\u7f16\u8bd1\u5f00\u542f SSD \u7f13\u5b58\u201d\u90fd\u662f\u4ee5\u8bb9\u4f20\u8bb9\uff0c\u52a0\u4e86\u53ea\u4f1a\u88ab\u5ffd\u7565\u6216\u62a5\u9519\u3002\n<\/div>\n<p>\u771f\u6b63\u53ef\u7528\u3001\u4e14\u4e0a\u6e38\u786e\u5b9e\u5b58\u5728\u7684\u957f\u4e0a\u4e0b\u6587\u624b\u6bb5\uff1a<\/p>\n<ol>\n<li><strong>KV \u91cf\u5316<\/strong>\uff08\u6240\u6709\u6a21\u5f0f\u901a\u7528\uff09\u2014\u2014 \u89c1\u7b2c\u4e94\u8282\u3002<\/li>\n<li><strong>KV \u8bbe\u5907\u7ea7\u5378\u8f7d<\/strong>\uff1a<code>--kv-offload<\/code> \/ <code>--no-kv-offload<\/code>\uff08<code>-kvo<\/code>\uff0c<span class=\"tag ok\">\u9ed8\u8ba4\u5f00\u542f<\/span>\uff09\u2192 <code>LLAMA_ARG_KV_OFFLOAD<\/code>\u3002\u628a KV cache \u653e\u5230\u53e6\u4e00\u540e\u7aef\uff08\u7b2c\u4e8c\u5757 GPU \/ CPU\uff09\uff0c<strong>\u4e0d\u662f\u78c1\u76d8<\/strong>\u3002\u591a\u5361 \/ \u5f02\u6784\u673a\u5668\u624d\u6709\u610f\u4e49\u3002<\/li>\n<li><strong>Context checkpoints<\/strong>\uff1a<code>--ctx-checkpoints N<\/code>\uff08\u9ed8\u8ba4 <strong>32<\/strong>\uff09\u3001<code>--checkpoint-min-step<\/code>\uff08\u9ed8\u8ba4 <strong>8192<\/strong>\uff09\u2192 <code>LLAMA_ARG_CTX_CHECKPOINTS<\/code> \/ <code>LLAMA_ARG_CHECKPOINT_MIN_SPACING_NT<\/code>\u3002\u5728 slot \u5185\u4fdd\u5b58\u591a\u4e2a\u4e0a\u4e0b\u6587\u68c0\u67e5\u70b9\uff0c\u914d\u5408 slot save \u4e00\u8d77\u6301\u4e45\u5316\uff0c\u9002\u5408\u957f\u6587\u6863\u65ad\u70b9\u7eed\u8bfb\u3002<\/li>\n<li><strong>Context shift<\/strong>\uff1a<code>--context-shift<\/code>\uff08\u65e0\u9650\u751f\u6210\u9ed8\u8ba4\u5f00\u542f\uff09\u3002\u4e0a\u4e0b\u6587\u8d85\u957f\u65f6\u81ea\u52a8\u79fb\u4f4d\u907f\u514d OOM\u3002<\/li>\n<\/ol>\n<div class=\"note\">\n  \u4f4e\u5185\u5b58 CPU \u673a\u5668\u7684\u6b63\u786e\u505a\u6cd5\uff1a\u5f00\u5927 <code>-c<\/code>\uff0c\u7528 <code>q4_0<\/code> \u91cf\u5316 KV\uff0c\u9760 RAM + context-shift\uff1b<strong>\u4e0d\u8981<\/strong>\u4f7f\u7528\u4e0d\u5b58\u5728\u7684 SSD \u53c2\u6570\u3002\n<\/div>\n<h2>\u4e94\u3001\u591a GPU \u5b9e\u6218\uff1aKV \u5378\u8f7d\u4e0e\u9009\u5361<\/h2>\n<p>\u5f53\u4f60\u6709\u591a\u5757\u663e\u5361\uff08\u4f8b\u5982\u4e00\u5757 32G\u3001\u4e00\u5757 16G\uff09\u65f6\uff0c\u81ea\u7136\u4f1a\u60f3\u8ba9\u67d0\u5757\u5361\u4e13\u804c\u505a KV \u7f13\u5b58\u3002\u8fd9\u91cc\u5fc5\u987b\u5148\u8bf4\u6e05 <code>--kv-offload<\/code> \u7684\u771f\u5b9e\u884c\u4e3a\uff0c\u5426\u5219\u5f88\u5bb9\u6613\u914d\u9519\u3002<\/p>\n<div class=\"danger\">\n  \u274c <strong>\u5173\u952e\u4e8b\u5b9e<\/strong>\uff1a<code>--kv-offload<\/code> \/ <code>--no-kv-offload<\/code>\uff08<code>-kvo<\/code>\uff09\u53ea\u662f KV \u5378\u8f7d\u7684<strong>\u5168\u5c40\u5f00\u5173\uff08\u9ed8\u8ba4\u5f00\u542f\uff09<\/strong>\uff0c\u5b83<strong>\u4e0d\u6307\u5b9a\u628a KV \u653e\u5230\u54ea\u5757\u5361<\/strong>\uff0c\u66f4\u505a\u4e0d\u5230\u201c\u67d0\u5757\u5361\u53ea\u653e KV\u201d\u3002\u6e90\u7801\u91cc\u7b2c N \u5c42\u6743\u91cd\u7684 KV \u56fa\u5b9a\u843d\u5728\u627f\u8f7d\u8be5\u5c42\u6743\u91cd\u7684\u540c\u4e00\u5757\u8bbe\u5907\u4e0a\uff0c\u56e0\u6b64 KV \u7f13\u5b58\u6c38\u8fdc\u4e0e\u5404\u5c42\u6743\u91cd\u540c\u5361\u3002\u9009\u5361\u7531 <code>--split-mode<\/code> \/ <code>--tensor-split<\/code> \/ <code>--main-gpu<\/code> \/ <code>--devices<\/code> \u51b3\u5b9a\u3002\n<\/div>\n<p>\u6240\u4ee5 llama.cpp <strong>\u6ca1\u6709\u201cA \u5361\u7eaf\u63a8\u7406\u3001B \u5361\u7eaf KV\u201d\u8fd9\u79cd\u62d3\u6251<\/strong>\u3002\u8981\u8ba9\u7b2c\u4e8c\u5757\u5361\u7684\u663e\u5b58\u7528\u6765\u653e KV\uff0c\u552f\u4e00\u529e\u6cd5\u662f\u8ba9\u5b83\u4e5f\u627f\u8f7d\u4e00\u90e8\u5206\u5c42\u7684\u6743\u91cd\u2014\u2014\u90a3\u90e8\u5206 KV \u4f1a\u81ea\u7136\u8ddf\u8fc7\u6765\u3002<\/p>\n<h3>\u76f8\u5173\u9009\u5361\u53c2\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>--kv-offload<\/code> \/ <code>--no-kv-offload<\/code><\/td>\n<td><code>LLAMA_ARG_KV_OFFLOAD<\/code><\/td>\n<td>KV \u5378\u8f7d\u5f00\u5173\uff08\u9ed8\u8ba4\u5f00\u542f\uff09\uff1b\u4e0d\u8d1f\u8d23\u9009\u5361<\/td>\n<\/tr>\n<tr>\n<td><code>--split-mode {none,layer,row,tensor}<\/code><\/td>\n<td>\u2014<\/td>\n<td>\u591a\u5361\u5207\u5206\u65b9\u5f0f\uff1b<code>layer<\/code>\uff08\u9ed8\u8ba4\uff09\u6309\u5c42\u5207\u5e76\u540c\u65f6\u5207 KV\uff1b<code>none<\/code> \u53ea\u7528\u5355\u5361<\/td>\n<\/tr>\n<tr>\n<td><code>--tensor-split N0,N1,...<\/code><\/td>\n<td>\u2014<\/td>\n<td>\u5404\u5361\u5206\u914d\u6bd4\u4f8b\uff0c\u5982 <code>2,1<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>--main-gpu INDEX<\/code><\/td>\n<td>\u2014<\/td>\n<td><code>split-mode=none<\/code> \u65f6\u7684\u552f\u4e00\u5361\uff1b<code>row<\/code> \u6a21\u5f0f\u4e0b\u7684\u4e2d\u95f4\u7ed3\u679c\u4e0e KV \u5361<\/td>\n<\/tr>\n<tr>\n<td><code>--devices LIST<\/code><\/td>\n<td><code>LLAMA_ARG_DEVICE<\/code><\/td>\n<td>\u6307\u5b9a\u7528\u4e8e offload \u7684\u8bbe\u5907\u5217\u8868\uff08<code>none<\/code> = \u4e0d offload\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"note\">\n  \u5148\u786e\u8ba4\u8bbe\u5907\u987a\u5e8f\uff1a\u7528 <code>llama-server --list-devices<\/code> \u6216 <code>nvidia-smi<\/code> \u770b\u54ea\u5757\u662f index 0\u3002\u4e0b\u9762\u5047\u8bbe <strong>32G \u5361 = \u8bbe\u5907 0\uff0c16G \u5361 = \u8bbe\u5907 1<\/strong>\u3002\u82e5\u60f3\u5f3a\u5236 32G \u5361\u4e3a 0\uff0c\u542f\u52a8\u524d <code>set CUDA_VISIBLE_DEVICES=&lt;32G\u5361\u7684id&gt;,&lt;16G\u5361\u7684id&gt;<\/code>\u3002\n<\/div>\n<h3>\u65b9\u6848 A\uff1a32G \u5361\u5168\u5305\uff0c16G \u5361\u7a7a\u95f2<\/h3>\n<p>\u82e5\u6a21\u578b + KV \u5168\u585e\u8fdb 32G \u5c31\u591f\uff0c\u6839\u672c\u4e0d\u9700\u8981\u7b2c\u4e8c\u5757\u5361\uff1a<\/p>\n<pre><code class=\"language-powershell\"># \u5355\u5361\u6a21\u5f0f\uff1a\u5168\u90e8\u653e main-gpu 0\uff0832G \u5361\uff09\uff0c16G \u5361\u4e0d\u53c2\u4e0e\r\nllama-server -m model.gguf -ngl 99 -c 8192 --split-mode none --main-gpu 0<\/code><\/pre>\n<p>\u6b64\u65f6\u201c\u7b2c\u4e00\u5757\u8d1f\u8d23\u63a8\u7406\u201d\u6210\u7acb\uff0c\u4f46\u7b2c\u4e8c\u5757\u5361\u6ca1\u7528\u4e0a\u3002<\/p>\n<h3>\u65b9\u6848 B\uff1a\u4e24\u5757\u5361\u90fd\u7528\uff08\u63a8\u8350\uff0c\u7b2c\u4e8c\u5757\u5361\u663e\u5b58\u88ab KV + \u90e8\u5206\u5c42\u7528\u4e0a\uff09<\/h3>\n<p>\u628a\u6a21\u578b\u6309\u663e\u5b58\u6bd4\u4f8b\u5206\u5c42\u5230\u4e24\u5757\u5361\uff0c\u6bcf\u5757\u5361\u65e2\u653e\u81ea\u5df1\u90a3\u90e8\u5206\u6743\u91cd\u4e5f\u653e\u5bf9\u5e94 KV\uff0c\u8fd9\u662f 32G+16G \u7684\u6807\u51c6\u591a\u5361\u7528\u6cd5\uff1a<\/p>\n<pre><code class=\"language-powershell\"># 32G \u5361\u62ff 2 \u4efd\u300116G \u5361\u62ff 1 \u4efd\u6743\u91cd\u4e0e KV\uff08\u6309\u6bd4\u4f8b\u5206\u5c42\uff0cpipelined\uff09\r\nllama-server -m model.gguf -ngl 99 -c 32768 --split-mode layer --tensor-split 2,1<\/code><\/pre>\n<ul>\n<li><code>--tensor-split 2,1<\/code> \u8868\u793a\u8bbe\u5907 0 \u5360 2\/3\u3001\u8bbe\u5907 1 \u5360 1\/3\uff1b\u6bd4\u4f8b\u63a5\u8fd1\u663e\u5b58\u6bd4\u5373\u53ef\uff0c\u60f3\u66f4\u6fc0\u8fdb\u53ef <code>3,2<\/code> \u8ba9 16G \u5361\u591a\u5e26\u70b9\u3002<\/li>\n<li>16G \u5361\u786e\u5b9e\u201c\u6301\u6709 KV\u201d\uff0c\u53ea\u662f\u5b83\u540c\u65f6\u4e5f\u7b97\u81ea\u5df1\u90a3 1\/3 \u5c42\u7684\u63a8\u7406\u2014\u2014\u8fd9\u662f\u5f53\u524d\u67b6\u6784\u4e0b\u6700\u8d34\u8fd1\u76ee\u6807\u7684\u5f62\u6001\u3002<\/li>\n<\/ul>\n<h2>\u516d\u3001KV \u7f13\u5b58\u538b\u7f29\uff08\u964d\u4f4e\u663e\u5b58 \/ \u5185\u5b58\u5360\u7528\uff09<\/h2>\n<p>\u4fee\u6539 KV \u5b58\u50a8\u7cbe\u5ea6\u53ef\u5927\u5e45\u51cf\u5c11\u7f13\u5b58\u4f53\u79ef\uff0c\u4e0e\u4e0a\u9762\u6240\u6709\u7f13\u5b58\u673a\u5236\u53e0\u52a0\u4f7f\u7528\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570<\/th>\n<th>\u73af\u5883\u53d8\u91cf<\/th>\n<th>\u5141\u8bb8\u503c<\/th>\n<th>\u6548\u679c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>--cache-type-k TYPE<\/code><\/td>\n<td><code>LLAMA_ARG_CACHE_TYPE_K<\/code><\/td>\n<td rowspan=\"2\"><code>f32, f16, bf16, q8_0, q4_0, q4_1, iq4_nl, q5_0<\/code>\uff08\u53ca\u66f4\u591a\uff09<\/td>\n<td>K \u5411\u91cf\u91cf\u5316<\/td>\n<\/tr>\n<tr>\n<td><code>--cache-type-v TYPE<\/code><\/td>\n<td><code>LLAMA_ARG_CACHE_TYPE_V<\/code><\/td>\n<td>V \u5411\u91cf\u91cf\u5316<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u9ed8\u8ba4\u5747\u4e3a <strong>f16<\/strong>\uff082 \u5b57\u8282\/\u503c\uff09\u3002<\/li>\n<li>\u63a8\u8350 <code>q8_0<\/code>\uff1a\u51e0\u4e4e\u65e0\u635f\u3001\u7f13\u5b58\u4f53\u79ef\u51cf\u534a\uff1b\u6216 <code>q4_0<\/code>\uff1a\u4f53\u79ef\u964d\u4e3a 1\/4\uff0c\u8d28\u91cf\u8f7b\u5fae\u635f\u5931\u3002<\/li>\n<\/ul>\n<h2>\u4e03\u3001\u63a8\u8350\u751f\u4ea7\u914d\u7f6e<\/h2>\n<h3>\u684c\u9762 GPU\uff088G \u663e\u5b58\uff0c\u591a\u8bf7\u6c42\u5171\u4eab System \u63d0\u793a\uff09<\/h3>\n<pre><code class=\"language-env\"># \u7ebf\u7a0b\u6570\uff08\u6ce8\u610f\uff1a\u53d8\u91cf\u662f LLAMA_ARG_THREADS\uff0c\u4e0d\u662f LLAMA_NUM_THREADS\uff09\r\nLLAMA_ARG_THREADS=12\r\nLLAMA_ARG_CTX_SIZE=8192\r\nLLAMA_ARG_N_GPU_LAYERS=35\r\n# \u7f13\u5b58\u6838\u5fc3\uff08server \u9ed8\u8ba4\u5df2\u5f00 --cache-prompt\uff0c\u65e0\u9700\u518d\u8bbe\uff09\r\nLLAMA_ARG_CACHE_RAM=16384\r\nLLAMA_ARG_CACHE_REUSE=512\r\n# kv-unified \u5728 slot \u4e3a auto \u65f6\u9ed8\u8ba4\u5f00\uff0c\u4e0b\u9762\u4e00\u884c\u53ef\u7701\u7565\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\uff08\u76ee\u5f55\u987b\u5148\u5efa\u597d\uff09\r\nLLAMA_ARG_SLOT_SAVE_PATH=D:\\AI\\llama_kv_slots<\/code><\/pre>\n<h3>\u4f4e\u5185\u5b58 CPU \u673a\u5668\uff08\u65e0 SSD \u53c2\u6570\uff0c\u6539\u4e3a KV \u91cf\u5316 + context-shift\uff09<\/h3>\n<pre><code class=\"language-env\">LLAMA_ARG_CTX_SIZE=32768\r\n# \u4e0d\u8981\u52a0 LLAMA_ARG_SSD_CACHE \/ LLAMA_ARG_SSD_CACHE_PATH\uff08\u4e0a\u6e38\u4e0d\u5b58\u5728\uff09\r\nLLAMA_ARG_CACHE_TYPE_K=q4_0\r\nLLAMA_ARG_CACHE_TYPE_V=q4_0\r\nLLAMA_ARG_CACHE_RAM=4096\r\n# \u82e5\u6709\u591a\u8bbe\u5907\u53ef\u52a0\uff1aLLAMA_ARG_KV_OFFLOAD=1<\/code><\/pre>\n<h2>\u516b\u3001\u5e38\u89c1\u95ee\u9898<\/h2>\n<ol>\n<li><strong>\u7f13\u5b58\u4e0d\u751f\u6548\uff0c\u6bcf\u6b21\u90fd\u5b8c\u6574 prefill<\/strong>\n<ul>\n<li>server \u786e\u8ba4 <code>--cache-prompt<\/code> \u5df2\u5f00\uff08\u9ed8\u8ba4\u5f00\uff09\uff1bdecision model \u9700\u663e\u5f0f <code>--cache-ram N<\/code>\u3002<\/li>\n<li>\u524d\u7f00\u590d\u7528\u9760 unified KV + prompt caching\uff1b<code>--cache-reuse<\/code> \u53ea\u5f71\u54cd KV-shifting \u590d\u7528\u3001\u9ed8\u8ba4 0\uff0c\u4e0d\u4f1a\u8ba9\u524d\u7f00\u590d\u7528\u5931\u6548\u3002<\/li>\n<li>\u524d\u7f00 token \u5fc5\u987b \u2265 \u67d0\u957f\u5ea6\u624d\u4f1a\u590d\u7528\u2014\u2014\u8fd9\u7531 unified KV \u7684\u524d\u7f00\u5339\u914d\u51b3\u5b9a\uff0c\u4e0d\u662f <code>cache-reuse<\/code> \u9608\u503c\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>slot \u4fdd\u5b58 API \u627e\u4e0d\u5230<\/strong>\n<ul>\n<li>\u5fc5\u987b\u914d\u7f6e <code>--slot-save-path<\/code> \u4e14<strong>\u76ee\u5f55\u5df2\u5b58\u5728<\/strong>\uff0c\u5426\u5219\u8fd4\u56de\u4e0d\u652f\u6301\u7684\u63d0\u793a\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u914d\u7f6e SSD \u7f13\u5b58\u62a5\u9519<\/strong>\n<ul>\n<li>\u8be5\u529f\u80fd\u6839\u672c\u4e0d\u5b58\u5728\uff0c\u4efb\u4f55 <code>LLAMA_SSD*<\/code> \/ <code>LLAMA_ARG_SSD_CACHE*<\/code> \u90fd\u4f1a\u88ab\u5ffd\u7565\u6216\u62a5\u9519\uff0c\u76f4\u63a5\u5220\u6389\u5373\u53ef\u3002<\/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>\uff0c\u6216\u4fdd\u7559\u9ed8\u8ba4\u7684 <code>--cache-idle-slots<\/code> \u81ea\u52a8\u6e05\u7406\u7a7a\u95f2 slot\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h2>\u4e5d\u3001\u7f13\u5b58\u751f\u6548\u9a8c\u8bc1<\/h2>\n<div class=\"warn\">\n  \u26a0\ufe0f \u4e0d\u8981\u518d\u4f9d\u8d56\u201c\u63a7\u5236\u53f0\u8f93\u51fa <code>prompt cache is enabled<\/code> \u5373\u4ee3\u8868\u6b63\u5e38\u201d\u2014\u2014\u8be5\u6587\u6848\u662f <strong>TRACE \u7ea7<\/strong>\u65e5\u5fd7\uff0c\u9ed8\u8ba4\u4e0d\u6253\u5370\uff1b\u4e14\u4e0a\u6e38\u6574\u68f5\u6811<strong>\u6ca1\u6709 <code>cache hit<\/code> \u8fd9\u4e2a\u65e5\u5fd7\u884c<\/strong>\u3002\u5f00\u542f\u540e\u53ea\u6709 <code>--verbose<\/code> \u6216\u65e5\u5fd7\u7ea7\u522b trace \u65f6\u624d\u53ef\u89c1\uff0c\u539f\u6587\u5b57\u6837\u4e3a <code>prompt cache is enabled, size limit: N MiB<\/code>\u3002\n<\/div>\n<p><strong>\u53ef\u9760\u9a8c\u8bc1\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li><code>GET http:\/\/127.0.0.1:8080\/slots<\/code> \u2192 \u67e5\u770b\u6bcf\u4e2a slot \u7684 cached \/ prompt token \u6570\uff0c\u590d\u7528\u6210\u529f\u65f6\u8fd9\u4e9b\u6570\u503c\u4f1a\u4fdd\u7559\u3002<\/li>\n<li>\u542f\u52a8\u52a0 <code>--verbose<\/code>\uff0c\u53ef\u89c1 trace \u65e5\u5fd7\uff1a<code>updating prompt cache<\/code>\u3001<code>saving idle slot to prompt cache<\/code>\u3001<code>prompt cache is enabled, size limit: N MiB<\/code>\u3002<\/li>\n<li>\u6700\u76f4\u89c2\u7684\u4f53\u73b0\uff1a\u9996\u8bf7\u6c42\u4e4b\u540e\uff0c\u76f8\u540c\u524d\u7f00\u8bf7\u6c42\u7684 <strong>prefill \u8017\u65f6\u4ece\u51e0\u79d2\u964d\u5230\u51e0\u5341\u6beb\u79d2<\/strong>\u3002<\/li>\n<\/ul>\n<footer>\n<p>\u9644\uff1a\u914d\u5408 Windows \u73af\u5883\u53d8\u91cf\u7f16\u8f91\u5668\u201c\u4e00\u952e\u914d\u7f6e\u201d\u65f6\u8bf7\u9075\u5faa\u4e24\u6761\u89c4\u5219\u2014\u2014<br \/>\n    <br \/>\u2460 \u53d8\u91cf\u540d\u4e00\u5f8b\u7528 <code>LLAMA_ARG_<\/code> \u524d\u7f00\uff08\u5982 <code>LLAMA_ARG_CACHE_RAM<\/code>\uff09\uff1b\u5e03\u5c14\u53d6\u53cd\u7528 <code>LLAMA_ARG_NO_<\/code> \u524d\u7f00\u3002<br \/>\n    <br \/>\u2461 \u628a\u65e7\u7684 <code>LLAMA_NUM_THREADS<\/code> \u6539\u6210 <code>LLAMA_ARG_THREADS<\/code>\uff1b<strong>\u4e0d\u8981<\/strong>\u52a0\u5165 <code>LLAMA_ARG_SSD_CACHE<\/code> \/ <code>LLAMA_ARG_SSD_CACHE_PATH<\/code>\u3002\n  <\/p>\n<p>\u672c\u6587\u6240\u6709\u53c2\u6570\u5747\u5bf9\u7167 <code>ggml-org\/llama.cpp<\/code> master\uff08commit <code>5aeac37<\/code>\uff0c2026-10-18\uff09\u6e90\u7801\u9010\u6761\u6838\u9a8c\u3002<\/p>\n<\/footer>\n","protected":false},"excerpt":{"rendered":"<p>llama.cpp \u91cc\u201c\u5df2\u5904\u7406\u6587\u672c\u7f13\u5b58\u201d\u7684\u672c\u8d28\u662f KV Cache\uff1a\u6a21\u578b\u628a\u7cfb\u7edf\u63d0\u793a\u3001\u957f\u5bf9\u8bdd\u524d\u7f00\u8ba1\u7b97\u540e\u7684 Key [&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":5,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":2801,"href":"https:\/\/www.o6s.net\/index.php\/wp-json\/wp\/v2\/posts\/2508\/revisions\/2801"}],"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}]}}