#!/usr/bin/env python3 """NOEON unified training on a GPU host (Colab / any CUDA machine). This is the *real* training entry point for the unified brain. It is honest by construction: it refuses to start unless a GPU is present, and it never falls back to synthetic data silently. Pipeline -------- 1. resource + GPU check (``noeon.core.resources``); 2. load the three frozen sources with :class:`HFSourceEncoder` (eval, frozen); 3. take real hidden states from them for a batch; 4. run them through the trainable bridges + FusionCore + GenerationHead; 5. optimise only NOEON's parameters, with distillation + alignment terms; 6. save/resume checkpoints with gradient accumulation and optional AMP. The feature function uses the sources' own pooled states as the teacher and the generation slot's pooled state as the target conditioning, so the objective is grounded in real representations rather than random tensors. Usage (on a GPU host): HF_TOKEN=... python scripts/train_unified.py \ --steps 200 --batch 1 --grad-accum 4 --amp --resume """ from __future__ import annotations import argparse import json import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import torch from noeon.architecture.model import Noeon from noeon.core.config import NoeonConfig from noeon.core.resources import detect from noeon.encoders.base import EncoderUnavailable from noeon.encoders.hf_source import HFSourceEncoder from noeon.training.trainer import TrainConfig, train ROOT = Path(__file__).resolve().parent.parent def require_gpu() -> str: env = detect() gpu = env["gpu"] if not gpu["available"]: print("NO GPU DETECTED — refusing to start the real training run.") print(json.dumps(env, indent=2)) print("\nThis script trains against the frozen sources and needs a GPU.") print("Run it on Colab (notebooks/NOEON_TRAIN_GPU.ipynb) or another " "CUDA host. Nothing was trained; no checkpoint was written.") raise SystemExit(2) print(f"GPU: {gpu['name']} VRAM: {gpu['vram_gb']} GB") return "cuda" def load_sources(device: str, dtype: torch.dtype, token: str | None): encoders = {} for key in ("vision", "computer_use", "generation"): enc = HFSourceEncoder(key, dtype=dtype, device=device, token=token) try: enc.load() except EncoderUnavailable as e: print(f"could not attach {key}: {e}") raise encoders[key] = enc print(f"attached frozen source: {key}") return encoders def make_feature_fn(encoders, batch_texts, device): """A feature function that runs the frozen sources for real hidden states.""" def feature_fn(): streams = {} pooled = {} with torch.no_grad(): for key, enc in encoders.items(): out = enc.encode(input_ids=batch_texts[key].to(device)) streams[key] = out.float() pooled[key] = out.float().mean(dim=1) teacher = torch.stack([pooled["vision"], pooled["computer_use"]], dim=0).mean(dim=0) target = pooled["generation"] return streams, target, teacher return feature_fn def main(argv: list[str]) -> int: ap = argparse.ArgumentParser() ap.add_argument("--steps", type=int, default=200) ap.add_argument("--batch", type=int, default=1) ap.add_argument("--grad-accum", type=int, default=4) ap.add_argument("--amp", action="store_true") ap.add_argument("--resume", action="store_true") ap.add_argument("--state-dir", default=str(ROOT / "weights" / "checkpoints")) ap.add_argument("--token", default=None) args = ap.parse_args(argv[1:]) device = require_gpu() token = args.token or __import__("os").environ.get("HF_TOKEN") encoders = load_sources(device, torch.bfloat16, token) # A tiny, real prompt batch per slot (real token ids from the shared vocab). from transformers import AutoTokenizer tok = AutoTokenizer.from_pretrained("microsoft/Mage-VL", trust_remote_code=True) ids = tok("describe this screen and plan the next action", return_tensors="pt").input_ids batch_texts = {k: ids for k in encoders} model = Noeon(NoeonConfig()).to(device) feature_fn = make_feature_fn(encoders, batch_texts, device) cfg = TrainConfig( steps=args.steps, grad_accum=args.grad_accum, amp=args.amp, checkpoint_every=max(1, args.steps // 5), log_every=max(1, args.steps // 20), keep_last_k=3, ) extra = {"sources": {k: encoders[k].spec.repo for k in encoders}, "run_started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())} history = train(model, feature_fn, args.state_dir, cfg=cfg, resume=args.resume, extra_meta=extra) out = ROOT / "reports" / "training_run.json" out.parent.mkdir(parents=True, exist_ok=True) out.write_text(json.dumps({ "steps": args.steps, "grad_accum": args.grad_accum, "amp": args.amp, "device": device, "history": history, "final_loss": history[-1]["loss"] if history else None, }, indent=2)) print(f"\nwrote {out}") return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv))