Skip to content

Commit ec7c830

Browse files
committed
feat(web): add interactive web demo with WASM + Vite
Full end-to-end browser demo of the Attention Residuals algorithm: - Pure Rust WASM crate (web-demo/crate/) faithfully reimplements the core AttnRes operation (RMSNorm, depth-wise softmax, weighted combination) mirroring src/attn_res_op.rs and src/layer.rs exactly - TypeScript frontend (Vite + Canvas 2D) with academic-grade design: - Section 1: Problem statement (information dilution in standard residuals) - Section 2: Algorithm walkthrough (5-step AttnRes with equations) - Section 3: Interactive demo (configurable model, live heatmaps) - Section 4: Training simulation (loss curve, weight evolution, norm tracking) - Section 5: Standard vs AttnRes comparison - wasm-pack build (--target web), zero external WASM dependencies - Updated CLAUDE.md, AGENTS.md, README.md with web-demo docs and commands - Updated .gitignore for web-demo build artifacts https://claude.ai/code/session_01SEXHievpnMwNq3q2kz2zBB
1 parent 505cc62 commit ec7c830

16 files changed

Lines changed: 2700 additions & 2 deletions

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ Thumbs.db
3636

3737
# Criterion benchmark output
3838
target/criterion/
39+
40+
# Web demo build artifacts
41+
web-demo/node_modules/
42+
web-demo/dist/
43+
web-demo/crate/target/
44+
web-demo/crate/pkg/

AGENTS.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ benches/
4949
fixtures/ # Reference outputs from PyTorch
5050
├── attn_res_forward.json
5151
└── block_state_tracking.json
52+
53+
web-demo/ # Interactive web demo (WASM + Vite)
54+
├── crate/ # Rust WASM crate (pure-Rust AttnRes reimplementation)
55+
│ ├── Cargo.toml
56+
│ └── src/lib.rs # wasm-bindgen exports: AttnResEngine
57+
├── src/ # TypeScript frontend
58+
│ ├── main.ts # App entry point
59+
│ ├── style.css # Academic-grade styling
60+
│ ├── viz.ts # Canvas 2D heatmaps, charts
61+
│ └── diagrams.ts # Static architectural diagrams
62+
├── index.html # Single-page app
63+
├── package.json # Vite + TypeScript
64+
└── vite.config.ts # Build config
5265
```
5366

5467
## Commands
@@ -64,6 +77,11 @@ cargo bench # Run Criterion benchmarks
6477
cargo run --example train_tiny # Train example
6578
cargo run --example compare_residuals # Comparison example
6679
cargo run --example visualize_weights # Visualization example
80+
81+
# Web demo
82+
cd web-demo && npm run build:wasm # Build WASM crate
83+
cd web-demo && npm run dev # Start Vite dev server
84+
cd web-demo && npm run build # Production build (WASM + Vite)
6785
```
6886

6987
## Architecture Essentials
@@ -111,6 +129,16 @@ Input IDs → Embedding → [AttnResLayer × N] → RMSNorm → LM Head → Logi
111129

112130
`spec.md` is the authoritative specification. All algorithm implementations must match the pseudocode and equations defined there.
113131

132+
## Web Demo
133+
134+
The `web-demo/` directory contains a fully interactive browser-based demo. The WASM crate (`web-demo/crate/`) is a pure-Rust reimplementation of the core AttnRes algorithm (no burn dependency for WASM portability), faithfully mirroring `src/attn_res_op.rs`. It exposes:
135+
136+
- `AttnResEngine` — model creation, forward pass, training simulation
137+
- `compute_attn_res()` — interactive core operation with custom pseudo-queries
138+
- `train_step()` — simulated training showing depth attention pattern emergence
139+
140+
Frontend: Vite + TypeScript with Canvas 2D visualizations (heatmaps, bar charts, loss curves). Academic design with full algorithm explanation.
141+
114142
## Known Gaps
115143

116144
- No PyTorch checkpoint loading (safetensors format)

CLAUDE.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,20 @@ attnres-rs/
6363
│ ├── train_tiny.rs # Training example [agent: CREATE/MODIFY]
6464
│ ├── compare_residuals.rs # Standard vs AttnRes [agent: CREATE/MODIFY]
6565
│ └── visualize_weights.rs # Depth attention visualization [agent: CREATE/MODIFY]
66-
└── benches/
67-
└── attn_res_benchmark.rs # Criterion benchmarks [agent: CREATE/MODIFY]
66+
├── benches/
67+
│ └── attn_res_benchmark.rs # Criterion benchmarks [agent: CREATE/MODIFY]
68+
└── web-demo/ # Interactive browser demo [agent: CREATE/MODIFY]
69+
├── crate/ # Pure-Rust WASM crate (AttnRes reimplementation)
70+
│ ├── Cargo.toml
71+
│ └── src/lib.rs # wasm-bindgen exports
72+
├── src/ # TypeScript frontend
73+
│ ├── main.ts # App entry + controls
74+
│ ├── style.css # Academic-grade CSS
75+
│ ├── viz.ts # Canvas heatmaps/charts
76+
│ └── diagrams.ts # Static diagrams
77+
├── index.html # SPA entry point
78+
├── package.json # npm scripts (build:wasm, dev, build)
79+
└── vite.config.ts # Vite build config
6880
```
6981
</structure>
7082

@@ -81,6 +93,9 @@ attnres-rs/
8193
| Bench | `cargo bench` | Criterion benchmarks |
8294
| Run example | `cargo run --example train_tiny` | CPU (ndarray) by default |
8395
| Publish | `cargo publish` | ⚠ REQUIRES APPROVAL |
96+
| Web demo (WASM) | `cd web-demo && npm run build:wasm` | Requires wasm-pack + wasm32 target |
97+
| Web demo (dev) | `cd web-demo && npm run dev` | Vite dev server at localhost:5173 |
98+
| Web demo (build) | `cd web-demo && npm run build` | Production build (WASM + Vite) |
8499
</commands>
85100

86101
<conventions>

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ AttnResTransformer
6363

6464
**Note on `num_layers`**: This parameter counts *sublayers* (each transformer layer = 2 sublayers: attention + MLP). So `num_layers=8` means 4 transformer layers.
6565

66+
## Web Demo
67+
68+
An interactive browser-based demo runs the core AttnRes algorithm via Rust compiled to WASM:
69+
70+
```bash
71+
cd web-demo
72+
npm install
73+
npm run build:wasm # Compile Rust → WASM (requires wasm-pack)
74+
npm run dev # Start dev server at localhost:5173
75+
```
76+
77+
Features: configurable model parameters, live depth attention heatmaps, training simulation with loss curves, standard vs AttnRes comparison. No GPU required — runs entirely in the browser.
78+
6679
## Examples
6780

6881
```bash

web-demo/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
dist/
3+
crate/target/
4+
crate/pkg/

web-demo/crate/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "attnres-wasm"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "WASM bindings for the attnres-rs web demo — faithful reimplementation of the core Attention Residuals algorithm"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["cdylib"]
10+
11+
[dependencies]
12+
wasm-bindgen = "0.2"
13+
serde = { version = "1", features = ["derive"] }
14+
serde_json = "1"
15+
serde-wasm-bindgen = "0.6"
16+
js-sys = "0.3"
17+
console_error_panic_hook = "0.1"
18+
19+
[package.metadata.wasm-pack.profile.release]
20+
wasm-opt = false
21+
22+
[profile.release]
23+
opt-level = "s"
24+
lto = true

0 commit comments

Comments
 (0)