MoniesTO's picture
Restore inference package directories and document runnable download steps
e094a04 verified
Raw History Blame Contribute Delete
4.86 kB
# SPDX-License-Identifier: Apache-2.0
# Modified/extracted for the independent OSE preview; see NOTICE.
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import hashlib
EXPECTED_CHECKPOINT_SHA256 = "797914161296251f7c36c5e882fa676766c0ae91f0dfc71ff965b175983aab26"
def sha256_file(path: str | Path) -> str:
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(1024 * 1024), b""):
h.update(chunk)
return h.hexdigest()
@dataclass(slots=True)
class OSEOutput:
option_logits: list[float]
noul_logit: float
probabilities: list[float]
selected_index: int | None
def _require_torch():
try:
import torch
import torch.nn as nn
import torch.nn.functional as F
except ImportError as exc:
raise RuntimeError(
"OSE support requires PyTorch. Install with: pip install -e .[ose]"
) from exc
return torch, nn, F
def build_ose_module():
torch, nn, F = _require_torch()
class ResidualMLPBlock(nn.Module):
def __init__(self, width: int, ffn_ratio: int) -> None:
super().__init__()
self.norm = nn.LayerNorm(width)
self.up = nn.Linear(width, width * ffn_ratio)
self.down = nn.Linear(width * ffn_ratio, width)
def forward(self, x):
return x + self.down(F.gelu(self.up(self.norm(x))))
class OSEStructuredResidualMLP(nn.Module):
def __init__(
self,
width: int = 512,
layers: int = 3,
ffn_ratio: int = 4,
bottleneck: int = 128,
) -> None:
super().__init__()
self.state_projection = nn.Linear(27, width)
self.option_projection = nn.Linear(16, width)
self.token_type_embedding = nn.Embedding(2, width)
self.physical_encoder = nn.ModuleList(
[ResidualMLPBlock(width, ffn_ratio) for _ in range(layers)]
)
self.final_norm = nn.LayerNorm(width)
self.summary_projection = nn.Linear(width * 2, width)
self.structure_bottleneck = nn.Linear(width, bottleneck)
self.bottleneck_readout = nn.Linear(bottleneck, width)
self.variable_head = nn.Linear(width, 5)
self.constraint_head = nn.Linear(width, 12)
self.feasible_head = nn.Linear(width, 1)
self.risk_head = nn.Linear(width, 5)
self.transition_head = nn.Linear(width, 4)
self.outcome_head = nn.Linear(width, 3)
self.decision_hidden = nn.Linear(width * 2, width)
self.decision_option = nn.Linear(width, 1)
self.decision_noul = nn.Linear(bottleneck, 1)
def forward(self, state, options):
# state: [B, 27], options: [B, N, 16]
state_token = self.state_projection(state)
option_tokens = self.option_projection(options)
state_type = self.token_type_embedding.weight[0].view(1, 1, -1)
option_type = self.token_type_embedding.weight[1].view(1, 1, -1)
tokens = torch.cat(
[state_token.unsqueeze(1) + state_type, option_tokens + option_type],
dim=1,
)
for block in self.physical_encoder:
tokens = block(tokens)
tokens = self.final_norm(tokens)
state_vec = tokens[:, 0]
option_vecs = tokens[:, 1:]
# IMPORTANT: TaoRNCS passes only pre-gate admitted candidates here.
# Thus blocked candidates cannot pollute this mean-pooling context.
option_mean = option_vecs.mean(dim=1)
summary = F.gelu(
self.summary_projection(torch.cat([state_vec, option_mean], dim=-1))
)
bottleneck = F.gelu(self.structure_bottleneck(summary))
shared = F.gelu(self.bottleneck_readout(bottleneck))
shared_expanded = shared.unsqueeze(1).expand(-1, option_vecs.shape[1], -1)
decision_hidden = F.gelu(
self.decision_hidden(torch.cat([option_vecs, shared_expanded], dim=-1))
)
option_logits = self.decision_option(decision_hidden).squeeze(-1)
noul_logit = self.decision_noul(bottleneck)
decision_logits = torch.cat([option_logits, noul_logit], dim=-1)
return {
"decision_logits": decision_logits,
"option_logits": option_logits,
"noul_logit": noul_logit,
"bottleneck": bottleneck,
"shared": shared,
}
return OSEStructuredResidualMLP