Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Refactor: split CheferInterpretable into AttentionInterpretable and GradientInterpretable

オープン
#1,166 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

@LIU-kaiyu がすでに取り組んでいます。

2026年9月6日 から。

  • #1232 @LIU-kaiyu による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
リファクタリング
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python, pytorch

調査の方向性

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 の本文から書いたものです。

説明

enhancement refactor

Background

CheferInterpretable (in pyhealth/interpret/api.py) currently bundles two distinct concerns into a single interface:

  1. Forward attention capture — set_attention_hooks stores attention weight tensors during the forward pass so they can be retrieved via get_attention_layers.
  2. Backward gradient capture — set_attention_hooks also 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)
  • AttentionRollout checks isinstance(model, AttentionInterpretable)
  • CheferRelevance checks isinstance(model, GradientInterpretable)
  • Keep CheferInterpretable = GradientInterpretable as a backwards-compatible alias

Impact

  • Models (Transformer, StageAttentionNet) would need to support a "forward-only hooks" mode in set_attention_hooks, so attention maps are captured without registering backward hooks. This would also remove the torch.no_grad() incompatibility currently documented for AttentionRollout.
  • 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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sunlabuiuc/PyHealth のほかの issue

sunlabuiuc/PyHealth の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。