我没有改文件。只读结论:现有离线入口已经够做第一轮 fused K2 / split-stage1 验证,但要注意默认 site-packages 里的 infllm_v2 不暴露新符号;必须用本地扩展路径 PYTHONPATH=/user_4813494d/openbmb/kernels/infllmv2_cuda_impl,否则 varlen_fwd_stage1_k1_mask / stage1_k2_indices_to_k1_mask 不存在。
1. 可复用入口
脚本:
prefill/stage1_offline_profile.py :当前 exact oracle。合成生产形状,跑 stage1 -> pool -> topk,并用 compressed_attention 校验 topk。
prefill/stage1_k2_candidate_recall.py :用真实 capture 的 query_layer/compressed_k2/exact_topk_idx 做 K2 candidate recall。
prefill/stage1_k1_mask_bench.py :最接近 split-stage1 的现成离线 bench。它能跑 full stage1、all-true mask、all-false mask、K2 candidate mask、first-pass-only。
prefill/stage1_k2_score_candidate_bench.py :K2 two-call bench。已有 full_ms / k2_score_ms / mask_from_score_min_ms / masked_stage1_ms / two_call_total_min_ms。
prefill/stage1_no_p_upper_bound.py :测 no-P 和 first-pass-only 上界。
prefill/single_longest_prefill.py :在线单条 line91 客户端;主要用于重新 capture,不是离线 bench。
Python 函数:
minicpm_sparse_utils.py _infllmv2_attn_stage1:当前生产 stage1 wrapper。
minicpm_sparse_utils.py _infllmv2_attn_stage1_k1_mask:第二 K1 pass mask 入口。
minicpm_sparse_utils.py _infllmv2_attn_stage1_blockmax:exact blockmax fusion wrapper。
minicpm_sparse_utils.py _stage1_build_k2_score_mask:当前 K2-score -> topk -> uint64 mask 逻辑。
minicpm_sparse_utils.py compressed_attention:生产 topk 路径,已有 env gate:SGLANG_MINICPM_STAGE1_BLOCKMAX_FUSION、SGLANG_MINICPM_STAGE1_K2_SCORE_SELECTIVE。
C++/pybind:
entry.cu stage1_k2_indices_to_k1_mask
entry.cu varlen_fwd_stage1_k1_mask
entry.cu stage1_blockmax_finalize
flash_fwd_kernel.h INFLLM_V2_STAGE1_FIRST_PASS_ONLY
flash_blockmask.h fwdIterator(..., use_stage1_k1_blockmask=true),当前是第二 K1 pass mask,不影响第一 K2 denominator pass。
当前 capture 可直接用:
text Copy code
/tmp/prefill_stage1_capture8/stage1_candidate_L9_0001.pt
layer=9
q=(8087,32,128), k1=(32768,2,128), k2=(8192,2,128)
actual k1=32760, actual k2=8189, exact_topk=(2,8087,96)
2. 最小离线验证命令
bash Copy code
export PYTHONPATH=/user_4813494d/openbmb/kernels/infllmv2_cuda_impl:/user_4813494d/openbmb/demo-sala/sglang/python:${PYTHONPATH:-}
export CAP=/tmp/prefill_stage1_capture8/stage1_candidate_L9_0001.pt
符号确认:
bash Copy code
python3 - <<'PY'
from infllm_v2 import C
print(C.__file__)
print([x for x in dir(C) if "stage1" in x or "uint64" in x])
PY
exact baseline / blockmax oracle:
bash Copy code
python3 prefill/stage1_offline_profile.py \
--contexts 524183 --iters 1 --warmup 1 --rounds 1 \
--e2e-iters 1 --e2e-warmup 1
SGLANG_MINICPM_STAGE1_BLOCKMAX_FUSION=1 \
python3 prefill/stage1_offline_profile.py \
--contexts 524183 --iters 1 --warmup 1 --rounds 1 \
--e2e-iters 1 --e2e-warmup 1
K2 candidate recall:
bash Copy code
python3 prefill/stage1_k2_candidate_recall.py \
--capture "$CAP" --rows all --topks 1024,1536,2048 --radii 8
K1 mask / split-stage1 timing:
bash Copy code
python3 prefill/stage1_k1_mask_bench.py \
--capture "$CAP" --candidate-topk 1024 --radius 8 \
--warmup 1 --repeat 3
K2 two-call timing:
bash Copy code
python3 prefill/stage1_k2_score_candidate_bench.py \
--capture "$CAP" --candidate-topk 1024 --radius 8 \
--warmup 1 --repeat 3
要扫 8 层 capture,用 shell loop 包一下;现有 recall 脚本传 glob 时只取第一个匹配文件。
3. 推荐接入点
优先修改 prefill/stage1_k2_score_candidate_bench.py 。
原因:它已经有真实 capture 加载、full baseline、K2 score、C mask builder、masked stage1、two-call 总耗时字段。给 fused candidate/blockmask 加一个 --candidate-backend=fused|k2-score 和 --stage1-backend=fused-split|k1-mask,改动最小。
必须补两类检查:
对每个 candidate/masked/fused 输出继续跑 pool -> topk,和 data["exact_topk_idx"] 做严格 equality / miss count。
blockmax 路径也接入同一 capture bench,不只依赖 synthetic stage1_offline_profile.py。
不建议把这个塞进 stage1_offline_profile.py 主流程;它适合继续当 exact synthetic oracle。capture + fused/blockmask 的实验最好留在 capture bench 里。
4. 继续/停止指标
必须记录:
正确性:topk_equal、miss_count、miss_rows、global_recall、global_full_row_rate、按 layer_id 分开统计。
candidate 规模:candidate_frac、avg_candidate_blocks/tiles、max_candidate_blocks/tiles。
性能:full_ms、k2_score_ms、mask_build_ms、masked_stage1_ms、fused_total_ms、speedup_vs_full、折算 x8 sparse layers。
exact fusion:blockmax_ms、pool/topk saved_ms、manual_vs_exact_topk_equal。
环境:infllm_v2.C 路径、.so md5、capture path、q_len/k1_len/k2_len、env gate。
判断标准:exact blockmax 必须 topk 完全一致;K2/split-stage1 只要有 miss,就不能当提交优化,除非有在线 exact-check fallback。性能上 fused 后至少要明显低于 full_ms + pool/topk,否则 K2 two-call 已经证明方向不够。