> Agent-ready spec from Diff of the Day (2026-11-29, idea 3). Give this file to your coding agent (Claude Code, Codex, Cursor) and ask it to build the P0 requirements first.

# Spec: DeepCrossAttention on a small GPT

## Problem Statement
Google Research describes DeepCrossAttention (DCA), which replaces the fixed residual sum with learned, per-token routing over earlier layers. On LM1B and C4 it matched standard transformers in up to five times fewer training steps, and a 30-layer DCA model beat a 42-layer baseline on LM1B, while adding under 0.2% parameters. Engineers who train small models cannot easily tell whether this helps at their scale.

## Goals
- Add a DCA-style residual module to a small GPT behind one config flag.
- Train baseline and DCA models with the same data, seed set and step budget on one GPU.
- Report validation loss curves and steps to reach the baseline's final loss.
- Keep the parameter increase under 1%.

## Non-Goals
- Reproducing the paper's exact numbers: different scale and data.
- Large-scale training: one GPU, under a few hours.
- Production inference changes: research only.

## User Stories
- As an ML engineer, I want a flag that switches residual type so that the comparison is fair.
- As an engineer, I want loss curves for both runs on one chart so that I can see the gap.
- As an engineer, I want three seeds per variant so that I know the gap is not noise.
- Edge case: as an engineer, I want training to stop with a clear message if DCA weights produce NaNs, so that a broken run is not reported as a result.

## Requirements
### P0 (must have)
1. **DCA module**: each block combines earlier layer outputs with learned static weights plus an input-dependent projection.
   - [ ] With routing weights set to identity, the model's output equals the baseline's on a fixed batch.
2. **Config flag** residual: standard or dca, and a history size k.
3. **Training script** with fixed data split and step budget for both variants.
   - [ ] Two runs with the same seed and flag produce the same first 100 losses.
4. **Report**: parameter counts, final validation loss, steps to baseline loss, chart.

### P1 (nice to have)
- Three seeds per variant with mean and spread.
- Retrofit: start DCA from a trained baseline checkpoint.

### P2 (future)
- Try the block variant to cut memory.

## Success Metrics
- Leading: both variants train to the step budget without NaNs.
- Lagging: you know whether DCA reaches baseline loss in fewer steps at your scale.

## Open Questions
- Which dataset size fits one GPU in a few hours? (you)

## Timeline
One weekend.

## Agent Instructions
- **Suggested stack**: PyTorch 2.x, a nanoGPT-style codebase, matplotlib for charts.
- **Build order**: identity test for the module → config flag → training script → report.
- **Safety**: training only; cap GPU hours in the script; do not download datasets from unknown mirrors.
- **Done when**: report.md shows both curves and the steps-to-baseline number.

## Sources
- Supercharging transformer residual connections: https://research.google/blog/supercharging-transformer-residual-connections/
- nanoGPT: https://github.com/karpathy/nanoGPT
