TL;DR
Captum v0.7 expands PyTorch’s flagship model interpretability library to support generative large language models (LLMs). By introducing high-level APIs for both perturbation- and gradient-based attribution, it lets developers evaluate how arbitrary text segments—from individual tokens to semantic phrases or few-shot examples—drive model generations and reveal latent biases.
What Problem Does This Paper Address?
While Large Language Models (LLMs) power increasingly critical applications, explaining their generation behavior remains difficult. Most prior text-interpretability toolkits rely almost exclusively on gradient-based, token-level attributions.
However, perturbing individual tokens or subwords (e.g., masking the word "Palm" in "Palm Coast, FL") often produces ungrammatical text that falls far outside the model's training distribution, yielding unreliable explanations. Furthermore, users often want explanations at the level of semantic units (e.g., names, locations, occupations) or prompt structures (such as in-context few-shot examples) rather than raw subword tokens.
Methods
The authors introduce new LLM-tailored attribution utilities within Captum:
- Perturbation-Based Attribution (
LLMAttribution): Supports methods including Feature Ablation, LIME, Kernel SHAP, and Shapley Value Sampling. Users can define custom interpretable features using template wrappers (TextTemplateFeature), group correlated inputs via feature masking, and provide coherent baselines (ProductBaselines) so perturbations remain within the natural language distribution. - Flexible Targets: Attributions can be computed relative to either the model's most likely decoded sequence, a specific user-defined target sequence (via target log-probabilities), or custom metrics such as logit entropy.
- Gradient-Based Attribution (
LLMGradientAttribution): Adapts methods like Layer Integrated Gradients and Saliency to handle non-differentiable embedding lookups, aggregating gradients across embedding dimensions to provide per-token attributions. - Visualization Tools: Built-in heatmap visualizers mapping attributions across prompt features and output targets.
Datasets
The paper does not benchmark on standard static NLP datasets. Instead, it demonstrates the tooling on two illustrative case studies:
- Demographic and occupational association study: A synthetic prompt template ("[Name] lives in [City], [State] and is a [Occupation]. [Pronoun] personal interests include...") paired with multiple baselines.
- In-context few-shot evaluation: A four-example movie review sentiment classification prompt tested on a target review ("I really liked the movie, it had a captivating plot!").
Key Insights and Ideas
- Custom Semantic Features Outperform Raw Token Perturbations: Grouping subword tokens and correlated words into structured features (e.g., binding "Palm Coast" and "FL", or "Dave" and "His") preserves linguistic validity during perturbations.
- Surfacing Latent Stereotypes and Biases: Attribution scores explicitly quantify social and contextual priors learned by the LLM. In the hobby generation example, male indicators ("Dave", "His") boosted the likelihood of generating "golfing" while penalizing "cooking".
- Few-Shot Examples Can Act as Inhibitors: Counterintuitively, Shapley Value analysis revealed that adding in-context demonstrations actually lowered model confidence in predicting the correct sentiment label compared to a pure zero-shot baseline.
Key Results
- Learned Associations Case Study: Using Shapley Value Sampling, features contributed significantly to hobby targets:
- For the target "Golfing":
(City) Palm Coasthad the highest positive impact (+1.0810), followed by(Occupation) lawyer(+0.7584) and(Name) Dave(+0.4660). - For the target "Cooking":
(Name) Dave(-0.4515) and(State) FL(-0.3513) exerted strong negative attributions, while(Occupation) lawyerhad a slight positive effect (+0.0331). - Few-Shot Prompt Impact: When evaluating few-shot examples on the target prediction "Positive":
- All four few-shot demonstration examples received negative Shapley values relative to the zero-shot baseline:
Total waste of time!! -> Negative(-0.2085),Won't recommend -> Negative(-0.0399),'The movie was ok...' -> Negative(-0.0413), and even the positive example'I loved it...' -> Positive(-0.2751).
Limitations and Future Work
- Manual Feature and Baseline Configuration: Users must currently specify prompt templates, feature boundaries, and baseline distributions by hand. The authors plan to automate text feature extraction and baseline candidate generation.
- Computational Overhead: Perturbation methods like Shapley Value Sampling require repeated model forward evaluations, which can become computationally expensive on very large models. Optimizing runtime performance is marked as future work.
- Scope of Methods: The current release focuses on attribution; future iterations aim to integrate broader categories of interpretability tools (e.g., mechanistic interpretability or representation analysis).
Summary updated