[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6XGEMWA065JSBC6ZGJHA05W":3,"skill-same:01M6XGEMWA065JSBC6ZGJHA05W":86},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":39,"linkOnly":40,"markdownContent":41,"markdownBody":42,"frontMatter":43,"refName":52,"commitSha":53,"skillDirectory":54,"forksCount":14,"repositoryHtmlUrl":55,"repositoryHomepageUrl":6,"ownerHtmlUrl":56,"ownerBlogUrl":6,"ownerDescription":6,"updatedAt":57,"install":58,"resources":71},"01M6XGEMWA065JSBC6ZGJHA05W","pyvene-interventions",null,"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.","fadrienne\u002FMyGitHub","fadrienne","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F197874571?v=4","01M6X64V8VWCRSMFZ4EPS2BWXG","Francesca Adrienne","01M6X64VD5MW4F9PHDWQKS7ZAF",0,"MIT","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fopenscience\u002Fbackend\u002Fcli\u002Fskills\u002Fml-training\u002Fpyvene\u002FSKILL.md",[],[19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"pyvene interventions","pyvene","synthetic","when","declarative","for","Interpretability","Causal Tracing","Activation Patching","Causal Intervention","testing","activation","use","model","patching","Transformers","tracing","framework","training","pytorch","2026-11-10 10:12:30",false,"---\nname: pyvene-interventions\ndescription: 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.\ncategory: ml-training\nversion: 1.0.0\nauthor: Synthetic Sciences\nlicense: MIT\ntags: [Causal Intervention, pyvene, Activation Patching, Causal Tracing, Interpretability]\ndependencies: [pyvene>=0.1.8, torch>=2.0.0, transformers>=4.30.0]\n---\n\n# pyvene: Causal Interventions for Neural Networks\n\npyvene 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.\n\n**GitHub**: [stanfordnlp\u002Fpyvene](https:\u002F\u002Fgithub.com\u002Fstanfordnlp\u002Fpyvene) (840+ stars)\n**Paper**: [pyvene: A Library for Understanding and Improving PyTorch Models via Interventions](https:\u002F\u002Faclanthology.org\u002F2024.naacl-demo.16) (NAACL 2024)\n\n## When to Use pyvene\n\n**Use pyvene when you need to:**\n- Perform causal tracing (ROME-style localization)\n- Run activation patching experiments\n- Conduct interchange intervention training (IIT)\n- Test causal hypotheses about model components\n- Share\u002Freproduce intervention experiments via HuggingFace\n- Work with any PyTorch architecture (not just transformers)\n\n**Consider alternatives when:**\n- You need exploratory activation analysis → Use **TransformerLens**\n- You want to train\u002Fanalyze SAEs → Use **SAELens**\n- You need remote execution on massive models → Use **nnsight**\n- You want lower-level control → Use **nnsight**\n\n## Installation\n\n```bash\npip install pyvene\n```\n\nStandard import:\n```python\nimport pyvene as pv\n```\n\n## Core Concepts\n\n### IntervenableModel\n\nThe main class that wraps any PyTorch model with intervention capabilities:\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\n# Load base model\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n\n# Define intervention configuration\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=8,\n            component=\"block_output\",\n            intervention_type=pv.VanillaIntervention,\n        )\n    ]\n)\n\n# Create intervenable model\nintervenable = pv.IntervenableModel(config, model)\n```\n\n### Intervention Types\n\n| Type | Description | Use Case |\n|------|-------------|----------|\n| `VanillaIntervention` | Swap activations between runs | Activation patching |\n| `AdditionIntervention` | Add activations to base run | Steering, ablation |\n| `SubtractionIntervention` | Subtract activations | Ablation |\n| `ZeroIntervention` | Zero out activations | Component knockout |\n| `RotatedSpaceIntervention` | DAS trainable intervention | Causal discovery |\n| `CollectIntervention` | Collect activations | Probing, analysis |\n\n### Component Targets\n\n```python\n# Available components to intervene on\ncomponents = [\n    \"block_input\",      # Input to transformer block\n    \"block_output\",     # Output of transformer block\n    \"mlp_input\",        # Input to MLP\n    \"mlp_output\",       # Output of MLP\n    \"mlp_activation\",   # MLP hidden activations\n    \"attention_input\",  # Input to attention\n    \"attention_output\", # Output of attention\n    \"attention_value_output\",  # Attention value vectors\n    \"query_output\",     # Query vectors\n    \"key_output\",       # Key vectors\n    \"value_output\",     # Value vectors\n    \"head_attention_value_output\",  # Per-head values\n]\n```\n\n## Workflow 1: Causal Tracing (ROME-style)\n\nLocate where factual associations are stored by corrupting inputs and restoring activations.\n\n### Step-by-Step\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2-xl\")\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2-xl\")\n\n# 1. Define clean and corrupted inputs\nclean_prompt = \"The Space Needle is in downtown\"\ncorrupted_prompt = \"The ##### ###### ## ## ########\"  # Noise\n\nclean_tokens = tokenizer(clean_prompt, return_tensors=\"pt\")\ncorrupted_tokens = tokenizer(corrupted_prompt, return_tensors=\"pt\")\n\n# 2. Get clean activations (source)\nwith torch.no_grad():\n    clean_outputs = model(**clean_tokens, output_hidden_states=True)\n    clean_states = clean_outputs.hidden_states\n\n# 3. Define restoration intervention\ndef run_causal_trace(layer, position):\n    \"\"\"Restore clean activation at specific layer and position.\"\"\"\n    config = pv.IntervenableConfig(\n        representations=[\n            pv.RepresentationConfig(\n                layer=layer,\n                component=\"block_output\",\n                intervention_type=pv.VanillaIntervention,\n                unit=\"pos\",\n                max_number_of_units=1,\n            )\n        ]\n    )\n\n    intervenable = pv.IntervenableModel(config, model)\n\n    # Run with intervention\n    _, patched_outputs = intervenable(\n        base=corrupted_tokens,\n        sources=[clean_tokens],\n        unit_locations={\"sources->base\": ([[[position]]], [[[position]]])},\n        output_original_output=True,\n    )\n\n    # Return probability of correct token\n    probs = torch.softmax(patched_outputs.logits[0, -1], dim=-1)\n    seattle_token = tokenizer.encode(\" Seattle\")[0]\n    return probs[seattle_token].item()\n\n# 4. Sweep over layers and positions\nn_layers = model.config.n_layer\nseq_len = clean_tokens[\"input_ids\"].shape[1]\n\nresults = torch.zeros(n_layers, seq_len)\nfor layer in range(n_layers):\n    for pos in range(seq_len):\n        results[layer, pos] = run_causal_trace(layer, pos)\n\n# 5. Visualize (layer x position heatmap)\n# High values indicate causal importance\n```\n\n### Checklist\n- [ ] Prepare clean prompt with target factual association\n- [ ] Create corrupted version (noise or counterfactual)\n- [ ] Define intervention config for each (layer, position)\n- [ ] Run patching sweep\n- [ ] Identify causal hotspots in heatmap\n\n## Workflow 2: Activation Patching for Circuit Analysis\n\nTest which components are necessary for a specific behavior.\n\n### Step-by-Step\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n\n# IOI task setup\nclean_prompt = \"When John and Mary went to the store, Mary gave a bottle to\"\ncorrupted_prompt = \"When John and Mary went to the store, John gave a bottle to\"\n\nclean_tokens = tokenizer(clean_prompt, return_tensors=\"pt\")\ncorrupted_tokens = tokenizer(corrupted_prompt, return_tensors=\"pt\")\n\njohn_token = tokenizer.encode(\" John\")[0]\nmary_token = tokenizer.encode(\" Mary\")[0]\n\ndef logit_diff(logits):\n    \"\"\"IO - S logit difference.\"\"\"\n    return logits[0, -1, john_token] - logits[0, -1, mary_token]\n\n# Patch attention output at each layer\ndef patch_attention(layer):\n    config = pv.IntervenableConfig(\n        representations=[\n            pv.RepresentationConfig(\n                layer=layer,\n                component=\"attention_output\",\n                intervention_type=pv.VanillaIntervention,\n            )\n        ]\n    )\n\n    intervenable = pv.IntervenableModel(config, model)\n\n    _, patched_outputs = intervenable(\n        base=corrupted_tokens,\n        sources=[clean_tokens],\n    )\n\n    return logit_diff(patched_outputs.logits).item()\n\n# Find which layers matter\nresults = []\nfor layer in range(model.config.n_layer):\n    diff = patch_attention(layer)\n    results.append(diff)\n    print(f\"Layer {layer}: logit diff = {diff:.3f}\")\n```\n\n## Workflow 3: Interchange Intervention Training (IIT)\n\nTrain interventions to discover causal structure.\n\n### Step-by-Step\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM\nimport torch\n\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\n\n# 1. Define trainable intervention\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=6,\n            component=\"block_output\",\n            intervention_type=pv.RotatedSpaceIntervention,  # Trainable\n            low_rank_dimension=64,  # Learn 64-dim subspace\n        )\n    ]\n)\n\nintervenable = pv.IntervenableModel(config, model)\n\n# 2. Set up training\noptimizer = torch.optim.Adam(\n    intervenable.get_trainable_parameters(),\n    lr=1e-4\n)\n\n# 3. Training loop (simplified)\nfor base_input, source_input, target_output in dataloader:\n    optimizer.zero_grad()\n\n    _, outputs = intervenable(\n        base=base_input,\n        sources=[source_input],\n    )\n\n    loss = criterion(outputs.logits, target_output)\n    loss.backward()\n    optimizer.step()\n\n# 4. Analyze learned intervention\n# The rotation matrix reveals causal subspace\nrotation = intervenable.interventions[\"layer.6.block_output\"][0].rotate_layer\n```\n\n### DAS (Distributed Alignment Search)\n\n```python\n# Low-rank rotation finds interpretable subspaces\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=8,\n            component=\"block_output\",\n            intervention_type=pv.LowRankRotatedSpaceIntervention,\n            low_rank_dimension=1,  # Find 1D causal direction\n        )\n    ]\n)\n```\n\n## Workflow 4: Model Steering (Honest LLaMA)\n\nSteer model behavior during generation.\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\nmodel = AutoModelForCausalLM.from_pretrained(\"meta-llama\u002FLlama-2-7b-hf\")\ntokenizer = AutoTokenizer.from_pretrained(\"meta-llama\u002FLlama-2-7b-hf\")\n\n# Load pre-trained steering intervention\nintervenable = pv.IntervenableModel.load(\n    \"zhengxuanzenwu\u002Fintervenable_honest_llama2_chat_7B\",\n    model=model,\n)\n\n# Generate with steering\nprompt = \"Is the earth flat?\"\ninputs = tokenizer(prompt, return_tensors=\"pt\")\n\n# Intervention applied during generation\noutputs = intervenable.generate(\n    inputs,\n    max_new_tokens=100,\n    do_sample=False,\n)\n\nprint(tokenizer.decode(outputs[0]))\n```\n\n## Saving and Sharing Interventions\n\n```python\n# Save locally\nintervenable.save(\".\u002Fmy_intervention\")\n\n# Load from local\nintervenable = pv.IntervenableModel.load(\n    \".\u002Fmy_intervention\",\n    model=model,\n)\n\n# Share on HuggingFace\nintervenable.save_intervention(\"username\u002Fmy-intervention\")\n\n# Load from HuggingFace\nintervenable = pv.IntervenableModel.load(\n    \"username\u002Fmy-intervention\",\n    model=model,\n)\n```\n\n## Common Issues & Solutions\n\n### Issue: Wrong intervention location\n```python\n# WRONG: Incorrect component name\nconfig = pv.RepresentationConfig(\n    component=\"mlp\",  # Not valid!\n)\n\n# RIGHT: Use exact component name\nconfig = pv.RepresentationConfig(\n    component=\"mlp_output\",  # Valid\n)\n```\n\n### Issue: Dimension mismatch\n```python\n# Ensure source and base have compatible shapes\n# For position-specific interventions:\nconfig = pv.RepresentationConfig(\n    unit=\"pos\",\n    max_number_of_units=1,  # Intervene on single position\n)\n\n# Specify locations explicitly\nintervenable(\n    base=base_tokens,\n    sources=[source_tokens],\n    unit_locations={\"sources->base\": ([[[5]]], [[[5]]])},  # Position 5\n)\n```\n\n### Issue: Memory with large models\n```python\n# Use gradient checkpointing\nmodel.gradient_checkpointing_enable()\n\n# Or intervene on fewer components\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=8,  # Single layer instead of all\n            component=\"block_output\",\n        )\n    ]\n)\n```\n\n### Issue: LoRA integration\n```python\n# pyvene v0.1.8+ supports LoRAs as interventions\nconfig = pv.RepresentationConfig(\n    intervention_type=pv.LoRAIntervention,\n    low_rank_dimension=16,\n)\n```\n\n## Key Classes Reference\n\n| Class | Purpose |\n|-------|---------|\n| `IntervenableModel` | Main wrapper for interventions |\n| `IntervenableConfig` | Configuration container |\n| `RepresentationConfig` | Single intervention specification |\n| `VanillaIntervention` | Activation swapping |\n| `RotatedSpaceIntervention` | Trainable DAS intervention |\n| `CollectIntervention` | Activation collection |\n\n## Supported Models\n\npyvene works with any PyTorch model. Tested on:\n- GPT-2 (all sizes)\n- LLaMA \u002F LLaMA-2\n- Pythia\n- Mistral \u002F Mixtral\n- OPT\n- BLIP (vision-language)\n- ESM (protein models)\n- Mamba (state space)\n\n## Reference Documentation\n\nFor detailed API documentation, tutorials, and advanced usage, see the `references\u002F` folder:\n\n| File | Contents |\n|------|----------|\n| [references\u002FREADME.md](references\u002FREADME.md) | Overview and quick start guide |\n| [references\u002Fapi.md](references\u002Fapi.md) | Complete API reference for IntervenableModel, intervention types, configurations |\n| [references\u002Ftutorials.md](references\u002Ftutorials.md) | Step-by-step tutorials for causal tracing, activation patching, DAS |\n\n## External Resources\n\n### Tutorials\n- [pyvene 101](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fpyvene_101.html)\n- [Causal Tracing Tutorial](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fadvanced_tutorials\u002FCausal_Tracing.html)\n- [IOI Circuit Replication](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fadvanced_tutorials\u002FIOI_Replication.html)\n- [DAS Introduction](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fadvanced_tutorials\u002FDAS_Main_Introduction.html)\n\n### Papers\n- [Locating and Editing Factual Associations in GPT](https:\u002F\u002Farxiv.org\u002Fabs\u002F2202.05262) - Meng et al. (2022)\n- [Inference-Time Intervention](https:\u002F\u002Farxiv.org\u002Fabs\u002F2306.03341) - Li et al. (2023)\n- [Interpretability in the Wild](https:\u002F\u002Farxiv.org\u002Fabs\u002F2211.00593) - Wang et al. (2022)\n\n### Official Documentation\n- [Official Docs](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002F)\n- [API Reference](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Fapi\u002F)\n\n## Comparison with Other Tools\n\n| Feature | pyvene | TransformerLens | nnsight |\n|---------|--------|-----------------|---------|\n| Declarative config | Yes | No | No |\n| HuggingFace sharing | Yes | No | No |\n| Trainable interventions | Yes | Limited | Yes |\n| Any PyTorch model | Yes | Transformers only | Yes |\n| Remote execution | No | No | Yes (NDIF) |\n","# pyvene: Causal Interventions for Neural Networks\n\npyvene 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.\n\n**GitHub**: [stanfordnlp\u002Fpyvene](https:\u002F\u002Fgithub.com\u002Fstanfordnlp\u002Fpyvene) (840+ stars)\n**Paper**: [pyvene: A Library for Understanding and Improving PyTorch Models via Interventions](https:\u002F\u002Faclanthology.org\u002F2024.naacl-demo.16) (NAACL 2024)\n\n## When to Use pyvene\n\n**Use pyvene when you need to:**\n- Perform causal tracing (ROME-style localization)\n- Run activation patching experiments\n- Conduct interchange intervention training (IIT)\n- Test causal hypotheses about model components\n- Share\u002Freproduce intervention experiments via HuggingFace\n- Work with any PyTorch architecture (not just transformers)\n\n**Consider alternatives when:**\n- You need exploratory activation analysis → Use **TransformerLens**\n- You want to train\u002Fanalyze SAEs → Use **SAELens**\n- You need remote execution on massive models → Use **nnsight**\n- You want lower-level control → Use **nnsight**\n\n## Installation\n\n```bash\npip install pyvene\n```\n\nStandard import:\n```python\nimport pyvene as pv\n```\n\n## Core Concepts\n\n### IntervenableModel\n\nThe main class that wraps any PyTorch model with intervention capabilities:\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\n# Load base model\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n\n# Define intervention configuration\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=8,\n            component=\"block_output\",\n            intervention_type=pv.VanillaIntervention,\n        )\n    ]\n)\n\n# Create intervenable model\nintervenable = pv.IntervenableModel(config, model)\n```\n\n### Intervention Types\n\n| Type | Description | Use Case |\n|------|-------------|----------|\n| `VanillaIntervention` | Swap activations between runs | Activation patching |\n| `AdditionIntervention` | Add activations to base run | Steering, ablation |\n| `SubtractionIntervention` | Subtract activations | Ablation |\n| `ZeroIntervention` | Zero out activations | Component knockout |\n| `RotatedSpaceIntervention` | DAS trainable intervention | Causal discovery |\n| `CollectIntervention` | Collect activations | Probing, analysis |\n\n### Component Targets\n\n```python\n# Available components to intervene on\ncomponents = [\n    \"block_input\",      # Input to transformer block\n    \"block_output\",     # Output of transformer block\n    \"mlp_input\",        # Input to MLP\n    \"mlp_output\",       # Output of MLP\n    \"mlp_activation\",   # MLP hidden activations\n    \"attention_input\",  # Input to attention\n    \"attention_output\", # Output of attention\n    \"attention_value_output\",  # Attention value vectors\n    \"query_output\",     # Query vectors\n    \"key_output\",       # Key vectors\n    \"value_output\",     # Value vectors\n    \"head_attention_value_output\",  # Per-head values\n]\n```\n\n## Workflow 1: Causal Tracing (ROME-style)\n\nLocate where factual associations are stored by corrupting inputs and restoring activations.\n\n### Step-by-Step\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2-xl\")\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2-xl\")\n\n# 1. Define clean and corrupted inputs\nclean_prompt = \"The Space Needle is in downtown\"\ncorrupted_prompt = \"The ##### ###### ## ## ########\"  # Noise\n\nclean_tokens = tokenizer(clean_prompt, return_tensors=\"pt\")\ncorrupted_tokens = tokenizer(corrupted_prompt, return_tensors=\"pt\")\n\n# 2. Get clean activations (source)\nwith torch.no_grad():\n    clean_outputs = model(**clean_tokens, output_hidden_states=True)\n    clean_states = clean_outputs.hidden_states\n\n# 3. Define restoration intervention\ndef run_causal_trace(layer, position):\n    \"\"\"Restore clean activation at specific layer and position.\"\"\"\n    config = pv.IntervenableConfig(\n        representations=[\n            pv.RepresentationConfig(\n                layer=layer,\n                component=\"block_output\",\n                intervention_type=pv.VanillaIntervention,\n                unit=\"pos\",\n                max_number_of_units=1,\n            )\n        ]\n    )\n\n    intervenable = pv.IntervenableModel(config, model)\n\n    # Run with intervention\n    _, patched_outputs = intervenable(\n        base=corrupted_tokens,\n        sources=[clean_tokens],\n        unit_locations={\"sources->base\": ([[[position]]], [[[position]]])},\n        output_original_output=True,\n    )\n\n    # Return probability of correct token\n    probs = torch.softmax(patched_outputs.logits[0, -1], dim=-1)\n    seattle_token = tokenizer.encode(\" Seattle\")[0]\n    return probs[seattle_token].item()\n\n# 4. Sweep over layers and positions\nn_layers = model.config.n_layer\nseq_len = clean_tokens[\"input_ids\"].shape[1]\n\nresults = torch.zeros(n_layers, seq_len)\nfor layer in range(n_layers):\n    for pos in range(seq_len):\n        results[layer, pos] = run_causal_trace(layer, pos)\n\n# 5. Visualize (layer x position heatmap)\n# High values indicate causal importance\n```\n\n### Checklist\n- [ ] Prepare clean prompt with target factual association\n- [ ] Create corrupted version (noise or counterfactual)\n- [ ] Define intervention config for each (layer, position)\n- [ ] Run patching sweep\n- [ ] Identify causal hotspots in heatmap\n\n## Workflow 2: Activation Patching for Circuit Analysis\n\nTest which components are necessary for a specific behavior.\n\n### Step-by-Step\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\nimport torch\n\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\ntokenizer = AutoTokenizer.from_pretrained(\"gpt2\")\n\n# IOI task setup\nclean_prompt = \"When John and Mary went to the store, Mary gave a bottle to\"\ncorrupted_prompt = \"When John and Mary went to the store, John gave a bottle to\"\n\nclean_tokens = tokenizer(clean_prompt, return_tensors=\"pt\")\ncorrupted_tokens = tokenizer(corrupted_prompt, return_tensors=\"pt\")\n\njohn_token = tokenizer.encode(\" John\")[0]\nmary_token = tokenizer.encode(\" Mary\")[0]\n\ndef logit_diff(logits):\n    \"\"\"IO - S logit difference.\"\"\"\n    return logits[0, -1, john_token] - logits[0, -1, mary_token]\n\n# Patch attention output at each layer\ndef patch_attention(layer):\n    config = pv.IntervenableConfig(\n        representations=[\n            pv.RepresentationConfig(\n                layer=layer,\n                component=\"attention_output\",\n                intervention_type=pv.VanillaIntervention,\n            )\n        ]\n    )\n\n    intervenable = pv.IntervenableModel(config, model)\n\n    _, patched_outputs = intervenable(\n        base=corrupted_tokens,\n        sources=[clean_tokens],\n    )\n\n    return logit_diff(patched_outputs.logits).item()\n\n# Find which layers matter\nresults = []\nfor layer in range(model.config.n_layer):\n    diff = patch_attention(layer)\n    results.append(diff)\n    print(f\"Layer {layer}: logit diff = {diff:.3f}\")\n```\n\n## Workflow 3: Interchange Intervention Training (IIT)\n\nTrain interventions to discover causal structure.\n\n### Step-by-Step\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM\nimport torch\n\nmodel = AutoModelForCausalLM.from_pretrained(\"gpt2\")\n\n# 1. Define trainable intervention\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=6,\n            component=\"block_output\",\n            intervention_type=pv.RotatedSpaceIntervention,  # Trainable\n            low_rank_dimension=64,  # Learn 64-dim subspace\n        )\n    ]\n)\n\nintervenable = pv.IntervenableModel(config, model)\n\n# 2. Set up training\noptimizer = torch.optim.Adam(\n    intervenable.get_trainable_parameters(),\n    lr=1e-4\n)\n\n# 3. Training loop (simplified)\nfor base_input, source_input, target_output in dataloader:\n    optimizer.zero_grad()\n\n    _, outputs = intervenable(\n        base=base_input,\n        sources=[source_input],\n    )\n\n    loss = criterion(outputs.logits, target_output)\n    loss.backward()\n    optimizer.step()\n\n# 4. Analyze learned intervention\n# The rotation matrix reveals causal subspace\nrotation = intervenable.interventions[\"layer.6.block_output\"][0].rotate_layer\n```\n\n### DAS (Distributed Alignment Search)\n\n```python\n# Low-rank rotation finds interpretable subspaces\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=8,\n            component=\"block_output\",\n            intervention_type=pv.LowRankRotatedSpaceIntervention,\n            low_rank_dimension=1,  # Find 1D causal direction\n        )\n    ]\n)\n```\n\n## Workflow 4: Model Steering (Honest LLaMA)\n\nSteer model behavior during generation.\n\n```python\nimport pyvene as pv\nfrom transformers import AutoModelForCausalLM, AutoTokenizer\n\nmodel = AutoModelForCausalLM.from_pretrained(\"meta-llama\u002FLlama-2-7b-hf\")\ntokenizer = AutoTokenizer.from_pretrained(\"meta-llama\u002FLlama-2-7b-hf\")\n\n# Load pre-trained steering intervention\nintervenable = pv.IntervenableModel.load(\n    \"zhengxuanzenwu\u002Fintervenable_honest_llama2_chat_7B\",\n    model=model,\n)\n\n# Generate with steering\nprompt = \"Is the earth flat?\"\ninputs = tokenizer(prompt, return_tensors=\"pt\")\n\n# Intervention applied during generation\noutputs = intervenable.generate(\n    inputs,\n    max_new_tokens=100,\n    do_sample=False,\n)\n\nprint(tokenizer.decode(outputs[0]))\n```\n\n## Saving and Sharing Interventions\n\n```python\n# Save locally\nintervenable.save(\".\u002Fmy_intervention\")\n\n# Load from local\nintervenable = pv.IntervenableModel.load(\n    \".\u002Fmy_intervention\",\n    model=model,\n)\n\n# Share on HuggingFace\nintervenable.save_intervention(\"username\u002Fmy-intervention\")\n\n# Load from HuggingFace\nintervenable = pv.IntervenableModel.load(\n    \"username\u002Fmy-intervention\",\n    model=model,\n)\n```\n\n## Common Issues & Solutions\n\n### Issue: Wrong intervention location\n```python\n# WRONG: Incorrect component name\nconfig = pv.RepresentationConfig(\n    component=\"mlp\",  # Not valid!\n)\n\n# RIGHT: Use exact component name\nconfig = pv.RepresentationConfig(\n    component=\"mlp_output\",  # Valid\n)\n```\n\n### Issue: Dimension mismatch\n```python\n# Ensure source and base have compatible shapes\n# For position-specific interventions:\nconfig = pv.RepresentationConfig(\n    unit=\"pos\",\n    max_number_of_units=1,  # Intervene on single position\n)\n\n# Specify locations explicitly\nintervenable(\n    base=base_tokens,\n    sources=[source_tokens],\n    unit_locations={\"sources->base\": ([[[5]]], [[[5]]])},  # Position 5\n)\n```\n\n### Issue: Memory with large models\n```python\n# Use gradient checkpointing\nmodel.gradient_checkpointing_enable()\n\n# Or intervene on fewer components\nconfig = pv.IntervenableConfig(\n    representations=[\n        pv.RepresentationConfig(\n            layer=8,  # Single layer instead of all\n            component=\"block_output\",\n        )\n    ]\n)\n```\n\n### Issue: LoRA integration\n```python\n# pyvene v0.1.8+ supports LoRAs as interventions\nconfig = pv.RepresentationConfig(\n    intervention_type=pv.LoRAIntervention,\n    low_rank_dimension=16,\n)\n```\n\n## Key Classes Reference\n\n| Class | Purpose |\n|-------|---------|\n| `IntervenableModel` | Main wrapper for interventions |\n| `IntervenableConfig` | Configuration container |\n| `RepresentationConfig` | Single intervention specification |\n| `VanillaIntervention` | Activation swapping |\n| `RotatedSpaceIntervention` | Trainable DAS intervention |\n| `CollectIntervention` | Activation collection |\n\n## Supported Models\n\npyvene works with any PyTorch model. Tested on:\n- GPT-2 (all sizes)\n- LLaMA \u002F LLaMA-2\n- Pythia\n- Mistral \u002F Mixtral\n- OPT\n- BLIP (vision-language)\n- ESM (protein models)\n- Mamba (state space)\n\n## Reference Documentation\n\nFor detailed API documentation, tutorials, and advanced usage, see the `references\u002F` folder:\n\n| File | Contents |\n|------|----------|\n| [references\u002FREADME.md](references\u002FREADME.md) | Overview and quick start guide |\n| [references\u002Fapi.md](references\u002Fapi.md) | Complete API reference for IntervenableModel, intervention types, configurations |\n| [references\u002Ftutorials.md](references\u002Ftutorials.md) | Step-by-step tutorials for causal tracing, activation patching, DAS |\n\n## External Resources\n\n### Tutorials\n- [pyvene 101](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fpyvene_101.html)\n- [Causal Tracing Tutorial](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fadvanced_tutorials\u002FCausal_Tracing.html)\n- [IOI Circuit Replication](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fadvanced_tutorials\u002FIOI_Replication.html)\n- [DAS Introduction](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Ftutorials\u002Fadvanced_tutorials\u002FDAS_Main_Introduction.html)\n\n### Papers\n- [Locating and Editing Factual Associations in GPT](https:\u002F\u002Farxiv.org\u002Fabs\u002F2202.05262) - Meng et al. (2022)\n- [Inference-Time Intervention](https:\u002F\u002Farxiv.org\u002Fabs\u002F2306.03341) - Li et al. (2023)\n- [Interpretability in the Wild](https:\u002F\u002Farxiv.org\u002Fabs\u002F2211.00593) - Wang et al. (2022)\n\n### Official Documentation\n- [Official Docs](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002F)\n- [API Reference](https:\u002F\u002Fstanfordnlp.github.io\u002Fpyvene\u002Fapi\u002F)\n\n## Comparison with Other Tools\n\n| Feature | pyvene | TransformerLens | nnsight |\n|---------|--------|-----------------|---------|\n| Declarative config | Yes | No | No |\n| HuggingFace sharing | Yes | No | No |\n| Trainable interventions | Yes | Limited | Yes |\n| Any PyTorch model | Yes | Transformers only | Yes |\n| Remote execution | No | No | Yes (NDIF) |\n",{"name":5,"tags":44,"author":45,"license":15,"version":46,"category":47,"description":7,"dependencies":48},[28,20,27,26,25],"Synthetic Sciences","1.0.0","ml-training",[49,50,51],"pyvene>=0.1.8","torch>=2.0.0","transformers>=4.30.0","main","4aa39efd1cdab0b4657ecee16c92a92011a5b10f","openscience\u002Fbackend\u002Fcli\u002Fskills\u002Fml-training\u002Fpyvene","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub","https:\u002F\u002Fgithub.com\u002Ffadrienne","2026-10-28 00:43:09",{"source":8,"skillName":5,"commands":59,"prompt":69,"zipAvailable":70},[60,63,66],{"tool":61,"command":62},"npx","npx skills add fadrienne\u002FMyGitHub --skill pyvene-interventions",{"tool":64,"command":65},"bunx","bunx skills add fadrienne\u002FMyGitHub --skill pyvene-interventions",{"tool":67,"command":68},"pnpm","pnpm dlx skills add fadrienne\u002FMyGitHub --skill pyvene-interventions","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fopenscience\u002Fbackend\u002Fcli\u002Fskills\u002Fml-training\u002Fpyvene\u002FSKILL.md",true,[72,78,82],{"path":73,"resourceType":74,"resourceTypeDesc":75,"sizeBytes":76,"sourceHtmlUrl":77},"references\u002Fapi.md","REFERENCE","参考文档",7871,"https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fopenscience\u002Fbackend\u002Fcli\u002Fskills\u002Fml-training\u002Fpyvene\u002Freferences\u002Fapi.md",{"path":79,"resourceType":74,"resourceTypeDesc":75,"sizeBytes":80,"sourceHtmlUrl":81},"references\u002FREADME.md",2105,"https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fopenscience\u002Fbackend\u002Fcli\u002Fskills\u002Fml-training\u002Fpyvene\u002Freferences\u002FREADME.md",{"path":83,"resourceType":74,"resourceTypeDesc":75,"sizeBytes":84,"sourceHtmlUrl":85},"references\u002Ftutorials.md",10111,"https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fopenscience\u002Fbackend\u002Fcli\u002Fskills\u002Fml-training\u002Fpyvene\u002Freferences\u002Ftutorials.md",[87,118,143,177,204,231],{"skillNo":88,"name":89,"nameZh":6,"description":90,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":91,"categories":92,"keywords":98,"publishedAt":39},"01M7167B28PK20HP7R7ZED2DY0","statistical-power","Sample size calculation, power analysis, and effect size conversion for scientific studies. Use BEFORE data collection (a priori) to determine required N, or after (post-hoc) to compute achieved power. Covers parametric tests (t-test, ANOVA, chi-square, proportions, correlation, regression), simulation-based power for complex\u002Fclustered\u002Fnested designs, and power curves. Use this skill when someone asks 'how many samples do I need', 'what is my power', 'what effect size can I detect', or any variant. Pairs with \u002Fexperimental-design (choose design first, then compute power) and \u002Fstatistical-analysis (analyze data after collection).","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fscientific-agent-skills\u002Fskills\u002Fstatistical-power\u002FSKILL.md",[93],{"categoryNo":94,"categoryName":95,"taxonomyType":96,"taxonomyTypeDesc":97},"01JBSEEDCAT000000000000023","Skill与智能体开发","SCENARIO","使用场景",[99,100,101,102,103,104,105,106,22,107,108,109,110,111,112,113,114,115,116,117],"power","statistical power","effect","scipy","regression","choose","design","uv","Bash","collection","analysis","calculation","ANOVA","conversion","analyze","t-test","chi-square","simulation","sample",{"skillNo":119,"name":120,"nameZh":6,"description":121,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":122,"categories":123,"keywords":127,"publishedAt":39},"01M71677EBCM8EPCE9BZCK8505","statistical-analysis","Guided statistical analysis with test selection and reporting. Use when you need help choosing appropriate tests for your data, assumption checking, power analysis, and APA-formatted results. Best for academic research reporting, test selection guidance. For implementing specific models programmatically use statsmodels.","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fscientific-agent-skills\u002Fskills\u002Fstatistical-analysis\u002FSKILL.md",[124],{"categoryNo":125,"categoryName":126,"taxonomyType":96,"taxonomyTypeDesc":97},"01JBSEEDCAT000000000000008","测试与质量",[109,128,99,129,130,131,22,132,133,134,24,135,31,136,137,138,139,140,141,142],"statistical analysis","academic research","implementing","selection","you","reporting","data","tests","with","test","research","results","statsmodels","models","help",{"skillNo":144,"name":145,"nameZh":6,"description":146,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":147,"categories":148,"keywords":158,"publishedAt":39},"01M71673R93XAHFH81DBNVNG7H","scientific-writing","Core skill for the deep research and writing tool. Write scientific manuscripts in full paragraphs (never bullet points). Use two-stage process with (1) section outlines with key points using research-lookup then (2) convert to flowing prose. IMRAD structure, citations (APA\u002FAMA\u002FVancouver), figures\u002Ftables, reporting guidelines (CONSORT\u002FSTROBE\u002FPRISMA), for research papers and journal submissions.","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fscientific-agent-skills\u002Fskills\u002Fscientific-writing\u002FSKILL.md",[149,152,153],{"categoryNo":150,"categoryName":151,"taxonomyType":96,"taxonomyTypeDesc":97},"01JBSEEDCAT000000000000018","写作与文案",{"categoryNo":94,"categoryName":95,"taxonomyType":96,"taxonomyTypeDesc":97},{"categoryNo":154,"categoryName":155,"taxonomyType":156,"taxonomyTypeDesc":157},"01JBSEEDCAT000000000000036","内容创作者","PROFESSION","适用职业",[159,160,161,162,163,164,165,166,167,168,107,169,170,171,172,173,174,175,133,176],"writing","scientific","scientific writing","Citations","deep","deep research","never","papers","journal","false","api","prisma","structure","openrouter","Outlines","core skill","read","features",{"skillNo":178,"name":179,"nameZh":6,"description":180,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":181,"categories":182,"keywords":187,"publishedAt":39},"01M7166ZPKXBF4EBPHVTJPQBB1","scientific-visualization","Meta-skill for publication-ready figures. Use when creating journal submission figures requiring multi-panel layouts, significance annotations, error bars, colorblind-safe palettes, and specific journal formatting (Nature, Science, Cell). Orchestrates matplotlib\u002Fseaborn\u002Fplotly with publication styles. For quick exploration use seaborn or plotly directly.","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fscientific-agent-skills\u002Fskills\u002Fscientific-visualization\u002FSKILL.md",[183,186],{"categoryNo":184,"categoryName":185,"taxonomyType":96,"taxonomyTypeDesc":97},"01JBSEEDCAT000000000000013","分析与可视化",{"categoryNo":94,"categoryName":95,"taxonomyType":96,"taxonomyTypeDesc":97},[188,160,189,190,191,192,193,194,195,167,22,196,197,198,199,200,201,202,24,203],"visualization","scientific visualization","error","ready","quick","exploration","meta-skill","Meta","safe","plotly","annotations","formatting","Publication","figures","panel","submission",{"skillNo":205,"name":206,"nameZh":6,"description":207,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":208,"categories":209,"keywords":216,"publishedAt":39},"01M7166VXJWQSSC3RQZP60WBGR","research-code","Debug, profile, review, and restructure Python research and analysis code. Use when analysis scripts crash, produce wrong results, run too slowly, or need a correctness review. Covers: reading tracebacks and diagnosing root causes, hypothesis-driven debugging, reviewing code for common statistical\u002Fnumerical errors, profiling data pipelines, improving reproducibility (seeds, environments, determinism), and structuring analysis scripts for sharing. Distinct from \u002Fexploratory-data-analysis (which understands data) and \u002Fstatistical-analysis (which chooses and runs tests) — this skill focuses on the code itself.","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fscientific-agent-skills\u002Fskills\u002Fresearch-code\u002FSKILL.md",[210,213],{"categoryNo":211,"categoryName":212,"taxonomyType":96,"taxonomyTypeDesc":97},"01JBSEEDCAT000000000000021","资料检索与研究",{"categoryNo":214,"categoryName":215,"taxonomyType":156,"taxonomyTypeDesc":157},"01JBSEEDCAT000000000000030","开发工程师",[217,218,138,219,220,221,222,106,223,22,107,109,224,225,226,227,228,229,175,230],"code","research code","which","memory","data-analysis","errors","debugging","common","review","data pipelines","run","scripts","line","write",{"skillNo":232,"name":233,"nameZh":6,"description":234,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":235,"licenseFileUrl":6,"sourceHtmlUrl":236,"categories":237,"keywords":244,"publishedAt":39},"01M7166CF5HSR7SVZYZFWTDX55","markdown-mermaid-writing","Comprehensive markdown and Mermaid diagram writing skill. Use when creating any scientific document, report, analysis, or visualization. Establishes text-based diagrams as the default documentation standard with full style guides (markdown + mermaid), 24 diagram type references, and 9 document templates.","Apache-2.0","https:\u002F\u002Fgithub.com\u002Ffadrienne\u002FMyGitHub\u002Fblob\u002Fmain\u002Fscientific-agent-skills\u002Fskills\u002Fmarkdown-mermaid-writing\u002FSKILL.md",[238,241,242,243],{"categoryNo":239,"categoryName":240,"taxonomyType":96,"taxonomyTypeDesc":97},"01JBSEEDCAT000000000000002","文档处理",{"categoryNo":150,"categoryName":151,"taxonomyType":96,"taxonomyTypeDesc":97},{"categoryNo":94,"categoryName":95,"taxonomyType":96,"taxonomyTypeDesc":97},{"categoryNo":154,"categoryName":155,"taxonomyType":156,"taxonomyTypeDesc":157},[159,245,246,247,248,249,188,250,251,22,107,109,252,253,254,255,256,257,160,175],"markdown mermaid writing","mermaid","markdown","community","diagrams","project","default","team","guides","text","documentation","references","diagram"]