Publish Learned virtual-computer state-transition world model
Browse files- README.md +36 -0
- evaluation.json +20 -0
- model_machine.safetensors +3 -0
- source/app.py +83 -0
- source/model.py +42 -0
- source/requirements.txt +4 -0
- source/schema.py +85 -0
- source/train.py +195 -0
README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: KernelMind Neural Model Machine
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "6.5.1"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# KernelMind Neural Model Machine
|
| 13 |
+
|
| 14 |
+
This project trains the machine that follows KernelMind AI OS. A compact neural
|
| 15 |
+
world model learns the transition function of an eight-bit virtual computer from
|
| 16 |
+
current state, capability flags, and one of thirteen OS actions. Training covers
|
| 17 |
+
the exhaustive 53,248-state/action corpus.
|
| 18 |
+
|
| 19 |
+
The evaluation separates held-out one-step transition accuracy from six-step
|
| 20 |
+
autoregressive rollout fidelity. The Space places the neural prediction beside
|
| 21 |
+
the deterministic reference transition for direct inspection.
|
| 22 |
+
|
| 23 |
+
It is a learned virtual-machine dynamics model, not a hypervisor and not a model
|
| 24 |
+
with access to the host operating system.
|
| 25 |
+
|
| 26 |
+
## Verified local result
|
| 27 |
+
|
| 28 |
+
The 6,937-parameter model reached 99.962% exact transition accuracy and 100%
|
| 29 |
+
blocked-action accuracy across 5,325 held-out transitions. In a separate
|
| 30 |
+
autoregressive audit, it reproduced every step and final state across 2,000
|
| 31 |
+
randomized six-action rollouts.
|
| 32 |
+
|
| 33 |
+
```bash
|
| 34 |
+
uv run python projects/kernelmind-model-machine/train.py
|
| 35 |
+
uv run pytest tests/test_kernelmind_model_machine.py
|
| 36 |
+
```
|
evaluation.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model": "KernelMind Neural Model Machine",
|
| 3 |
+
"parameters": 6937,
|
| 4 |
+
"training_transitions": 42598,
|
| 5 |
+
"heldout_transitions": 5325,
|
| 6 |
+
"best_epoch": 4,
|
| 7 |
+
"test": {
|
| 8 |
+
"exact_transition_accuracy": 0.9996244311332703,
|
| 9 |
+
"state_bit_accuracy": 0.999953031539917,
|
| 10 |
+
"blocked_accuracy": 1.0,
|
| 11 |
+
"transitions": 5325
|
| 12 |
+
},
|
| 13 |
+
"multi_step_audit": {
|
| 14 |
+
"rollouts": 2000,
|
| 15 |
+
"steps_per_rollout": 6,
|
| 16 |
+
"exact_step_fraction": 1.0,
|
| 17 |
+
"exact_six_step_rollout_fraction": 1.0
|
| 18 |
+
},
|
| 19 |
+
"boundary": "Models an eight-bit virtual state; it does not control the host OS"
|
| 20 |
+
}
|
model_machine.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:85d0c02a033f9bc934deaa864ffa29304209a241f7cd4d88f283ac9c2f36852b
|
| 3 |
+
size 28444
|
source/app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from model import NeuralModelMachine
|
| 8 |
+
from safetensors.torch import load_file
|
| 9 |
+
from schema import ACTIONS, CAPABILITY_NAMES, STATE_NAMES, reference_transition
|
| 10 |
+
|
| 11 |
+
ARTIFACT_DIR = (
|
| 12 |
+
Path(__file__).resolve().parent / "artifacts" / "kernelmind-model-machine"
|
| 13 |
+
)
|
| 14 |
+
MODEL = NeuralModelMachine()
|
| 15 |
+
MODEL.load_state_dict(load_file(ARTIFACT_DIR / "model_machine.safetensors"))
|
| 16 |
+
MODEL.eval()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@torch.inference_mode()
|
| 20 |
+
def run_transition(
|
| 21 |
+
action: str,
|
| 22 |
+
active_state: list[str],
|
| 23 |
+
active_capabilities: list[str],
|
| 24 |
+
) -> tuple[dict, dict]:
|
| 25 |
+
state = [int(name in active_state) for name in STATE_NAMES]
|
| 26 |
+
capabilities = [int(name in active_capabilities) for name in CAPABILITY_NAMES]
|
| 27 |
+
action_index = ACTIONS.index(action)
|
| 28 |
+
predicted, blocked_probability = MODEL.transition(
|
| 29 |
+
torch.tensor([state], dtype=torch.float32),
|
| 30 |
+
torch.tensor([capabilities], dtype=torch.float32),
|
| 31 |
+
torch.tensor([action_index]),
|
| 32 |
+
)
|
| 33 |
+
reference_state, reference_blocked = reference_transition(
|
| 34 |
+
state, capabilities, action
|
| 35 |
+
)
|
| 36 |
+
learned_state = predicted[0].int().tolist()
|
| 37 |
+
learned = {
|
| 38 |
+
"next_state": dict(zip(STATE_NAMES, learned_state, strict=True)),
|
| 39 |
+
"blocked_probability": float(blocked_probability[0]),
|
| 40 |
+
}
|
| 41 |
+
reference = {
|
| 42 |
+
"next_state": dict(zip(STATE_NAMES, reference_state, strict=True)),
|
| 43 |
+
"blocked": bool(reference_blocked),
|
| 44 |
+
"exact_match": learned_state == reference_state
|
| 45 |
+
and int(blocked_probability[0] >= 0.5) == reference_blocked,
|
| 46 |
+
}
|
| 47 |
+
return learned, reference
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
with gr.Blocks(title="KernelMind Neural Model Machine") as demo:
|
| 51 |
+
gr.Markdown(
|
| 52 |
+
"# KernelMind Neural Model Machine\n"
|
| 53 |
+
"A trained world model predicts how an OS action changes virtual machine "
|
| 54 |
+
"state. Compare its transition directly with the deterministic reference."
|
| 55 |
+
)
|
| 56 |
+
action = gr.Dropdown(ACTIONS, value="WRITE_BACKUP", label="Action")
|
| 57 |
+
state = gr.CheckboxGroup(
|
| 58 |
+
STATE_NAMES,
|
| 59 |
+
value=["file_exists", "service_running"],
|
| 60 |
+
label="Current state bits",
|
| 61 |
+
)
|
| 62 |
+
capabilities = gr.CheckboxGroup(
|
| 63 |
+
CAPABILITY_NAMES,
|
| 64 |
+
value=["network"],
|
| 65 |
+
label="Capabilities",
|
| 66 |
+
)
|
| 67 |
+
initial = run_transition(
|
| 68 |
+
"WRITE_BACKUP", ["file_exists", "service_running"], ["network"]
|
| 69 |
+
)
|
| 70 |
+
with gr.Row():
|
| 71 |
+
learned = gr.JSON(value=initial[0], label="Learned transition")
|
| 72 |
+
reference = gr.JSON(value=initial[1], label="Reference transition")
|
| 73 |
+
button = gr.Button("Advance the model machine", variant="primary")
|
| 74 |
+
button.click(
|
| 75 |
+
run_transition,
|
| 76 |
+
inputs=[action, state, capabilities],
|
| 77 |
+
outputs=[learned, reference],
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|
| 83 |
+
|
source/model.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from schema import ACTIONS
|
| 5 |
+
from torch import nn
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class NeuralModelMachine(nn.Module):
|
| 9 |
+
def __init__(self) -> None:
|
| 10 |
+
super().__init__()
|
| 11 |
+
self.action_embedding = nn.Embedding(len(ACTIONS), 16)
|
| 12 |
+
self.network = nn.Sequential(
|
| 13 |
+
nn.Linear(8 + 4 + 16, 64),
|
| 14 |
+
nn.LayerNorm(64),
|
| 15 |
+
nn.SiLU(),
|
| 16 |
+
nn.Linear(64, 64),
|
| 17 |
+
nn.SiLU(),
|
| 18 |
+
nn.Linear(64, 9),
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def forward(
|
| 22 |
+
self,
|
| 23 |
+
state: torch.Tensor,
|
| 24 |
+
capabilities: torch.Tensor,
|
| 25 |
+
action: torch.Tensor,
|
| 26 |
+
) -> torch.Tensor:
|
| 27 |
+
action_features = self.action_embedding(action)
|
| 28 |
+
return self.network(torch.cat([state, capabilities, action_features], dim=1))
|
| 29 |
+
|
| 30 |
+
@torch.inference_mode()
|
| 31 |
+
def transition(
|
| 32 |
+
self,
|
| 33 |
+
state: torch.Tensor,
|
| 34 |
+
capabilities: torch.Tensor,
|
| 35 |
+
action: torch.Tensor,
|
| 36 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 37 |
+
probabilities = torch.sigmoid(self(state, capabilities, action))
|
| 38 |
+
return (probabilities[:, :8] >= 0.5).float(), probabilities[:, 8]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def parameter_count(model: nn.Module) -> int:
|
| 42 |
+
return sum(parameter.numel() for parameter in model.parameters())
|
source/requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5,<7
|
| 2 |
+
safetensors>=0.6,<1
|
| 3 |
+
torch>=2.7,<3
|
| 4 |
+
|
source/schema.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
ACTIONS = [
|
| 4 |
+
"PAD",
|
| 5 |
+
"REPORT",
|
| 6 |
+
"OPEN_APP",
|
| 7 |
+
"READ_FILE",
|
| 8 |
+
"WRITE_FILE",
|
| 9 |
+
"DELETE_FILE",
|
| 10 |
+
"INSTALL_PACKAGE",
|
| 11 |
+
"SEARCH_LOCAL",
|
| 12 |
+
"SEARCH_WEB",
|
| 13 |
+
"RESTART_SERVICE",
|
| 14 |
+
"WRITE_BACKUP",
|
| 15 |
+
"ASK_CONFIRMATION",
|
| 16 |
+
"REFUSE",
|
| 17 |
+
]
|
| 18 |
+
STATE_NAMES = [
|
| 19 |
+
"file_exists",
|
| 20 |
+
"app_open",
|
| 21 |
+
"package_installed",
|
| 22 |
+
"service_running",
|
| 23 |
+
"backup_exists",
|
| 24 |
+
"report_available",
|
| 25 |
+
"confirmation_pending",
|
| 26 |
+
"refusal_logged",
|
| 27 |
+
]
|
| 28 |
+
CAPABILITY_NAMES = ["administrator", "network", "confirmed", "sensitive_target"]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def reference_transition(
|
| 32 |
+
state: list[int],
|
| 33 |
+
capabilities: list[int],
|
| 34 |
+
action: str,
|
| 35 |
+
) -> tuple[list[int], int]:
|
| 36 |
+
next_state = list(state)
|
| 37 |
+
administrator, network, confirmed, sensitive = map(bool, capabilities)
|
| 38 |
+
blocked = 0
|
| 39 |
+
if action == "REPORT":
|
| 40 |
+
next_state[5] = 1
|
| 41 |
+
elif action == "OPEN_APP":
|
| 42 |
+
next_state[1] = 1
|
| 43 |
+
elif action == "READ_FILE":
|
| 44 |
+
if not state[0] or (sensitive and not administrator):
|
| 45 |
+
blocked = 1
|
| 46 |
+
else:
|
| 47 |
+
next_state[5] = 1
|
| 48 |
+
elif action == "WRITE_FILE":
|
| 49 |
+
if sensitive and not administrator:
|
| 50 |
+
blocked = 1
|
| 51 |
+
else:
|
| 52 |
+
next_state[0] = 1
|
| 53 |
+
elif action == "DELETE_FILE":
|
| 54 |
+
if not confirmed or sensitive or not state[0]:
|
| 55 |
+
blocked = 1
|
| 56 |
+
else:
|
| 57 |
+
next_state[0] = 0
|
| 58 |
+
elif action == "INSTALL_PACKAGE":
|
| 59 |
+
if not (confirmed and administrator and network):
|
| 60 |
+
blocked = 1
|
| 61 |
+
else:
|
| 62 |
+
next_state[2] = 1
|
| 63 |
+
elif action == "SEARCH_LOCAL":
|
| 64 |
+
next_state[5] = 1
|
| 65 |
+
elif action == "SEARCH_WEB":
|
| 66 |
+
if not network:
|
| 67 |
+
blocked = 1
|
| 68 |
+
else:
|
| 69 |
+
next_state[5] = 1
|
| 70 |
+
elif action == "RESTART_SERVICE":
|
| 71 |
+
if not (confirmed and administrator):
|
| 72 |
+
blocked = 1
|
| 73 |
+
else:
|
| 74 |
+
next_state[3] = 1
|
| 75 |
+
elif action == "WRITE_BACKUP":
|
| 76 |
+
if not state[0] or (sensitive and not administrator):
|
| 77 |
+
blocked = 1
|
| 78 |
+
else:
|
| 79 |
+
next_state[4] = 1
|
| 80 |
+
elif action == "ASK_CONFIRMATION":
|
| 81 |
+
next_state[6] = 1
|
| 82 |
+
elif action == "REFUSE":
|
| 83 |
+
next_state[7] = 1
|
| 84 |
+
return next_state, blocked
|
| 85 |
+
|
source/train.py
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import itertools
|
| 4 |
+
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import torch
|
| 10 |
+
import trackio
|
| 11 |
+
from model import NeuralModelMachine, parameter_count
|
| 12 |
+
from safetensors.torch import save_file
|
| 13 |
+
from schema import ACTIONS, CAPABILITY_NAMES, STATE_NAMES, reference_transition
|
| 14 |
+
from torch.nn import functional as F
|
| 15 |
+
|
| 16 |
+
PROJECT_DIR = Path(__file__).resolve().parent
|
| 17 |
+
ARTIFACT_DIR = PROJECT_DIR / "artifacts" / "kernelmind-model-machine"
|
| 18 |
+
DATA_DIR = PROJECT_DIR / "data"
|
| 19 |
+
SEED = 2087
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def build_rows() -> list[dict]:
|
| 23 |
+
rows = []
|
| 24 |
+
for state in itertools.product([0, 1], repeat=8):
|
| 25 |
+
for capabilities in itertools.product([0, 1], repeat=4):
|
| 26 |
+
for action_index, action in enumerate(ACTIONS):
|
| 27 |
+
next_state, blocked = reference_transition(
|
| 28 |
+
list(state), list(capabilities), action
|
| 29 |
+
)
|
| 30 |
+
rows.append(
|
| 31 |
+
{
|
| 32 |
+
"state": list(state),
|
| 33 |
+
"capabilities": list(capabilities),
|
| 34 |
+
"action": action,
|
| 35 |
+
"action_index": action_index,
|
| 36 |
+
"next_state": next_state,
|
| 37 |
+
"blocked": blocked,
|
| 38 |
+
}
|
| 39 |
+
)
|
| 40 |
+
return rows
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def tensors(rows: list[dict]) -> tuple[torch.Tensor, ...]:
|
| 44 |
+
return (
|
| 45 |
+
torch.tensor([row["state"] for row in rows], dtype=torch.float32),
|
| 46 |
+
torch.tensor([row["capabilities"] for row in rows], dtype=torch.float32),
|
| 47 |
+
torch.tensor([row["action_index"] for row in rows]),
|
| 48 |
+
torch.tensor(
|
| 49 |
+
[row["next_state"] + [row["blocked"]] for row in rows],
|
| 50 |
+
dtype=torch.float32,
|
| 51 |
+
),
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@torch.inference_mode()
|
| 56 |
+
def evaluate(model: NeuralModelMachine, rows: list[dict]) -> dict:
|
| 57 |
+
state, capabilities, action, targets = tensors(rows)
|
| 58 |
+
probabilities = torch.sigmoid(model(state, capabilities, action))
|
| 59 |
+
predictions = (probabilities >= 0.5).float()
|
| 60 |
+
exact = (predictions == targets).all(1)
|
| 61 |
+
return {
|
| 62 |
+
"exact_transition_accuracy": float(exact.float().mean()),
|
| 63 |
+
"state_bit_accuracy": float(
|
| 64 |
+
(predictions[:, :8] == targets[:, :8]).float().mean()
|
| 65 |
+
),
|
| 66 |
+
"blocked_accuracy": float(
|
| 67 |
+
(predictions[:, 8] == targets[:, 8]).float().mean()
|
| 68 |
+
),
|
| 69 |
+
"transitions": len(rows),
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
@torch.inference_mode()
|
| 74 |
+
def rollout_audit(model: NeuralModelMachine, samples: int = 2_000) -> dict:
|
| 75 |
+
rng = np.random.default_rng(SEED + 1)
|
| 76 |
+
exact_steps = 0
|
| 77 |
+
exact_rollouts = 0
|
| 78 |
+
for _ in range(samples):
|
| 79 |
+
state = rng.integers(0, 2, size=8).tolist()
|
| 80 |
+
capabilities = rng.integers(0, 2, size=4).tolist()
|
| 81 |
+
learned = torch.tensor([state], dtype=torch.float32)
|
| 82 |
+
reference = state
|
| 83 |
+
rollout_exact = True
|
| 84 |
+
for action_index in rng.integers(0, len(ACTIONS), size=6):
|
| 85 |
+
action = ACTIONS[action_index]
|
| 86 |
+
reference, reference_blocked = reference_transition(
|
| 87 |
+
reference, capabilities, action
|
| 88 |
+
)
|
| 89 |
+
predicted, blocked_probability = model.transition(
|
| 90 |
+
learned,
|
| 91 |
+
torch.tensor([capabilities], dtype=torch.float32),
|
| 92 |
+
torch.tensor([action_index]),
|
| 93 |
+
)
|
| 94 |
+
learned = predicted
|
| 95 |
+
step_exact = learned[0].int().tolist() == reference
|
| 96 |
+
step_exact = step_exact and (
|
| 97 |
+
int(blocked_probability[0] >= 0.5) == reference_blocked
|
| 98 |
+
)
|
| 99 |
+
exact_steps += int(step_exact)
|
| 100 |
+
rollout_exact = rollout_exact and step_exact
|
| 101 |
+
exact_rollouts += int(rollout_exact)
|
| 102 |
+
return {
|
| 103 |
+
"rollouts": samples,
|
| 104 |
+
"steps_per_rollout": 6,
|
| 105 |
+
"exact_step_fraction": exact_steps / (samples * 6),
|
| 106 |
+
"exact_six_step_rollout_fraction": exact_rollouts / samples,
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def main() -> None:
|
| 111 |
+
torch.manual_seed(SEED)
|
| 112 |
+
torch.set_num_threads(1)
|
| 113 |
+
rows = build_rows()
|
| 114 |
+
rng = np.random.default_rng(SEED)
|
| 115 |
+
order = rng.permutation(len(rows))
|
| 116 |
+
train_end = int(0.8 * len(rows))
|
| 117 |
+
validation_end = int(0.9 * len(rows))
|
| 118 |
+
train_rows = [rows[index] for index in order[:train_end]]
|
| 119 |
+
validation_rows = [rows[index] for index in order[train_end:validation_end]]
|
| 120 |
+
test_rows = [rows[index] for index in order[validation_end:]]
|
| 121 |
+
state, capabilities, action, targets = tensors(train_rows)
|
| 122 |
+
model = NeuralModelMachine()
|
| 123 |
+
optimizer = torch.optim.AdamW(model.parameters(), lr=2e-3, weight_decay=1e-5)
|
| 124 |
+
best = -1.0
|
| 125 |
+
best_state = None
|
| 126 |
+
best_epoch = 0
|
| 127 |
+
trackio.init(
|
| 128 |
+
project="kernelmind-model-machine",
|
| 129 |
+
name="neural-os-transition-runtime-v1",
|
| 130 |
+
config={
|
| 131 |
+
"parameters": parameter_count(model),
|
| 132 |
+
"training_transitions": len(train_rows),
|
| 133 |
+
"state_bits": len(STATE_NAMES),
|
| 134 |
+
"capability_bits": len(CAPABILITY_NAMES),
|
| 135 |
+
},
|
| 136 |
+
)
|
| 137 |
+
for epoch in range(1, 81):
|
| 138 |
+
permutation = torch.randperm(len(state))
|
| 139 |
+
model.train()
|
| 140 |
+
for start in range(0, len(state), 512):
|
| 141 |
+
indexes = permutation[start : start + 512]
|
| 142 |
+
logits = model(state[indexes], capabilities[indexes], action[indexes])
|
| 143 |
+
loss = F.binary_cross_entropy_with_logits(logits, targets[indexes])
|
| 144 |
+
optimizer.zero_grad(set_to_none=True)
|
| 145 |
+
loss.backward()
|
| 146 |
+
optimizer.step()
|
| 147 |
+
validation = evaluate(model, validation_rows)
|
| 148 |
+
if validation["exact_transition_accuracy"] > best:
|
| 149 |
+
best = validation["exact_transition_accuracy"]
|
| 150 |
+
best_epoch = epoch
|
| 151 |
+
best_state = {
|
| 152 |
+
name: value.detach().cpu().clone()
|
| 153 |
+
for name, value in model.state_dict().items()
|
| 154 |
+
}
|
| 155 |
+
if epoch == 1 or epoch % 10 == 0:
|
| 156 |
+
trackio.log({"epoch": epoch, **validation})
|
| 157 |
+
if best == 1.0 and epoch >= 20:
|
| 158 |
+
break
|
| 159 |
+
assert best_state is not None
|
| 160 |
+
model.load_state_dict(best_state)
|
| 161 |
+
test = evaluate(model, test_rows)
|
| 162 |
+
rollouts = rollout_audit(model)
|
| 163 |
+
report = {
|
| 164 |
+
"model": "KernelMind Neural Model Machine",
|
| 165 |
+
"parameters": parameter_count(model),
|
| 166 |
+
"training_transitions": len(train_rows),
|
| 167 |
+
"heldout_transitions": len(test_rows),
|
| 168 |
+
"best_epoch": best_epoch,
|
| 169 |
+
"test": test,
|
| 170 |
+
"multi_step_audit": rollouts,
|
| 171 |
+
"boundary": "Models an eight-bit virtual state; it does not control the host OS",
|
| 172 |
+
}
|
| 173 |
+
ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
|
| 174 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 175 |
+
save_file(model.state_dict(), ARTIFACT_DIR / "model_machine.safetensors")
|
| 176 |
+
(ARTIFACT_DIR / "evaluation.json").write_text(
|
| 177 |
+
json.dumps(report, indent=2), encoding="utf-8"
|
| 178 |
+
)
|
| 179 |
+
pd.DataFrame(
|
| 180 |
+
{
|
| 181 |
+
"state": [row["state"] for row in rows],
|
| 182 |
+
"capabilities": [row["capabilities"] for row in rows],
|
| 183 |
+
"action": [row["action"] for row in rows],
|
| 184 |
+
"next_state": [row["next_state"] for row in rows],
|
| 185 |
+
"blocked": [row["blocked"] for row in rows],
|
| 186 |
+
}
|
| 187 |
+
).to_parquet(DATA_DIR / "transitions.parquet", index=False)
|
| 188 |
+
trackio.log({**test, **rollouts})
|
| 189 |
+
trackio.finish()
|
| 190 |
+
print(json.dumps(report, indent=2))
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
if __name__ == "__main__":
|
| 194 |
+
main()
|
| 195 |
+
|