|
| 1 | +# AGENTS.md — AI Agent Technical Context |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +**attnres-rs** is the first Rust implementation of Attention Residuals (MoonshotAI/Kimi paper) using the [burn](https://github.com/tracel-ai/burn) deep learning framework. It provides a drop-in replacement for standard residual connections in Transformers. |
| 6 | + |
| 7 | +## Tech Stack |
| 8 | + |
| 9 | +| Component | Technology | Version | |
| 10 | +|-------------|-----------------|----------| |
| 11 | +| Language | Rust | 2021 edition (1.80+) | |
| 12 | +| ML Framework| burn | 0.20 | |
| 13 | +| Test Backend| NdArray | (CPU, deterministic) | |
| 14 | +| Testing | cargo test + proptest + criterion | — | |
| 15 | +| Linting | clippy + rustfmt | — | |
| 16 | +| CI | GitHub Actions | test, clippy, fmt, build-examples | |
| 17 | + |
| 18 | +## Project Structure |
| 19 | + |
| 20 | +``` |
| 21 | +src/ |
| 22 | +├── lib.rs # Public API re-exports + module declarations |
| 23 | +├── config.rs # AttnResConfig — validated builder pattern |
| 24 | +├── attn_res_op.rs # Core AttnRes operation (depth-wise softmax attention) |
| 25 | +├── block_state.rs # BlockState — cumulative block representation tracking |
| 26 | +├── layer.rs # AttnResLayer — transformer layer with dual AttnRes |
| 27 | +├── model.rs # AttnResTransformer — full model (embed → layers → LM head) |
| 28 | +├── rms_norm.rs # RMSNorm implementation |
| 29 | +├── two_phase.rs # Two-phase inference optimization |
| 30 | +├── attention.rs # Multi-head self-attention |
| 31 | +├── feed_forward.rs # SwiGLU-style MLP |
| 32 | +└── utils.rs # Causal mask generation helpers |
| 33 | +
|
| 34 | +tests/ |
| 35 | +├── unit_tests.rs # Core algorithm correctness tests |
| 36 | +├── differential_tests.rs # PyTorch reference comparison tests |
| 37 | +├── property_tests.rs # proptest property-based tests |
| 38 | +└── integration_tests.rs # Full model training loop tests |
| 39 | +
|
| 40 | +examples/ |
| 41 | +├── train_tiny.rs # Train a small model on synthetic data |
| 42 | +├── compare_residuals.rs # Compare AttnRes vs standard residuals |
| 43 | +└── visualize_weights.rs # Visualize depth attention patterns |
| 44 | +
|
| 45 | +benches/ |
| 46 | +└── attn_res_benchmark.rs # Criterion benchmarks |
| 47 | +
|
| 48 | +fixtures/ # Reference outputs from PyTorch |
| 49 | +├── attn_res_forward.json |
| 50 | +└── block_state_tracking.json |
| 51 | +``` |
| 52 | + |
| 53 | +## Commands |
| 54 | + |
| 55 | +```bash |
| 56 | +cargo build # Build the project |
| 57 | +cargo test --all-features # Run all 57 tests |
| 58 | +cargo test test_name # Run specific test |
| 59 | +cargo clippy -- -D warnings # Lint (warnings = errors) |
| 60 | +cargo fmt # Format code |
| 61 | +cargo fmt -- --check # Check formatting without modifying |
| 62 | +cargo bench # Run Criterion benchmarks |
| 63 | +cargo run --example train_tiny # Train example |
| 64 | +cargo run --example compare_residuals # Comparison example |
| 65 | +cargo run --example visualize_weights # Visualization example |
| 66 | +``` |
| 67 | + |
| 68 | +## Architecture Essentials |
| 69 | + |
| 70 | +### Core Algorithm (AttnRes) |
| 71 | + |
| 72 | +Standard residual: `x_{l+1} = x_l + f_l(x_l)` (fixed unit weights) |
| 73 | + |
| 74 | +AttnRes: `x_{l+1} = Σ α_i · v_i` where α = softmax(w_l · RMSNorm(V)) over depth dimension |
| 75 | + |
| 76 | +Key invariants: |
| 77 | +1. **Zero-init pseudo-queries** → starts as uniform averaging (standard residual behavior) |
| 78 | +2. **Two AttnRes per transformer layer** — one before self-attention, one before MLP |
| 79 | +3. **Softmax over depth** (block/layer dimension), NOT over sequence tokens |
| 80 | +4. **RMSNorm on keys** to prevent magnitude domination |
| 81 | +5. **Block boundaries** at every `block_size/2` sublayers |
| 82 | + |
| 83 | +### Data Flow |
| 84 | + |
| 85 | +``` |
| 86 | +Input IDs → Embedding → [AttnResLayer × N] → RMSNorm → LM Head → Logits |
| 87 | + ↓ |
| 88 | + AttnResOp(pre-attn) → RMSNorm → MultiHeadAttention |
| 89 | + AttnResOp(pre-mlp) → RMSNorm → FeedForward |
| 90 | +``` |
| 91 | + |
| 92 | +### Configuration |
| 93 | + |
| 94 | +`AttnResConfig::new(d_model, num_layers, num_blocks)` where: |
| 95 | +- `d_model`: Hidden dimension |
| 96 | +- `num_layers`: Number of **sublayers** (transformer layers × 2) |
| 97 | +- `num_blocks`: Number of blocks for Block AttnRes (set = num_layers for Full AttnRes) |
| 98 | + |
| 99 | +## Boundaries |
| 100 | + |
| 101 | +### Read-Only (never modify) |
| 102 | +- `spec.md`, `paper.md`, `research_report.md`, `implementation_plan.md`, `LICENSE` |
| 103 | + |
| 104 | +### Gated (requires approval) |
| 105 | +- `Cargo.toml` (dependency changes) |
| 106 | +- `.github/workflows/` (CI changes) |
| 107 | +- `cargo publish` |
| 108 | + |
| 109 | +## Source of Truth |
| 110 | + |
| 111 | +`spec.md` is the authoritative specification. All algorithm implementations must match the pseudocode and equations defined there. |
| 112 | + |
| 113 | +## Known Gaps |
| 114 | + |
| 115 | +- No safetensors serialization |
| 116 | +- Two-phase inference not integrated into main forward path |
| 117 | +- GPU backends (wgpu, CUDA, Metal) untested |
| 118 | +- No distributed training support |
0 commit comments