pyvene-interventions
Skill này làm gì
Skill này giải thích pyvene, một thư viện từ Stanford NLP để thay đổi nội bộ mô hình một cách có kiểm soát. Skill dành cho nhà nghiên cứu muốn kiểm tra phần nào của mô hình gây ra một hành vi.
Khi nào nên dùng
- ✓Khi bạn muốn chạy activation patching để xem phần nào của mô hình quan trọng với một câu trả lời.
- ✓Khi bạn muốn làm causal tracing theo kiểu ROME.
- ✓Khi bạn muốn chia sẻ một thí nghiệm can thiệp để người khác có thể tái tạo.
Liên kết skill
https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/04-mechanistic-interpretability/pyvene/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Tìm xem những lớp nào của mô hình ngôn ngữ khiến nó đưa ra một câu trả lời cụ thể.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Bạn mô tả các can thiệp trong một cấu hình kiểu từ điển rồi gắn chúng vào mô hình. Thư viện sau đó chạy mô hình với các thay đổi đó và báo cáo tác động.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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)
Câu hỏi thường gặp
Activation patching là gì?
Đó là phương pháp hoán đổi các giá trị bên trong từ lần chạy này sang lần chạy khác, để xem mỗi phần ảnh hưởng đến đầu ra bao nhiêu.
Nó có chia sẻ được thí nghiệm không?
Có. Skill nhắc đến việc chia sẻ cấu hình can thiệp qua HuggingFace.
Thư viện nào tốt hơn để khám phá activation?
Skill gợi ý TransformerLens để phân tích activation mang tính khám phá.
Chi tiết
Mô tả gốc của tác giả
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.
Độ phổ biến
- Sao GitHub
- 14k
- Vị trí xếp hạng
- #11185
- Giấy phép
- MIT
- Cập nhật
- 10/10/2026
Bình luận
- …
Thêm skill từ kho mã này
autoresearch
14kHướng dẫn agent của bạn qua một dự án nghiên cứu AI đầy đủ, từ tổng quan tài liệu đến thí nghiệm và viết báo, với một vòng lặp kiểm thử và rà soát lặp lại.
prompt-guard
14kCho phép agent của bạn quét văn bản để phát hiện prompt injection và các nỗ lực jailbreak trước khi nó đến ứng dụng AI, dùng bộ phân loại nhỏ của Meta.
pytorch-fsdp2
14kChỉ cho agent của bạn cách thêm PyTorch FSDP2 vào script huấn luyện để các mô hình lớn được chia ra nhiều GPU và lưu đúng cách.
ml-training-recipes
14kCung cấp cho agent của bạn các công thức huấn luyện mạng nơ-ron đã được kiểm chứng, gồm chọn mô hình, bộ tối ưu, learning rate và xử lý loss tăng vọt.
Skill tương tự
pytorch-patterns
280kGiúp agent của bạn dùng PyTorch cho các mô hình học máy. Áp dụng thực hành tốt nhất để huấn luyện mô hình hiệu quả và đảm bảo kết quả có thể lặp lại.
evm-token-decimals
280kGiúp agent tránh lỗi về số thập phân khi làm việc với các mạng blockchain khác nhau. Đảm bảo giá trị token chính xác trong giao dịch và phép tính.
ito-inference
280kGiúp agent kiểm tra xem các mô hình AI đã sẵn sàng để dùng chưa. Kết nối yêu cầu tài nguyên tính toán với dịch vụ mô hình đã sẵn sàng.