为了优化显存利用率,我决定从 LMStudio 迁移到 llama.cpp。虽然遇到了一些坑,但解决后效果不错,这里记录一下关键步骤和排查经验。
基础启动脚本
下载官方 release 包后,建议将启动命令封装为 .bat 文件,方便管理参数。确保模型路径正确,并根据机器配置调整 GPU_LAYERS 和 THREADS。
@echo off
setlocal
set "MODEL_PATH=F:\Models\Yakyu"
set "MODEL_FILE=Qwen3-235B-A22B-Instruct-2507-UD-Q8.gguf"
set CTX_SIZE=32768
set HOST=127.0.0.1
set PORT=1234
echo 正在启动 OpenAI 兼容 API 服务...
echo 模型:%MODEL_PATH%\%MODEL_FILE%
echo 端口:http://%HOST%:%PORT%
echo.
llama-server.exe ^
-m "%MODEL_PATH%\%MODEL_FILE%" ^
--ctx-size %CTX_SIZE% ^
--host %HOST% ^
--port %PORT%
echo.
echo 服务已停止。按任意键关闭窗口...
pause >nul
运行后若 CMD 窗口显示 all slots are idle,说明模型加载完成。如果输出乱码,尝试将 bat 文件保存为 ANSI 编码格式。
关于参数,默认开启了自动适应显存 (--fit on)。对于大多数设备,不强制指定 --gpu-layers 反而效果更好。当然,参考相关硬件优化文章,合理设置 --n-cpu-moe 值也能提升部分 MOE 模型的推理速度。
分片模型合并
如果下载的 GGUF 模型是分片(shard)的,需要先合并成一个完整文件才能使用。使用 llama-gguf-split 工具执行合并操作:
D:\Yakyu\llama-b7640-bin-win-cuda-12.4-x64\llama-gguf-split.exe --merge F:/Models/Yakyu/Qwen3-235B-A22B-Instruct-2507/Qwen3-235B-A22B-Instruct-2507-UD-Q8_K_XL-00001-of-00006.gguf F:/Models/Yakyu/Qwen3-235B-A22B-Instruct-2507-UD-Q8.gguf
直接在 cmd 中运行即可。
SillyTavern 调用报错处理
在使用 SillyTavern 调用 DeepSeek 类模型时,可能会遇到 Chat Completion API Assistant response prefill is incompatible with enable_thinking 错误。
解决方案
- 关闭思考模式:在启动参数中添加
--reasoning-budget 0。 - 修复聊天模板:关闭思考后,可能会出现输出空白或内容无关的问题,这是因为内置模板不兼容。建议使用 Unsloth 提供的修复版 Jinja 模板。
修改后的启动脚本示例:
@echo off
setlocal
set "MODEL_PATH=F:\Models\Yakyu"
set "MODEL_FILE=DeepSeek-V3.1-Terminus-MXFP4_MOE.gguf"
set CTX_SIZE=32768
set HOST=127.0.0.1
set PORT=1234
echo 模型:%MODEL_PATH%\%MODEL_FILE%
echo 端口:http://%HOST%:%PORT%
echo.
llama-server.exe ^
-m "%MODEL_PATH%\%MODEL_FILE%" ^
--reasoning-budget 0 ^
--chat-template-file "Unslothdeepseek-v3-chat-template.jinja" ^
--ctx-size %CTX_SIZE% ^
--host %HOST% ^
--port %PORT%
echo.
echo 服务已停止。按任意键关闭窗口...
pause >nul
对应的 Jinja 模板文件需放在同一目录下,内容如下:
{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{% if enable_thinking is defined and enable_thinking is false %}{% set thinking = false %}{% elif enable_thinking is defined and enable_thinking is true %}{% set thinking = true %}{% elif not thinking is defined %}{% set thinking = false %}{% endif %}{% set ns = namespace(is_first=false, is_tool=false,, is_first_sp=true, is_last_user=false) %}{%- for message in messages %}{%- if message['role'] == 'system' %}{%- if ns.is_first_sp %}{% set ns.system_prompt = ns.system_prompt + message['content'] %}{% set ns.is_first_sp = false %}{%- else %}{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}{%- endif %}{%- endif %}{%- endfor %}{{ bos_token }}{{ ns.system_prompt }}{%- for message in messages %}{%- if message['role'] == 'user' %}{%- set ns.is_tool = false -%}{%- set ns.is_first = false -%}{%- set ns.is_last_user = true -%}{{'<|User|>' + message['content']}}{%- endif %}{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}{%- if ns.is_last_user %}{{'<|Assistant|></think>'}}{%- endif %}{%- set ns.is_last_user = false -%}{%- set ns.is_first = false %}{%- set ns.is_tool = false -%}{%- for tool in message['tool_calls'] %}{%- if not ns.is_first %}{%- if message['content'] is none %}{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + (tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments'] | tojson) + '<|tool▁call▁end|>'}}{%- else %}{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + (tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments'] | tojson) + '<|tool▁call▁end|>'}}{%- endif %}{%- set ns.is_first = true -%}{%- else %}{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + (tool['function']['arguments'] if tool['function']['arguments'] is string else tool['function']['arguments'] | tojson) + '<|tool▁call▁end|>'}}{%- endif %}{%- endfor %}{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}{%- endif %}{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}{%- if ns.is_last_user %}{{'<|Assistant|>'}}{%- if message['prefix'] is defined and message['prefix'] and thinking %}{{''}}{%- endif %}{%- endif %}{%- set ns.is_last_user = false -%}{%- if ns.is_tool %}{{message['content'] + '<|end▁of▁sentence|>'}}{%- set ns.is_tool = false -%}{%- else %}{%- set content = message['content'] -%}{%- if '</think>' in content %}{%- set splitted = content.split('</think>') -%}{%- set content = splitted[1:] | join('</think>') -%}{%- endif %}{{content + '<|end▁of▁sentence|>'}}{%- endif %}{%- endif %}{%- if message['role'] == 'tool' %}{%- set ns.is_last_user = false -%}{%- set ns.is_tool = true -%}{{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}{%- endif %}{%- endfor -%}{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}{{'<|Assistant|>'}}{%- if not thinking %}{{'</think>'}}{%- else %}{{'<think>'}}{%- endif %}{% endif %}

