artyomsavov/grokking-carries
GitHub: artyomsavov/grokking-carries
通过逆向工程一个微型字符级 transformer,以数学方式揭示其学习并执行加减法算术运算的内部算法与 circuit 机制。
Stars: 0 | Forks: 0
# 深入理解 Carries


逆向工程一个微型 transformer,以数学方式证明它是如何学习和计算算术的。
## 概述
一项针对小型 GPT 风格 transformer 中算术运算的机械可解释性研究。该项目在加法和减法上训练了一个字符级 transformer,然后逆向工程了它学习到的内部算法——具体来说,是它如何表示数轴,以及如何在不同数位上实现进位和借位。
**方法:** linear probing · causal tracing · activation patching · logit lens · integrated gradients · attention ablation
## 动机
进位传播是一种顺序、非局部的操作:个位的进位可能会级联波及剩余的所有位置。这使其成为可解释性研究的理想测试平台——因为我们已经准确掌握了真实的算法,所以在模型中发现的任何 circuit 都可以与之进行对照验证。在一个小到足以让我们完全刻画其特性的模型中研究这一现象,能够让我们探讨 grokking 是否对应于某种特定的、可识别的 circuit 的涌现。
## 快速开始
该项目使用 [uv](https://github.com/astral-sh/uv) 进行快速且确定性的依赖管理。
1. 安装 `uv`(如果尚未安装):
```
curl -LsSf [https://astral.sh/uv/install.sh](https://astral.sh/uv/install.sh) | sh
```
2. 克隆仓库并同步环境:
```
git clone [https://github.com/artyomsavov/grokking-carries.git](https://github.com/artyomsavov/grokking-carries.git)
cd grokking-carries
uv sync
```
3. 启动 Jupyter 以交互方式运行 pipeline:
```
uv run jupyter notebook
```
*注意:请从 `notebooks/01_generate_data.ipynb` 开始构建算术语料库,然后继续使用 `03_learning_pipeline.ipynb` 来训练模型或探索预训练权重。*
## 模型配置
默认架构经过刻意的限制,以便进行详尽的可解释性分析。
| 超参数 | 值 | 描述 |
| --- | --- | --- |
| `d_model` | 128 | 隐藏层维度大小 |
| `n_layers` | 2 | transformer block 数量 |
| `n_heads` | 4 | attention head 数量 |
| `vocab_size` | 15 | 数字 0-9、运算符及特殊 token |
| `ctx_length` | 32 | 最大序列长度 |
*所有超参数均可在 `src/grokking_carries/config.py` 中覆盖。*
## 结果
*(关于这些结果的详细且可复现的分析可在 `notebooks/04_attention_ablation.ipynb` 到 `06_logits_lens.ipynb` 中找到)*
## 仓库布局
```
.
├── data/ # Generated datasets (train.jsonl, test.jsonl)
├── notebooks/ # Interactive pipelines for training and analysis
│ ├── 01_generate_data.ipynb
│ ├── 02_build_grokking_heart.ipynb
│ ├── 03_learning_pipeline.ipynb
│ ├── 04_attention_ablation.ipynb
│ ├── 05_linear_probing.ipynb
│ └── 06_logits_lens.ipynb
├── scripts/ # Utility scripts
├── src/grokking_carries/ # Core package
│ ├── config.py # Hyperparameters
│ ├── data/ # Data generation and tokenization
│ ├── interpretability/ # Hooks, probing, attribution
│ ├── model/ # Transformer architecture and saved weights
│ └── training/ # Training engine
├── pyproject.toml # Project metadata and dependencies
└── uv.lock # Locked dependency versions
```
## 引用
```
@software{grokking_carries,
author = {Savov Artyom, Michail Mishin},
title = {Grokking Carries: Mechanistic Interpretability of Arithmetic in Small Transformers},
year = {2026},
url = {[https://github.com/artyomsavov/grokking-carries](https://github.com/artyomsavov/grokking-carries)}
}
```
## 许可证
基于 [MIT 许可证](https://www.google.com/search?q=LICENSE)分发。
标签:Apex, NoSQL, Transformer, 云资产清单, 人工智能, 可解释性, 机器学习, 用户模式Hook绕过, 算术推理, 逆向工具, 逆向工程