Download src/clsa/world_model/dynamics.py from fahadhafeezofficial/clsa-code: direct link, hf CLI and curl.
- Browser
- Download file 3.39 kB
-
https://huggingface.co/datasets/fahadhafeezofficial/clsa-code/resolve/main/src/clsa/world_model/dynamics.py
- Command line
-
hf download hf://datasets/fahadhafeezofficial/clsa-code/src/clsa/world_model/dynamics.py
-
curl -L -o dynamics.py https://huggingface.co/datasets/fahadhafeezofficial/clsa-code/resolve/main/src/clsa/world_model/dynamics.py
3.39 kB
| # 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) | |