This PR adds grad_ellm, a target-dependent, token-level attribution method for decoder-only Llama and Mistral models.
The method combines internal Q/K/V projections with gradients of the selected next-token objective and sums attribution scores over the last n_layers layers. The paper-reproduction configuration uses attributed_fn="logit" and normalize=True.
The integration includes:
Native method registration, allowing users to select grad_ellm through inseq.load_model.
Dedicated token-level output classes that preserve both token axes and support Inseq visualization, aggregation, and serialization.
Internal helpers for exact token-ID replay and indexed CUDA device handling.
Tests covering the reference equations, MHA/GQA, batching and padding, attribution objectives, hook cleanup, method switching, and output processing.
A method card documenting the algorithm, configuration, and supported scope.
This PR also updates the Value Zeroing hidden-state hook to handle Tensor, tuple, and list block outputs while preserving the full batch, device, and dtype. This addresses compatibility with Transformer layers that return a Tensor directly.
The initial scope is standard floating-point Llama and Mistral models on a single CPU or CUDA device. Quantization, model sharding/offload, training mode, and other model families are outside this implementation’s supported scope.
Related Issue
N/A.
Type of Change
🚀 New feature
🔧 Bug fix
📚 Documentation update
Validation
Complete make test & fast-test validation.
The build docs hook passed in that run.
make lint currently fails at the Safety check, which reports 113 findings across 20 packages.
Comparison of the provided dependency files against upstream Inseq v0.7.1 (dcf6cd4bd4a5c9134af6fad38e930f3671c713b3) shows that 112 findings concern 19 exact package versions already present in the upstream lockfile.
The remaining finding concerns wheel 0.45.1, which is absent from both lockfiles but is bundled with their shared setuptools 80.9.0 version. Its installed origin still needs confirmation.
A clean upstream environment scan has not yet been reproduced. These dependency findings remain unresolved, and make lint is not claimed to pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds grad_ellm, a target-dependent, token-level attribution method for decoder-only Llama and Mistral models.
The method combines internal Q/K/V projections with gradients of the selected next-token objective and sums attribution scores over the last n_layers layers. The paper-reproduction configuration uses attributed_fn="logit" and normalize=True.
The integration includes:
This PR also updates the Value Zeroing hidden-state hook to handle Tensor, tuple, and list block outputs while preserving the full batch, device, and dtype. This addresses compatibility with Transformer layers that return a Tensor directly.
The initial scope is standard floating-point Llama and Mistral models on a single CPU or CUDA device. Quantization, model sharding/offload, training mode, and other model families are outside this implementation’s supported scope.
Related Issue
N/A.
Type of Change
Validation
Comparison of the provided dependency files against upstream Inseq v0.7.1 (dcf6cd4bd4a5c9134af6fad38e930f3671c713b3) shows that 112 findings concern 19 exact package versions already present in the upstream lockfile.
The remaining finding concerns wheel 0.45.1, which is absent from both lockfiles but is bundled with their shared setuptools 80.9.0 version. Its installed origin still needs confirmation.
A clean upstream environment scan has not yet been reproduced. These dependency findings remain unresolved, and make lint is not claimed to pass.
Checklist