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|
|
|
|
|
|
|
|
|
|
|
|
|
| 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`).
|