File size: 7,267 Bytes
3425207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from __future__ import annotations

import itertools
import json
from pathlib import Path

import numpy as np
import pandas as pd
import torch
import trackio
from model import NeuralModelMachine, parameter_count
from safetensors.torch import save_file
from schema import ACTIONS, CAPABILITY_NAMES, STATE_NAMES, reference_transition
from torch.nn import functional as F

PROJECT_DIR = Path(__file__).resolve().parent
ARTIFACT_DIR = PROJECT_DIR / "artifacts" / "kernelmind-model-machine"
DATA_DIR = PROJECT_DIR / "data"
SEED = 2087


def build_rows() -> list[dict]:
    rows = []
    for state in itertools.product([0, 1], repeat=8):
        for capabilities in itertools.product([0, 1], repeat=4):
            for action_index, action in enumerate(ACTIONS):
                next_state, blocked = reference_transition(
                    list(state), list(capabilities), action
                )
                rows.append(
                    {
                        "state": list(state),
                        "capabilities": list(capabilities),
                        "action": action,
                        "action_index": action_index,
                        "next_state": next_state,
                        "blocked": blocked,
                    }
                )
    return rows


def tensors(rows: list[dict]) -> tuple[torch.Tensor, ...]:
    return (
        torch.tensor([row["state"] for row in rows], dtype=torch.float32),
        torch.tensor([row["capabilities"] for row in rows], dtype=torch.float32),
        torch.tensor([row["action_index"] for row in rows]),
        torch.tensor(
            [row["next_state"] + [row["blocked"]] for row in rows],
            dtype=torch.float32,
        ),
    )


@torch.inference_mode()
def evaluate(model: NeuralModelMachine, rows: list[dict]) -> dict:
    state, capabilities, action, targets = tensors(rows)
    probabilities = torch.sigmoid(model(state, capabilities, action))
    predictions = (probabilities >= 0.5).float()
    exact = (predictions == targets).all(1)
    return {
        "exact_transition_accuracy": float(exact.float().mean()),
        "state_bit_accuracy": float(
            (predictions[:, :8] == targets[:, :8]).float().mean()
        ),
        "blocked_accuracy": float(
            (predictions[:, 8] == targets[:, 8]).float().mean()
        ),
        "transitions": len(rows),
    }


@torch.inference_mode()
def rollout_audit(model: NeuralModelMachine, samples: int = 2_000) -> dict:
    rng = np.random.default_rng(SEED + 1)
    exact_steps = 0
    exact_rollouts = 0
    for _ in range(samples):
        state = rng.integers(0, 2, size=8).tolist()
        capabilities = rng.integers(0, 2, size=4).tolist()
        learned = torch.tensor([state], dtype=torch.float32)
        reference = state
        rollout_exact = True
        for action_index in rng.integers(0, len(ACTIONS), size=6):
            action = ACTIONS[action_index]
            reference, reference_blocked = reference_transition(
                reference, capabilities, action
            )
            predicted, blocked_probability = model.transition(
                learned,
                torch.tensor([capabilities], dtype=torch.float32),
                torch.tensor([action_index]),
            )
            learned = predicted
            step_exact = learned[0].int().tolist() == reference
            step_exact = step_exact and (
                int(blocked_probability[0] >= 0.5) == reference_blocked
            )
            exact_steps += int(step_exact)
            rollout_exact = rollout_exact and step_exact
        exact_rollouts += int(rollout_exact)
    return {
        "rollouts": samples,
        "steps_per_rollout": 6,
        "exact_step_fraction": exact_steps / (samples * 6),
        "exact_six_step_rollout_fraction": exact_rollouts / samples,
    }


def main() -> None:
    torch.manual_seed(SEED)
    torch.set_num_threads(1)
    rows = build_rows()
    rng = np.random.default_rng(SEED)
    order = rng.permutation(len(rows))
    train_end = int(0.8 * len(rows))
    validation_end = int(0.9 * len(rows))
    train_rows = [rows[index] for index in order[:train_end]]
    validation_rows = [rows[index] for index in order[train_end:validation_end]]
    test_rows = [rows[index] for index in order[validation_end:]]
    state, capabilities, action, targets = tensors(train_rows)
    model = NeuralModelMachine()
    optimizer = torch.optim.AdamW(model.parameters(), lr=2e-3, weight_decay=1e-5)
    best = -1.0
    best_state = None
    best_epoch = 0
    trackio.init(
        project="kernelmind-model-machine",
        name="neural-os-transition-runtime-v1",
        config={
            "parameters": parameter_count(model),
            "training_transitions": len(train_rows),
            "state_bits": len(STATE_NAMES),
            "capability_bits": len(CAPABILITY_NAMES),
        },
    )
    for epoch in range(1, 81):
        permutation = torch.randperm(len(state))
        model.train()
        for start in range(0, len(state), 512):
            indexes = permutation[start : start + 512]
            logits = model(state[indexes], capabilities[indexes], action[indexes])
            loss = F.binary_cross_entropy_with_logits(logits, targets[indexes])
            optimizer.zero_grad(set_to_none=True)
            loss.backward()
            optimizer.step()
        validation = evaluate(model, validation_rows)
        if validation["exact_transition_accuracy"] > best:
            best = validation["exact_transition_accuracy"]
            best_epoch = epoch
            best_state = {
                name: value.detach().cpu().clone()
                for name, value in model.state_dict().items()
            }
        if epoch == 1 or epoch % 10 == 0:
            trackio.log({"epoch": epoch, **validation})
        if best == 1.0 and epoch >= 20:
            break
    assert best_state is not None
    model.load_state_dict(best_state)
    test = evaluate(model, test_rows)
    rollouts = rollout_audit(model)
    report = {
        "model": "KernelMind Neural Model Machine",
        "parameters": parameter_count(model),
        "training_transitions": len(train_rows),
        "heldout_transitions": len(test_rows),
        "best_epoch": best_epoch,
        "test": test,
        "multi_step_audit": rollouts,
        "boundary": "Models an eight-bit virtual state; it does not control the host OS",
    }
    ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
    DATA_DIR.mkdir(parents=True, exist_ok=True)
    save_file(model.state_dict(), ARTIFACT_DIR / "model_machine.safetensors")
    (ARTIFACT_DIR / "evaluation.json").write_text(
        json.dumps(report, indent=2), encoding="utf-8"
    )
    pd.DataFrame(
        {
            "state": [row["state"] for row in rows],
            "capabilities": [row["capabilities"] for row in rows],
            "action": [row["action"] for row in rows],
            "next_state": [row["next_state"] for row in rows],
            "blocked": [row["blocked"] for row in rows],
        }
    ).to_parquet(DATA_DIR / "transitions.parquet", index=False)
    trackio.log({**test, **rollouts})
    trackio.finish()
    print(json.dumps(report, indent=2))


if __name__ == "__main__":
    main()