Refactor: split CheferInterpretable into AttentionInterpretable and GradientInterpretable
维护者通常 3 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 重构
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
调研方向
Start with pyhealth/interpret/api.py and inspect the attention-hook implementations in Transformer and StageAttentionNet, along with the AttentionRollout and CheferRelevance checks described in the issue. A linked open pull request (#1232) is already working on this issue. Done means forward-only hooks work without backward hooks, gradient-based interpretation retains gradients, and the CheferInterpretable alias preserves compatibility.
由索引模型根据 Issue 内容生成。
描述
Background
CheferInterpretable (in pyhealth/interpret/api.py) currently bundles two distinct concerns into a single interface:
- Forward attention capture —
set_attention_hooksstores attention weight tensors during the forward pass so they can be retrieved viaget_attention_layers. - Backward gradient capture —
set_attention_hooksalso registers backward hooks so that attention gradients are stored after.backward().
This bundling made sense when CheferRelevance was the only attention-based interpreter (it needs both). But PR #1158 adds AttentionRollout (Abnar & Zuidema 2020), which is gradient-free: it only needs the forward attention maps and explicitly discards attn_grad with for attn_map, _ in layers:. Requiring backward hook registration for a gradient-free method is semantically wrong, even if it works in practice.
Proposed refactor
Split the interface into two levels:
AttentionInterpretable ← forward attention capture only
set_attention_hooks(enabled) (captures attn maps, no backward hooks)
get_attention_layers() (returns attn maps; attn_grad may be None)
get_relevance_tensor(R, **data)
GradientInterpretable(AttentionInterpretable) ← extends with backward hooks
set_attention_hooks(enabled) (also registers backward hooks)
get_attention_layers() (attn_grad is always populated)
AttentionRolloutchecksisinstance(model, AttentionInterpretable)CheferRelevancechecksisinstance(model, GradientInterpretable)- Keep
CheferInterpretable = GradientInterpretableas a backwards-compatible alias
Impact
- Models (
Transformer,StageAttentionNet) would need to support a "forward-only hooks" mode inset_attention_hooks, so attention maps are captured without registering backward hooks. This would also remove thetorch.no_grad()incompatibility currently documented forAttentionRollout. - No breaking changes to existing callers if the alias is kept.
Context
Surfaced during review of PR #1158. The duck-typing AttentionRollout uses today is a reasonable workaround until this refactor lands.
- 主要语言
- Python
- 星标
- 1.7k
- 派生
- 803
- 平均合并
- 5 天 20 小时
- 30 天内合并 PR
- 56
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
sunlabuiuc/PyHealth 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 77/100
sunlabuiuc/PyHealth#1333 · 1 条评论 ·
维护者通常 3 天内回复
-
难度 4/5 3-5 天 新手友好度 30/100
sunlabuiuc/PyHealth#1297 ·
维护者通常 3 天内回复
-
难度 4/5 3-5 天 新手友好度 22/100
sunlabuiuc/PyHealth#1295 · 1 条评论 ·
维护者通常 3 天内回复
-
难度 3/5 1-2 天 新手友好度 42/100
sunlabuiuc/PyHealth#1294 · 1 条评论 ·
维护者通常 3 天内回复
-
Internal transfers are counted as readmissions可能已有人在做 @thebean27 于 21 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 35/100
sunlabuiuc/PyHealth#1269 · 3 条评论 ·
维护者通常 3 天内回复
查看 sunlabuiuc/PyHealth 的全部 Issue
相似的 Issue
-
component:benchmark
难度 2/5 1-3 小时 新手友好度 76/100
agentic-os-org/ANOLISA#9177 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
distrochooser/distrochooser#520 ·
维护者通常 2 天内回复
-
Status: Needs confirmation Status: Needs triage
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
-
[BUG] Published Docker images lack the redis extra: HEADROOM_COMPRESS_SESSION_STORE stops startup可能已有人在做 @AlessandroSpallina 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
headroomlabs-ai/headroom#4513 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 65/100
codespell-project/codespell#4060 ·
维护者通常 1 天内回复