HAKO upload: hako/train/phase2_granite_joint.py
Browse files
hako/train/phase2_granite_joint.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Phase 2 -- Granite-4.0-1B integration + joint cooperative training with
|
| 2 |
+
the diffusion game loop and full orchestration.
|
| 3 |
+
|
| 4 |
+
Granite is decomposed (int4 kept) and registered as the SECOND routed expert
|
| 5 |
+
bank; its latents come from the frozen dequantized prototypes (real source
|
| 6 |
+
weights), so the joint batches carry both banks:
|
| 7 |
+
zs = {"qwen": z_q, "granite": p_g}
|
| 8 |
+
Cooperation: orthogonal ensemble penalty (T-ENS), MoE cross-attention across
|
| 9 |
+
banks (T-ATTVAR), chain recursion with dynamic ordering (T-CHAIN), and the
|
| 10 |
+
game curriculum (T-CURRICULUM) drive the fused representation.
|
| 11 |
+
"""
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import logging
|
| 15 |
+
import time
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
import numpy as np
|
| 19 |
+
import torch
|
| 20 |
+
|
| 21 |
+
from hako.checkpoint import save_state
|
| 22 |
+
from hako.memory_manager import MemoryManager
|
| 23 |
+
from hako.sources import loader
|
| 24 |
+
from hako.sources.decompose import decompose_source, dequantize_int4
|
| 25 |
+
from hako.telemetry import Telemetry
|
| 26 |
+
from hako.train.trainer import HAKOSystem
|
| 27 |
+
|
| 28 |
+
log = logging.getLogger("hako.phase2")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def run(cfg, tel: Telemetry, mem: MemoryManager, deadline_s: int,
|
| 32 |
+
carry: dict) -> dict:
|
| 33 |
+
t0 = time.time()
|
| 34 |
+
art_dir = Path(cfg.artifacts_dir)
|
| 35 |
+
ckpt_dir = Path(cfg.ckpt_dir)
|
| 36 |
+
sys_: HAKOSystem = carry["system"]
|
| 37 |
+
|
| 38 |
+
# ---------------- 1. fetch + decompose Granite (one at a time) --------
|
| 39 |
+
snap = loader.fetch_source(loader.GRANITE, cfg.hf_cache, mem)
|
| 40 |
+
npz_path = art_dir / "granite1b_decomposed.npz"
|
| 41 |
+
meta = decompose_source(loader.GRANITE, snap, cfg, npz_path, seed=cfg.seed)
|
| 42 |
+
tel.orchestration(event="granite_decomposed", inertia=float(meta["inertia"]))
|
| 43 |
+
|
| 44 |
+
# ---------------- 2. register + build granite latent pool -------------
|
| 45 |
+
sys_.register_source("granite", npz_path, d_src=cfg.granite_proto_dim)
|
| 46 |
+
data = np.load(npz_path, allow_pickle=False)
|
| 47 |
+
protos = dequantize_int4(data["int4_proto_packed"],
|
| 48 |
+
data["int4_proto_scales"],
|
| 49 |
+
int(data["proto_dim_for_unpack"][0]))
|
| 50 |
+
protos_t = torch.as_tensor(protos, dtype=torch.float32)
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
P_N = torch.stack([sys_.forward_sample({"granite": p}, train=False)["z"]
|
| 53 |
+
for p in protos_t])
|
| 54 |
+
carry["granite_protos"] = protos_t
|
| 55 |
+
|
| 56 |
+
# ---------------- 3. joint cooperative training loop ------------------
|
| 57 |
+
Z_t = carry["Z_t"]
|
| 58 |
+
labels = carry["labels"]
|
| 59 |
+
labels_t = torch.as_tensor(labels)
|
| 60 |
+
gen = torch.Generator().manual_seed(cfg.seed + 2)
|
| 61 |
+
step_in_phase = 0
|
| 62 |
+
pgo_every = 150
|
| 63 |
+
while time.time() - t0 < deadline_s:
|
| 64 |
+
idx = torch.randint(0, len(Z_t), (cfg.batch_pairs,), generator=gen)
|
| 65 |
+
batch = []
|
| 66 |
+
for i in idx.tolist():
|
| 67 |
+
g_idx = int(torch.randint(0, len(protos_t), (1,),
|
| 68 |
+
generator=gen).item())
|
| 69 |
+
zs = {"qwen": Z_t[i], "granite": protos_t[g_idx]}
|
| 70 |
+
noise = torch.randn(cfg.N_dim, generator=gen) * 0.05
|
| 71 |
+
tgt = {"z_clean": carry["Z_N"][i],
|
| 72 |
+
"z_noisy": carry["Z_N"][i] + noise * 0.02,
|
| 73 |
+
"y": torch.nn.functional.one_hot(
|
| 74 |
+
labels_t[i], cfg.qwen_K[0]).float()}
|
| 75 |
+
batch.append((zs, tgt))
|
| 76 |
+
out = sys_.batch_step(batch, phase="phase2")
|
| 77 |
+
step_in_phase += 1
|
| 78 |
+
if step_in_phase % 300 == 0:
|
| 79 |
+
sys_.ghsom_growth_pass(carry["Z_N"], parent_qe=float(out["qe_max"]) * 2)
|
| 80 |
+
sys_.rebuild_optimizer()
|
| 81 |
+
if step_in_phase % pgo_every == 0:
|
| 82 |
+
sys_.pgo.tune()
|
| 83 |
+
sys_.pgo_proposed = sys_.pgo.propose()
|
| 84 |
+
tel.routing(event="pgo_update",
|
| 85 |
+
**{k: float(v) for k, v in sys_.pgo.status().items()})
|
| 86 |
+
if step_in_phase % 400 == 0:
|
| 87 |
+
log.info("phase2 step %d loss=%.4f level=%d diff=%.3f",
|
| 88 |
+
step_in_phase, out["loss_total"],
|
| 89 |
+
sys_.diffusion.level, sys_.diffusion.difficulty)
|
| 90 |
+
if step_in_phase % 500 == 0:
|
| 91 |
+
save_state(ckpt_dir / "hako_phase2_partial.npz",
|
| 92 |
+
**sys_.state_blocks())
|
| 93 |
+
|
| 94 |
+
path = save_state(ckpt_dir / "hako_phase2.npz", **sys_.state_blocks())
|
| 95 |
+
freed = loader.purge_source(loader.GRANITE, cfg.hf_cache, mem)
|
| 96 |
+
tel.orchestration(event="phase2_done", steps=int(step_in_phase),
|
| 97 |
+
freed_bytes=int(freed),
|
| 98 |
+
game_level=int(sys_.diffusion.level),
|
| 99 |
+
difficulty=float(sys_.diffusion.difficulty))
|
| 100 |
+
carry["system"] = sys_
|
| 101 |
+
carry["checkpoint"] = str(path)
|
| 102 |
+
return carry
|