Prhub

#25085 Fix swa component host hit

原始 PR 作者 ispobock 合并时间 2026-05-12 21:03 文件变更 1 提交数 1 评论 3 代码增减 +1 / -1

执行摘要

修复 SWA 组件 host hit 计数起点错误

在 Unified HiCache 的 SWA 组件中,finalize_match_result 原先从 last_device_node 开始遍历,当匹配结果包含仅存在于 host 的节点时,无法正确识别 host hit,导致 host_hit_length 未被更新。

该 PR 修复了具体的计数问题,值得关注;建议后续结合 review 反馈评估是否需要进一步优化以处理 device-only 节点场景。

讨论亮点

Review 中指出使用 last_host_node 可能跳过 device-only 节点,导致滑动窗口计数偏低。但该 PR 仅有一行改动,且作者已合并,未展开进一步讨论。

实现拆解

  1. 定位问题:在 python/sglang/srt/mem_cache/unified_cache_components/swa_component.pyfinalize_match_result 方法中,原先使用 result.last_device_node 作为遍历起点。
  2. 修复:将第 98 行 node = result.last_device_node 改为 node = result.last_host_node,确保遍历从最深的 host 节点开始,以正确检查 host-only 模板。
文件 模块 状态 重要度
python/sglang/srt/mem_cache/unified_cache_components/swa_component.py 缓存层 modified 4.82

关键符号

finalize_match_result

关键源码片段

python/sglang/srt/mem_cache/unified_cache_components/swa_component.py core-logic

核心修复文件,修改 `finalize_match_result` 方法中的遍历起点,影响 host hit 检测逻辑。

# python/sglang/srt/mem_cache/unified_cache_components/swa_component.pydef finalize_match_result(self, result, params, value_chunks, best_value_len):
    ct = self.component_type
    n_swa = 0
    # 关键修复:从 last_host_node 开始遍历,确保能检测到 host-only 模板
    node = result.last_host_node
    root = self.cache.root_node
    while node is not root and n_swa < self.sliding_window_size:
        cd = node.component_data[ct]
        if cd.value is None and cd.host_value is not None:
            return result._replace(host_hit_length=max(result.host_hit_length, 1))
        if cd.value is not None:
            n_swa += len(cd.value)
        elif cd.host_value is not None:
            n_swa += len(cd.host_value)
        else:
            break
        node = node.parent
    return result

评论区精华

遍历起点选择可能导致 device-only 节点被跳过 正确性

gemini-code-assist[bot] 指出使用 `last_host_node` 会跳过 device-only 节点,可能低估滑动窗口计数。

结论:作者未回应并直接合并,但当前修复解决了 host hit 检测问题,device 场景需后续评估。 · 已解决

风险与影响

当匹配结束于纯 device 节点时(如新插入未备份的 token),last_host_nodelast_device_node 的祖先,从较浅的节点开始遍历会跳过设备部分,可能导致 n_swa 低估。Review 已指出此风险,但 PR 作者认为当前场景下 host hit 检测更重要。

仅影响 SWA 组件的 host hit 检测路径,对非 SWA 组件无影响。改动极小,风险可控。

潜在低估滑动窗口计数

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论