Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Refactor: split CheferInterpretable into AttentionInterpretable and GradientInterpretable

Aperta
#1,166 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@LIU-kaiyu ci sta già lavorando.

Dal 6/9/2026.

  • #1232 di @LIU-kaiyu — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python, pytorch

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
1.7k
Fork
803
Merge medio
5g 20h
PR unite (30g)
56

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sunlabuiuc/PyHealth

Tutte le issue di sunlabuiuc/PyHealth

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.