Download example_usage.py from Kai9987kai/supermix-v91: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/Kai9987kai/supermix-v91/resolve/main/example_usage.py
- Command line
-
hf download hf://Kai9987kai/supermix-v91/example_usage.py
-
curl -L -o example_usage.py https://huggingface.co/Kai9987kai/supermix-v91/resolve/main/example_usage.py
3.04 kB
| """End-to-end inference & Cognitive Fabric verification demo for Supermix v91 (Male-CNS Connectome).""" | |
| 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" / "v91_cns_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 | |
| 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 v91 (Male-CNS Connectome) 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()) | |