liveBook · Manning

Interpretability

As explained in Transformers in Action

Explainability is the degree to which a model's outputs and the factors behind them can be understood or audited.

Interpretability is the ability to understand or audit a model’s outputs and the factors behind them. For large language models (LLMs), analytical tools can reveal how inputs relate to generated text, helping identify possible biases or errors. Understanding pretraining data also helps trace the origins of downstream problems. section 10.0 section 10.1

Visualizations can show attention attribution at the layer, attention-head, or token level. Captum provides another way to analyze model behavior through attribution scores. section 10.2 section 10.2.1

Captum supports two approaches to estimating attribution scores:

  • Perturbation-based methods evaluate a model multiple times with varied inputs. They do not require access to model weights. section 10.2.1
  • Gradient-based methods use backpropagated gradient information and require access to model weights. section 10.2.1

Attributing generated text

For Llama 3 8B Instruct, generate a continuation to a prompt about a person’s location, occupation, and interests. Wrap the model with Captum’s FeatureAblation and LLMAttribution, then use TextTokenInput to attribute a target continuation to the prompt tokens. Plot the result as a token-attribution heat map. Feature ablation systematically removes input features to estimate their effects on the target. section 10.2.1

Listing 10.7 Getting the attribution result of your LLM
inp = TextTokenInput(
    eval_prompt,
    tokenizer,
    skip_tokens=[1])

target = "hiking, skiing, and playing the piano."
attr_res = llm_attr.attribute(inp, target=target)

Create a token-based input and calculate attribution for a chosen target continuation. section 10.2.1

Listing 10.9 Plot attribution results of your LLM
inp = TextTemplateInput(
    template="{} lives in {}, 
		  {} and is a {}. {} personal interests include",
    values=["Nicole", "Zurich", "Switzerland", "data scientist", "Her"],)

target = "hiking, skiing, and playing the piano."
attr_res = llm_attr.attribute(inp, target=target)
attr_res.plot_token_attr(show=True)

Use a template to analyze selected values, such as the person’s location and occupation, rather than individual prompt tokens. section 10.2.1

Interpretability also benefits from knowing where an answer’s information came from. Retrieval-augmented generation makes retrieved sources available for auditing, while the Foundation Model Transparency Index assesses broader disclosures about model development, characteristics, and applications. These complement analysis of model behavior rather than performing the same job. section 5.4.2 section 10.3.1

How Transformers in Action covers it

How deep it goes
Compares perturbation and gradient attribution, then works through token and template attribution with heat maps.
Examples
Uses a Llama 3 continuation about a person’s interests to compare token-level and templated input attribution.
What it assumes
Requires Hugging Face access and a read token for the demonstrated Llama 3 model.
What you can do after
Apply it via Captum to calculate and plot attribution for generated text.
How it connects
Links attribution to attention, bias, pretraining data, and retrieval-augmented generation.

Also explained in

Compare how 16 books explain interpretability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Transformers in Action. The section links point to the parts of the book it draws on.