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as the **fast half** of the
|
| 4 |
+
SOVEREIGN system. Same contract as the v3 teacher; distilled from it;
|
| 5 |
+
usable as a rollout engine and a CPU sanity baseline.
|
| 6 |
+
|
| 7 |
+
---
|
| 8 |
+
|
| 9 |
+
## 1. Why this exists
|
| 10 |
+
|
| 11 |
+
| Role | Detail |
|
| 12 |
+
|---|---|
|
| 13 |
+
| Fast rollout engine | ~90Γ cheaper than the 290M teacher β the PPO/league/wake-sleep loop gets ~10Γ more sim experience per GPU-hour (the bottleneck of the whole project is rollout throughput, not model capacity). |
|
| 14 |
+
| Distillation student | The teacher's wake-sleep loop (distill.py) distills into this. Contract is identical, so KL on the (B,3,1024) heatmaps is a direct op. |
|
| 15 |
+
| CPU sanity baseline | β€5M params, small activations β smoke-trainable on Kaggle CPU without touching the GPU quota. |
|
| 16 |
+
|
| 17 |
+
## 2. Architecture
|
| 18 |
+
|
| 19 |
+
| Module | Shapes | ~Params |
|
| 20 |
+
|---|---|---|
|
| 21 |
+
| stem + coords | 5β16 @128Β² | 1K |
|
| 22 |
+
| e1βe4 (spine) | 16/32/64/128/192 @64Β²..8Β², 1β2 ResBlocks | ~1.7M |
|
| 23 |
+
| FiLM | 9β192 scale/shift on the bottleneck | 3K |
|
| 24 |
+
| decoder d0βd4 | 192β32 @16Β²..256Β² with e3/e2/e1 skips | ~810K |
|
| 25 |
+
| heat mixer + SHARED tower | 32β48β48β48β3 @256Β² | ~150K (one tower emits all 3 kinds β 3Γ cheaper than per-kind towers) |
|
| 26 |
+
| gate / seg / next_seg / threat / expand | 32β{3,4,4,1,1} @64Β² | 1K |
|
| 27 |
+
| pooled-MLP global (cortex replacement) | 201β512 β kind 3 / value 1 / win 2 / econ 9 | ~640K |
|
| 28 |
+
| pct Beta | (32+9+3)β128β128β2 | ~22K |
|
| 29 |
+
| cell-value | 64β32β1 @32Β² | 2K |
|
| 30 |
+
| GRU memory | 201β96β192 | ~105K |
|
| 31 |
+
| **TOTAL** | printed by the smoke test | **β3.3M β under the 5M cap** |
|
| 32 |
+
|
| 33 |
+
Kept from v3 (scaled down): return-conditioning (rtg through FiLM + all
|
| 34 |
+
heads), gate head (mask-free legality), intent mirrors (threat/expand),
|
| 35 |
+
next_seg dynamics aux, win head, econ forecaster, Beta pct, cell-value.
|
| 36 |
+
|
| 37 |
+
Dropped, honestly: the 77M transformer cortex β pooled-MLP global head;
|
| 38 |
+
the MCTS planning block β OUT; SE channel attention β OUT; the 3072-wide
|
| 39 |
+
MLPs β 512/256. Nano does not search β the teacher does, and the
|
| 40 |
+
visit-target distillation happens on the teacher's side.
|
| 41 |
+
|
| 42 |
+
## 3. Memory & speed (measured math, not vibes)
|
| 43 |
+
|
| 44 |
+
Corrected FLOP derivation (hook-counted by `opt.py count_flops`; the
|
| 45 |
+
first draft overestimated because the per-kind towers ran 3Γ the 256Β²
|
| 46 |
+
convs β that waste was removed in the speed pass):
|
| 47 |
+
|
| 48 |
+
| Block | MACs/frame |
|
| 49 |
+
|---|---|
|
| 50 |
+
| spine (stem + e1βe4) | ~0.65 G |
|
| 51 |
+
| decoder (d0βd4) | ~1.7 G |
|
| 52 |
+
| heat (mixer 48ch + SHARED tower @256Β²) | ~3.6 G (was ~10.4 G with per-kind towers) |
|
| 53 |
+
| **Total** | **~6 G MACs β 12 GFLOPs** |
|
| 54 |
+
| with `heat_res=64` | heat ~0.34 G β total β 2.7 G MACs β 5.4 GFLOPs |
|
| 55 |
+
|
| 56 |
+
| Item | Estimate (honest Β±2Γ band) |
|
| 57 |
+
|---|---|
|
| 58 |
+
| Weights (fp32) | ~13 MB |
|
| 59 |
+
| Grads + Adam | ~40 MB |
|
| 60 |
+
| Activations (bs 16, 256Β², fp32) | ~300β500 MB peak |
|
| 61 |
+
| T4 fp16 forward | ~3β8 ms Β· act ~5β12 ms |
|
| 62 |
+
| CPU (8-core) forward | ~60β200 ms Β· with heat_res=64 ~30β90 ms |
|
| 63 |
+
| CPU + quantize_cpu.py | Linear/GRU int8 only β convs stay fp32 (stated) |
|
| 64 |
+
|
| 65 |
+
`bench.py` turns every one of these numbers into a measurement β run it
|
| 66 |
+
on the pipeline machine and trust its output over this table. Against
|
| 67 |
+
the 560 ms sim tick, T4 rollouts are ~50β100Γ cheaper than the teacher's
|
| 68 |
+
forward+MCTS; CPU rollouts are viable with `heat_res`.
|
| 69 |
+
|
| 70 |
+
## 3b. The speed stack (opt.py / bench.py / quantize_cpu.py)
|
| 71 |
+
|
| 72 |
+
| Lever | How | Honest expected gain | Caveat |
|
| 73 |
+
|---|---|---|---|
|
| 74 |
+
| Shared heat tower (already in the model) | one tower emits 3 kind channels | ~3Γ on the heat block (was 65% of all FLOPs) | kept full-res 3Γ3 cross-talk |
|
| 75 |
+
| `opt.fold_bn` | BN folded into convs at eval | ~10β20% + less sync | keep unfolded weights for training; smoke asserts exactness |
|
| 76 |
+
| `torch.compile` | full-graph compile | 1.3β2Γ | one-time compile cost per shape |
|
| 77 |
+
| fp16 autocast | T4-native half precision | 1.5β2Γ on T4 | rollouts only β training uses fp32 master |
|
| 78 |
+
| `heat_res=64` | tower at 64Β², logits upsampled | ~2Γ total | blunts 256Β² heat detail β ablate before trusting |
|
| 79 |
+
| channels_last (NHWC) | `opt.to_channels_last` + auto NCHW conversion at interpolate/grid_sample | ~5β15% | safe by construction; bench verifies |
|
| 80 |
+
| `quantize_cpu.py` | dynamic int8 Linear/GRU | partial CPU win | convs stay fp32 β stated |
|
| 81 |
+
| `bench.py` | measures all of the above | β | claims become measurements |
|
| 82 |
+
|
| 83 |
+
## 4. The real shard schema (why v3 crashed, fixed in prep_shard)
|
| 84 |
+
|
| 85 |
+
```
|
| 86 |
+
rgb (354,256,256,3) uint8 frames every 2 ticks
|
| 87 |
+
lab (354,256,256) uint8 0 water / 1 neutral / 2 me / 3 enemy
|
| 88 |
+
nums (354,8) float32 [bal_log, me_frac, red, e1, e2, tick_frac, income, kills]
|
| 89 |
+
kind (354,) int64 0 expand / 1 attack / 2 bank
|
| 90 |
+
cell (354,) int64 32x32 grid idx 0..1023
|
| 91 |
+
pct (354,) float32 troops fraction
|
| 92 |
+
logp (354,) float32 TRUE behavior logprob (old shards -2.0)
|
| 93 |
+
reward (708,) float32 PER TICK (2Γ frames), -1.3 death terminal
|
| 94 |
+
alive (4,) int64 PER EPISODE
|
| 95 |
+
lens (4,) int64 PER EPISODE TICK counts (frames = lens//2)
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
`prep_shard()` reads exactly this: per-frame reward = sum of the frame's
|
| 99 |
+
2 ticks, per-episode returns over `lens//2` frames, episode-survival
|
| 100 |
+
broadcast for win labels and weights. The smoke asserts
|
| 101 |
+
`len(reward) == lens.sum()` and `sum(lens//2) == len(rgb)`.
|
| 102 |
+
|
| 103 |
+
## 5. The six v3 review bugs β all fixed, all asserted
|
| 104 |
+
|
| 105 |
+
| # | Bug | Fix in nano |
|
| 106 |
+
|---|---|---|
|
| 107 |
+
| 1 | Adapter assumed alive/reward/lens are per-frame | `prep_shard` reads the real schema (above); smoke asserts it |
|
| 108 |
+
| 2 | `CE((B,3,gΒ²), (B,))` crashes on the cell head | take the kind row first: `cell_logits[arange(B), kind]` |
|
| 109 |
+
| 3 | seg/next_seg CE (B,H,W) silently broadcasts | `.mean(dim=(1,2))` per sample |
|
| 110 |
+
| 4 | act() logprob/entropy broadcast to (B,B) | Beta params squeezed to (B,); **shape-asserted in act()** |
|
| 111 |
+
| 5 | `F.categorical_entropy` does not exist | manual `-(pΒ·log p).sum()` (`cat_entropy`) |
|
| 112 |
+
| 6 | `torch.from_numpy` on 0-dim scalars | all prep outputs are β₯1-dim tensors |
|
| 113 |
+
|
| 114 |
+
The smoke test runs forward + act(B=4) + full stage-A loss + backward on
|
| 115 |
+
the **real** shard (via `$HF_TOKEN`) and prints which path it ran. It
|
| 116 |
+
falls back to a schema-EXACT synthetic replica only when the shard cannot
|
| 117 |
+
be fetched, and says so loudly.
|
| 118 |
+
|
| 119 |
+
## 6. Honest limitations
|
| 120 |
+
|
| 121 |
+
1. **No transformer cortex.** Pooled-MLP global heads cannot do the
|
| 122 |
+
multi-blob relational reasoning the v3 cortex does. Nano inherits
|
| 123 |
+
strategy through distillation, not by construction.
|
| 124 |
+
2. **No search.** Nano is a reactive policy; the planning lives in the
|
| 125 |
+
teacher (search.py). Do not expect teacher-grade decision quality
|
| 126 |
+
from nano alone.
|
| 127 |
+
3. **Sub-tick reward alignment is approximate.** Reward is per-tick and
|
| 128 |
+
frames are every 2 ticks; prep sums each frame's 2 ticks and
|
| 129 |
+
accumulates returns at the frame rate (Ξ³=0.995 per frame). This is
|
| 130 |
+
the honest approximation β an exact sub-tick alignment needs the
|
| 131 |
+
pipeline's tick-level recorder.
|
| 132 |
+
4. **Single-frame input.** No frame stacking (contract). Temporal signal
|
| 133 |
+
comes from the gate and the tiny GRU only.
|
| 134 |
+
5. **5M cap forces choices.** Every head is narrower than ideal; the
|
| 135 |
+
width is justified by the rollout-engine role, not by accuracy.
|
| 136 |
+
6. **CPU smoke at bs 16 only.** GPU-scale training of nano is untested
|
| 137 |
+
here; the recipe (AdamW 3e-4, same stage-A loss) is documented, not
|
| 138 |
+
benchmarked.
|
| 139 |
+
|
| 140 |
+
## 7. Integration notes
|
| 141 |
+
|
| 142 |
+
- Import: `from model_nano import Nano, NanoConfig, stage_a_loss`.
|
| 143 |
+
- distill.py: replace `small_config()` with `NanoConfig` and import Nano.
|
| 144 |
+
- Rollouts: `a = net.act(rgb, nums, rtg=rtg)` per tick; carry `a["state"]`
|
| 145 |
+
through each episode when `use_mem=True`.
|
| 146 |
+
- CPU sanity: bs 8, lr 3e-4, 100 steps on the prep'd real shard, watch
|
| 147 |
+
`terms` fall.
|