Prhub

#27747 fix: DSV4 BCG compress-prefill plan OOB on underfilled (tiny) prefill replay

原始 PR 作者 YAMY1234 合并时间 2026-06-11 11:02 文件变更 1 提交数 1 评论 6 代码增减 +5 / -1

执行摘要

修复 DSV4 BCG 压缩预填充越界访问

在 BCG 模式下,启动 warmup 或短预填充请求被回放到大图桶(如 2048 token)时,plan_compress_prefill_kernel0 的 MTP 均匀路径使用填充后的缓冲区大小 num_q 作为循环上界,导致 batch_id = k / E 超出实际 batch 数,读取未初始化的共享状态并生成无效计划条目,进而在后续内核中索引越界引发 IMA。

该 PR 虽简单但核心,值得精读以理解 BCG 预填充计划的边界条件。评审者的回复也表明这是一个容易忽略的细节。

讨论亮点

评审者 DarkSharpness 表示 'LGTM. I never consider the padded prefill case in prefill plan. Thanks for the fix.',没有其他讨论。

实现拆解

在 python/sglang/jit_kernel/csrc/deepseek_v4/c_plan.cuh 中,将 MTP 均匀路径(Path 1)的循环上界由 num_q(填充后的缓冲区大小)改为 num_real_q = params.batch_size * E(真实 token 数),并添加注释说明原因。同时确保填充的尾部条目已被 Stage D 标记为 invalid,与通用路径行为一致。

文件 模块 状态 重要度
python/sglang/jit_kernel/csrc/deepseek_v4/c_plan.cuh JIT 内核 modified 4.05

关键符号

plan_compress_prefill_kernel0

关键源码片段

python/sglang/jit_kernel/csrc/deepseek_v4/c_plan.cuh core-logic

核心修复文件:修改了 MTP 均匀路径的循环上界,防止越界访问。

__global__ __launch_bounds__(1024, 1)
void plan_compress_prefill_kernel0(...) {
    // ...
    if (is_mtp_extend) {
        // Path 1: token-driven. Each global token id maps to exactly one (batch_id, j).
        const uint32_t E = s_max_extend;
        // num_q is the padded buffer size (graph bucket), not the work size: cap the
        // loop at the real token count so batch_id = k / E stays < batch_size on an
        // underfilled replay; Stage D pads [counter, num_q) with invalid.
        const uint32_t num_real_q = params.batch_size * E;
        for (uint32_t k = tx; k < num_real_q; k += block_size) {
            const uint32_t batch_id = k / E;
            const uint32_t j = k % E;
            const int32_t pl = s_prefix_len[batch_id];
            // ...
        }
    }
    // ...
}

评论区精华

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

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

风险与影响

风险极低。改动仅将循环上界从缓冲区大小改为真实 token 数,当预填充未填满时只是减少无效迭代;当填满时(batch_size * E == num_q)行为完全不变。不影响通用路径和非均匀路径。

修复了 DeepSeek-V4 在 BCG 模式下启动时因 warmup 请求导致的崩溃,影响了所有启用 --enforce-piecewise-cuda-graph 且图桶远大于请求的配置。对正常长预填充无影响。

核心路径变更 无测试覆盖

关联 Issue

未识别关联 Issue

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

完整报告

参与讨论