Visualization¶
Renderer plugins turn ExplainRunOutput attributions into figures.
Renderers¶
Plugin key |
Output |
|---|---|
|
SHAP-style inline colored tokens (red / blue diverging highlights) |
|
Horizontal bar chart with SHAP palette, value labels, and sign legend |
They need the viz extra (pip install "mllm-shap[viz]") and use the SHAP palette: red #FF0D57 positive, blue #1E88E5
negative. Token backgrounds fade from white to the signed colour, as in
shap.plots.text.
Quick start¶
from mllm_shap import render_attribution
output = ExplainRunner(config).explain_sync(snapshot)
bar = render_attribution(output, snapshot, output.player_set, renderer="token_bar")
text = render_attribution(output, snapshot, output.player_set, renderer="token_text")
bar.savefig("attribution.png", dpi=150, bbox_inches="tight")
output.player_set labels the players; title= adds a title; registry= takes your
own PluginRegistry. Figures below come from
Attribution visualization (Qwen/Qwen2.5-1.5B-Instruct, tfidf_cosine).
Words of a prompt¶
token_text, words in reading order: “yes”, “no.”, “Refund?” and “laptop” push
toward the original answer, “opened it.” pulls against it.¶
token_bar: the same values sorted by absolute size, labelled with their sign.¶
Messages of a conversation¶
With players: messages each message is a player, labelled by role, speaker and
position.
A five-message support thread: Alice’s order and Bob’s two messages carry the answer; the assistant’s greeting and Alice’s “used it once” matter less.¶
plot_conversation() keeps the thread’s shape, players shaded in
place, other text grey; render_conversation_html() returns HTML. Both
handle message and token players.
plot_conversation with players: messages.¶
With players: tokens and plugin_options: {tokens: {roles: [user]}} only users’
words are players; other messages stay present, in grey.
Users’ words: “Can”, “Answer” and “yes” in Bob’s question decide; the assistant’s greeting is context.¶
Spoken words¶
plot_audio_segments() colours each spoken word (players:
sgpa_segments, Audio alignment) over the waveform and RMS energy. Real models:
Audio input.
plot_audio_segments on a recorded sentence; “curiosity” gets the most credit
(a text model reading the masked transcript, to show the layout).¶
The sgpa_segments option text_roles: [user] makes typed user words players too:
typed and spoken words share one run and colour scale.
plot_conversation() adds a waveform strip below each audio message.
Typed and spoken words of one explanation (a text model reading the masked transcript, to show the layout).¶
Image regions¶
plot_image_players() shades each cell or box (image_grid,
image_boxes, Image input); opacity grows with the value’s size.
“Is the dog’s tongue sticking out?” Qwen2.5-VL-3B says yes, crediting the tongue (Which part of the photo decided the answer?).¶
plot_conversation() draws a picture strip below each image message
on the words’ scale, so with question: true you see whether the answer came from the
photo or the question.
Jupyter HTML¶
display_attribution_html() shows coloured tokens in a notebook
without matplotlib; render_attribution_html() returns the HTML string:
from mllm_shap import display_attribution_html, render_attribution_html
display_attribution_html(output, snapshot, output.player_set)
html = render_attribution_html(output, snapshot, output.player_set, title="Refund")
Notebooks: Attribution visualization, End-to-end text explainability on real backends. API: Visualization.