Ternary-Bonsai-2-27B本地部署参数优化

在一台 4060 Ti 的本地机器上部署了 Ternary-Bonsai-2-27B,为了确保能够正常顺利不卡顿的高效输出,需要修改配置参数,优化办法如下。

启动命令:

./start_server.ps1 -port 8080 -ngl 99 -c 65536 -np 1

启动说明:

说明:
将该工作目录放在C盘目录下,类似这种结构:C:\AI\AImodels\TernaryBonsai
以管理员模式运行PowerShell,进入到 C:\AI\AImodels\TernaryBonsai,选择 ./start_server.ps1运行
web访问:http://127.0.0.1:8080/
api调用:
API:http://127.0.0.1:8080/v1/chat/completions
Model:Ternary-Bonsai-2-27B-PQ2_0.gguf
APIKEY:sk-bonsai-60fa5e9ac9a7444d95802bc8

 ./start_server.ps1 -port 8080 -ngl 99 -c 65536 -np 1

我本地的启动脚本 start_server.ps1:

# =============================================================
# Ternary Bonsai 2 27B - llama-server launcher (Windows)
# Target  : 172.17.10.204  (RTX 4060 Ti 16GB / i5-9600KF 6C / 32GB RAM / Win11)
# Runtime : PrismML llama.cpp fork (prism-b10709 or newer, CUDA 12)
# Model   : .\Model\Ternary-Bonsai-2-27B-PQ2_0.gguf (+ optional mmproj)
#
# Tuned for 16GB VRAM, single user, offline, API-key only, code/doc tasks:
#   -ngl 99          full GPU offload (weights ~6.7GB)
#   -c 262144        full native context in ONE slot (-np 1 -> 262K per request)
#   KV q8_0          ~4.2GB KV -> total VRAM ~12.5GB, ~3.5GB headroom
#   --no-mmproj      vision disabled by default (code/doc only, saves ~0.6GB)
#   --no-webui       API-only (set BONSAI_WEBUI=1 to enable)
#
# Usage:
#   .\start_server_204.ps1                              # defaults below
#   $env:BONSAI_PORT="8081"; .\start_server_204.ps1     # change port
#   $env:BONSAI_CTX="131072"; .\start_server_204.ps1    # smaller context
#   $env:BONSAI_KV="q4_0";  .\start_server_204.ps1      # KV: q8_0 (default) / q4_0 / f16
#   $env:BONSAI_PARALLEL="2"; .\start_server_204.ps1    # split ctx across slots
#   $env:BONSAI_MMPROJ="1"; .\start_server_204.ps1      # enable vision (needs mmproj file)
#   $env:BONSAI_WEBUI="1";  .\start_server_204.ps1      # enable Web UI
#   $env:BONSAI_API_KEY="sk-xxx"; .\start_server_204.ps1  # override api_key.txt
#   $env:BONSAI_EXTRA_ARGS="--metrics"; .\start_server_204.ps1  # append raw args
# =============================================================
$ErrorActionPreference = "Stop"

$Root     = $PSScriptRoot
$ModelDir = Join-Path $Root "Model"
$BinDir   = Join-Path $Root "Runtime\Bonsai-demo\bin\cuda"

# ---- tunables (env override) ----
$Port     = if ($env:BONSAI_PORT)     { $env:BONSAI_PORT }     else { "8080" }
$Host_    = if ($env:BONSAI_HOST)     { $env:BONSAI_HOST }     else { "0.0.0.0" }
$Ngl      = if ($env:BONSAI_NGL)      { $env:BONSAI_NGL }      else { "99" }
$Ctx      = if ($env:BONSAI_CTX)      { $env:BONSAI_CTX }      else { "62144" }
$Parallel = if ($env:BONSAI_PARALLEL) { $env:BONSAI_PARALLEL } else { "1" }
$KvType   = if ($env:BONSAI_KV)       { $env:BONSAI_KV }       else { "q8_0" }
$Alias    = if ($env:BONSAI_ALIAS)    { $env:BONSAI_ALIAS }    else { "Ternary-Bonsai-2-27B" }

# ---- API key: env BONSAI_API_KEY > api_key.txt > none (open access, warn) ----
$ApiKeyEnv = $env:BONSAI_API_KEY
$KeyFile   = Join-Path $Root "api_key.txt"
if (-not $ApiKeyEnv -and -not (Test-Path $KeyFile)) {
    Write-Host "[WARN] No BONSAI_API_KEY and no api_key.txt -> server will be OPEN (no auth)." -ForegroundColor Yellow
}

# ---- locate model ----
$Model = Get-ChildItem -Path $ModelDir -Filter "Ternary-Bonsai-2-27B-PQ2_0.gguf" -File -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $Model) {
    $Model = Get-ChildItem -Path $ModelDir -Filter "Ternary-Bonsai-2-27B-PTQ1_0.gguf" -File -ErrorAction SilentlyContinue | Select-Object -First 1
}
if (-not $Model) { Write-Host "[ERR] No Bonsai 2 GGUF found in $ModelDir" -ForegroundColor Red; exit 1 }

$UseVision = ($env:BONSAI_MMPROJ -eq "1")
$Mmproj = $null
if ($UseVision) {
    $Mmproj = Get-ChildItem -Path $ModelDir -Filter "*mmproj*.gguf" -File -ErrorAction SilentlyContinue | Select-Object -First 1
    if (-not $Mmproj) { Write-Host "[ERR] BONSAI_MMPROJ=1 but no mmproj gguf in $ModelDir" -ForegroundColor Red; exit 1 }
}

# ---- runtime check ----
$ServerExe = Join-Path $BinDir "llama-server.exe"
if (-not (Test-Path $ServerExe)) { Write-Host "[ERR] llama-server.exe not found in $BinDir" -ForegroundColor Red; exit 1 }
$env:Path = "$BinDir;$env:Path"

# rough VRAM estimate (weights 6.7GB + KV at q8_0 ~= 16.4KB/token) for the banner
$kvBytes = 262144 * 16.4KB * ($Ctx -as [double]) / 262144
if ($KvType -eq "q4_0") { $kvBytes *= 0.5 }
if ($KvType -eq "f16")  { $kvBytes *= 2 }
$kvGB = [math]::Round($kvBytes / 1GB, 1)

Write-Host ""
Write-Host "=== Ternary Bonsai 2 27B -> 172.17.10.204 (RTX 4060 Ti 16GB) ==="
Write-Host "  Model : $($Model.Name)"
Write-Host "  Vision: $(if ($Mmproj) { $Mmproj.Name } else { 'disabled (BONSAI_MMPROJ=1 to enable)' })"
Write-Host "  WebUI : $(if ($env:BONSAI_WEBUI -eq '1') { 'enabled' } else { 'disabled (BONSAI_WEBUI=1 to enable)' })"
Write-Host "  Args  : -ngl $Ngl -c $Ctx -np $Parallel -kv $KvType  (est. VRAM ~$([math]::Round(6.7 + $kvGB + 1.3, 1))GB / 16GB)"
Write-Host "  API   : http://${Host_}:${Port}/v1/chat/completions   (model name: $Alias)"
Write-Host ""

# ---- build args ----
$serverArgs = @(
    "-m", $Model.FullName,
    "--alias", $Alias,
    "--host", $Host_, "--port", $Port,
    "-ngl", $Ngl, "-fa", "on", "-c", $Ctx, "-np", $Parallel,
    "--temp", "1.0", "--top-p", "0.95", "--top-k", "20",
    "--jinja",
    "--no-webui", "--no-mmproj"
)
if ($KvType -ne "f16") { $serverArgs += @("--cache-type-k", $KvType, "--cache-type-v", $KvType) }
if ($Mmproj)           { $serverArgs += @("--mmproj", $Mmproj.FullName) }
if ($env:BONSAI_WEBUI -eq "1") { $serverArgs = $serverArgs | Where-Object { $_ -ne "--no-webui" } }
if ($ApiKeyEnv)        { $serverArgs += @("--api-key", $ApiKeyEnv) }
elseif (Test-Path $KeyFile) { $serverArgs += @("--api-key-file", $KeyFile) }
if ($env:BONSAI_EXTRA_ARGS) { $serverArgs += ($env:BONSAI_EXTRA_ARGS -split "\s+") }

& $ServerExe @serverArgs
exit $LASTEXITCODE

AI给的推荐结果,一套"稳跑"参数

image

<html style="margin:0;padding:0;">
<title>4060Ti 16GB 运行 Bonsai-27B 参数方案</title>
<div style="width:100%;box-sizing:border-box;max-width:980px;background:#fbfcfe;border:1px solid #e3e8ef;border-radius:12px;padding:16px 18px 14px;font-family:-apple-system,'PingFang SC','Microsoft YaHei',sans-serif;color:#1f2937;">
  <div style="font-size:16px;font-weight:700;margin-bottom:4px;">Ternary-Bonsai-2-27B-PQ2_0 · 4060 Ti 16GB 稳跑方案</div>
  <div style="font-size:12px;color:#6b7280;margin-bottom:10px;">模型 7.2GB + mmproj 1.5GB · 显存预算按 q8_0 KV 估算</div>
  <div style="display:flex;gap:12px;flex-wrap:wrap;">
    <div style="flex:1;min-width:300px;background:#f0fdf4;border:1px solid #bbf7d0;border-radius:10px;padding:12px;">
      <div style="font-size:13px;font-weight:700;color:#166534;margin-bottom:6px;">方案 A · 最稳(推荐先用这个)</div>
      <div style="font-size:12.5px;font-family:monospace;background:#fff;border:1px solid #e2e8f0;border-radius:6px;padding:6px 8px;line-height:1.8;color:#1f2937;">-c 32768<br>-ngl 99<br>-np 1<br>-fa on<br>--jinja</div>
      <div style="font-size:12px;color:#374151;margin-top:6px;">显存 ≈ 7.2+1.5+3 = <b>11.7GB</b>,留 4GB 余量,最不容易 OOM</div>
    </div>
    <div style="flex:1;min-width:300px;background:#fffbeb;border:1px solid #fde68a;border-radius:10px;padding:12px;">
      <div style="font-size:13px;font-weight:700;color:#92400e;margin-bottom:6px;">方案 B · 上下文更大(32K→64K)</div>
      <div style="font-size:12.5px;font-family:monospace;background:#fff;border:1px solid #e2e8f0;border-radius:6px;padding:6px 8px;line-height:1.8;color:#1f2937;">-c 65536<br>-ngl 99<br>-np 1<br>-ctk q4_0 -ctv q4_0<br>-fa on --jinja</div>
      <div style="font-size:12px;color:#374151;margin-top:6px;">KV 用 q4_0 减半:显存 ≈ 7.2+1.5+3 = <b>11.7GB</b>,上下文翻倍</div>
    </div>
  </div>
  <div style="font-size:12.5px;color:#374151;margin-top:10px;background:#f1f5f9;border:1px solid #e2e8f0;border-radius:8px;padding:8px 10px;">
    <b>关键原则:</b>① <b>-c 千万别开 524288</b>(KV 要 ~50GB,这就是之前"无输出"的根因);② <b>-np 别开大</b>(会切小每个请求的上下文并浪费内存,1 路并发就够);③ 先跑通方案 A,再试方案 B。
  </div>
</div>
</html>

启动命令(二选一)

方式 ①:环境变量驱动 start_server.ps1(脚本只认环境变量,不认命令行参数)

# 方案 A
$env:BONSAI_NGL="99"
$env:BONSAI_CTX="32768"
$env:BONSAI_PARALLEL="1"
.\start_server.ps1

# 方案 B(想上 64K 上下文)
$env:BONSAI_NGL="99"
$env:BONSAI_CTX="65536"
$env:BONSAI_PARALLEL="1"
$env:BONSAI_KV="q4_0"
.\start_server.ps1

方式 ②:直接跑 llama-server.exe(最可控,推荐)

cd C:\AI\AImodels\TernaryBonsai
.\Runtime\Bonsai-demo\bin\cuda\llama-server.exe `
  -m Model\Ternary-Bonsai-2-27B-PQ2_0.gguf `
  -mmproj Model\*mmproj*.gguf `
  -c 32768 -ngl 99 -np 1 -fa on --jinja

纯文本场景可去掉 -mmproj 那行(省 1.5GB 显存,更稳)。

启动后三步验证

# ① 看启动日志(关键行):
#    "n_ctx = 32768"(或 65536)——不是 11008/22016
#    "server is listening on [http://0.0.0.0:8080](http://0.0.0.0:8080)" —— 服务已就绪

# ② 确认 API 上下文
curl [http://127.0.0.1:8080/v1/models](http://127.0.0.1:8080/v1/models)

# ③ 发一条测试请求
curl [http://127.0.0.1:8080/v1/chat/completions](http://127.0.0.1:8080/v1/chat/completions) -d '{
  "model": "Ternary-Bonsai-2-27B",
  "messages": [{"role": "user", "content": "你好,介绍一下你自己"}],
  "max_tokens": 512
}'

能返回正常 JSON 文本 = 跑通了。

如果还有问题的排查优先级

现象 处理
启动无输出/卡住 确认旧进程已停:Get-Process | Where-Object {$_.ProcessName -like "*llama*"} | Stop-Process -Force,再重跑
报 KV 内存不足 把 -c 降到 16384,或 KV 换 q4_0(方案 B 参数)
报 context_exceeded 请求 tokens 超过 -c,压缩输入或加大 -c(但别超 65536)
输出乱码/空白 确认加了 --jinja(chat template 必需)

另外提醒一句:这套部署包如果来源不是 PrismML 官方渠道(Bonsai-demo / HuggingFace prism-ml),建议先用官方包核对文件后再跑——之前那台机器上发现过恶意文件。先把启动日志的 n_ctx 行发我,我确认参数是否真生效。

posted @ 2026-10-13 14:48  念槐聚  阅读(14)  评论(0)    收藏  举报