amer224 commited on
Commit
0563f73
Β·
verified Β·
1 Parent(s): 9e8f55c

Add brains/sovereign-v3/ARCHITECTURE.md

Browse files
Files changed (1) hide show
  1. brains/sovereign-v3/ARCHITECTURE.md +161 -0
brains/sovereign-v3/ARCHITECTURE.md ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SOVEREIGN v3 β€” Architecture (~272M dense, return-conditioned)
2
+
3
+ A large dense spatial policy for **territorial.io, last-survivor
4
+ elimination**, sized per mandate (**β‰₯200M, <400M**), trained on **2Γ— T4**
5
+ with the shared PPO pipeline. v3 is not a width-bump over v2: it adds four
6
+ mechanisms that change *what the network learns*, each grounded in
7
+ literature and trained on labels the shards already contain.
8
+
9
+ ## 1. Module budget
10
+
11
+ | Module | Params | What it is |
12
+ |---|---|---|
13
+ | Vision spine | ~70M | stem + 5 down stages, 96β†’832 ch @ 64Β²..4Β², ResBlock+SE |
14
+ | Strategic cortex | ~77M | multi-scale transformer: 1 GLOBAL + 16 coarse (@4Β²) + 64 fine (@8Β²) tokens, 6 layers, d=1024, 16 heads, FFN 4096 |
15
+ | Tactical decoder | ~66M | 6 up stages with skips, 1024 ch @8Β² β†’ 64 ch @256Β² |
16
+ | Command heads | ~55M | kind/value/win/econ MLPs (3072-wide), heat towers, pct-Beta, gate/seg/dynamics aux |
17
+ | Memory core | ~5M | GRU 768 over cortex pool + context |
18
+ | **Total** | **~272M** | printed exactly by `python model.py` (param_breakdown) |
19
+
20
+ ## 2. The v3 mechanisms (the "10x" substance)
21
+
22
+ ### 2.1 Return-conditioned policy (RPO)
23
+ Every head is conditioned on **remaining return-to-go `rtg`** (z-scored MC
24
+ return; concatenated to `nums`, FiLM + all heads). Stage A teaches
25
+ `p(a | s, rtg)`; PPO computes advantages against `V(s, rtg)` with the
26
+ *actual* rtg of the rollout; **at play time the pipeline sets an
27
+ aspiration rtg** (e.g. +1.5 z) β€” the policy then behaves in its
28
+ "must-win" regime. This is return-conditioned/robust policy optimization
29
+ (RPO, ICLR 2023), which beat PPO consistently on continuous control.
30
+ Default `rtg=0` keeps the contract byte-identical.
31
+
32
+ ### 2.2 Opponent-intent mirror heads (invented for this dataset)
33
+ Shards are consecutive frames, so `lab[t+1]` is free. Two new heads:
34
+ - `threat` (64Γ—64): pixels that **become enemy** next frame β†’ the model
35
+ learns *where opponents are about to strike* β€” implicit opponent
36
+ modeling from data (predict-the-next-frame class of objectives:
37
+ TWISTER/AC-CPC, MuDreamer, Dreamer line).
38
+ - `expand` (64Γ—64): pixels that **become mine** next frame β†’ where my own
39
+ frontier propagates, i.e. the reward gradient of a click.
40
+
41
+ These are BCE-supervised; no extra labels were ever recorded.
42
+
43
+ ### 2.3 One-step spatial dynamics (`next_seg`)
44
+ A 4-class segmentation of `lab[t+1]` @64Γ—64, CE-supervised. This is a
45
+ partial **world model inside the policy** β€” the seed of a future MCTS
46
+ loop (the explicit roadmap once a β‰₯25% win-rate baseline exists).
47
+
48
+ ### 2.4 Win-probability value
49
+ A second value head outputs `P(alive at episode end)` (CE vs episode
50
+ outcome). GAE uses the RTG value for learning; win-prob drives eval,
51
+ late-game shaping, and endgame decision thresholds (documented in
52
+ train_notes).
53
+
54
+ ### 2.5 Memory core (optional, contract-safe)
55
+ A single-layer GRU (768) over `[cortex pool, ctx]`, carried through
56
+ `forward(state=...)` / `act(state=...)`. Default stateless (fairness);
57
+ train_notes gives the truncated-BPTT sequence recipe + env code sketch.
58
+
59
+ ### 2.6 Multi-scale attention cortex
60
+ 16 coarse tokens see the whole map; 64 fine tokens see 8Γ—8 patches β€”
61
+ global strategy and local structure in one attention pass over 81 tokens
62
+ (compute-trivial; the FFNs carry the 77M of capacity).
63
+
64
+ ## 3. Memory & speed (2Γ— T4, fp16)
65
+
66
+ | Item | Estimate |
67
+ |---|---|
68
+ | Weights (fp32 master) | 1.09 GB |
69
+ | Gradients | 1.09 GB |
70
+ | Adam m+v | 2.18 GB |
71
+ | Activations (fp16, bs 16/GPU, 256Β²) | ~4 GB (grad checkpoint flag halves it) |
72
+ | CUDA + overhead | ~1 GB |
73
+ | **Per-GPU total** | **β‰ˆ9.4 GB of 16 GB** βœ“ |
74
+
75
+ Play speed: ~150–200 GFLOPs/frame β†’ **<30 ms on a T4 in fp16** vs 560 ms
76
+ sim ticks. `grad_ckpt=True` checkpoints the 4 heaviest stages;
77
+ `heat_hidden`/`head_hidden`/`enc_ch` remain one-line trims.
78
+
79
+ ## 4. Module walkthrough
80
+
81
+ ```
82
+ rgb (B,3,256,256) ─ stem(5β†’96,/2) ─ e1 96@64Β² ─ e2 160@32Β² ─ e3 288@16Β²
83
+ β–² x,y coords β”‚ β”‚
84
+ β”‚ β”‚
85
+ β”‚ β–Ό
86
+ nums+rtg (B,9) ─ FiLM ─► e5 832@4Β² ◄── e4 512@8Β² β”€β”€β”€β”€β”˜
87
+ β”‚ β”‚
88
+ β”‚ └─ Cortex: [GLOBAL]+coarse(16)+fine(64)
89
+ β”‚ 6Γ— attention d=1024 β†’ g (B,1024)
90
+ β”‚ └─ GRU(768) memory (optional) ─┐
91
+ β–Ό β–Ό
92
+ dec0 1024@8Β² ← e4 ─ dec1 512@16Β² ← e3 ─ dec2 288@32Β² ← e2
93
+ ─ dec3 160@64Β² ← e1 ─ dec4 96@128Β² ─ dec5 64@256Β²
94
+ β”‚ β”‚
95
+ gate 3@64Β² (log-mask) heat mixer 64β†’128β†’128 β†’ per-kind towers
96
+ seg 4@64Β² (aux) (3Γ— (128β†’192β†’192β†’1)) β†’ heat_raw @256Β²
97
+ next_seg 4@64Β² (dyn) + gate logits β†’ heat
98
+ threat 1@64Β² (intent) pct Beta @clicked px (64+9+3 β†’ 1024 β†’ Ξ±,Ξ²)
99
+ expand 1@64Β² (intent) kind/value/win/econ ← MLP(1024+9 β†’ 3072)
100
+ cell-value 1@32Β² (aux)
101
+ ```
102
+
103
+ | Block | Shapes | Role |
104
+ |---|---|---|
105
+ | stem + coords | 5β†’96 @128Β² | demux + static-map grounding |
106
+ | e1…e5 | 96/160/288/512/832 @64Β²..4Β² | hierarchical features, SE channel attention |
107
+ | FiLM | 9β†’832 | economy + aspiration conditioning |
108
+ | Cortex | 81 tokens Γ— 1024, 6 layers | global strategy, multi-scale relations |
109
+ | GRU | 1033β†’768β†’1024 | optional temporal memory |
110
+ | d…dec5 | 832β†’64 with skips | per-pixel action mass |
111
+ | heat mixer + towers | 64β†’128β†’128β†’3Γ—192β†’3 | per-kind 256Β² action logits |
112
+ | gate | 96β†’3 @64Β² β†’ log | mask-free legality |
113
+ | seg / next_seg | 96β†’4 @64Β² | perception + one-step dynamics |
114
+ | threat / expand | 96β†’1 @64Β² | opponent-intent mirrors |
115
+ | kind/value/win/econ | 1033β†’3072β†’3072β†’out | intent, V(s,rtg), P(win), next-context |
116
+ | pct | (64+9+3)β†’1024β†’1024β†’(Ξ±,Ξ²) | Beta troop-% at the clicked pixel |
117
+ | cell-value | 288β†’128β†’1 @32Β² | per-cell utility |
118
+
119
+ ## 5. Output contract (unchanged β€” drop-in)
120
+
121
+ - `forward(rgb, nums, mask=None, grid=32, rtg=None, state=None,
122
+ return_all=False)` β†’ `(click, kind_logits, pct, value)`; `click` is the
123
+ kind-marginal logsumexp pooled to `grid` (default 32Γ—32) β€” same 4-tuple
124
+ order as the rival's `TeacherV2/StudentV2`.
125
+ - `forward(..., return_all=True)` β†’ dict incl. per-kind `heat`, `gate`,
126
+ `seg`, `next_seg`, `threat`, `expand`, `econ`, `win_prob`,
127
+ `cell_value`, `pct_params`, `mem_state`.
128
+ - `act(...)` β†’ `{kind, cell, yx, pct, logprob, value, win_prob, entropy,
129
+ state}` with the correct `p(kind)Β·p(cell|kind)Β·p(pct|kind,cell)`.
130
+ - `lab` is supervision-only (gate/seg/dynamics) during pretraining.
131
+
132
+ ## 6. Deliberate limitations (honest)
133
+
134
+ - **Self-identity is a color prior** (sim always renders us red) β€”
135
+ documented, sim-specific.
136
+ - **Aspiration rtg is an untuned knob** at play time (ablation #1).
137
+ - **No MCTS yet**: the dynamics head is the seed; search comes after a
138
+ real baseline.
139
+ - **Capacity vs data**: 272M wants samples; Stage A has GBs, PPO is the
140
+ bottleneck β€” wake-sleep distillation is the documented relief valve.
141
+ - **GRU changes rollout code** if enabled; default off keeps the recipe
142
+ identical for the shared pipeline.
143
+
144
+ ## 7. References
145
+
146
+ 1. RPO β€” "Robust Policy Optimization in Deep RL" (ICLR 2023 rejected-sub /
147
+ openreview 9f352523), return-conditioned advantages beat PPO.
148
+ 2. TWISTER β€” "Learning Transformer-based World Models with AC-CPC" (ICLR
149
+ 2025): prediction auxiliaries improve RL representations.
150
+ 3. MuDreamer (arXiv:2405.15083), Dreamer 4 (arXiv:2509.24527): world-model
151
+ prediction objectives; scalable model-based RL.
152
+ 4. IQL (Kostrikov et al., arXiv:2110.06169): expectile value; AWAC (Nair
153
+ et al., arXiv:2006.09359): offline→online via advantage-weighted BC.
154
+ 5. "Efficient Online RL Fine-Tuning Need Not Retain Offline Data" (ICLR
155
+ 2025, arXiv:2412.07762): pretrain-init + warmup, KL-guarded online
156
+ phase, no offline replay retention.
157
+ 6. Peng et al. 2019 (AWR), Schulman et al. 2017 (PPO), Vaswani et al.
158
+ 2017 (attention), Ronneberger et al. 2015 (U-Net), Hu et al. 2018 (SE).
159
+ 7. territorial.fandom.com/wiki/Attacks; community tick/income/cap
160
+ measurements; amerameryou1-blip/bot (`economy.py`, `model_v2.py`,
161
+ `train_v2.py`, `ATTACK_META.md`).