pyvene-interventions
ما الذي تفعله
تشرح هذه المهارة pyvene، وهي مكتبة من Stanford NLP لتغيير الأجزاء الداخلية للنموذج بطريقة مضبوطة. وهي موجهة للباحثين الذين يريدون اختبار أي أجزاء النموذج تسبب سلوكًا معينًا.
متى تستخدمها
- ✓عندما تريد تشغيل تصحيح التنشيطات لمعرفة أي أجزاء النموذج تؤثر في إجابة.
- ✓عندما تريد إجراء تتبع سببي على طريقة ROME.
- ✓عندما تريد مشاركة تجربة تدخل حتى يستطيع الآخرون إعادة إنتاجها.
رابط المهارة
https://github.com/Orchestra-Research/AI-Research-SKILLs/blob/main/04-mechanistic-interpretability/pyvene/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“اكتشف أي طبقات في نموذج لغوي تجعله يعطي إجابة محددة.”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
تصف التدخلات في إعداد بسيط يشبه القاموس، وتربطها بالنموذج. ثم تشغّل المكتبة النموذج بتلك التغييرات، وتعرض تأثيرها.
المحتوى التقني (ملف 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
- آخر تحديث
- 10/10/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
autoresearch
14kيرشد وكيلك خلال مشروع بحث كامل في الذكاء الاصطناعي، من مراجعة الأدبيات إلى التجارب والكتابة، بدورة متكررة من الاختبار والمراجعة.
prompt-guard
14kيتيح لوكيلك فحص النصوص بحثًا عن محاولات حقن الأوامر وكسر القيود قبل وصولها إلى تطبيق الذكاء الاصطناعي، باستخدام مصنّف Meta الصغير.
pytorch-fsdp2
14kيوضح لوكيلك كيفية إضافة تجزئة PyTorch FSDP2 إلى سكربت التدريب، حتى تُقسَّم النماذج الكبيرة على عدة وحدات معالجة رسومية وتُحفظ بشكل صحيح.
ml-training-recipes
14kيمنح وكيلك وصفات تدريب مجربة للشبكات العصبية، تشمل اختيار النموذج والمحسّنات ومعدلات التعلم ومعالجة قفزات الخسارة.
مهارات مشابهة
pytorch-patterns
280kيساعد وكيلك على استخدام PyTorch في نماذج تعلم الآلة. يطبّق أفضل الممارسات لتدريب النماذج بكفاءة ويضمن نتائج قابلة للتكرار.
evm-token-decimals
280kيساعد وكيلك على تجنب أخطاء الأعداد العشرية عبر شبكات البلوكتشين المختلفة. يضمن دقة قيم الرموز في المعاملات والحسابات.
ito-inference
280kيتيح لوكيلك التحقق من توفر نماذج الذكاء الاصطناعي للاستخدام. يربط طلب الحوسبة بخدمة النموذج الجاهزة.