Skip to content

[Bug] Windows + full offload 模式下 --cache-ram 0 --ctx-checkpoints 0 导致多轮对话每轮都重新 prefill,严重影响交互体验 #8774

Closed

Description

@lamboJw

Issue: Windows + full offload 模式下 --cache-ram 0 --ctx-checkpoints 0 导致多轮对话每轮都重新 prefill,严重影响交互体验

问题描述

在 Unsloth Studio 的 Windows + full offload 调优路径中(代码位于 studio/backend/core/inference/llama_cpp.py),--cache-ram 和 --ctx-checkpoints 被硬编码为 0。这导致 llama-server 在多轮对话的每一轮都完全重新 prefill prompt,即使上下文大部分是重复的。

复现代码路径

文件: unsloth/studio/backend/core/inference/llama_cpp.py
触发条件: sys.platform == "win32" and full_offload_tuning_active

当前行为(硬编码):

cmd.extend(["--cache-ram", "0"])
cmd.extend(["--ctx-checkpoints", "0"])

实测数据

硬件环境:

  • CPU: AMD Ryzen 7 5700X (8C16T)
  • GPU: NVIDIA GeForce RTX 4070 Super 12G + RTX 4060 Ti 16G (双卡,SLI 关闭)
  • 内存: 64 GB DDR4
  • 操作系统: Windows 11 Pro (WDDM 3.x)
  • 模型: Qwen3.6-35B-A3B-MTP-GGUF (IQ4_NL)
  • MTP speculative decoding: draft-mtp, spec-draft-n-max 2

llama-server 启动参数:

llama-server -m models/Qwen3.6-35B-A3B-UD-IQ4_NL.gguf --port 5389 --parallel 1 \
  --flash-attn on --no-context-shift -c 144512 --alias Qwen3.6-35B-A3B-MTP-GGUF \
  -ngl -1 --fit off --metrics --slot-save-path cache/llama-slots \
  --threads 2 --jinja --cache-type-k q8_0 --cache-type-v q8_0 \
  --spec-type draft-mtp --spec-draft-n-max 2 \
  --mmproj models/mmproj-BF16.gguf \
  --cache-ram 0 --ctx-checkpoints 0

同一多轮对话会话中各 turn 的 prompt eval time:

Turn Task ID Prompt Tokens Prompt Eval Time Decode Tokens Decode Time
1 15 395 625 ms 1542 15105 ms
2 655 27287 11832 ms 150 1733 ms
3 730 28221 12331 ms 592 6615 ms
4 964 28684 12874 ms 263 2917 ms
5 1079 ~28000 ~13000 ms* - cancelled
6 1137 28324 12253 ms 56 566 ms
7 1840 31074 13842 ms 300 3958 ms
8 1984 4591 1714 ms (small continuation) 1844 17305 ms
9 2720 4609 1790 ms (small continuation) 1524 13838 ms
10 3300 151 118 ms (tiny continuation) 1452 13314 ms
11 3882 39451 17886 ms 259 3425 ms
12 4011 31227 14552 ms 70 1084 ms
13 4063 156 119 ms (tiny continuation) 1986 19116 ms
14 4857 42316 19348 ms 297 4209 ms
15 5003 156 137 ms (tiny continuation) 1979 18441 ms
16 5784 49307 24801 ms - ongoing

*Task 1079 被用户取消,未完成。

关键观察:

  • Turn 2-7、11-12、14-16:prompt 包含完整的历史对话(~28k-49k tokens),每轮都需要 12-25 秒重新 prefill
  • 这些 turn 之间上下文绝大部分是相同的,只有少量新消息追加
  • 即使启用了 --slot-save-path(KV slot checkpointing),由于 --cache-ram 0 --ctx-checkpoints 0,checkpoint 机制无法工作
  • 日志中可以看到 LCP similarity 匹配到了高相似度的历史 slot(f_sim_best = 0.967-0.995, f_keep = 0.712-0.995),但 checkpoint 没有被加载

补充数据(来自 port 14796 日志,更长对话 ~78k tokens):

以下数据展示了 f_keep 与 prompt eval time 的明确关系:

Task ID f_sim_best f_keep Prompt Tokens Prompt Eval Time 行为
278 0.661 0.252 30,623 13,615 ms full prefill(放弃复用)
2206 0.674 0.671 41,013 18,877 ms full prefill(放弃复用)
2887 0.628 0.629 44,142 20,667 ms full prefill(放弃复用)
3665 0.608 0.604 45,768 21,510 ms full prefill(放弃复用)
4736 0.539 0.544 51,764 25,166 ms full prefill(放弃复用)
7798 0.422 0.429 66,301 33,954 ms full prefill(放弃复用)
10172 0.770 0.345 31,368 14,140 ms full prefill(放弃复用)
10285 0.514 0.875 53,775 26,764 ms full prefill
11723 0.361 0.358 76,909 41,499 ms full prefill(放弃复用)
--- --- --- --- --- ---
325 0.834 0.997 6,110 3,245 ms checkpoint 复用 ✅
568 0.972 0.984 1,044 904 ms checkpoint 复用 ✅
1632 0.986 0.997 546 620 ms checkpoint 复用 ✅
1901 0.971 0.983 1,148 1,155 ms checkpoint 复用 ✅
2006 0.976 0.994 998 891 ms checkpoint 复用 ✅
2423 0.963 0.989 1,593 1,155 ms checkpoint 复用 ✅
2529 0.977 0.994 1,008 960 ms checkpoint 复用 ✅
2723 0.996 0.990 177 378 ms checkpoint 复用 ✅
2807 0.997 0.995 143 359 ms checkpoint 复用 ✅
2968 0.997 0.997 126 412 ms checkpoint 复用 ✅
3072 0.997 0.994 126 458 ms checkpoint 复用 ✅
3351 0.977 0.984 1,037 973 ms checkpoint 复用 ✅
3444 0.997 0.995 126 398 ms checkpoint 复用 ✅
3872 0.989 0.991 502 627 ms checkpoint 复用 ✅
4032 0.996 0.992 189 434 ms checkpoint 复用 ✅
4111 0.946 0.996 2,673 1,809 ms checkpoint 复用 ✅
4415 0.974 0.985 1,327 1,124 ms checkpoint 复用 ✅
4819 0.972 0.998 1,487 1,168 ms checkpoint 复用 ✅
6616 0.968 0.983 1,940 1,485 ms checkpoint 复用 ✅
7045 0.969 0.983 1,961 1,544 ms checkpoint 复用 ✅
7412 0.970 0.985 1,961 1,561 ms checkpoint 复用 ✅
9199 0.959 0.960 13,683 9,175 ms checkpoint 复用(大continuation)✅
10252 0.995 0.995 152 409 ms checkpoint 复用 ✅
11250 0.998 0.975 149 542 ms checkpoint 复用 ✅
11305 0.998 0.998 162 632 ms checkpoint 复用 ✅
12237 0.967 0.986 2,624 2,196 ms checkpoint 复用 ✅

f_keep 阈值效应(关键发现):

f_keep ≥ 0.95 → prompt eval ~0.4-2s (高效 checkpoint 复用) ✅
f_keep 0.85-0.95 → 数据不足,需更多样本
f_keep < 0.70 → prompt eval 12-41s (完整 prefill,放弃复用) ❌

核心规律:

  • f_keep 是决定 checkpoint 是否被复用的关键指标
  • 当 f_keep < ~0.7 时,llama.cpp 判断 partial prompt processing 的额外开销超过了完整 prefill 的收益,选择放弃复用、从头计算
  • llama.cpp 内部 LCP 匹配阈值是 0.100,但低于 0.7 的匹配实际上没有加速效果,反而因为 partial prefill overhead 可能更慢

补充说明:

  • Task 653(port 7337 日志): f_keep=0.544 → prompt eval 25s;Task 10285(port 14796): f_sim_best=0.514, f_keep=0.875 → prompt eval 26.8s,说明即使 f_keep 看似较高,当 f_sim_best 太低时仍会触发 full prefill
  • Task 10354(port 14796): f_sim_best=0.705, f_keep=0.998 → prompt eval 14.8s,这是一个 borderline 案例——f_keep 极高但仍有较大 prompt tokens(22k),可能是 context 过大导致 checkpoint 切换开销增加

为什么需要这些参数

--cache-ram N 将 prompt cache(KV 前缀)保留在主机 RAM 中,避免每轮重新 prefill。
--ctx-checkpoints N 保留 N 个上下文检查点,支持 slot 之间的快速切换。

当前 Unsloth Studio 的设置理由是为了"减少 WDDM/PCI-E overhead",但代价是每个包含完整历史的 turn 都要多花 12-25 秒等待 prefill。对于交互式对话来说,这个延迟比 decode 阶段慢 20 tok/s 更影响体验。

建议方案

方案 A:增加可配置参数(推荐)

将 --cache-ram、--ctx-checkpoints 的值从硬编码改为可通过 UI/配置调整的参数,默认值可以根据用户选择切换:

  • "速度优先"模式:--cache-ram 16000 --ctx-checkpoints 256(大幅减少 prefill 时间)
  • "显存保守"模式:保持当前 --cache-ram 0 --ctx-checkpoints 0

方案 B:调整默认值

将默认值从 0 改为有意义的非零值,例如:

cmd.extend(["--cache-ram", "16000"])
cmd.extend(["--ctx-checkpoints", "256"])

性能影响预估

以 Turn 2 (task 655) 为例:

  • 当前(cache-ram=0): prompt eval 11.8s + decode 1.7s = 总延迟 ~13.5s
  • 如果启用 cache-ram: prompt eval 预计可降至 <2s(参考 Turn 8/9/10 的小 continuation),decode 阶段因 KV 在 RAM 中可能略有提升 → 总延迟可能降至 ~4-5s
  • 用户体验: 从"每轮等待 10+ 秒看进度条"改善为"几乎无感过渡"

参考

  • Unsloth Studio 相关代码: studio/backend/core/inference/llama_cpp.py (~line 14600)
  • llama.cpp 文档: --cache-ram, --ctx-checkpoints, --slot-save-path

Activity

  1. added
    feature request
    on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions