Abstract
The rapid advancement of generative language models (LMs) has outpaced the development of tools to interpret their internal decision-making processes. Traditional feature attribution methods, designed for static classification, struggle with the autoregressive nature of modern Large Language Models (LLMs). This paper presents Inseq, an open-source Python library that unifies advanced attribution methods, introduces contrastive analysis, and enables interpretability research on consumer-grade hardware through quantization.
Introduction
Feature attribution—the process of identifying which input tokens most significantly influence a model's output—is a cornerstone of NLP interpretability. However, the shift from discriminative models (like BERT) to generative, autoregressive models (like GPT-4 or Llama) has introduced significant hurdles. In generative settings, the "target" of attribution changes at every step of the sequence generation. Furthermore, the sheer scale of modern LLMs often restricts interpretability research to well-funded industrial labs with massive GPU clusters.
The Inseq toolkit addresses these challenges by providing a high-level, extensible wrapper for the Hugging Face Transformers ecosystem, built upon the Captum framework. It centralizes diverse attribution methods and introduces novel metrics to explain not just what a model generates, but why it chooses specific tokens over alternatives.
The Challenge of Generative Attribution
Unlike classification, where a single prediction is attributed to the input, generative models produce text token-by-token. Each new token depends on both the original prompt and all previously generated tokens. This creates a dynamic attribution matrix that grows with the sequence length, making the "target" of attribution a moving target.
Inseq simplifies this complexity by supporting three primary families of attribution:
- Gradient-based methods: Utilizing internal gradients (e.g., Integrated Gradients, DeepLIFT) to quantify token importance.
- Internals-based methods: Analyzing internal states like attention weights.
- Perturbation-based methods: Observing output changes when parts of the input are masked or modified (e.g., LIME, Occlusion).
Contrastive Attribution and Practical Applications
One of Inseq's most powerful features is its support for contrastive attribution. Instead of merely asking why a model generated a specific token, researchers can ask: "Why did the model generate token A instead of token B?" This is particularly useful for auditing biases. For instance, in machine translation, one can analyze why a model chose a masculine pronoun over a feminine one, tracing the influence back to stereotypical nouns in the source text.
As shown in the figure above, a few lines of code allow a researcher to load a model and perform a contrastive analysis. The resulting heatmap clearly highlights how the source token "_manager" heavily influences the probability difference between gendered Italian articles ("il" vs "la"), providing quantitative proof of stereotypical bias.
Democratizing Interpretability
To ensure that interpretability research is accessible to the broader community, Inseq integrates 4-bit and 8-bit quantization via the bitsandbytes library. This allows researchers to run attribution on LLMs using consumer-grade GPUs, which would otherwise lack the memory required for the multiple backward passes or perturbations necessary for high-fidelity attribution.
Additionally, the toolkit introduces PECoRe (Context Reliance), a framework designed to detect "hallucinations" or "context neglect." By comparing attributions to the provided context versus the model's internal parameters, PECoRe can identify when a model is relying on its pre-trained "memorized" knowledge rather than the relevant information provided in a prompt, such as in Retrieval-Augmented Generation (RAG) setups.
Conclusion
Inseq represents a significant step toward standardizing and democratizing the interpretability of generative LMs. By providing a unified interface for complex attribution methods and lowering the hardware barrier to entry, it enables more rigorous auditing of the models that are increasingly central to modern AI applications. Future work will focus on expanding support beyond the Hugging Face ecosystem and further refining metrics for context reliance and model fairness.
Summary updated