-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
treescope
support and new visualizations
#283
Conversation
Issue: Ideally EDIT: Solved by temporarily unhooking |
Description
The new treescope stand-alone package provides powerful utilities for visualizing objects and tensors in an interactive environment. It would be a perfect addition to improve the flexibility of visualizations for input attribution scores offered by Inseq.
Proposed changes
1. Interactive Objects Visualization
treescope
by default in notebook environments to improve the explorability ofAttributionModel
weights andFeatureAttributionOutput
objects:Notes
1.1. Define a
__treescope_repr__
forFeatureAttributionOutput
andFeatureAttributionSequenceOutput
classes to make the visualization more informative:- Add length info for
source
andtarget
fields.- Override
source_attributions
andtarget_attributions
default array visualizations with the one used in the newshow_granular_attributions
method (see below for details)- Add highlighted tokens in the format of
show_tokens_importance
with hoverable scores to everystep_score
.1.2 Since we use red as positive and blue as negative following feature attribution common practices, the default
treescope
colormap will need to be reversed:2. New
show_granular
method for granular attribution visualizationThe new
show_granular
method of theFeatureAttributionSequenceOuput
class would exploit NDArray visualizations fromtreescope
to complement the currentshow
method, enabling the visualization of N-dimensional attribution tensors without aggregation. By contrast,show
currently applies the default aggregator without.aggregate()
, since it is limited to outputs that can be visualized as 2D matrices.Notes
2.1. Will require adding named dimensions to all
FeatureAttributionSequenceOutput
objects. The first two will beInput Tokens
andGenerated Tokens
by default, but all the following ones will depend on the object, e.g.Embedding Dimension
forsaliency
attribution (3D),Attention Head
andModel Layer
forattention
attribution (4D).2.2 To make the visualization more effective, every extra dimension beyond the 3rd or with more than a
maximum_size
parameter elements (e.g. 20) will be moved to a slider.Example: Visualizing dimension 41 of a saliency attribution tensor with 95 input tokens and 18 generated tokens
Example: Visualizing attention weights across all model layers for attention head 0 (input/generated token pair and numeric score shown on hover)
2.3 Enable slicing of dimensions with a
slices
dictionary (e.g.slices = {2: range(20)}
to get the first 20 dimensions of the saliency tensor from 2.2. If the sliced dimension is shorter thanmaximum_size
, it is visualized.Example: Saliency attribution tensor from 2.2 with 0:20 slicing over
Embedding Dimension
applied3. New
show_tokens
method for text highlightsThe new
show_tokens
method would make large attributions more legible by presenting scores as text highlights, exploiting thetext_on_color
method intreescope
. Hoverable input tokens with highlights will be shown for every generation step, with color highlight matching the respective token attribution score for the generated token:Notes
3.1 Parameters:
-
replace_char
: a dict to map special characters to arbitrary characters (default: no replacement). E.g.replace_char = {"Ġ": " ", "▁": " ", "Ċ": ""}
to clean up special characters for GPT-like and SentecePiece tokenization.-
wrap_after
: If an int, this is the number of tokens after which a newline is inserted (e.g. 10 in the figure). If it's a string or an iterable, a newline is inserted if the current token is contained in it. Default: no wrapping (one line per attributed sequence).