Langsung ke konten
Skillpedia
Machine learning33k Β· #8574

mechanistic-interpretability-pyvene

Fungsinya

Skill ini membantu AI Anda merancang dan menjalankan eksperimen intervensi pada model PyTorch menggunakan pustaka pyvene. Skill ini ditujukan bagi peneliti yang ingin menguji bagian mana dari sebuah model yang menyebabkan perilaku tertentu.

Kapan dipakai

  • βœ“Saat Anda ingin mencari lapisan model mana yang bertanggung jawab atas jawaban tertentu.
  • βœ“Saat Anda ingin menjalankan activation patching untuk menguji gagasan sebab-akibat tentang sebuah model.
  • βœ“Saat Anda ingin membagikan eksperimen intervensi yang bisa direproduksi kepada orang lain.

Link skill

https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/ai-research/mechanistic-interpretability-pyvene/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/ai-research/mechanistic-interpretability-pyvene/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œBisakah Anda membantu saya menguji lapisan mana dari model bahasa ini yang menyebabkan jawaban yang salah?”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill ini menjelaskan bagian model yang ingin Anda ubah dan input yang akan dibandingkan. pyvene kemudian menerapkan perubahan saat model berjalan dan mencatat bagaimana keluarannya berbeda.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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)
Pertanyaan umum

Apa itu eksperimen intervensi?

Eksperimen ini mengubah sebuah bagian internal model saat berjalan, lalu memeriksa apakah keluarannya berubah. Dengan begitu terlihat bagian mana yang berpengaruh.

Bisakah saya membagikan pengaturan eksperimen saya?

Bisa. Pengaturan pyvene dapat disimpan dan dibagikan, termasuk melalui Hugging Face hub.

Apakah berlaku untuk selain model bahasa?

Ya. Alat ini bisa dipakai dengan arsitektur PyTorch apa pun, tidak hanya transformer.

Detail

Deskripsi asli dari pembuat

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.

repo: davila7/claude-code-templates

Popularitas

Bintang GitHub
33k
Peringkat
#8574
Lisensi
MIT
Diperbarui
2/11/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa