pyvene-interventions
できること
このスキルは、モデルの内部を制御された方法で変えるためのStanford NLPのライブラリであるpyveneを説明します。どの部分がある振る舞いの原因かを検証したい研究者に向けたものです。
使う場面
- ✓どのモデルの部分が答えに影響するかを見るため、アクティベーションのパッチングを行いたいとき。
- ✓ROME方式の因果トレーシングを行いたいとき。
- ✓他の人が再現できるように、介入実験を共有したいとき。
スキルのリンク
https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/04-mechanistic-interpretability/pyvene/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“言語モデルのどの層が、特定の答えを返すように仕向けているかを調べてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
辞書形式のシンプルな設定で介入を記述し、それをモデルにアタッチします。ライブラリはその変更を加えてモデルを実行し、効果を報告します。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
pyvene: Causal Interventions for Neural Networks
pyvene is Stanford NLP's library for performing causal interventions on PyTorch models. It provides a declarative, dict-based framework for activation patching, causal tracing, and interchange intervention training - making intervention experiments reproducible and shareable.
GitHub: stanfordnlp/pyvene (840+ stars) Paper: pyvene: A Library for Understanding and Improving PyTorch Models via Interventions (NAACL 2024)
When to Use pyvene
Use pyvene when you need to:
- Perform causal tracing (ROME-style localization)
- Run activation patching experiments
- Conduct interchange intervention training (IIT)
- Test causal hypotheses about model components
- Share/reproduce intervention experiments via HuggingFace
- Work with any PyTorch architecture (not just transformers)
Consider alternatives when:
- You need exploratory activation analysis → Use TransformerLens
- You want to train/analyze SAEs → Use SAELens
- You need remote execution on massive models → Use nnsight
- You want lower-level control → Use nnsight
Installation
pip install pyvene
Standard import:
import pyvene as pv
Core Concepts
IntervenableModel
The main class that wraps any PyTorch model with intervention capabilities:
import pyvene as pv
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load base model
model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
# Define intervention configuration
config = pv.IntervenableConfig(
representations=[
pv.RepresentationConfig(
layer=8,
component="block_output",
intervention_type=pv.VanillaIntervention,
)
]
)
# Create intervenable model
intervenable = pv.IntervenableModel(config, model)
Intervention Types
| Type | Description | Use Case |
|---|---|---|
VanillaIntervention |
Swap activations between runs | Activation patching |
AdditionIntervention |
Add activations to base run | Steering, ablation |
SubtractionIntervention |
Subtract activations | Ablation |
ZeroIntervention |
Zero out activations | Component knockout |
RotatedSpaceIntervention |
DAS trainable intervention | Causal discovery |
CollectIntervention |
Collect activations | Probing, analysis |
Component Targets
# Available components to intervene on
components = [
"block_input", # Input to transformer block
"block_output", # Output of transformer block
"mlp_input", # Input to MLP
"mlp_output", # Output of MLP
"mlp_activation", # MLP hidden activations
"attention_input", # Input to attention
"attention_output", # Output of attention
"attention_value_output", # Attention value vectors
"query_output", # Query vectors
"key_output", # Key vectors
"value_output", # Value vectors
"head_attention_value_output", # Per-head values
]
Workflow 1: Causal Tracing (ROME-style)
Locate where factual associations are stored by corrupting inputs and restoring activations.
Step-by-Step
import pyvene as pv
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model = AutoModelForCausalLM.from_pretrained("gpt2-xl")
tokenizer = AutoTokenizer.from_pretrained("gpt2-xl")
# 1. Define clean and corrupted inputs
clean_prompt = "The Space Needle is in downtown"
corrupted_prompt = "The ##### ###### ## ## ########" # Noise
clean_tokens = tokenizer(clean_prompt, return_tensors="pt")
corrupted_tokens = tokenizer(corrupted_prompt, return_tensors="pt")
# 2. Get clean activations (source)
with torch.no_grad():
clean_outputs = model(**clean_tokens, output_hidden_states=True)
clean_states = clean_outputs.hidden_states
# 3. Define restoration intervention
def run_causal_trace(layer, position):
"""Restore clean activation at specific layer and position."""
config = pv.IntervenableConfig(
representations=[
pv.RepresentationConfig(
layer=layer,
component="block_output",
intervention_type=pv.VanillaIntervention,
unit="pos",
max_number_of_units=1,
)
]
)
intervenable = pv.IntervenableModel(config, model)
よくある質問
アクティベーションのパッチングとは何ですか?
一方の実行から内部の値を別の実行に入れ替えて、各部分が出力にどれだけ影響するかを見る方法です。
実験を共有できますか?
はい。スキルは、HuggingFaceを通じて介入の設定を共有できることに触れています。
アクティベーションを調べるには、どのライブラリがよいですか?
探索的な分析にはTransformerLensをスキルは勧めています。
詳細
作者による元の説明
Provides guidance for performing causal interventions on PyTorch models using pyvene's declarative intervention framework. Use when conducting causal tracing, activation patching, interchange intervention training, or testing causal hypotheses about model behavior.
人気
- GitHubのスター
- 14k
- ランキング
- #11185
- ライセンス
- MIT
- 更新日
- 2026/10/10
コメント
- …
このリポジトリの他のスキル
autoresearch
14kエージェントが、大まかなアイデアから実験、執筆まで、文献レビューを含むAI研究プロジェクト全体を、テストと見直しを繰り返しながら進められるよう支援します。
prompt-guard
14kエージェントが、Metaの小さな分類器を使い、AIアプリに届く前のテキストにプロンプトインジェクションやジェイルブレイクの試みがないか確認できるよう支援します。
pytorch-fsdp2
14kエージェントが、大規模なモデルを複数のGPUに分割して正しく保存できるよう、PyTorchのFSDP2を学習スクリプトに追加する方法を示します。
ml-training-recipes
14kエージェントが、モデルの選択、オプティマイザー、学習率、損失の急上昇への対処を含む、ニューラルネットワーク学習の実証済みの手順を使えるよう支援します。