Prhub

#53773 [Kimi Bug] Fix k3 torch.AcceleratorError: CUDA error: an illegal memory access was encountered

原始 PR 作者 yewentao256 合并时间 2026-08-26 08:38 文件变更 1 提交数 1 评论 2 代码增减 +2 / -0

执行摘要

修复 K3 profiling 后 Mamba 上下文未释放导致的 CUDA 非法内存访问

提交信息指出修复 torch.AcceleratorError: CUDA error: an illegal memory access was encountered。PR 描述为 A combined bug with #53306 and #52388,说明该问题源于 #53306(MRV2 CUDA graph profiling)与 #52388(K3 Mamba 元数据优化)的组合,两者共同导致 profiling 后 _mamba_ctx 未释放,进而引发非法内存访问。

该 PR 值得精读,尤其关注 _teardown_profiling_state 中对各类 profiling 状态的清理模式。其核心贡献是明确 Mamba 上下文需要在 profiling 后显式释放,这对理解 MRV2 的 profiling 生命周期有重要参考价值。

讨论亮点

评审过程中,njhill 批准了该 PR,表达 Thanks @yewentao256。没有针对改动的讨论线程。claude[bot] 的评论仅为自动提示,无实际技术内容。

实现拆解

  1. 入口定位:修改文件 vllm/v1/worker/gpu/cudagraph_utils.py 中的 _teardown_profiling_state 函数,该函数在 CUDA graph profiling 结束时被调用,负责释放 profiling 期间的临时状态。
  2. 核心改动:在函数开头,torch.accelerator.synchronize() 之后,新增以下代码:
    • 检查 runner.model_state 是否具有 _mamba_ctx 属性;
    • 若有,则将其置为 None,从而释放对 Mamba 上下文 tensor 的引用。
  3. 原因与影响:_mamba_ctx 是 Kimi-K3 Mamba 混合解码所需的设备端状态,profiling 时其 tensor 与 CUDA graph 捕获相关联。若不释放,real capture 阶段会基于已捕获的 tensor 重新分配,但 profiling 阶段的 _mamba_ctx 仍引用了旧的设备内存,导致后续解码访问已释放内存,触发 CUDA 非法地址错误。
  4. 配套改动:无测试文件变更,但改动点位于核心 profiling teardown 路径,属于低风险的资源清理逻辑。
文件 模块 状态 重要度
vllm/v1/worker/gpu/cudagraph_utils.py 执行器 modified 6.04

关键符号

_teardown_profiling_state

关键源码片段

vllm/v1/worker/gpu/cudagraph_utils.py core-logic

核心修复文件,在 `_teardown_profiling_state` 中新增清理 `_mamba_ctx` 的逻辑,阻断 CUDA 非法内存访问的根因。

# vllm/v1/worker/gpu/cudagraph_utils.py
​
def _teardown_profiling_state(runner: "GPUModelRunner") -> None:
    """Release the profiling KV cache and captured graphs while keeping model
    weights, so the real ``initialize_kv_cache`` starts from a clean slate."""
    torch.accelerator.synchronize()
​
    # 关键修复:K3 Mamba 混合模型在 profiling 时持有 `_mamba_ctx` tensor
    # (见 #52388),若不释放,real capture 阶段会重新分配设备内存,
    # 但旧引用仍指向已释放内存,导致 CUDA illegal memory access(#53306 组合问题)。
    if hasattr(runner.model_state, "_mamba_ctx"):
        runner.model_state._mamba_ctx = None
​
    # 释放 profiling KV cache 及相关状态,保持模型权重不变……
    if hasattr(runner, "kv_caches"):
        runner.kv_caches.clear()
    if hasattr(runner, "attn_groups"):
        runner.attn_groups.clear()
    if hasattr(runner, "kv_cache_config"):
        del runner.kv_cache_config
    # 释放 profiling 阶段捕获的 cudagraph 及一次性图池……
    runner.cudagraph_manager = None
    # ……后续清理 encoder graphs、attention 层的 kv_cache、
    # 重置 num_gpu_blocks、移除 LoRA、触发 GC 与显存回收……
    gc.collect()
    torch.accelerator.empty_cache()

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

  • 风险点:改动位于 _teardown_profiling_state 核心路径,影响所有使用 MRV2 的模型。但改动本身是条件性清理,仅当模型状态存在 _mamba_ctx 时执行,且置为 None 不会影响其他属性。
  • 回归风险:若 _mamba_ctx 在 profiling 后仍需保留(例如某些模型依赖该上下文作为初始状态),则可能导致功能缺失。但本 PR 针对的是 K3 特有的 profiling 后释放场景,且测试(CI 触发)验证了修复。
  • 兼容性:该改动基于 hasattr 判断,对无 _mamba_ctx 的模型无影响,不会引入兼容性问题。
  • 用户影响:修复了 Kimi-K3 在高 TP 部署时可能出现的 CUDA 非法内存访问崩溃,提升了稳定性。
  • 系统影响:改动是普遍适用的资源清理,不会影响其他模型的 profiling 行为。
  • 团队影响:该修复为 Mamba 混合模型在 MRV2 下的正确释放提供了参考模式,后续可推广到其他类似状态。
缺少测试覆盖 核心路径变更

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论