Skip to main content

Interpretability for Sequence Generation Models 🔍

Project description

Intepretability for Sequence Generation Models 🔍


Build status Python Version Code style: black License

Inseq is a Pytorch-based hackable toolkit to democratize the access to common post-hoc interpretability analyses of sequence generation models.

Installation

Inseq is available on PyPI and can be installed with pip:

pip install inseq
Dev Installation To install the package, clone the repository and run the following commands:
cd inseq
make poetry-download # Download and install the Poetry package manager
make install # Installs the package and all dependencies

If you have a GPU available, use make install-gpu to install the latest torch version with GPU support.

For library developers, you can use the make install-dev command to install and its GPU-friendly counterpart make install-dev-gpu to install all development dependencies (quality, docs, extras).

After installation, you should be able to run make fast-test and make lint without errors.

FAQ Installation
  • Installing the tokenizers package requires a Rust compiler installation. You can install Rust from https://rustup.rs and add $HOME/.cargo/env to your PATH.

  • Installing sentencepiece requires various packages, install with sudo apt-get install cmake build-essential pkg-config or brew install cmake gperftools pkg-config.

Example usage in Python

This example uses the Integrated Gradients attribution method to attribute the English-French translation of a sentence taken from the WinoMT corpus:

import inseq

model = inseq.load_model("Helsinki-NLP/opus-mt-en-fr", "integrated_gradients")
out = model.attribute(
  "The developer argued with the designer because her idea cannot be implemented.",
  n_steps=100
)
out.show()

This produces a visualization of the attribution scores for each token in the input sentence (token-level aggregation is handled automatically). Here is what the visualization looks like inside a Jupyter Notebook:

WinoMT Attribution Map

Inseq also supports decoder-only models such as GPT-2, enabling usage of a variety of attribution methods and customizable settings directly from the console:

import inseq

model = inseq.load_model("gpt2", "integrated_gradients")
model.attribute(
    "Hello ladies and",
    generation_args={"max_new_tokens": 9},
    n_steps=500,
    internal_batch_size=50
).show()

GPT-2 Attribution in the console

What does Inseq support?

  • Feature attribution of sequence generation for most ForConditionalGeneration (encoder-decoder) and ForCausalLM (decoder-only) models from 🤗 Transformers

  • Support for single and batched attribution using multiple gradient-based feature attribution methods from Captum

  • Post-hoc aggregation of feature attribution maps via Aggregator classes.

  • Attribution visualization in notebooks, browser and command line.

  • Command line interface for attributing single examples or entire 🤗 datasets.

  • Custom attribution of target functions, supporting advanced usage for cases such as contrastive and uncertainty-weighted feature attributions.

  • Extract and visualize custom scores for every generation step alongsides attribution maps.

What we plan to support in the future?

  • Attention-based and occlusion-based feature attribution methods

  • Interoperability with other interpretability libraries

  • Rich and interactive visualizations in a tabbed interface

Using the Inseq client

The Inseq library also provides useful client commands to enable repeated attribution of individual examples and even entire 🤗 datasets directly from the console. See the available options by typing inseq -h in the terminal after installing the package.

For now, two commands are supported:

  • ìnseq attribute: Wraps the attribute method shown above, requires explicit inputs to be attributed.

  • inseq attribute-dataset: Enables attribution for a full dataset using Hugging Face datasets.load_dataset.

Both commands support the full range of parameters available for attribute, attribution visualization in the console and saving outputs to disk.

Example: The following command can be used to perform attribution (both source and target-side) of Italian translations for a dummy sample of 20 English sentences taken from the FLORES-101 parallel corpus, using a MarianNMT translation model from Hugging Face transformers. We save the visualizations in HTML format in the file attributions.html. See the --help flag for more options.

inseq attribute-dataset \
  --model_name_or_path Helsinki-NLP/opus-mt-en-it \
  --attribution_method saliency \
  --do_prefix_attribution \
  --dataset_name inseq/dummy_enit \
  --input_text_field en \
  --dataset_split "train[:20]" \
  --viz_path attributions.html \
  --batch_size 8 \
  --hide

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inseq-0.3.1.tar.gz (71.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inseq-0.3.1-py3-none-any.whl (84.5 kB view details)

Uploaded Python 3

File details

Details for the file inseq-0.3.1.tar.gz.

File metadata

  • Download URL: inseq-0.3.1.tar.gz
  • Upload date:
  • Size: 71.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for inseq-0.3.1.tar.gz
Algorithm Hash digest
SHA256 ebc5c958430100ca942bf4891f90be001f5cbe2e80287f7044d1f72e4495ae88
MD5 0ffd50c88afd2ff00f081c5dc4e5c209
BLAKE2b-256 82c775073351a755a3ccbbf5e166bf9059b204ebd92d243cf0426798c456f756

See more details on using hashes here.

File details

Details for the file inseq-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: inseq-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 84.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for inseq-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 369c8e78cd5ad0bc23e14c15102fca2e245c880c911f9549edf3513064bcd387
MD5 f1c4bd6ec237b262da5b0ae7cc410053
BLAKE2b-256 f8449dd8b6d054fbd53410bc8ac13334b8a5408b16ca94041e2fe30d4eb8182d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page