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日 7時間
- マージ済み PR(30日)
- 58
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
sunlabuiuc/PyHealth のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
sunlabuiuc/PyHealth#1346 ·
メンテナーはふだん 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 が 23 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
sunlabuiuc/PyHealth#1269 · コメント 3 件 ·
メンテナーはふだん 3 日以内に返信
sunlabuiuc/PyHealth の issue をすべて見る
似ている issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
awslabs/visual-asset-management-system#937 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
follow-up
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 83/100
メンテナーはふだん 3 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 2 日以内に返信