GLM-5.1 本地部署依赖精确的 transformers==5.3.0 与 SGLang 解析器配置,代码 Agent 工作流必须启用 Interleaved + Preserved Thinking 模式以防止多轮遗忘。
适合的任务:本地私有化部署、高并发 API 代理、Coding Agent(Claude Code、Roo Code、Kilo Code)长程任务执行。
不适合的任务:与依赖 transformers==4.x 的旧模型(如 DeepSeek 旧版)共用同一个 Python 虚拟环境。
适用的模型版本:GLM-5.1、GLM-5.1-FP8。
适用的客户端、Agent 或 API:SGLang (v0.5.10+)、KT-Kernel、vLLM、Claude Code、Roo Code、Kilo Code。
推荐的推理档位和参数:
通用对话:temperature=1.0,top_p=0.95,max_tokens=131072。
终端代码任务(Terminal Bench):temperature=0.7,top_p=1.0,max_tokens=16384,context_length=202752。
多轮工具调用(Tau2-Bench):temperature=0,max_tokens=16384,并开启 preserved thinking。
export PYTORCH_ALLOC_CONF=expandable_segments:True
export SGLANG_ENABLE_JIT_DEEPGEMM=0
python -m sglang.launch_server \
--host 0.0.0.0 \
--port 30000 \
--model /path/to/GLM-5.1-FP8 \
--kt-weight-path /path/to/GLM-5.1-FP8 \
--kt-cpuinfer 96 \
--kt-threadpool-count 2 \
--kt-num-gpu-experts 30 \
--kt-method FP8 \
--kt-gpu-prefill-token-threshold 1024 \
--kt-enable-dynamic-expert-update \
--kt-expert-placement-strategy uniform \
--trust-remote-code \
--mem-fraction-static 0.75 \
--served-model-name GLM5.1 \
--enable-mixed-chunk \
--tensor-parallel-size 8 \
--enable-p2p-check \
--disable-shared-experts-fusion \
--chunked-prefill-size 16384 \
--max-running-requests 4 \
--max-total-tokens 128000 \
--attention-backend flashinfer \
--fp8-gemm-backend cutlass \
--kv-cache-dtype bf16 \
--tool-call-parser glm47 \
--reasoning-parser glm45 \
--watchdog-timeout 3000在多轮代码 Agent(如 Claude Code / Roo Code / API 代理)中,必须保留多轮思考历史:
{
"model": "GLM5.1",
"messages": [
{
"role": "user",
"content": "Analyze the codebase and implement the missing test cases."
}
],
"temperature": 0.7,
"top_p": 1.0,
"max_tokens": 16384,
"chat_template_kwargs": {
"enable_thinking": true,
"clear_thinking": false
}
}若需完全关闭思考模式(仅做常规低延迟补全):
{
"model": "GLM5.1",
"messages": [
{
"role": "user",
"content": "Summarize this diff."
}
],
"chat_template_kwargs": {
"enable_thinking": false
}
}创建独立 Python 虚拟环境,强制安装指定依赖:pip install transformers==5.3.0 sglang-kt。
确保 CUDA 版本 ≥ 12.0(FP8 推荐 CUDA 12.8+),通过 kt-kernel/install.sh 编译专用加速算子。
启动 SGLang 服务,必须显式传入 --tool-call-parser glm47 与 --reasoning-parser glm45,否则工具调用和思维链解析会间歇性失败。
在 OpenAI 兼容客户端中将 base URL 设置为 http://localhost:30000/v1,并在请求中注入 chat_template_kwargs。
针对 OOM 问题,优先微调 --kt-num-gpu-experts 和 --chunked-prefill-size。
官方文档明确指出:GLM-5.1 默认启用 thinking mode,支持 Interleaved Thinking(常规交互)和 Interleaved + Preserved Thinking(代码 Agent 推荐)。
多轮长任务(如 Tau2-Bench、Terminal-Bench 2)中未保留 thinking 会导致模型多轮策略断层。
依赖约束:transformers==5.3.0 为硬性要求,常规 pip 默认安装的 4.x 版本无法加载模型。
CPU-GPU 异构推理对系统内存带宽和 PCI-E 通道要求较高;CPU 卸载专家数过多时首 token 延迟(TTFT)会明显上升。
transformers==5.3.0 与部分旧架构模型(如 DeepSeek 旧版本)存在环境冲突,不宜混装在同一环境。
Preserved thinking 模式会随轮次增加消耗更多上下文窗口,需要设置合理的 max-total-tokens。
官方教程将 Interleaved + Preserved Thinking 明确标记为“Recommended for agentic workflows, especially code agents (e.g., Claude Code, Roo Code, Kilo Code)”,并指出这是长时域任务中保持连贯推理的核心开关。
GLM-5.1