AIO1 / code /aio /actor.py
M3rcyzzz's picture
AIO1: inference-only release; weights unchanged
818f91f verified
Raw History Blame Contribute Delete
10.7 kB
import contextlib
import math
import torch
from torch import nn
from aio.interface import NCV, NMV, CF, MF, shape as shape_of
TOKEN = 128
MEMORY = 256
WIDTH = 512
EXPERTS = 1
DEPTH = 3
TMEM = 64
ATT = 64
NTOK = NCV + NMV
STATE = MEMORY + NTOK * TMEM
def symlog(x):
return x.sign() * torch.log1p(x.abs())
def symexp(x):
return x.sign() * torch.expm1(x.abs())
def state_size(ncv=NCV, nmv=NMV):
return MEMORY + (ncv + nmv) * TMEM
def unpack(obs):
NCV, NMV = shape_of(obs.shape[-1])
cv = obs[..., :NCV * CF].reshape(*obs.shape[:-1], NCV, CF)
mv = obs[..., NCV * CF:NCV * CF + NMV * MF].reshape(*obs.shape[:-1], NMV, MF)
link = obs[..., -NCV * NMV:].reshape(*obs.shape[:-1], NCV, NMV)
return (cv, mv, link)
def fp32_rnn():
return torch.autocast('cuda', enabled=False) if torch.is_autocast_enabled() else contextlib.nullcontext()
def _f32(h):
return None if h is None else h.float()
def pool(x, mask):
return (x * mask[..., None]).sum(-2) / mask.sum(-1, keepdim=True).clamp_min(1)
class PublicChannels(nn.Module):
def __init__(self):
super().__init__()
self.cv = nn.Sequential(nn.Linear(CF, TOKEN), nn.LayerNorm(TOKEN), nn.SiLU())
self.mv = nn.Sequential(nn.Linear(MF + 1, TOKEN), nn.LayerNorm(TOKEN), nn.SiLU())
self.join = nn.Sequential(nn.Linear(TOKEN * 3, TOKEN), nn.LayerNorm(TOKEN), nn.SiLU())
def embed(self, obs, previous_action):
cv, mv, links = unpack(obs)
cm, mm = (cv[..., -1], mv[..., -1])
ce = self.cv(symlog(cv * cm[..., None])) * cm[..., None]
me = self.mv(torch.cat((symlog(mv * mm[..., None]), previous_action[..., None] * mm[..., None]), -1))
links = links * cm[..., :, None] * mm[..., None, :]
return (ce, me, cm, mm, links)
def combine(self, ce, me, cm, mm, links):
linked = torch.einsum('...ij,...id->...jd', links, ce)
linked = linked / links.sum(-2)[..., None].clamp_min(1)
global_cv = pool(ce, cm)
joint = self.join(torch.cat((me, linked, global_cv[..., None, :].expand_as(me)), -1)) * mm[..., None]
return (joint, torch.cat((global_cv, pool(joint, mm)), -1), mm)
def forward(self, obs, previous_action):
return self.combine(*self.embed(obs, previous_action))
def split_tokens(hidden, b, NCV=NCV, NMV=NMV):
if hidden is None:
return (None, None)
hc = hidden[..., :NCV * TMEM].reshape(1, b * NCV, TMEM).contiguous()
hm = hidden[..., NCV * TMEM:].reshape(1, b * NMV, TMEM).contiguous()
return (hc, hm)
class TokenMemory(nn.Module):
def __init__(self):
super().__init__()
self.cv_gru = nn.GRU(TOKEN * 2, TMEM, batch_first=True)
self.mv_gru = nn.GRU(TOKEN * 2, TMEM, batch_first=True)
self.cv_out = nn.Linear(TMEM, TOKEN)
self.mv_out = nn.Linear(TMEM, TOKEN)
self.q = nn.Linear(TOKEN, ATT)
self.k = nn.Linear(TOKEN, ATT)
self.v = nn.Linear(TOKEN, ATT)
self.att_out = nn.Linear(ATT, TOKEN)
self.link_bias = nn.Parameter(torch.tensor(2.0))
for layer in (self.cv_out, self.mv_out, self.att_out):
nn.init.zeros_(layer.weight)
nn.init.zeros_(layer.bias)
def forward(self, ce, me, cm, mm, links, hidden=None):
b, T, NCV, NMV = (ce.shape[0], ce.shape[1], ce.shape[2], me.shape[2])
to_mv = torch.einsum('...ij,...id->...jd', links, ce) / links.sum(-2)[..., None].clamp_min(1)
to_cv = torch.einsum('...ij,...jd->...id', links, me) / links.sum(-1)[..., None].clamp_min(1)
xc = torch.cat((ce, to_cv), -1).transpose(1, 2).reshape(b * NCV, T, TOKEN * 2)
xm = torch.cat((me, to_mv), -1).transpose(1, 2).reshape(b * NMV, T, TOKEN * 2)
hc, hm = split_tokens(hidden, b, NCV, NMV)
with fp32_rnn():
oc, hc = self.cv_gru(xc.float(), _f32(hc))
om, hm = self.mv_gru(xm.float(), _f32(hm))
tc = oc.reshape(b, NCV, T, TMEM).transpose(1, 2)
tm = om.reshape(b, NMV, T, TMEM).transpose(1, 2)
ce = ce + self.cv_out(tc) * cm[..., None]
me = me + self.mv_out(tm) * mm[..., None]
score = torch.einsum('...jd,...id->...ji', self.q(me), self.k(ce)) / math.sqrt(ATT)
score = (score + self.link_bias * links.transpose(-1, -2)).masked_fill(cm[..., None, :] <= 0, -1000000000.0)
me = me + self.att_out(torch.einsum('...ji,...id->...jd', score.softmax(-1), self.v(ce))) * mm[..., None]
state = torch.cat((hc.reshape(1, b, NCV * TMEM), hm.reshape(1, b, NMV * TMEM)), -1)
return (ce, me, torch.cat((tc, tm), 2), state)
def _grown(module, keys):
keys.missing_keys[:] = [k for k in keys.missing_keys if 'tokens' not in k.split('.')]
class History(nn.Module):
def __init__(self):
super().__init__()
self.channels = PublicChannels()
self.gru = nn.GRU(TOKEN * 2, MEMORY, batch_first=True)
self.tokens = TokenMemory()
self.register_load_state_dict_post_hook(_grown)
def flatten_parameters(self):
for gru in (self.gru, self.tokens.cv_gru, self.tokens.mv_gru):
gru.flatten_parameters()
def steps(self, obs, previous, hidden=None):
hg = ht = None
if hidden is not None:
hg = hidden[..., :MEMORY].contiguous()
ht = hidden[..., MEMORY:] if hidden.shape[-1] > MEMORY else None
ce, me, cm, mm, links = self.channels.embed(obs, previous)
ce, me, tok, ht = self.tokens(ce, me, cm, mm, links, ht)
joint, x, mask = self.channels.combine(ce, me, cm, mm, links)
with fp32_rnn():
memory, hg = self.gru(x.float(), _f32(hg))
return (joint, memory, mask, tok, torch.cat((hg, ht), -1))
def forward(self, obs, previous, hidden=None):
joint, memory, mask, _, hidden = self.steps(obs, previous, hidden)
return (joint, memory, mask, hidden)
@torch.no_grad()
def prefix(self, obs, previous, lengths, host_lengths=None, init=None):
b = obs.shape[0]
NTOK = sum(shape_of(obs.shape[-1]))
if obs.shape[1] == 0:
return obs.new_zeros(1, b, MEMORY + NTOK * TMEM) if init is None else init
_, memory, _, tok, _ = self.steps(obs, previous, init)
rows = torch.arange(b, device=obs.device)
last = (lengths - 1).clamp_min(0).long()
state = torch.cat((memory[rows, last], tok[rows, last].reshape(b, NTOK * TMEM)), -1)
return (state * (lengths > 0).to(state.dtype)[:, None])[None]
prefix_steps = prefix
class Residual(nn.Module):
def __init__(self, hidden=WIDTH):
super().__init__()
self.net = nn.Sequential(nn.LayerNorm(WIDTH), nn.Linear(WIDTH, hidden), nn.SiLU(), nn.Linear(hidden, WIDTH))
def forward(self, x):
return x + self.net(x) / math.sqrt(DEPTH)
class ExpertHead(nn.Module):
def __init__(self, inputs, outputs, experts=None, hidden=WIDTH, depth=DEPTH):
super().__init__()
self.hidden, self.depth = (hidden, depth)
self.project = nn.Linear(inputs, WIDTH)
self.gate, self.experts = self.stacks(EXPERTS if experts is None else experts, hidden, depth)
self.norm = nn.LayerNorm(WIDTH)
self.out = nn.Linear(WIDTH, outputs)
nn.init.normal_(self.out.weight, std=0.001)
nn.init.zeros_(self.out.bias)
@staticmethod
def stacks(experts, hidden=WIDTH, depth=DEPTH):
gate = nn.Linear(WIDTH, experts) if experts > 1 else None
return (gate, nn.ModuleList([nn.Sequential(*[Residual(hidden) for _ in range(depth)]) for _ in range(experts)]))
def keep(self, k):
self.experts = nn.ModuleList([self.experts[k]])
self.gate = None
return self
def _load_from_state_dict(self, state_dict, prefix, *args):
n = len({k[len(prefix):].split('.')[1] for k in state_dict if k.startswith(prefix + 'experts.')})
if n and n != len(self.experts):
gate, experts = self.stacks(n, self.hidden, self.depth)
like = self.project.weight
self.experts = experts.to(like)
self.gate = None if gate is None else gate.to(like)
self._grow(state_dict, prefix)
super()._load_from_state_dict(state_dict, prefix, *args)
@torch.no_grad()
def _grow(self, state_dict, prefix):
own = self.state_dict()
blocks = {tuple(k[len(prefix):].split('.')[:3]) for k in state_dict if k.startswith(prefix + 'experts.')}
if not blocks:
return
for k, v in own.items():
parts = k.split('.')
if parts[0] != 'experts':
continue
key = prefix + k
last = parts[-2] == '3'
if key not in state_dict:
if tuple(parts[:3]) in blocks:
continue
state_dict[key] = torch.zeros_like(v) if last else v.clone()
continue
old = state_dict[key]
if old.shape == v.shape:
continue
assert old.dim() == v.dim() and all((o <= n for o, n in zip(old.shape, v.shape))), f'{key}: {tuple(old.shape)} does not grow into {tuple(v.shape)}'
new = torch.zeros_like(v) if last else v.clone()
new[tuple((slice(0, o) for o in old.shape))] = old.to(new)
state_dict[key] = new
def forward(self, x):
x = self.project(x)
if self.gate is None:
return (self.out(self.norm(self.experts[0](x))), x.new_ones(*x.shape[:-1], 1))
gate = self.gate(x).softmax(-1)
features = torch.stack([expert(x) for expert in self.experts], -2)
return (self.out(self.norm((features * gate[..., None]).sum(-2))), gate)
class Actor(nn.Module):
def __init__(self):
super().__init__()
self.history = History()
self.head = ExpertHead(TOKEN + MEMORY, 2)
with torch.no_grad():
self.head.out.bias[1] = -1.0
def features(self, batch):
h = self.history.prefix(batch['prefix_obs'], batch['prefix_previous'], batch['prefix_lengths'])
return self.history(batch['obs'], batch['previous'], h)
@staticmethod
def head_input(joint, memory):
return torch.cat((joint, memory[..., None, :].expand(*joint.shape[:-1], MEMORY)), -1)
def distribution(self, joint, memory, mask):
output, gates = self.head(self.head_input(joint, memory))
output = output.float()
return (output[..., 0] * mask, output[..., 1].clamp(-5.0, 1.0), gates)
def forward(self, obs, previous, hidden=None):
joint, memory, mask, hidden = self.history(obs, previous, hidden)
mean, logstd, gates = self.distribution(joint, memory, mask)
return (mean, logstd, mask, hidden, gates)