Strata优化DeepSeek V4.1:KV拼接与常驻路由加速本地推理
Sorry Sam, my "quite terrible" home DRAM is at 100% now: patched Strata to make DeepSeek V4.1 agent sessions start 20× faster on a good old standard gaming GPU, no datacenter required
本地部署DeepSeek V4.1的同学必看,这篇详细拆解了如何通过KV拼接和常驻路由解决长上下文冷启动慢的痛点,有完整参数和构建命令,直接落地压测你的链路。
Sam Altman recently said that "from a global allocation perspective, actually, it's quite terrible to have everybody have a bunch of the world's limited supply of DRAM sitting in their own computer, and being used maybe 5% of the time."
Sam Altman 最近表示:“从全球资源分配的角度来看,让每个人都把世界有限的 DRAM 供应大量囤积在自己的电脑里,并且可能只有 5% 的时间在使用,这其实是非常糟糕的。”
Challenge accepted. Here's my "quite terrible" home setup: an RX 9070 XT 16 GB, a 5800X3D, 64 GB of DDR4 that's now worth more than the rest of the PC, and a PCIe 3.0 NVMe. It runs DeepSeek V4.1 Flash at 1M context (IST-DAS 1.75 bpw; the lab reports 98.7% of the full model's task average) through Strata, fully offline, with the experts spread across VRAM, RAM and the SSD.
挑战接受。这是我的“非常糟糕”的家庭配置:RX 9070 XT 16 GB、5800X3D、64 GB DDR4(其价值现在超过了电脑其余部分的总和),以及 PCIe 3.0 NVMe。它通过 Strata 在完全离线的情况下运行 DeepSeek V4.1 Flash,上下文长度为 1M(IST-DAS 1.75 bpw;实验室报告称其达到完整模型任务平均水平的 98.7%),专家模块分布在 VRAM、RAM 和 SSD 上。
Generation at ~7 tok/s was fine. The problem was prompt reading: every new OpenCode session re-read the full ~37K-token system prompt plus tools, about 100 s before the first token. That's datacenter-grade waiting without the datacenter.
生成速度约为每秒 7 个 token,这没问题。问题出在提示词读取上:每个新的 OpenCode 会话都会重新读取完整的约 37K token 的系统提示词及工具,在第一 token 输出前耗时约 100 秒。这相当于没有数据中心硬件却有着数据中心级别的等待时间。
So I patched the DeepSeek engine. Two changes:
因此我修补了 DeepSeek 引擎。做了两项更改:
1. KV splice: stop re-reading what you already read
1. KV 拼接:停止重复读取已读内容
Agents put per-session text (working directory, date, project CLAUDE.md, skill paths with random IDs) inside the system prompt, so the prefix cache misses on every new session. The engine now aligns a new prompt against every saved conversation, cuts the stale lines out of the cached state (moving the cached rows down, rotating the window rings, recording a RoPE gap), and reads only the changed lines plus your message. Changed lines move as whole lines, so the model never sees fragments.
代理程序将每次会话的文本(工作目录、日期、项目 CLAUDE.md、带有随机 ID 的技能路径)放入系统提示词中,导致前缀缓存在新会话中全部失效。引擎现在会将新提示词与每条保存的对话进行对齐,从缓存状态中剔除过时的行(将缓存行下移,旋转窗口环,记录 RoPE 间隙),并仅读取变更的行加上你的消息。变更的行以整行移动,因此模型永远不会看到碎片化的内容。
2. Resident-only routing: make the RAM earn its keep
2. 常驻路由:让 RAM 发挥其价值
Even a short prompt touches nearly every expert of every layer, so prompt reads streamed most of the model off the SSD: 58 GB for a 1.2K-token prompt. During prompt reading only, tokens now pick from experts already sitting in VRAM or RAM. Generation routes normally. Turns out home DRAM is pretty useful when you actually let it do things.
即使是短提示词也会触及每一层的几乎所有专家模块,因此在提示词读取期间,大部分模型数据被从 SSD 流式传输到内存:一个 1.2K token 的提示词涉及 58 GB 的数据。仅在提示词读取阶段,token 选择来自已经位于 VRAM 或 RAM 中的专家模块。生成阶段的路由则正常进行。事实证明,当你真正让家庭 DRAM 发挥作用时,它相当有用。
Results (same machine) as seen above on the body image
结果(同一台机器)如上图中所示
Caveats:
注意事项:
- HIP (gfx1201) only. I haven't built or tested CUDA, SYCL or Metal.
- The splice is approximate: tokens after a cut were computed with the removed text in context. I haven't seen it matter for short guidance lines, but there's no formal eval.
- Resident routing changes answers slightly (substitute experts during prompt reads). My hard coding suite didn't move (12/13 both ways), but that's one run each.
- Both are on by default in the branch; STRATA_DS4_REORDER=0 and STRATA_DS4_PROMPT_RESIDENT=0 turn them off.
- It also includes a one-line fix for a GPU page fault I hit on a 37K-token prompt (#4912).
- 仅限 HIP (gfx1201)。我尚未构建或测试 CUDA、SYCL 或 Metal。
- 拼接是近似的:切割后的 token 是在包含被移除文本的上下文中计算的。我尚未发现这对简短的指导性行有影响,但尚无正式评估。
- 常驻路由会略微改变答案(在提示词读取期间替换专家模块)。我的硬编码套件结果未变(双向均为 12/13),但这只是各运行了一次。
- 两者在分支中默认开启;设置 STRATA_DS4_REORDER=0 和 STRATA_DS4_PROMPT_RESIDENT=0 可将其关闭。
- 它还包含一行针对我在 37K token 提示词上遇到的 GPU 页面错误的修复 (#4912)。
Branch: https://github.com/PhialsBasement/Strata/tree/dsv41-kv-splice
分支:https://github.com/PhialsBasement/Strata/tree/dsv41-kv-splice
Build (HIP example):
构建(HIP 示例):
git clone -b dsv41-kv-splice https://github.com/PhialsBasement/Strata.git cd Strata cmake -S . -B build-hip -G Ninja -DCMAKE_BUILD_TYPE=Release -DSTRATA_ENABLE_HIP=ON \ -DSTRATA_ENABLE_CUDA=OFF -DCMAKE_HIP_ARCHITECTURES=gfx1201 -DSTRATA_BUILD_DSV41=ON cmake --build build-hip --target strata-dsv41 DS4_CHUNK_MIN=16 STRATA_DSV41_EXE=$PWD/build-hip/strata-dsv41 strata launch --preset <your preset>If you use OpenCode, also add "interleaved": { "field": "reasoning_content" } to the model entry, otherwise OpenCode drops earlier reasoning and the cache misses more often.
如果你使用 OpenCode,还需在模型条目中添加 "interleaved": { "field": "reasoning_content" },否则 OpenCode 会丢弃早期的推理内容,导致缓存命中率降低。
Upstream issues with details: #4921 (splice), #4936 (resident routing). If you try it on other hardware, especially CUDA, numbers in those threads would really help get it merged.
上游问题详情:#4921(splice)、#4936(resident routing)。如果你在其它硬件上尝试,尤其是 CUDA,那些线程中的数值将极大地有助于推动合并。
Utilization report for Sam pissman, VRAM holding experts, RAM holding experts, SSD streaming the rest at full bandwidth. The only idle component in this system is my wallet, thanks to DRAM prices.
Sam Pissman 的利用率报告:VRAM 承载专家,RAM 承载专家,SSD 以全带宽流式传输其余部分。该系统里唯一的闲置组件是我的钱包,这都要归功于 DRAM 的价格。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力