Skip to content

Add MA-GIG explainer - #177

Open
leekwoon wants to merge 1 commit into
mainfrom
feat/magig-explainer
Open

Add MA-GIG explainer#177
leekwoon wants to merge 1 commit into
mainfrom
feat/magig-explainer

Conversation

@leekwoon

Copy link
Copy Markdown
Collaborator

Adds MA-GIG (Manifold-Aligned Guided Integrated Gradients, ICML 2026) as a PnPXAI explainer, with a worked tutorial.

Paper: https://arxiv.org/abs/2605.02167  ·  Reference code: https://github.com/leekwoon/ma-gig

What it does

Guided IG picks the features with the smallest gradients at each step and moves them toward the input. That update is axis-aligned in pixel space, and the tangent space of natural images is not, so the path drifts off the data manifold and the error accumulates along it. MA-GIG runs the same greedy selection inside the latent space of a pretrained VAE: an axis-aligned step in latent space becomes a column of the decoder Jacobian in pixel space, i.e. a tangent vector, so the decoded path stays close to the manifold and gradients are read off plausible images.

Contents

File
pnpxai/explainers/magig.py the explainer, self-contained (no dependency on the paper's repo)
tutorials/MAGIG_example_all_explainers.md worked example on Oxford-IIIT Pet with the paper's fine-tuned ResNet-18
tests/explainers/test_explainers.py 10 tests using a stub autoencoder, so they run in ~5 s with no download
docs/api/explainer/magig.md, mkdocs.yml, README.md registration

Verification

Attributions are bit-identical to the reference implementation at the paper's configuration (n_steps=200, fraction=0.05, slerp, black baseline) — max|diff| = 0.0, Pearson r = 1.0 on ResNet-18 and GoogLeNet.

On the tutorial's 50-image benchmark, DiffID is 0.476 for MA-GIG against 0.347 for IntegratedGradients started from the same baseline: a gap of +0.129, against the +0.085 the paper reports for this dataset and classifier.

Notes for review

Not in AVAILABLE_EXPLAINERS. Constructing MAGIG downloads an autoencoder, and one explanation runs 200 decoder passes, so including it in auto-recommendation would make every AutoExplanation heavy. The tutorial adds it explicitly with manager.add_explainer, as the LEAR tutorial does. Happy to change this if you would rather it be recommended.

No new checkpoints hosted. The paper uses the Stable Diffusion 2.1 autoencoder; stabilityai/sd-vae-ft-mse publishes weights identical to it (verified: maximum parameter difference 0.0), so it loads from there — the same pattern gfgp.py uses for its generative model. The tutorial's classifier comes from the paper's own repository.

cudnn.deterministic matters for this method. The greedy step selects latent dimensions by a low quantile of gradient magnitude, where the distribution is dense — roughly 130 of 4096 dimensions sit within 1e-7 of the threshold — so a last-bit difference flips which dimensions move and the rest of the path diverges. Under PyTorch's default non-deterministic cuDNN setting, two identical calls return visibly different maps; with torch.backends.cudnn.deterministic = True they are bit-identical. The constructor warns when the flag is unset on CUDA, and the tutorial sets it.

MA-GIG (Manifold-Aligned Guided Integrated Gradients, ICML 2026) builds the
integration path in the latent space of a pretrained VAE rather than in pixel
space. An axis-aligned step in latent space is mapped by the decoder Jacobian
into a correlated, tangent-aligned step in pixel space, so the decoded path
stays close to the data manifold and gradients are evaluated on plausible
images instead of on uniformly-dimmed ones.

- pnpxai/explainers/magig.py: the explainer, self-contained
- tutorials/MAGIG_example_all_explainers.md: worked example on Oxford-IIIT Pet
  with the paper's fine-tuned ResNet-18, compared against IntegratedGradients
- unit tests using a stub autoencoder, so they need no download
- API docs page, mkdocs nav and README entries

Verified against the reference implementation at the paper's configuration
(n_steps=200, fraction=0.05, slerp, black baseline): attributions are
bit-identical on ResNet-18 and GoogLeNet.

MAGIG is exported but deliberately left out of AVAILABLE_EXPLAINERS, since
constructing it downloads an autoencoder and a single explanation runs 200
decoder passes. The tutorial adds it explicitly via manager.add_explainer, as
the LEAR tutorial does.

Reference: https://arxiv.org/abs/2605.02167
@leekwoon
leekwoon force-pushed the feat/magig-explainer branch from 1f7b866 to 716d917 Compare July 28, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant