Short History Attention Debug
本次修改实现了一个默认关闭的 debug hook,用来分析 current token 到 short history 上一帧 token 的对应关系。
背景结论
默认设置下:
- current chunk 是 33 个视频帧,对应 9 个 latent frames。
- 默认视频分辨率是 384x640。
- VAE latent 分辨率是 48x80。
- transformer patch 是
(1, 2, 2),所以 current token grid 是9 x 24 x 40。 - short history 经过
patch_short后是2 x 24 x 40。 - 本次只在 short history 的上一帧里找对应 token,也就是每个 current latent frame 的
24 x 40tokens 对应 previous short frame 的24 x 40tokens。
已实现内容
新增 short_attn_debug pipeline 参数。开启后,attention hook 会在指定 block、chunk、denoise step、current frame 上导出 current token 到 previous short token 的 top-k 匹配。
实现位置:
helios/diffusers_version/transformer_helios_diffusers.pyhelios/diffusers_version/pipeline_helios_diffusers.pyhelios/modules/transformer_helios.pyhelios/pipelines/pipeline_helios.pytools/visualize_short_attention_matches.py
导出的 artifact 默认包含:
match_yx: 每个 current token 匹配到的 previous short token 坐标query_yx: current token 自身坐标displacement_yx: token 位移topk_indices: top-k 匹配 indextopk_scores: top-k raw attention scorestop1_scoretop2_scoremargin:top1_score - top2_score
不会保存完整 960 x 960 attention matrix,避免显存和磁盘开销过大。
使用示例
在 pipeline 调用里加:
output = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
height=384,
width=640,
num_frames=99,
short_attn_debug={
"output_dir": "short_attn_debug",
"blocks": [30],
"steps": ["last"],
"chunks": [0],
"current_frame": -1,
"prev_short_frame": 1,
"pass_names": ["cond"],
"topk": 2,
"query_chunk_size": 128,
},
).frames[0]
默认推荐先看:
- block
30 - denoise last step
- chunk
0 - current latent frame
-1 - previous short frame
1 - cond pass
可视化
生成图:
python tools/visualize_short_attention_matches.py \
short_attn_debug/short_attn_chunk0_step49_block30_frame8_cond.pt \
--output-dir short_attn_debug \
--stride 4
输出:
*_flow_quiver.png: 稀疏 token flow 箭头图*_displacement_heatmap.png: 位移大小热图*_confidence_heatmap.png:top1 - top2margin 热图*_top1_score_heatmap.png: top1 raw score 热图
当前实现细节
匹配计算使用 attn1 里已经 projection、norm、rotary 后的 Q/K:
score = Q_current_frame @ K_previous_short_frame.T / sqrt(head_dim)
然后对 heads 做平均,再取 top-k。
为了控制开销,计算按 query chunk 分块进行。默认每次处理 128 个 query tokens。
未做但可继续扩展的方案
full softmax attention heatmap
当前保存的是 top-k raw scores,不保存完整 softmax 分布。后续如果要做单点 token viewer,可以只对用户点击的 token 计算完整
24 x 40softmax heatmap。click-to-inspect viewer
可以做一个交互式界面:左边点 current token,右边显示它对 previous short frame 的
24 x 40score/attention heatmap,并框出 top-k。多层对比
当前支持配置多个 blocks,例如
[10, 20, 30, 39]。后续可以把这些层的 flow 和 confidence 并排画出来,观察 correspondence 在不同深度的变化。多 denoise step 对比
当前支持
steps=["first", "last"]或具体 step index。后续可以把 first/mid/last step 的图并排,观察匹配是否从噪声期到收敛期变稳定。多 current latent frame 可视化
当前推荐只看一个 current frame,例如
-1。后续可以对0..8全部导出,生成 grid 或小视频。warped previous frame
可以用 token correspondence 把 previous short frame 的 patch/grid warp 到 current frame,再与 current frame 做差分。这比 flow 图更接近视觉验证,但实现成本更高。
mutual nearest neighbor 过滤
当前是 current -> previous short 的单向 top-k。后续可加 previous short -> current,再保留互为 top-1 的 matches,用来过滤不稳定对应。
one-to-one matching
如果需要严格的一对一 token matching,可以在 score matrix 上做 Hungarian 或 optimal transport。但
960 x 960会比当前 top-k 更重,而且未必符合 attention 本身的多对一行为。