Upload model_v3.py with huggingface_hub
Browse files- model_v3.py +98 -0
model_v3.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Spatial-only autoencoder (v3): compress only what is actually big.
|
| 2 |
+
|
| 3 |
+
Lesson from v2: squeezing tiny exact state (pairwise alliances, player
|
| 4 |
+
scalars) through the latent and reconstructing it lossily is manufactured
|
| 5 |
+
difficulty — three training runs got alliance precision to 0.55 when the
|
| 6 |
+
policy could just read the bit. The bypass split:
|
| 7 |
+
|
| 8 |
+
AE compresses (high-dimensional, spatial):
|
| 9 |
+
- tile ownership grid + terrain + fallout
|
| 10 |
+
- static structure planes (city/port/defense post/silo/SAM/factory)
|
| 11 |
+
|
| 12 |
+
Bypass straight to the policy (small, exact):
|
| 13 |
+
- pairwise diplomacy (alliances, embargoes, expiry, pending requests)
|
| 14 |
+
- per-player scalars (troops, gold, tiles, alive, ...)
|
| 15 |
+
- transient units (nukes/transports/warships) as (owner, pos, target)
|
| 16 |
+
- attack aggregates, globals, legality masks
|
| 17 |
+
|
| 18 |
+
Latent: z_grid (B, latent_c, H/16, W/16). No global vector: the policy can
|
| 19 |
+
pool spatially itself, and per-player/global state arrives via bypass.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
import torch
|
| 23 |
+
import torch.nn as nn
|
| 24 |
+
|
| 25 |
+
from ae.units import STATIC_CLASSES
|
| 26 |
+
|
| 27 |
+
MAX_SLOTS = 128
|
| 28 |
+
OWNER_EMB_DIM = 8
|
| 29 |
+
TERRAIN_CHANNELS = 3 # land, magnitude, fallout
|
| 30 |
+
NUM_STATIC = len(STATIC_CLASSES)
|
| 31 |
+
|
| 32 |
+
# BCE positive-weight multiplier per static class (× --unit-pos-weight).
|
| 33 |
+
STATIC_CLASS_WEIGHTS = [1.0, 1.0, 1.0, 4.0, 4.0, 2.0]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def conv_block(c_in: int, c_out: int, stride: int) -> nn.Sequential:
|
| 37 |
+
return nn.Sequential(
|
| 38 |
+
nn.Conv2d(c_in, c_out, kernel_size=3, stride=stride, padding=1),
|
| 39 |
+
nn.GroupNorm(8, c_out),
|
| 40 |
+
nn.SiLU(),
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def deconv_block(c_in: int, c_out: int) -> nn.Sequential:
|
| 45 |
+
return nn.Sequential(
|
| 46 |
+
nn.ConvTranspose2d(c_in, c_out, kernel_size=4, stride=2, padding=1),
|
| 47 |
+
nn.GroupNorm(8, c_out),
|
| 48 |
+
nn.SiLU(),
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class SpatialAE(nn.Module):
|
| 53 |
+
def __init__(self, latent_c: int = 64):
|
| 54 |
+
super().__init__()
|
| 55 |
+
self.latent_c = latent_c
|
| 56 |
+
self.owner_emb = nn.Embedding(MAX_SLOTS, OWNER_EMB_DIM)
|
| 57 |
+
|
| 58 |
+
self.enc_stem = nn.Sequential(
|
| 59 |
+
conv_block(OWNER_EMB_DIM + TERRAIN_CHANNELS, 32, stride=1),
|
| 60 |
+
conv_block(32, 64, stride=2),
|
| 61 |
+
conv_block(64, 96, stride=2),
|
| 62 |
+
conv_block(96, 128, stride=2),
|
| 63 |
+
conv_block(128, 128, stride=2), # -> 1/16
|
| 64 |
+
)
|
| 65 |
+
self.enc_fuse = nn.Sequential(
|
| 66 |
+
conv_block(128 + NUM_STATIC, 128, stride=1),
|
| 67 |
+
nn.Conv2d(128, latent_c, kernel_size=1),
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
self.dec_in = conv_block(latent_c, 128, stride=1)
|
| 71 |
+
self.dec_tiles = nn.Sequential(
|
| 72 |
+
deconv_block(128, 128),
|
| 73 |
+
deconv_block(128, 96),
|
| 74 |
+
deconv_block(96, 64),
|
| 75 |
+
deconv_block(64, 32),
|
| 76 |
+
nn.Conv2d(32, MAX_SLOTS, kernel_size=1),
|
| 77 |
+
)
|
| 78 |
+
# Static structure occupancy logits at 1/16 resolution.
|
| 79 |
+
self.dec_units = nn.Conv2d(128, NUM_STATIC, kernel_size=1)
|
| 80 |
+
|
| 81 |
+
def encode(
|
| 82 |
+
self,
|
| 83 |
+
owners: torch.Tensor, # (B, H, W) int64
|
| 84 |
+
terrain: torch.Tensor, # (B, 3, H, W)
|
| 85 |
+
static_planes: torch.Tensor, # (B, NUM_STATIC, H/16, W/16)
|
| 86 |
+
) -> torch.Tensor:
|
| 87 |
+
emb = self.owner_emb(owners).permute(0, 3, 1, 2)
|
| 88 |
+
g = self.enc_stem(torch.cat([emb, terrain], dim=1))
|
| 89 |
+
return self.enc_fuse(torch.cat([g, static_planes], dim=1))
|
| 90 |
+
|
| 91 |
+
def decode(self, z_grid: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
| 92 |
+
h = self.dec_in(z_grid)
|
| 93 |
+
return self.dec_tiles(h), self.dec_units(h)
|
| 94 |
+
|
| 95 |
+
def forward(self, owners, terrain, static_planes):
|
| 96 |
+
z_grid = self.encode(owners, terrain, static_planes)
|
| 97 |
+
tile_logits, unit_logits = self.decode(z_grid)
|
| 98 |
+
return tile_logits, unit_logits, z_grid
|