Metadata-Version: 2.1
Name: inseq
Version: 0.3.1
Summary: Interpretability for Sequence Generation Models 🔍
Home-page: https://github.com/inseq-team/inseq
License: Apache Software License 2.0
Keywords: seq2seq,transformers,natural language processing,XAI,explainable ai,interpretability
Author: The Inseq Team
Maintainer: gsarti
Maintainer-email: gabriele.sarti996@gmail.com
Requires-Python: >=3.8.1,<3.12
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Environment :: GPU
Classifier: Environment :: GPU :: NVIDIA CUDA
Classifier: Environment :: GPU :: NVIDIA CUDA :: 11.3
Classifier: Framework :: Jupyter
Classifier: Framework :: Sphinx
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Visualization
Provides-Extra: datasets
Provides-Extra: notebook
Provides-Extra: sklearn
Requires-Dist: captum (>=0.5.0,<0.6.0)
Requires-Dist: datasets[torch] (>=2.3.2,<3.0.0) ; extra == "datasets"
Requires-Dist: ipykernel (>=6.15.0,<7.0.0) ; extra == "notebook"
Requires-Dist: ipywidgets (>=8.0.0rc2,<9.0.0) ; extra == "notebook"
Requires-Dist: joblib (>=1.2.0,<2.0.0) ; extra == "sklearn"
Requires-Dist: json-tricks (>=3.15.5,<4.0.0)
Requires-Dist: matplotlib (>=3.5.2,<4.0.0)
Requires-Dist: numpy (>=1.22.4,<2.0.0)
Requires-Dist: poethepoet (>=0.13.1,<0.14.0)
Requires-Dist: protobuf (>=3.20.2,<4.0.0)
Requires-Dist: rich (>=10.13.0,<11.0.0)
Requires-Dist: scikit-learn (>=1.1.1,<2.0.0) ; extra == "sklearn"
Requires-Dist: scipy (>=1.8.1,<2.0.0)
Requires-Dist: torch (>=1.13.0,<2.0.0)
Requires-Dist: torchtyping (>=0.1.4,<0.2.0)
Requires-Dist: tqdm (>=4.64.0,<5.0.0)
Requires-Dist: transformers[sentencepiece,tokenizers,torch] (>=4.22.0,<5.0.0)
Project-URL: Repository, https://github.com/inseq-team/inseq
Description-Content-Type: text/markdown

<div align="center">
  <img src="/docs/source/images/inseq_logo.png" width="300"/>
  <h4>Intepretability for Sequence Generation Models 🔍</h4>
</div>
<br/>
<div align="center">

[![Build status](https://github.com/inseq-team/inseq/workflows/build/badge.svg?branch=master&event=push)](https://github.com/inseq-team/inseq/actions?query=workflow%3Abuild)
[![Python Version](https://img.shields.io/pypi/pyversions/inseq.svg)](https://pypi.org/project/inseq/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![License](https://img.shields.io/github/license/inseq-team/inseq)](https://github.com/inseq-team/inseq/blob/main/LICENSE)

</div>

Inseq is a Pytorch-based hackable toolkit to democratize the access to common post-hoc **in**terpretability analyses of **seq**uence generation models.

## Installation

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

```bash
pip install inseq
```

<details>
  <summary>Dev Installation</summary>
To install the package, clone the repository and run the following commands:

```bash
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.
</details>

<details>
  <summary>FAQ Installation</summary>

  - Installing the `tokenizers` package requires a Rust compiler installation. You can install Rust from [https://rustup.rs](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`.
</details>

## 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:

```python
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](docs/source/images/heatmap_winomt.png)

Inseq also supports decoder-only models such as [GPT-2](https://huggingface.co/transformers/model_doc/gpt2.html), enabling usage of a variety of attribution methods and customizable settings directly from the console:

```python
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](docs/source/images/inseq_python_console.gif)

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

```bash
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
```
