File size: 3,045 Bytes
aaeb56d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""End-to-end inference & Cognitive Fabric verification demo for Supermix v97 (Hebbian + Entropy Halt)."""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent
SOURCE_DIR = ROOT / "source"
if str(SOURCE_DIR) not in sys.path:
    sys.path.insert(0, str(SOURCE_DIR))

import torch  # noqa: E402
import mimomix_decoding as decoding  # noqa: E402
import mimomix_text as text_utils  # noqa: E402
from mimomix_core import MiMoMixConfig, MiMoMixModel  # noqa: E402
from scf import CognitiveFabricRuntime, SCFFeatureFlags  # noqa: E402
from scf.verification import SCFVerificationGate  # noqa: E402

DEFAULT_CHECKPOINT = ROOT / "weights" / "v97_scf_connectome.selected.pt"


def load_checkpoint(path: Path = DEFAULT_CHECKPOINT):
    payload = torch.load(path, map_location="cpu", weights_only=False)
    model = MiMoMixModel(MiMoMixConfig(**payload["config"]))
    model.load_state_dict(payload["state_dict"])
    model.eval()
    tokenizer = text_utils.WordTokenizer.from_dict(payload["tokenizer"])
    return model, tokenizer


@torch.no_grad()
def generate_reply(model: MiMoMixModel, tokenizer, message: str, max_new_tokens: int = 64) -> str:
    ids, _ = tokenizer.encode_turn(message, None)
    result = decoding.speculative_generate(
        model,
        torch.tensor([ids]),
        max_new_tokens=max_new_tokens,
        eos_token_id=text_utils.EOS,
    )
    return tokenizer.decode(result.new_tokens[0].tolist()).strip()


def main() -> int:
    parser = argparse.ArgumentParser(description="Run Supermix v97 (Hebbian + Entropy Halt) demo")
    parser.add_argument("--checkpoint", type=Path, default=DEFAULT_CHECKPOINT)
    args = parser.parse_args()

    model, tokenizer = load_checkpoint(args.checkpoint)
    params = sum(p.numel() for p in model.parameters())
    print(f"Loaded {args.checkpoint.name}: {params:,} params | vocab {tokenizer.vocab_size}")
    
    prompts = [
        "nums = [10, 12, 4]\nr = nums[1] + nums[2] Final r?",
        "How many Li20 neurons does the male CNS contain?",
        "inverse of [[4, 7], [2, 6]]",
        "eigenvalues of [[4, 1], [2, 3]]",
    ]
    gate = SCFVerificationGate()
    runtime = CognitiveFabricRuntime()
    flags = SCFFeatureFlags.all_enabled()

    for q in prompts:
        neural = generate_reply(model, tokenizer, q)
        ver = gate.verify(query=q, candidate_answer=neural)
        rt = runtime.run_task(q, flags_override=flags)
        tele = model.telemetry()
        print(f"\nQ: {q}")
        print(f"Neural: {neural}")
        print(
            f"SCF verified={ver['verified']} | answer={ver['verified_answer']} "
            f"| source={ver['verifier_source']} | family={rt['routing']['selected_family']}"
        )
        print(
            f"Telemetry: mhc={bool(tele.get('mhc'))} | engram={bool(tele.get('engram'))} "
            f"| deltanet={bool(tele.get('deltanet'))} | cns={bool(tele.get('cns_core'))}"
        )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())