gradient-detection

Localize influential model components using gradient-based salience scores for mechanistic interpretability.

75|7|Updated May 2, 2026
One-click install
npx skills add https://github.com/zjunlp/Mechanist --skill gradient-detection-zjunlp
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: gradient-detection
Source: https://github.com/zjunlp/Mechanist/tree/main/skills/mechanism-skills/gradient-detection
Command: npx skills add https://github.com/zjunlp/Mechanist --skill gradient-detection-zjunlp

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, transformers, transformer-lens, numpy, matplotlib, seaborn, pandas, scipy, and includes scripts (resource) and references (resource) components.

What problem does it solve? Identifying which tokens, activations, or parameters inside a large language model drive a specific output is expensive with exhaustive interventions. Gradient Detection provides fast first-order salience scores that rank influential internal objects with only backward passes, serving as an initial localization step before causal validation. ## Core Features & Use Cases - Inputs and Layer-wise States: Compute gradient norms, gradient-input scores, and integrated gradients over input embeddings and residual stream states to rank influential tokens and layers, with demos for analyzing fast vs slow thinking fine-tuning. - Intermediate Outputs: Score internal computational units using Relevance Patching (RelP) and Layer-wise Relevance Propagation for circuit discovery, including IOI task analysis with TransformerLens. - Parameters: Rank attention and FFN weight matrices by gradient salience, with demos for extracting linguistic regions in multilingual models. - Use Case: A researcher studying how a model performs indirect object identification can run the RelP demo scripts to compute component relevance scores, identify name-mover attention heads, and compare LRP rule configurations before running costly activation patching experiments. ## Quick Start Ask the assistant to compute layer-wise gradient norms for a model on a given text and visualize which layers contribute most to the target output.

Frequently Asked Questions about gradient-detection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I compute layer-wise gradient norms in a language model?

Run a forward pass with labels to get the loss, call backward, then iterate over named parameters and compute the L2 norm of each gradient. The calculate_gradients.py script demonstrates this with Hugging Face models and saves per-layer statistics to JSONL.

What is the difference between gradient norm and gradient-input scores?

Gradient norm measures sensitivity of the target to an object alone, while the gradient-input score multiplies the gradient by the object's value, approximating its actual contribution. Both are first-order proxies for intervention effects.

How does RelP compare to attribution patching for circuit discovery?

RelP applies Layer-wise Relevance Propagation rules through TransformerLens to propagate relevance, while attribution patching uses standard gradients. The ioi_task_analysis.py script compares both methods on the IOI task using logit differences.

Can gradient-based salience replace causal interventions like activation patching?

No. Gradients are local proxies and can be offset by downstream computation, so salient objects may not be causally responsible. Gradient rankings should be paired with causal attribution methods to validate genuine responsibility for the target behavior.

What models and libraries are required to run the gradient analysis demos?

The demos use PyTorch with Hugging Face Transformers for gradient statistics and TransformerLens with LRP enabled for relevance patching. Models like gpt2-small or Llama-2 are loaded from Hugging Face, with CUDA recommended for larger models.