File size: 3,392 Bytes
fd5366a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | # Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""p_phi(s_{t+1} | do(a_t), s_t): a neural state-space model over latent state.
This is not next-token prediction: the input is a latent state plus an action
embedding, and the outputs are per-modality state deltas and action outcomes.
The do() operator appears as an explicit action input whose parents are severed
by construction: the model never sees the correlational context that produced
the action, only (s_t, a_t).
"""
from __future__ import annotations
import torch
import torch.nn as nn
class CausalWorldModel(nn.Module):
def __init__(
self,
state_dim: int,
action_dim: int = 16,
hidden: int = 192,
n_z: int = 64,
n_b: int = 12,
n_e: int = 9,
n_img: int = 8,
n_tool: int = 12,
dropout: float = 0.1,
):
super().__init__()
self.n_z, self.n_b, self.n_e, self.n_img, self.n_tool = n_z, n_b, n_e, n_img, n_tool
self.state_dim = state_dim
self.trunk = nn.Sequential(
nn.Linear(state_dim + action_dim, hidden),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden, hidden),
nn.GELU(),
)
self.head_z = nn.Linear(hidden, n_z)
self.head_b = nn.Linear(hidden, n_b)
self.head_e = nn.Linear(hidden, n_e)
self.head_img = nn.Linear(hidden, n_img)
self.head_tool = nn.Linear(hidden, n_tool)
self.head_success = nn.Linear(hidden, 1)
self.head_harm = nn.Linear(hidden, 1)
self.head_irr = nn.Linear(hidden, 1)
self.head_progress = nn.Linear(hidden, 1)
def forward(self, s: torch.Tensor, a: torch.Tensor) -> dict[str, torch.Tensor]:
h = self.trunk(torch.cat([s, a], dim=-1))
return {
"dz": self.head_z(h),
"db": self.head_b(h),
"de": self.head_e(h),
"dimg": self.head_img(h),
"dtool": self.head_tool(h),
"success_logit": self.head_success(h).squeeze(-1),
"harm_logit": self.head_harm(h).squeeze(-1),
"irr_logit": self.head_irr(h).squeeze(-1),
"progress": self.head_progress(h).squeeze(-1),
"hidden": h,
}
# Convenience accessors used by the controller -------------------------
def predict(self, s: torch.Tensor, a: torch.Tensor) -> dict[str, torch.Tensor]:
out = self.forward(s, a)
out["p_harm"] = torch.sigmoid(out["harm_logit"])
out["p_success"] = torch.sigmoid(out["success_logit"])
out["p_irr"] = torch.sigmoid(out["irr_logit"])
return out
class RiskHead(nn.Module):
"""Predicts p_hat(a), c_hat(a) from the rolled-out next state and action."""
def __init__(self, state_dim: int, action_dim: int = 16, hidden: int = 128):
super().__init__()
self.body = nn.Sequential(
nn.Linear(state_dim + action_dim, hidden),
nn.GELU(),
nn.Linear(hidden, hidden // 2),
nn.GELU(),
)
self.p = nn.Linear(hidden // 2, 1)
self.c = nn.Linear(hidden // 2, 1)
def forward(self, s: torch.Tensor, a: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
h = self.body(torch.cat([s, a], dim=-1))
return torch.sigmoid(self.p(h)).squeeze(-1), torch.sigmoid(self.c(h)).squeeze(-1)
|