Skip to main content

Token Attributions

This guide covers how to compute, interpret, and visualize token attributions using Fair Forge’s explainability module.

Installation

This installs:
  • interpreto - Attribution computation library
  • torch - PyTorch for model inference
  • transformers - HuggingFace model support

Basic Usage

Step 1: Load Your Model

Step 2: Format Your Prompt

You must format prompts according to your model’s requirements. The explainability module does not handle prompt formatting to avoid coupling with specific LLM formats.

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:
Use when: You need fine-grained analysis of individual tokens, debugging tokenization issues.Example output: ['▁What', '▁is', '▁grav', 'ity', '?']

Attribution Methods

Using Different Methods

Pass the method class directly to explain():

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 contribution
  • 0.5 = Neutral (when all scores are equal)
  • 0.0 = Maximum negative contribution

Example Analysis

Best Practices

Attribution methods work better with float16 precision:
Some attribution methods conflict with autocast:
LIME is a good default - it’s model-agnostic and provides reliable results. Switch to gradient methods if you need speed.
Word-level attributions are most interpretable. Use token-level only for debugging tokenization issues.
Different methods may highlight different aspects. Compare results from 2-3 methods for important analyses.

Troubleshooting

Reduce max_length or use a smaller model:
Check that your prompt and target are not empty and that the model can process them:
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