Visualization

Renderer plugins turn ExplainRunOutput attributions into figures.

Renderers

Plugin key

Output

token_text

SHAP-style inline colored tokens (red / blue diverging highlights)

token_bar

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

The refund prompt with each word shaded red or blue by its value.

token_text, words in reading order: “yes”, “no.”, “Refund?” and “laptop” push toward the original answer, “opened it.” pulls against it.

Bar chart of the 13 word attributions, sorted by size.

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.

Bar chart of five conversation messages labelled with role and speaker.

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.

The thread drawn message by message, each message shaded by its value.

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.

The thread with the users' words shaded and the assistant message 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.

Waveform of "I had that curiosity beside me at this moment" with nine word segments; "curiosity" is shaded strong red, "moment" light red.

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.

A typed question and a spoken answer, words shaded on one colour scale, with the spoken answer's waveform below.

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.

A boxer dog with its tongue out; the cells over the tongue are shaded red.

“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.