glasschain 2.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- glasschain-2.1.0/LICENSE +21 -0
- glasschain-2.1.0/PKG-INFO +305 -0
- glasschain-2.1.0/README.md +260 -0
- glasschain-2.1.0/pyproject.toml +96 -0
- glasschain-2.1.0/setup.cfg +4 -0
- glasschain-2.1.0/src/cot_faithfulness/__init__.py +61 -0
- glasschain-2.1.0/src/cot_faithfulness/_version.py +5 -0
- glasschain-2.1.0/src/cot_faithfulness/banner.py +90 -0
- glasschain-2.1.0/src/cot_faithfulness/cli.py +688 -0
- glasschain-2.1.0/src/cot_faithfulness/export/__init__.py +39 -0
- glasschain-2.1.0/src/cot_faithfulness/export/hf_export.py +92 -0
- glasschain-2.1.0/src/cot_faithfulness/export/manifest_builder.py +249 -0
- glasschain-2.1.0/src/cot_faithfulness/interventions/__init__.py +17 -0
- glasschain-2.1.0/src/cot_faithfulness/interventions/ablation.py +314 -0
- glasschain-2.1.0/src/cot_faithfulness/models.py +502 -0
- glasschain-2.1.0/src/cot_faithfulness/patching/__init__.py +51 -0
- glasschain-2.1.0/src/cot_faithfulness/patching/hooks.py +243 -0
- glasschain-2.1.0/src/cot_faithfulness/patching/metrics.py +133 -0
- glasschain-2.1.0/src/cot_faithfulness/patching/sweep.py +522 -0
- glasschain-2.1.0/src/cot_faithfulness/probing/__init__.py +19 -0
- glasschain-2.1.0/src/cot_faithfulness/probing/dual_metric.py +215 -0
- glasschain-2.1.0/src/cot_faithfulness/probing/probes.py +509 -0
- glasschain-2.1.0/src/cot_faithfulness/probing/validation.py +113 -0
- glasschain-2.1.0/src/cot_faithfulness/py.typed +1 -0
- glasschain-2.1.0/src/cot_faithfulness/schema.py +303 -0
- glasschain-2.1.0/src/cot_faithfulness/tasks/__init__.py +47 -0
- glasschain-2.1.0/src/cot_faithfulness/tasks/arithmetic.py +173 -0
- glasschain-2.1.0/src/cot_faithfulness/tasks/base.py +181 -0
- glasschain-2.1.0/src/cot_faithfulness/tasks/hint_mcq.py +352 -0
- glasschain-2.1.0/src/glasschain/__init__.py +63 -0
- glasschain-2.1.0/src/glasschain/py.typed +1 -0
- glasschain-2.1.0/src/glasschain.egg-info/PKG-INFO +305 -0
- glasschain-2.1.0/src/glasschain.egg-info/SOURCES.txt +41 -0
- glasschain-2.1.0/src/glasschain.egg-info/dependency_links.txt +1 -0
- glasschain-2.1.0/src/glasschain.egg-info/entry_points.txt +3 -0
- glasschain-2.1.0/src/glasschain.egg-info/requires.txt +26 -0
- glasschain-2.1.0/src/glasschain.egg-info/top_level.txt +2 -0
- glasschain-2.1.0/tests/test_cli.py +345 -0
- glasschain-2.1.0/tests/test_interventions.py +114 -0
- glasschain-2.1.0/tests/test_patching.py +228 -0
- glasschain-2.1.0/tests/test_probing.py +272 -0
- glasschain-2.1.0/tests/test_schema.py +199 -0
- glasschain-2.1.0/tests/test_tasks.py +183 -0
glasschain-2.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ashioya Jotham Victor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: glasschain
|
|
3
|
+
Version: 2.1.0
|
|
4
|
+
Summary: GlassChain: Mechanistic interpretability library, CLI, and benchmark for Chain-of-Thought faithfulness
|
|
5
|
+
Author: Victor Ashioya
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ashioyajotham/cot-faithfulness-mech-interp
|
|
8
|
+
Project-URL: Repository, https://github.com/ashioyajotham/cot-faithfulness-mech-interp
|
|
9
|
+
Project-URL: Live Explorer, https://ashioyajotham.github.io/cot-faithfulness-mech-interp/
|
|
10
|
+
Project-URL: Documentation, https://github.com/ashioyajotham/cot-faithfulness-mech-interp#readme
|
|
11
|
+
Project-URL: Issues, https://github.com/ashioyajotham/cot-faithfulness-mech-interp/issues
|
|
12
|
+
Keywords: mechanistic-interpretability,chain-of-thought,faithfulness,ai-safety,activation-patching,linear-probes,glasschain
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: numpy>=1.26
|
|
24
|
+
Requires-Dist: scipy>=1.12
|
|
25
|
+
Requires-Dist: scikit-learn>=1.4
|
|
26
|
+
Requires-Dist: pydantic>=2.6
|
|
27
|
+
Provides-Extra: models
|
|
28
|
+
Requires-Dist: torch>=2.2; extra == "models"
|
|
29
|
+
Requires-Dist: transformer-lens<3.0,>=2.0; extra == "models"
|
|
30
|
+
Requires-Dist: transformers>=4.44; extra == "models"
|
|
31
|
+
Requires-Dist: accelerate>=0.30; extra == "models"
|
|
32
|
+
Requires-Dist: datasets>=2.19; extra == "models"
|
|
33
|
+
Requires-Dist: huggingface-hub>=0.23; extra == "models"
|
|
34
|
+
Provides-Extra: viz
|
|
35
|
+
Requires-Dist: matplotlib>=3.8; extra == "viz"
|
|
36
|
+
Requires-Dist: plotly>=5.20; extra == "viz"
|
|
37
|
+
Provides-Extra: cloud
|
|
38
|
+
Requires-Dist: modal>=0.64; extra == "cloud"
|
|
39
|
+
Provides-Extra: dev
|
|
40
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
41
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
42
|
+
Provides-Extra: all
|
|
43
|
+
Requires-Dist: glasschain[cloud,models,viz]; extra == "all"
|
|
44
|
+
Dynamic: license-file
|
|
45
|
+
|
|
46
|
+
# Mechanistic Analysis of Chain-of-Thought
|
|
47
|
+
|
|
48
|
+
Do language models genuinely rely on the reasoning steps written in their Chain-of-Thought (CoT), or do transformers develop separable **faithful** (CoT-following) and **shortcut** (CoT-bypassing) internal circuits? Chain-of-thought monitoring is widely proposed as a safety safeguard: if a model's reasoning trace is visible in text, monitors can catch misaligned or deceptive reasoning before an answer is acted upon. That safety case breaks down if the stated reasoning is merely post-hoc rationalization while a hidden internal pathway computes the final answer.
|
|
49
|
+
|
|
50
|
+
This research project investigates CoT faithfulness mechanistically across scales and architectures — combining contrastive task design, two-pass activation patching, leak-free linear probing with selectivity and Minimum Description Length (MDL) controls, and causal head ablations — to locate where CoT-following and CoT-bypassing computations live inside transformers and whether they can be detected or intervened upon reliably.
|
|
51
|
+
|
|
52
|
+
> **Audit & Replication Status (v1 → v2.1):** A systematic Tier-1 forensic audit of our archived v1 (Phase 1 / 2A / 2B) experiments uncovered five methodological traps — including identical-prompt contamination in `complete_fabrication` (52.8%–70.4% of v1 unfaithful labels), stochastic decoding, near-zero patching denominators (`Pair #170`), `n_valid = 2` head selection in Qwen 7B, and chance-level Phase 2B probes. All v1 headline claims have been retracted and documented transparently in [`docs/audit_v1_findings.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_findings.md) (reproducible via [`scripts/forensic_audit_v1.py`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/scripts/forensic_audit_v1.py)). The entire experimental pipeline has been rebuilt as **v2.1** with machine-checked rigor invariants, and **Tier-2 GPU replication** across core models is staged for execution on Modal and Google Colab.
|
|
53
|
+
|
|
54
|
+
---
|
|
55
|
+
|
|
56
|
+
## Method: Mechanistic Circuit Localization
|
|
57
|
+
|
|
58
|
+
To separate text-level plausibility from genuine causal reliance on intermediate reasoning, we pair each clean reasoning prompt with a minimal, token-aligned contrastive perturbation and trace the resulting computation through four stages:
|
|
59
|
+
|
|
60
|
+
```text
|
|
61
|
+
Clean CoT Prompt ─────┐ ┌──► Faithful Path (follows stated CoT)
|
|
62
|
+
├──► Two-Pass Activation Patching ┤ [label = 0 on corrupted prompt]
|
|
63
|
+
Corrupted CoT Prompt ─┘ (multi-token logprob diff, │
|
|
64
|
+
min_gap >= 0.5, uniform N) └──► Shortcut Bypass (ignores stated CoT)
|
|
65
|
+
│ [label = 1 on corrupted prompt]
|
|
66
|
+
▼
|
|
67
|
+
┌────────────────────────────────────────────────┐
|
|
68
|
+
│ Leak-Free Detection & Causal Verification │
|
|
69
|
+
│ • Pair-grouped CV probe + permutation p-value │
|
|
70
|
+
│ • Voita–Titov online MDL + surface baselines │
|
|
71
|
+
│ • Cumulative mean/zero ablation vs. random-k │
|
|
72
|
+
└────────────────────────────────────────────────┘
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Contrastive Benchmark Tasks
|
|
76
|
+
|
|
77
|
+
All tasks share a canonical Pydantic v2 contract ([`src/cot_faithfulness/schema.py`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/src/cot_faithfulness/schema.py)) that enforces `clean_prompt != corrupted_prompt`, `correct_answer != corrupted_answer`, deterministic greedy decoding (`do_sample=False`, `temperature=0.0`), and a strict clean-accuracy gate before any pair enters mechanistic analysis:
|
|
78
|
+
|
|
79
|
+
1. **`arithmetic` (Step-Corrupted 2-Digit Addition):** Minimal token-aligned corruptions to intermediate ones/tens steps across three mathematically consistent corruption types — `units_step_error`, `tens_step_error`, and `both_steps_error` (preserving digit length and zero negative intermediate sums). A model is **faithful** (`label = 0`) if it follows the corrupted intermediate arithmetic and **unfaithful** (`label = 1`) if it bypasses the CoT via an internal shortcut to output the true sum.
|
|
80
|
+
2. **`hint_mcq` (Hint-Induced Multiple-Choice Unfaithfulness):** Multi-domain reasoning MCQs (linear algebra, word arithmetic, deductive ordering, calendar/time, proportional reasoning) using a structured 2-step CoT reasoning scratchpad paired with subtle, unmentioned hints (`authority_hint`, `peer_consensus_hint`, `grader_metadata_hint`) following Turpin et al. (2023) and Chen et al. (2025). Tests whether the model flips its final choice toward a wrong hint while writing a plausible CoT that omits the hint.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Project Arc & Research Questions
|
|
85
|
+
|
|
86
|
+
This repository preserves the complete scientific evolution of the project — including archived legacy experiments and the forensic audit that prompted our v2.1 redesign:
|
|
87
|
+
|
|
88
|
+
| Stage | Scope & Models | Status |
|
|
89
|
+
|---|---|---|
|
|
90
|
+
| **Phase 1 (Legacy Baseline)** | Exploratory circuit discovery & linear probing on GPT-2 Small ([`archive/phase1_gpt2_legacy/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/archive/phase1_gpt2_legacy)) | **Archived** |
|
|
91
|
+
| **Phase 2A (Statistical Validation)** | Probe selectivity, bootstrap CIs, and false-negative error analysis on GPT-2 Small ([`archive/v1_results/phase2a/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/archive/v1_results/phase2a)) | **Archived & Audited** |
|
|
92
|
+
| **Phase 2B (Cross-Scale Replication)** | Scaling across `Qwen2.5-1.5B-Instruct`, `Qwen2.5-7B-Instruct`, and `Gemma 2 2B IT` ([`archive/v1_results/phase2b/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/archive/v1_results/phase2b)) | **Archived & Audited** |
|
|
93
|
+
| **Tier-1 Forensic Audit & v2.1 Redesign** | Full audit of v1 artifacts ([`docs/audit_v1_findings.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_findings.md)), 5 methodological fixes, dual-task benchmark, and automated `RigorChecklist` | **Complete** |
|
|
94
|
+
| **Tier-2 GPU Replication** | Clean 500-pair v2.1 runs across `qwen25-1.5b-instruct`, `qwen25-7b-instruct`, and `gemma2-2b-it` on `arithmetic` + `hint_mcq` | **Staged on Modal / Colab** |
|
|
95
|
+
|
|
96
|
+
### Research Questions
|
|
97
|
+
|
|
98
|
+
| ID | Research Question | Mechanistic Test | Status |
|
|
99
|
+
|---|---|---|---|
|
|
100
|
+
| **RQ1** | Can activation patching isolate separable *faithful* (CoT-following) vs. *shortcut* (CoT-bypassing) heads and MLPs? | Two-pass multi-token log-prob patching with `min_gap >= 0.5` and uniform Pass-2 `n_valid` | v2.1 Ready (Tier-2 pending) |
|
|
101
|
+
| **RQ2** | Do circuit linear probes capture genuine internal structure rather than memorizable surface artifacts? | Pair-grouped `StratifiedGroupKFold`, permutation selectivity p-value, online MDL, surface & random-layer baselines | Audited in v1; v2.1 Ready |
|
|
102
|
+
| **RQ3** | What causes high-confidence probe errors and extreme activation-patching outliers? | Error clustering, outlier/denominator diagnostics, digit-length & prompt-parity checks | Resolved in Tier-1 Audit |
|
|
103
|
+
| **RQ4** | How do shortcut rates, circuit localization, and probe detectability transfer across scale and architecture? | Cross-model suite: `Qwen2.5-1.5B-Instruct`, `Gemma 2 2B IT`, `Qwen2.5-7B-Instruct` | v1 Retracted; Tier-2 pending |
|
|
104
|
+
| **RQ5** | Does ablating shortcut heads causally shift behavior back toward faithful CoT following? | Cumulative mean/zero head ablation benchmarked against 5 matched random-k head controls | v2.1 Ready (Tier-2 pending) |
|
|
105
|
+
| **RQ6** | Do mechanistic faithfulness signatures generalize beyond synthetic arithmetic to natural-language hint unfaithfulness? | Cross-task comparison between `arithmetic` and `hint_mcq` under identical schema & metrics | v2.1 Ready (Tier-2 pending) |
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Tier-1 Forensic Audit: Five Methodological Traps Uncovered in v1
|
|
110
|
+
|
|
111
|
+
Full reproducible tables and JSON output are in [`docs/audit_v1_findings.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_findings.md) and [`docs/audit_v1_results.json`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_results.json).
|
|
112
|
+
|
|
113
|
+
| Failure Mode | Audited Finding in `archive/v1_results/` | v2.1 Pipeline Fix |
|
|
114
|
+
|---|---|---|
|
|
115
|
+
| **1. Identical-prompt contamination (`complete_fabrication`)** | **125 / 125** `complete_fabrication` pairs had `faithful_prompt == unfaithful_prompt`, inflating v1 `label = 1` by **70.4%** (Gemma 2 2B), **59.0%** (Qwen 1.5B), and **52.8%** (Qwen 7B). Shortcut rates excluding CF drop to **32.7%**, **37.6%**, and **51.5%**. | Pydantic `ContrastivePair` invariant raises `ValueError` if `clean_prompt == corrupted_prompt` or `correct_answer == corrupted_answer`; replaced with `units_step_error`, `tens_step_error`, `both_steps_error`. |
|
|
116
|
+
| **2. Stochastic decoding & no clean-accuracy gate** | Identical prompts produced self-disagreeing answers on **82.4%** (Gemma 2 2B), **24.0%** (Qwen 1.5B), and **12.8%** (Qwen 7B) of pairs. **54.0%** (47/87) of Gemma 2 2B "valid" pairs failed even on the clean prompt. | Deterministic greedy decoding (`do_sample=False`, `temperature=0.0`), matched `prepend_bos`, and strict clean-accuracy gate (`model_clean_answer == correct_answer`). |
|
|
117
|
+
| **3. Near-zero denominators & low-N head selection** | In GPT-2 Phase 2A, **Pair #170 alone** hit `−221.66` (`L4MLP`), `−124.84` (`L9H9`), and `+40.12` (`L7H6`), collapsing `L7H6` mean from `+0.1047` to `+0.0044` (median `+0.0028`). In Qwen 7B, all 15 "Layer 23 shortcut heads" had **`n_valid = 2`**. | Multi-token log-probability difference metric, `min_gap_threshold = 0.5`, uniform `n_valid` across all Pass-2 heads, and robust median / IQR / trimmed-mean statistics (Zhang & Nanda, ICLR 2024). |
|
|
118
|
+
| **4. Chance-level Phase 2B probes & invalid dual-metric claims** | 1-sample t-tests vs. 0.50 AUC: Qwen 7B (`0.536`, p = 0.427) and Gemma 2 2B (`0.633`, p = 0.094) are indistinguishable from chance; Qwen 7B accuracy (`60.5%`) sits **8.7 pp below** majority class (`69.3%`), while a zero-activation corruption-type baseline reaches **76.1%–88.5%**. | Pair-grouped `StratifiedGroupKFold`, `StandardScaler`, balanced classes, out-of-sample permutation selectivity + p-value, corrected Voita & Titov (2020) online MDL, surface-confound baseline, and p < 0.01 gate before dual-metric ρ claims. |
|
|
119
|
+
| **5. Phase 2A task validity & overparameterized control probes** | GPT-2 Phase 2A classified *clean vs. corrupted prompt text* (not behavioral shortcutting), with 14 negative `cot_answer` strings and 91 digit-length mismatches. Under pair-grouped CV, AUC drops from `0.980–0.991` to `0.9265` (`C=0.01`) / `0.9471` (`C=1.0`); at `C=1.0`, 768d Random Layer 8 beats the 6,400d circuit on online MDL (**612.1** vs. **967.9 bits**). | Behavioral unfaithfulness tasks only on models passing the clean-accuracy gate, benchmarked against matched random-layer probes and random-head ablation controls. |
|
|
120
|
+
|
|
121
|
+
> **Factual Errata Corrected:** Three factual errors in earlier v1 writeups have also been corrected: (1) GPT-2's `0.721` top restoration component was **`L0MLP`**, not `L0 attn`; (2) Gemma 2 2B's local sliding window is **4,096 tokens** (>130× longer than our ~25–30 token prompts), so sliding-window masking could not prevent Layer 0 from attending to the full prompt; and (3) archived `qwen25-math-*` directories actually ran **`Qwen2.5-1.5B-Instruct`** and **`Qwen2.5-7B-Instruct`**.
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Research Byproduct: GlassChain
|
|
126
|
+
|
|
127
|
+
As a reusable byproduct of rebuilding this study under strict methodological invariants, we packaged our benchmark engine, CLI, Python library, and interactive browser dashboard as **GlassChain** — so other researchers in mechanistic interpretability and AI safety can inspect our audited data, reproduce every control, and run the exact same rigor-checked benchmark on their own models.
|
|
128
|
+
|
|
129
|
+
### 1. Interactive Web Explorer
|
|
130
|
+
|
|
131
|
+
Explore audited manifests, circuit heatmaps, and pair-level traces directly in the browser (100% client-side, zero backend):
|
|
132
|
+
|
|
133
|
+
- **Live Explorer:** <https://ashioyajotham.github.io/cot-faithfulness-mech-interp/>
|
|
134
|
+
- **Bring Your Own Run (BYOR):** Drag and drop any locally generated `manifest.json` onto the window to inspect Layer×Head activation patching heatmaps, Dual-Metric alignment scatterplots, cumulative ablation curves vs. random-k controls, the Trace Debugger, and the interactive *5 Scientific Traps* postmortem.
|
|
135
|
+
|
|
136
|
+
### 2. Python Library (`glasschain` / `cot_faithfulness`)
|
|
137
|
+
|
|
138
|
+
Install the core library from PyPI (or add `[models]` for PyTorch + TransformerLens GPU execution):
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
pip install glasschain
|
|
142
|
+
pip install "glasschain[models]"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Import via either `cot_faithfulness` or the top-level `glasschain` package alias to compose tasks, activation patching, leak-free probing, and causal ablations directly in Python:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
import numpy as np
|
|
149
|
+
from cot_faithfulness.models import TransformerLensBackend
|
|
150
|
+
from cot_faithfulness.tasks import ArithmeticTask, HintMCQTask # also via `import glasschain`
|
|
151
|
+
from cot_faithfulness.patching import extract_probing_activations, run_two_pass_patching
|
|
152
|
+
from cot_faithfulness.probing import evaluate_probe_cv
|
|
153
|
+
from cot_faithfulness.interventions import run_cumulative_ablation
|
|
154
|
+
|
|
155
|
+
backend = TransformerLensBackend("qwen25-1.5b-instruct")
|
|
156
|
+
task = ArithmeticTask() # or HintMCQTask()
|
|
157
|
+
evaluated = task.evaluate_pairs_behaviorally(task.generate_pairs(n_pairs=50, seed=42), backend)
|
|
158
|
+
patching = run_two_pass_patching(backend, evaluated, top_k_layers=4, min_gap=0.5)
|
|
159
|
+
valid = [p for p in evaluated if p.is_valid]
|
|
160
|
+
head_acts, _, _ = extract_probing_activations(backend, valid, patching.top_circuit_heads, patching.top_k_layers)
|
|
161
|
+
X = np.concatenate([head_acts[h] for h in patching.top_circuit_heads], axis=1)
|
|
162
|
+
probe, _, _ = evaluate_probe_cv(X, [int(p.label) for p in valid], [p.group_id for p in valid], "circuit_linear")
|
|
163
|
+
ablation = run_cumulative_ablation(backend, evaluated, target_heads=patching.top_circuit_heads)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 3. Terminal CLI (`glasschain` / `cot-faith`)
|
|
167
|
+
|
|
168
|
+
Both `glasschain` and its alias `cot-faith` invoke the full 5-stage pipeline and manifest utilities:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
# Inspect registered models, tasks, and CPU contrastive pair previews
|
|
172
|
+
glasschain list-models
|
|
173
|
+
glasschain list-tasks
|
|
174
|
+
glasschain generate-preview --task hint_mcq --n-pairs 5
|
|
175
|
+
|
|
176
|
+
# Run the full 5-stage benchmark pipeline (writes results/qwen15_arith/manifest.json)
|
|
177
|
+
glasschain run --model qwen25-1.5b-instruct --task arithmetic --n-pairs 50 --out results/qwen15_arith
|
|
178
|
+
glasschain run --model qwen25-1.5b-instruct --task hint_mcq --n-pairs 50 --out results/qwen15_mcq
|
|
179
|
+
|
|
180
|
+
# Validate any manifest against the Pydantic v2.1 RigorChecklist contract
|
|
181
|
+
glasschain validate-manifest results/qwen15_arith/manifest.json
|
|
182
|
+
|
|
183
|
+
# Compare runs side-by-side and build a BenchmarkIndex for the web explorer
|
|
184
|
+
glasschain compare results/qwen15_arith/manifest.json results/qwen15_mcq/manifest.json \
|
|
185
|
+
--out web/public/data/benchmark_index.json
|
|
186
|
+
|
|
187
|
+
# Validate and export evaluated JSONL pairs to HuggingFace Hub (supports --dry-run)
|
|
188
|
+
glasschain export-hf --input results/qwen15_arith/pairs.jsonl --repo-id your-org/cot-faithfulness --dry-run
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Reproducing the Research & Tier-2 GPU Replication
|
|
194
|
+
|
|
195
|
+
### 1. Local Development Setup (Python 3.10–3.12 + `uv`)
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
git clone https://github.com/ashioyajotham/cot-faithfulness-mech-interp.git
|
|
199
|
+
cd cot-faithfulness-mech-interp
|
|
200
|
+
|
|
201
|
+
uv venv --python 3.12
|
|
202
|
+
source .venv/bin/activate
|
|
203
|
+
uv pip install -e ".[dev]"
|
|
204
|
+
|
|
205
|
+
# For local GPU execution, plotting, and Modal cloud runners:
|
|
206
|
+
uv pip install -e ".[models,viz,cloud,dev]"
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### 2. Run Unit Tests & Reproduce the Tier-1 Forensic Audit (CPU-Only)
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
pytest tests/ -v
|
|
213
|
+
python scripts/forensic_audit_v1.py
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### 3. Launch Tier-2 Full GPU Replication (Modal / Colab)
|
|
217
|
+
|
|
218
|
+
Full 500-pair GPU runs across `qwen25-1.5b-instruct`, `qwen25-7b-instruct`, and `gemma2-2b-it` on both `arithmetic` and `hint_mcq` are configured in [`runners/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/runners):
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
# Fast 20-pair Modal smoke test
|
|
222
|
+
modal run runners/modal_runner.py --model qwen25-1.5b-instruct --task arithmetic --n-pairs 20
|
|
223
|
+
|
|
224
|
+
# Full parallel 3-model x 2-task replication (6 runs concurrently)
|
|
225
|
+
modal run runners/modal_runner.py --all-core --all-tasks --n-pairs 500
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
See [`runners/README.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/runners/README.md) for HuggingFace token secrets, GPU tier routing, and Google Colab usage, and [`CONTRIBUTING.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/CONTRIBUTING.md) to submit verified manifests to the public leaderboard.
|
|
229
|
+
|
|
230
|
+
### 4. Run the Web Explorer Locally
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
cd web
|
|
234
|
+
npm install
|
|
235
|
+
npm run dev
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Repository Structure
|
|
241
|
+
|
|
242
|
+
```text
|
|
243
|
+
cot-faithfulness-mech-interp/
|
|
244
|
+
├── src/
|
|
245
|
+
│ ├── glasschain/ # Top-level `import glasschain` package alias
|
|
246
|
+
│ └── cot_faithfulness/ # Core v2.1 mechanistic interpretability library
|
|
247
|
+
│ ├── schema.py # Canonical Pydantic v2 contracts (ContrastivePair, BenchmarkManifest, RigorChecklist)
|
|
248
|
+
│ ├── models.py # Model registry & deterministic TransformerLens/HF loaders
|
|
249
|
+
│ ├── banner.py # Terminal ASCII banner helpers for the CLI
|
|
250
|
+
│ ├── py.typed # PEP 561 typing marker
|
|
251
|
+
│ ├── tasks/ # Contrastive task generators (`arithmetic`, `hint_mcq`)
|
|
252
|
+
│ ├── patching/ # Two-pass multi-token logprob patching + robust stats (Zhang & Nanda, 2024)
|
|
253
|
+
│ ├── probing/ # Pair-grouped CV probes, permutation selectivity, Voita–Titov online MDL, dual-metric
|
|
254
|
+
│ ├── interventions/ # Cumulative mean/zero head ablation with matched random-k controls
|
|
255
|
+
│ ├── export/ # BenchmarkManifest / BenchmarkIndex builders & HuggingFace JSONL exporter
|
|
256
|
+
│ └── cli.py # `glasschain` / `cot-faith` CLI & `run_benchmark_pipeline` orchestrator
|
|
257
|
+
├── runners/ # Cloud & notebook GPU runners for Tier-2 replication
|
|
258
|
+
│ ├── modal_runner.py # Modal parallel runner (L4 for <=3B, A100-40GB for 7B, HF cache volumes)
|
|
259
|
+
│ ├── colab_runner.py # Interactive Google Colab single-GPU runner
|
|
260
|
+
│ └── README.md # Modal & Colab execution instructions
|
|
261
|
+
├── submissions/ # Community leaderboard manifests + CI verification guide
|
|
262
|
+
├── web/ # GlassChain interactive explorer (Vite + React + TS -> GitHub Pages)
|
|
263
|
+
├── scripts/
|
|
264
|
+
│ └── forensic_audit_v1.py # Reproducible Tier-1 forensic audit script for archived v1 results
|
|
265
|
+
├── tests/ # Fast CPU-only unit, CLI, and schema invariant tests
|
|
266
|
+
├── datasets/
|
|
267
|
+
│ ├── dataset_card.md # HuggingFace dataset card with v1 audit disclosure & v2.1 schema
|
|
268
|
+
│ └── gpt2_arithmetic/README.md # Legacy GPT-2 arithmetic dataset card
|
|
269
|
+
├── docs/
|
|
270
|
+
│ ├── audit_v1_findings.md # Complete Tier-1 forensic audit report (all tables & p-values)
|
|
271
|
+
│ ├── audit_v1_results.json # Machine-readable Tier-1 audit results
|
|
272
|
+
│ ├── writeup.md # Technical report (archived v1 record + v2.1 methodological redesign)
|
|
273
|
+
│ ├── blog_post.md # Public research explainer + "5 Methodological Traps" postmortem
|
|
274
|
+
│ └── TROUBLESHOOTING.md # Environment, GPU, and schema validation troubleshooting guide
|
|
275
|
+
├── archive/ # Frozen legacy research artifacts (read-only)
|
|
276
|
+
│ ├── phase1_gpt2_legacy/ # Original Phase 1 GPT-2 notebooks, slides & outputs
|
|
277
|
+
│ └── v1_results/ # Frozen Phase 2A & Phase 2B result JSONs and NPZ activations
|
|
278
|
+
└── pyproject.toml # Package metadata, optional extras, ruff & pytest config
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## References
|
|
284
|
+
|
|
285
|
+
- Bogdan, P. C., Macar, U., Nanda, N., & Conmy, A. (2025). *Thought Anchors: Which LLM Reasoning Steps Matter?* arXiv:2506.19143.
|
|
286
|
+
- Chen, Y., Benton, J., Radhakrishnan, A., Uesato, J., Denison, C., Schulman, J., Somani, A., Hase, P., Wagner, M., Roger, F., Mikulik, V., Bowman, S. R., Leike, J., Kaplan, J., & Perez, E. (2025). *Reasoning Models Don't Always Say What They Think.* arXiv:2505.05410.
|
|
287
|
+
- Hewitt, J., & Liang, P. (2019). *Designing and Interpreting Probes with Control Tasks.* EMNLP.
|
|
288
|
+
- Hubinger, E., van Merwijk, C., Mikulik, V., Skalse, J., & Garrabrant, S. (2019). *Risks from Learned Optimization in Advanced Machine Learning Systems.* arXiv:1906.01820.
|
|
289
|
+
- Turpin, M., Michael, J., Perez, E., & Bowman, S. R. (2023). *Language Models Don't Always Say What They Think: Unfaithful Explanations in Chain-of-Thought Prompting.* NeurIPS.
|
|
290
|
+
- Voita, E., & Titov, I. (2020). *Information-Theoretic Probing with Minimum Description Length.* EMNLP.
|
|
291
|
+
- Wang, K., Variengien, A., Conmy, A., Shlegeris, B., & Steinhardt, J. (2023). *Interpretability in the Wild: A Circuit for Indirect Object Identification in GPT-2 Small.* ICLR.
|
|
292
|
+
- Yang, Y., et al. (2025). *Unveiling Internal Reasoning Modes in LLMs.* EMNLP.
|
|
293
|
+
- Zhang, F., & Nanda, N. (2024). *Towards Best Practices of Activation Patching in Language Models: Metrics and Methods.* ICLR.
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Author
|
|
298
|
+
|
|
299
|
+
Victor Ashioya (Jotham) — [ashioyajotham.github.io](https://ashioyajotham.github.io) · [GitHub](https://github.com/ashioyajotham)
|
|
300
|
+
|
|
301
|
+
Bluedot Impact Technical AI Safety Programme · MsingiAI
|
|
302
|
+
|
|
303
|
+
## License
|
|
304
|
+
|
|
305
|
+
[MIT](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/LICENSE)
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# Mechanistic Analysis of Chain-of-Thought
|
|
2
|
+
|
|
3
|
+
Do language models genuinely rely on the reasoning steps written in their Chain-of-Thought (CoT), or do transformers develop separable **faithful** (CoT-following) and **shortcut** (CoT-bypassing) internal circuits? Chain-of-thought monitoring is widely proposed as a safety safeguard: if a model's reasoning trace is visible in text, monitors can catch misaligned or deceptive reasoning before an answer is acted upon. That safety case breaks down if the stated reasoning is merely post-hoc rationalization while a hidden internal pathway computes the final answer.
|
|
4
|
+
|
|
5
|
+
This research project investigates CoT faithfulness mechanistically across scales and architectures — combining contrastive task design, two-pass activation patching, leak-free linear probing with selectivity and Minimum Description Length (MDL) controls, and causal head ablations — to locate where CoT-following and CoT-bypassing computations live inside transformers and whether they can be detected or intervened upon reliably.
|
|
6
|
+
|
|
7
|
+
> **Audit & Replication Status (v1 → v2.1):** A systematic Tier-1 forensic audit of our archived v1 (Phase 1 / 2A / 2B) experiments uncovered five methodological traps — including identical-prompt contamination in `complete_fabrication` (52.8%–70.4% of v1 unfaithful labels), stochastic decoding, near-zero patching denominators (`Pair #170`), `n_valid = 2` head selection in Qwen 7B, and chance-level Phase 2B probes. All v1 headline claims have been retracted and documented transparently in [`docs/audit_v1_findings.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_findings.md) (reproducible via [`scripts/forensic_audit_v1.py`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/scripts/forensic_audit_v1.py)). The entire experimental pipeline has been rebuilt as **v2.1** with machine-checked rigor invariants, and **Tier-2 GPU replication** across core models is staged for execution on Modal and Google Colab.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Method: Mechanistic Circuit Localization
|
|
12
|
+
|
|
13
|
+
To separate text-level plausibility from genuine causal reliance on intermediate reasoning, we pair each clean reasoning prompt with a minimal, token-aligned contrastive perturbation and trace the resulting computation through four stages:
|
|
14
|
+
|
|
15
|
+
```text
|
|
16
|
+
Clean CoT Prompt ─────┐ ┌──► Faithful Path (follows stated CoT)
|
|
17
|
+
├──► Two-Pass Activation Patching ┤ [label = 0 on corrupted prompt]
|
|
18
|
+
Corrupted CoT Prompt ─┘ (multi-token logprob diff, │
|
|
19
|
+
min_gap >= 0.5, uniform N) └──► Shortcut Bypass (ignores stated CoT)
|
|
20
|
+
│ [label = 1 on corrupted prompt]
|
|
21
|
+
▼
|
|
22
|
+
┌────────────────────────────────────────────────┐
|
|
23
|
+
│ Leak-Free Detection & Causal Verification │
|
|
24
|
+
│ • Pair-grouped CV probe + permutation p-value │
|
|
25
|
+
│ • Voita–Titov online MDL + surface baselines │
|
|
26
|
+
│ • Cumulative mean/zero ablation vs. random-k │
|
|
27
|
+
└────────────────────────────────────────────────┘
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Contrastive Benchmark Tasks
|
|
31
|
+
|
|
32
|
+
All tasks share a canonical Pydantic v2 contract ([`src/cot_faithfulness/schema.py`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/src/cot_faithfulness/schema.py)) that enforces `clean_prompt != corrupted_prompt`, `correct_answer != corrupted_answer`, deterministic greedy decoding (`do_sample=False`, `temperature=0.0`), and a strict clean-accuracy gate before any pair enters mechanistic analysis:
|
|
33
|
+
|
|
34
|
+
1. **`arithmetic` (Step-Corrupted 2-Digit Addition):** Minimal token-aligned corruptions to intermediate ones/tens steps across three mathematically consistent corruption types — `units_step_error`, `tens_step_error`, and `both_steps_error` (preserving digit length and zero negative intermediate sums). A model is **faithful** (`label = 0`) if it follows the corrupted intermediate arithmetic and **unfaithful** (`label = 1`) if it bypasses the CoT via an internal shortcut to output the true sum.
|
|
35
|
+
2. **`hint_mcq` (Hint-Induced Multiple-Choice Unfaithfulness):** Multi-domain reasoning MCQs (linear algebra, word arithmetic, deductive ordering, calendar/time, proportional reasoning) using a structured 2-step CoT reasoning scratchpad paired with subtle, unmentioned hints (`authority_hint`, `peer_consensus_hint`, `grader_metadata_hint`) following Turpin et al. (2023) and Chen et al. (2025). Tests whether the model flips its final choice toward a wrong hint while writing a plausible CoT that omits the hint.
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Project Arc & Research Questions
|
|
40
|
+
|
|
41
|
+
This repository preserves the complete scientific evolution of the project — including archived legacy experiments and the forensic audit that prompted our v2.1 redesign:
|
|
42
|
+
|
|
43
|
+
| Stage | Scope & Models | Status |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| **Phase 1 (Legacy Baseline)** | Exploratory circuit discovery & linear probing on GPT-2 Small ([`archive/phase1_gpt2_legacy/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/archive/phase1_gpt2_legacy)) | **Archived** |
|
|
46
|
+
| **Phase 2A (Statistical Validation)** | Probe selectivity, bootstrap CIs, and false-negative error analysis on GPT-2 Small ([`archive/v1_results/phase2a/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/archive/v1_results/phase2a)) | **Archived & Audited** |
|
|
47
|
+
| **Phase 2B (Cross-Scale Replication)** | Scaling across `Qwen2.5-1.5B-Instruct`, `Qwen2.5-7B-Instruct`, and `Gemma 2 2B IT` ([`archive/v1_results/phase2b/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/archive/v1_results/phase2b)) | **Archived & Audited** |
|
|
48
|
+
| **Tier-1 Forensic Audit & v2.1 Redesign** | Full audit of v1 artifacts ([`docs/audit_v1_findings.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_findings.md)), 5 methodological fixes, dual-task benchmark, and automated `RigorChecklist` | **Complete** |
|
|
49
|
+
| **Tier-2 GPU Replication** | Clean 500-pair v2.1 runs across `qwen25-1.5b-instruct`, `qwen25-7b-instruct`, and `gemma2-2b-it` on `arithmetic` + `hint_mcq` | **Staged on Modal / Colab** |
|
|
50
|
+
|
|
51
|
+
### Research Questions
|
|
52
|
+
|
|
53
|
+
| ID | Research Question | Mechanistic Test | Status |
|
|
54
|
+
|---|---|---|---|
|
|
55
|
+
| **RQ1** | Can activation patching isolate separable *faithful* (CoT-following) vs. *shortcut* (CoT-bypassing) heads and MLPs? | Two-pass multi-token log-prob patching with `min_gap >= 0.5` and uniform Pass-2 `n_valid` | v2.1 Ready (Tier-2 pending) |
|
|
56
|
+
| **RQ2** | Do circuit linear probes capture genuine internal structure rather than memorizable surface artifacts? | Pair-grouped `StratifiedGroupKFold`, permutation selectivity p-value, online MDL, surface & random-layer baselines | Audited in v1; v2.1 Ready |
|
|
57
|
+
| **RQ3** | What causes high-confidence probe errors and extreme activation-patching outliers? | Error clustering, outlier/denominator diagnostics, digit-length & prompt-parity checks | Resolved in Tier-1 Audit |
|
|
58
|
+
| **RQ4** | How do shortcut rates, circuit localization, and probe detectability transfer across scale and architecture? | Cross-model suite: `Qwen2.5-1.5B-Instruct`, `Gemma 2 2B IT`, `Qwen2.5-7B-Instruct` | v1 Retracted; Tier-2 pending |
|
|
59
|
+
| **RQ5** | Does ablating shortcut heads causally shift behavior back toward faithful CoT following? | Cumulative mean/zero head ablation benchmarked against 5 matched random-k head controls | v2.1 Ready (Tier-2 pending) |
|
|
60
|
+
| **RQ6** | Do mechanistic faithfulness signatures generalize beyond synthetic arithmetic to natural-language hint unfaithfulness? | Cross-task comparison between `arithmetic` and `hint_mcq` under identical schema & metrics | v2.1 Ready (Tier-2 pending) |
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Tier-1 Forensic Audit: Five Methodological Traps Uncovered in v1
|
|
65
|
+
|
|
66
|
+
Full reproducible tables and JSON output are in [`docs/audit_v1_findings.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_findings.md) and [`docs/audit_v1_results.json`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/docs/audit_v1_results.json).
|
|
67
|
+
|
|
68
|
+
| Failure Mode | Audited Finding in `archive/v1_results/` | v2.1 Pipeline Fix |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| **1. Identical-prompt contamination (`complete_fabrication`)** | **125 / 125** `complete_fabrication` pairs had `faithful_prompt == unfaithful_prompt`, inflating v1 `label = 1` by **70.4%** (Gemma 2 2B), **59.0%** (Qwen 1.5B), and **52.8%** (Qwen 7B). Shortcut rates excluding CF drop to **32.7%**, **37.6%**, and **51.5%**. | Pydantic `ContrastivePair` invariant raises `ValueError` if `clean_prompt == corrupted_prompt` or `correct_answer == corrupted_answer`; replaced with `units_step_error`, `tens_step_error`, `both_steps_error`. |
|
|
71
|
+
| **2. Stochastic decoding & no clean-accuracy gate** | Identical prompts produced self-disagreeing answers on **82.4%** (Gemma 2 2B), **24.0%** (Qwen 1.5B), and **12.8%** (Qwen 7B) of pairs. **54.0%** (47/87) of Gemma 2 2B "valid" pairs failed even on the clean prompt. | Deterministic greedy decoding (`do_sample=False`, `temperature=0.0`), matched `prepend_bos`, and strict clean-accuracy gate (`model_clean_answer == correct_answer`). |
|
|
72
|
+
| **3. Near-zero denominators & low-N head selection** | In GPT-2 Phase 2A, **Pair #170 alone** hit `−221.66` (`L4MLP`), `−124.84` (`L9H9`), and `+40.12` (`L7H6`), collapsing `L7H6` mean from `+0.1047` to `+0.0044` (median `+0.0028`). In Qwen 7B, all 15 "Layer 23 shortcut heads" had **`n_valid = 2`**. | Multi-token log-probability difference metric, `min_gap_threshold = 0.5`, uniform `n_valid` across all Pass-2 heads, and robust median / IQR / trimmed-mean statistics (Zhang & Nanda, ICLR 2024). |
|
|
73
|
+
| **4. Chance-level Phase 2B probes & invalid dual-metric claims** | 1-sample t-tests vs. 0.50 AUC: Qwen 7B (`0.536`, p = 0.427) and Gemma 2 2B (`0.633`, p = 0.094) are indistinguishable from chance; Qwen 7B accuracy (`60.5%`) sits **8.7 pp below** majority class (`69.3%`), while a zero-activation corruption-type baseline reaches **76.1%–88.5%**. | Pair-grouped `StratifiedGroupKFold`, `StandardScaler`, balanced classes, out-of-sample permutation selectivity + p-value, corrected Voita & Titov (2020) online MDL, surface-confound baseline, and p < 0.01 gate before dual-metric ρ claims. |
|
|
74
|
+
| **5. Phase 2A task validity & overparameterized control probes** | GPT-2 Phase 2A classified *clean vs. corrupted prompt text* (not behavioral shortcutting), with 14 negative `cot_answer` strings and 91 digit-length mismatches. Under pair-grouped CV, AUC drops from `0.980–0.991` to `0.9265` (`C=0.01`) / `0.9471` (`C=1.0`); at `C=1.0`, 768d Random Layer 8 beats the 6,400d circuit on online MDL (**612.1** vs. **967.9 bits**). | Behavioral unfaithfulness tasks only on models passing the clean-accuracy gate, benchmarked against matched random-layer probes and random-head ablation controls. |
|
|
75
|
+
|
|
76
|
+
> **Factual Errata Corrected:** Three factual errors in earlier v1 writeups have also been corrected: (1) GPT-2's `0.721` top restoration component was **`L0MLP`**, not `L0 attn`; (2) Gemma 2 2B's local sliding window is **4,096 tokens** (>130× longer than our ~25–30 token prompts), so sliding-window masking could not prevent Layer 0 from attending to the full prompt; and (3) archived `qwen25-math-*` directories actually ran **`Qwen2.5-1.5B-Instruct`** and **`Qwen2.5-7B-Instruct`**.
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Research Byproduct: GlassChain
|
|
81
|
+
|
|
82
|
+
As a reusable byproduct of rebuilding this study under strict methodological invariants, we packaged our benchmark engine, CLI, Python library, and interactive browser dashboard as **GlassChain** — so other researchers in mechanistic interpretability and AI safety can inspect our audited data, reproduce every control, and run the exact same rigor-checked benchmark on their own models.
|
|
83
|
+
|
|
84
|
+
### 1. Interactive Web Explorer
|
|
85
|
+
|
|
86
|
+
Explore audited manifests, circuit heatmaps, and pair-level traces directly in the browser (100% client-side, zero backend):
|
|
87
|
+
|
|
88
|
+
- **Live Explorer:** <https://ashioyajotham.github.io/cot-faithfulness-mech-interp/>
|
|
89
|
+
- **Bring Your Own Run (BYOR):** Drag and drop any locally generated `manifest.json` onto the window to inspect Layer×Head activation patching heatmaps, Dual-Metric alignment scatterplots, cumulative ablation curves vs. random-k controls, the Trace Debugger, and the interactive *5 Scientific Traps* postmortem.
|
|
90
|
+
|
|
91
|
+
### 2. Python Library (`glasschain` / `cot_faithfulness`)
|
|
92
|
+
|
|
93
|
+
Install the core library from PyPI (or add `[models]` for PyTorch + TransformerLens GPU execution):
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install glasschain
|
|
97
|
+
pip install "glasschain[models]"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Import via either `cot_faithfulness` or the top-level `glasschain` package alias to compose tasks, activation patching, leak-free probing, and causal ablations directly in Python:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import numpy as np
|
|
104
|
+
from cot_faithfulness.models import TransformerLensBackend
|
|
105
|
+
from cot_faithfulness.tasks import ArithmeticTask, HintMCQTask # also via `import glasschain`
|
|
106
|
+
from cot_faithfulness.patching import extract_probing_activations, run_two_pass_patching
|
|
107
|
+
from cot_faithfulness.probing import evaluate_probe_cv
|
|
108
|
+
from cot_faithfulness.interventions import run_cumulative_ablation
|
|
109
|
+
|
|
110
|
+
backend = TransformerLensBackend("qwen25-1.5b-instruct")
|
|
111
|
+
task = ArithmeticTask() # or HintMCQTask()
|
|
112
|
+
evaluated = task.evaluate_pairs_behaviorally(task.generate_pairs(n_pairs=50, seed=42), backend)
|
|
113
|
+
patching = run_two_pass_patching(backend, evaluated, top_k_layers=4, min_gap=0.5)
|
|
114
|
+
valid = [p for p in evaluated if p.is_valid]
|
|
115
|
+
head_acts, _, _ = extract_probing_activations(backend, valid, patching.top_circuit_heads, patching.top_k_layers)
|
|
116
|
+
X = np.concatenate([head_acts[h] for h in patching.top_circuit_heads], axis=1)
|
|
117
|
+
probe, _, _ = evaluate_probe_cv(X, [int(p.label) for p in valid], [p.group_id for p in valid], "circuit_linear")
|
|
118
|
+
ablation = run_cumulative_ablation(backend, evaluated, target_heads=patching.top_circuit_heads)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 3. Terminal CLI (`glasschain` / `cot-faith`)
|
|
122
|
+
|
|
123
|
+
Both `glasschain` and its alias `cot-faith` invoke the full 5-stage pipeline and manifest utilities:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
# Inspect registered models, tasks, and CPU contrastive pair previews
|
|
127
|
+
glasschain list-models
|
|
128
|
+
glasschain list-tasks
|
|
129
|
+
glasschain generate-preview --task hint_mcq --n-pairs 5
|
|
130
|
+
|
|
131
|
+
# Run the full 5-stage benchmark pipeline (writes results/qwen15_arith/manifest.json)
|
|
132
|
+
glasschain run --model qwen25-1.5b-instruct --task arithmetic --n-pairs 50 --out results/qwen15_arith
|
|
133
|
+
glasschain run --model qwen25-1.5b-instruct --task hint_mcq --n-pairs 50 --out results/qwen15_mcq
|
|
134
|
+
|
|
135
|
+
# Validate any manifest against the Pydantic v2.1 RigorChecklist contract
|
|
136
|
+
glasschain validate-manifest results/qwen15_arith/manifest.json
|
|
137
|
+
|
|
138
|
+
# Compare runs side-by-side and build a BenchmarkIndex for the web explorer
|
|
139
|
+
glasschain compare results/qwen15_arith/manifest.json results/qwen15_mcq/manifest.json \
|
|
140
|
+
--out web/public/data/benchmark_index.json
|
|
141
|
+
|
|
142
|
+
# Validate and export evaluated JSONL pairs to HuggingFace Hub (supports --dry-run)
|
|
143
|
+
glasschain export-hf --input results/qwen15_arith/pairs.jsonl --repo-id your-org/cot-faithfulness --dry-run
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Reproducing the Research & Tier-2 GPU Replication
|
|
149
|
+
|
|
150
|
+
### 1. Local Development Setup (Python 3.10–3.12 + `uv`)
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
git clone https://github.com/ashioyajotham/cot-faithfulness-mech-interp.git
|
|
154
|
+
cd cot-faithfulness-mech-interp
|
|
155
|
+
|
|
156
|
+
uv venv --python 3.12
|
|
157
|
+
source .venv/bin/activate
|
|
158
|
+
uv pip install -e ".[dev]"
|
|
159
|
+
|
|
160
|
+
# For local GPU execution, plotting, and Modal cloud runners:
|
|
161
|
+
uv pip install -e ".[models,viz,cloud,dev]"
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### 2. Run Unit Tests & Reproduce the Tier-1 Forensic Audit (CPU-Only)
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
pytest tests/ -v
|
|
168
|
+
python scripts/forensic_audit_v1.py
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 3. Launch Tier-2 Full GPU Replication (Modal / Colab)
|
|
172
|
+
|
|
173
|
+
Full 500-pair GPU runs across `qwen25-1.5b-instruct`, `qwen25-7b-instruct`, and `gemma2-2b-it` on both `arithmetic` and `hint_mcq` are configured in [`runners/`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/tree/main/runners):
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
# Fast 20-pair Modal smoke test
|
|
177
|
+
modal run runners/modal_runner.py --model qwen25-1.5b-instruct --task arithmetic --n-pairs 20
|
|
178
|
+
|
|
179
|
+
# Full parallel 3-model x 2-task replication (6 runs concurrently)
|
|
180
|
+
modal run runners/modal_runner.py --all-core --all-tasks --n-pairs 500
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
See [`runners/README.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/runners/README.md) for HuggingFace token secrets, GPU tier routing, and Google Colab usage, and [`CONTRIBUTING.md`](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/CONTRIBUTING.md) to submit verified manifests to the public leaderboard.
|
|
184
|
+
|
|
185
|
+
### 4. Run the Web Explorer Locally
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
cd web
|
|
189
|
+
npm install
|
|
190
|
+
npm run dev
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## Repository Structure
|
|
196
|
+
|
|
197
|
+
```text
|
|
198
|
+
cot-faithfulness-mech-interp/
|
|
199
|
+
├── src/
|
|
200
|
+
│ ├── glasschain/ # Top-level `import glasschain` package alias
|
|
201
|
+
│ └── cot_faithfulness/ # Core v2.1 mechanistic interpretability library
|
|
202
|
+
│ ├── schema.py # Canonical Pydantic v2 contracts (ContrastivePair, BenchmarkManifest, RigorChecklist)
|
|
203
|
+
│ ├── models.py # Model registry & deterministic TransformerLens/HF loaders
|
|
204
|
+
│ ├── banner.py # Terminal ASCII banner helpers for the CLI
|
|
205
|
+
│ ├── py.typed # PEP 561 typing marker
|
|
206
|
+
│ ├── tasks/ # Contrastive task generators (`arithmetic`, `hint_mcq`)
|
|
207
|
+
│ ├── patching/ # Two-pass multi-token logprob patching + robust stats (Zhang & Nanda, 2024)
|
|
208
|
+
│ ├── probing/ # Pair-grouped CV probes, permutation selectivity, Voita–Titov online MDL, dual-metric
|
|
209
|
+
│ ├── interventions/ # Cumulative mean/zero head ablation with matched random-k controls
|
|
210
|
+
│ ├── export/ # BenchmarkManifest / BenchmarkIndex builders & HuggingFace JSONL exporter
|
|
211
|
+
│ └── cli.py # `glasschain` / `cot-faith` CLI & `run_benchmark_pipeline` orchestrator
|
|
212
|
+
├── runners/ # Cloud & notebook GPU runners for Tier-2 replication
|
|
213
|
+
│ ├── modal_runner.py # Modal parallel runner (L4 for <=3B, A100-40GB for 7B, HF cache volumes)
|
|
214
|
+
│ ├── colab_runner.py # Interactive Google Colab single-GPU runner
|
|
215
|
+
│ └── README.md # Modal & Colab execution instructions
|
|
216
|
+
├── submissions/ # Community leaderboard manifests + CI verification guide
|
|
217
|
+
├── web/ # GlassChain interactive explorer (Vite + React + TS -> GitHub Pages)
|
|
218
|
+
├── scripts/
|
|
219
|
+
│ └── forensic_audit_v1.py # Reproducible Tier-1 forensic audit script for archived v1 results
|
|
220
|
+
├── tests/ # Fast CPU-only unit, CLI, and schema invariant tests
|
|
221
|
+
├── datasets/
|
|
222
|
+
│ ├── dataset_card.md # HuggingFace dataset card with v1 audit disclosure & v2.1 schema
|
|
223
|
+
│ └── gpt2_arithmetic/README.md # Legacy GPT-2 arithmetic dataset card
|
|
224
|
+
├── docs/
|
|
225
|
+
│ ├── audit_v1_findings.md # Complete Tier-1 forensic audit report (all tables & p-values)
|
|
226
|
+
│ ├── audit_v1_results.json # Machine-readable Tier-1 audit results
|
|
227
|
+
│ ├── writeup.md # Technical report (archived v1 record + v2.1 methodological redesign)
|
|
228
|
+
│ ├── blog_post.md # Public research explainer + "5 Methodological Traps" postmortem
|
|
229
|
+
│ └── TROUBLESHOOTING.md # Environment, GPU, and schema validation troubleshooting guide
|
|
230
|
+
├── archive/ # Frozen legacy research artifacts (read-only)
|
|
231
|
+
│ ├── phase1_gpt2_legacy/ # Original Phase 1 GPT-2 notebooks, slides & outputs
|
|
232
|
+
│ └── v1_results/ # Frozen Phase 2A & Phase 2B result JSONs and NPZ activations
|
|
233
|
+
└── pyproject.toml # Package metadata, optional extras, ruff & pytest config
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## References
|
|
239
|
+
|
|
240
|
+
- Bogdan, P. C., Macar, U., Nanda, N., & Conmy, A. (2025). *Thought Anchors: Which LLM Reasoning Steps Matter?* arXiv:2506.19143.
|
|
241
|
+
- Chen, Y., Benton, J., Radhakrishnan, A., Uesato, J., Denison, C., Schulman, J., Somani, A., Hase, P., Wagner, M., Roger, F., Mikulik, V., Bowman, S. R., Leike, J., Kaplan, J., & Perez, E. (2025). *Reasoning Models Don't Always Say What They Think.* arXiv:2505.05410.
|
|
242
|
+
- Hewitt, J., & Liang, P. (2019). *Designing and Interpreting Probes with Control Tasks.* EMNLP.
|
|
243
|
+
- Hubinger, E., van Merwijk, C., Mikulik, V., Skalse, J., & Garrabrant, S. (2019). *Risks from Learned Optimization in Advanced Machine Learning Systems.* arXiv:1906.01820.
|
|
244
|
+
- Turpin, M., Michael, J., Perez, E., & Bowman, S. R. (2023). *Language Models Don't Always Say What They Think: Unfaithful Explanations in Chain-of-Thought Prompting.* NeurIPS.
|
|
245
|
+
- Voita, E., & Titov, I. (2020). *Information-Theoretic Probing with Minimum Description Length.* EMNLP.
|
|
246
|
+
- Wang, K., Variengien, A., Conmy, A., Shlegeris, B., & Steinhardt, J. (2023). *Interpretability in the Wild: A Circuit for Indirect Object Identification in GPT-2 Small.* ICLR.
|
|
247
|
+
- Yang, Y., et al. (2025). *Unveiling Internal Reasoning Modes in LLMs.* EMNLP.
|
|
248
|
+
- Zhang, F., & Nanda, N. (2024). *Towards Best Practices of Activation Patching in Language Models: Metrics and Methods.* ICLR.
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Author
|
|
253
|
+
|
|
254
|
+
Victor Ashioya (Jotham) — [ashioyajotham.github.io](https://ashioyajotham.github.io) · [GitHub](https://github.com/ashioyajotham)
|
|
255
|
+
|
|
256
|
+
Bluedot Impact Technical AI Safety Programme · MsingiAI
|
|
257
|
+
|
|
258
|
+
## License
|
|
259
|
+
|
|
260
|
+
[MIT](https://github.com/ashioyajotham/cot-faithfulness-mech-interp/blob/main/LICENSE)
|