Token Attributions
This guide covers how to compute, interpret, and visualize token attributions using Fair Forge’s explainability module.Installation
interpreto- Attribution computation librarytorch- PyTorch for model inferencetransformers- HuggingFace model support
Basic Usage
Step 1: Load Your Model
Step 2: Format Your Prompt
Step 3: Create Explainer and Compute
Step 4: Analyze Results
Configuration Options
AttributionExplainer Parameters
explain() Parameters
Granularity Levels
Choose the appropriate granularity for your use case:- Token
- Word
- Sentence
['▁What', '▁is', '▁grav', 'ity', '?']Attribution Methods
Using Different Methods
Pass the method class directly toexplain():
Method Comparison
Batch Processing
Process multiple prompt/target pairs efficiently:Output Schema
AttributionResult
TokenAttribution
Useful Methods
Visualization
In Jupyter Notebooks
Get HTML for Custom Display
Custom Result Parsers
Implement custom parsers for different attribution libraries:Convenience Function
For one-off computations without creating an explainer:Interpreting Results
Positive vs Negative Scores
- Positive scores: Token increases likelihood of the target output
- Negative scores: Token decreases likelihood of the target output
- Near-zero scores: Token has minimal impact
Normalized Scores
Normalized scores map to[0, 1] range for easier comparison:
1.0= Maximum positive contribution0.5= Neutral (when all scores are equal)0.0= Maximum negative contribution
Example Analysis
Best Practices
Use float16 for Models
Use float16 for Models
Attribution methods work better with float16 precision:
Disable Autocast
Disable Autocast
Some attribution methods conflict with autocast:
Start with LIME
Start with LIME
LIME is a good default - it’s model-agnostic and provides reliable results. Switch to gradient methods if you need speed.
Use Word Granularity
Use Word Granularity
Word-level attributions are most interpretable. Use token-level only for debugging tokenization issues.
Compare Multiple Methods
Compare Multiple Methods
Different methods may highlight different aspects. Compare results from 2-3 methods for important analyses.
Troubleshooting
CUDA Out of Memory
CUDA Out of Memory
Reduce
max_length or use a smaller model:Empty Attributions
Empty Attributions
Check that your prompt and target are not empty and that the model can process them:
Slow Computation
Slow Computation
Switch to faster gradient-based methods:
Next Steps
Explainability Overview
Learn about the module design and available methods
Example Notebook
See working examples in Jupyter