"""AVGC-hard curriculum fine-tune of stem_ms96 from IRM (≠ avgc-upweight λ).

Frozen IRM↔synth teacher (w=0.5 blend of IRM + stem_synth_switch logits) scores
every training frame; hardness = BCE or 1/(|logit|+eps). After warm-up:
  1) upsample hard windows (hardness-weighted end-frame sampling), and
  2) when AVGC is in the train set, ramp AVGC batch fraction above 1/n_env.

Student init: models/stem_ms96_irm/{held-*,all5}.pt (causal -inf mask restored).
Eval EMA 0.995. HMM p_stay 0.994 scale 0.8 + causal tau-30 hp30. Never AASD/Carta labels.

Distinct from avgc-upweight (loss multiplier on AVGC rows) which hurt AVGC H.

usage:
  STEPS=1000 flock multi_agent_opt/gpu.lock python -u stem_ms96_avgc_curriculum.py
  SMOKE=1 STEPS=40 python stem_ms96_avgc_curriculum.py
  TUNE=1 STEPS=1000 ...   # smoke HARD_KIND then full
"""
import copy
import json
import math
import os
import pickle
import time

import numpy as np
import torch
import torch.nn.functional as F

import ensemble_clf_nested as N
import ensemble_clf_transformer_v2 as V
import ensemble_clf_transformer_v4 as V4
import eval_external as E
from common import DS_KEYS

EXP = os.path.dirname(os.path.abspath(__file__))
IRM_DIR = os.path.join(EXP, "models", "stem_ms96_irm")
SYNTH_DIR = os.path.join(EXP, "models", "stem_ms96_synth_switch")
INIT_DIR = os.environ.get("INIT_DIR", IRM_DIR)
CKPT_DIR = os.path.join(EXP, "models", "stem_ms96_avgc_curriculum")
LOG = os.path.join(EXP, "experiments", "stem_ms96_avgc_curriculum.log")
OUT = os.path.join(EXP, "results", "research", "stem_ms96_avgc_curriculum.json")
EVID = "/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/results/research"
TEST_NAMES = ("AASD", "Carta2026")

STEPS = int(os.environ.get("STEPS", "1000"))
BATCH = int(os.environ.get("BATCH", "512"))
SMOKE = os.environ.get("SMOKE", "0") == "1"
TUNE = os.environ.get("TUNE", "0") == "1"
HARD_KIND = os.environ.get("HARD_KIND", "bce").strip().lower()  # bce | abs_logit
TEACHER_W = float(os.environ.get("TEACHER_W", "0.5"))  # blend = w*IRM + (1-w)*synth
WARM_FRAC = float(os.environ.get("WARM_FRAC", "0.2"))
HARD_MIX_MAX = float(os.environ.get("HARD_MIX_MAX", "0.75"))
MAX_AVGC_FRAC = float(os.environ.get("MAX_AVGC_FRAC", "0.45"))
EMA_DECAY = 0.995
LR = 8e-4
P_STAY = 0.994
SCALE = 0.8
CFG = ("stem_ms96", "ms", 96, 64, 2, True, LR)
WINDOW = 96
CHANCE_LOSS = 0.685
FUSE_TIP = dict(H=0.8192, Acc=75.52)  # IRM↔synth w=0.5 AVGC


def log_line(text):
    print(text, flush=True)
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    with open(LOG, "a") as fh:
        fh.write(text + "\n")


def load_stem(path, device):
    blob = torch.load(path, map_location="cpu", weights_only=False)
    name, family, window, d_model, nlayers, stem, _lr = blob["cfg"]
    if name != "stem_ms96" or not stem or window != WINDOW:
        raise RuntimeError(f"unexpected stem cfg {blob['cfg']}")
    c_in = int(np.asarray(blob["mu"]).shape[0])
    net = V.LogitTransformer(c_in, window, d_model=d_model, nlayers=nlayers, stem=True)
    good_mask = net.mask.detach().clone()
    net.load_state_dict(blob["state"])
    net.mask.copy_(good_mask)
    if torch.isnan(net.mask).any():
        raise RuntimeError(f"causal mask still has nan after restore from {path}")
    net = net.to(device)
    mu = np.asarray(blob["mu"], np.float32)
    sd = np.asarray(blob["sd"], np.float32)
    return net, mu, sd, list(blob["train"])


@torch.no_grad()
def teacher_frame_logits(model, feat_ds, mu, sd, device):
    """Per-dataset dict of trial_key -> (T,) logits at each frame (causal w96)."""
    model.eval()
    out = {}
    mu_t = torch.from_numpy(mu.reshape(1, 1, -1))
    sd_t = torch.from_numpy(sd.reshape(1, 1, -1))
    for ds, table in feat_ds.items():
        out[ds] = {}
        for key, X in table.items():
            T, C = X.shape
            pad = np.zeros((WINDOW - 1, C), np.float32)
            xp = torch.from_numpy(np.concatenate([pad, X], 0))
            pieces = []
            for i in range(0, T, 2048):
                sl = xp[i:i + 2048 + WINDOW - 1]
                w = sl.unfold(0, WINDOW, 1).permute(0, 2, 1).contiguous()
                w = (w - mu_t) / sd_t
                pieces.append(model(w.to(device))[:, -1].cpu().numpy())
            out[ds][key] = np.concatenate(pieces).astype(np.float64)
    return out


def blend_logits(irm_log, synth_log, w):
    out = {}
    for ds in irm_log:
        out[ds] = {}
        for key in irm_log[ds]:
            out[ds][key] = w * irm_log[ds][key] + (1.0 - w) * synth_log[ds][key]
    return out


def hardness_from_logit(logit, y, kind):
    logit = np.asarray(logit, np.float64)
    y = np.asarray(y, np.float64)
    if kind == "abs_logit":
        return 1.0 / (np.abs(logit) + 0.05)
    # BCE with logits, numerically stable via softplus
    # bce = softplus(-z) if y=1 else softplus(z); z = logit * (2y-1) style
    z = logit
    # F.binary_cross_entropy_with_logits: max(z,0) - z*y + log(1+exp(-|z|))
    bce = np.maximum(z, 0) - z * y + np.log1p(np.exp(-np.abs(z)))
    return np.maximum(bce, 1e-6)


def build_group_hardness(groups, train_ds, feat_ms, teacher_log, kind):
    """Per-group flat hardness aligned with sample_batch's pick index over g['total']."""
    frame_w = []
    stats = {}
    for g, ds in zip(groups, train_ds):
        keys = list(feat_ms[ds].keys())
        parts = []
        for ti, (X, y) in enumerate(g["trials"]):
            key = keys[ti]
            log = teacher_log[ds][key]
            if len(log) != len(y):
                raise RuntimeError(f"teacher len {len(log)} != y {len(y)} for {ds}/{key}")
            parts.append(hardness_from_logit(log, y, kind).astype(np.float64))
        w = np.concatenate(parts)
        if len(w) != g["total"]:
            raise RuntimeError(f"hardness len {len(w)} != total {g['total']} for {ds}")
        w = w / max(float(w.mean()), 1e-8)
        frame_w.append(w)
        stats[ds] = dict(mean=float(w.mean()), p90=float(np.quantile(w, 0.9)),
                         max=float(w.max()), n=int(len(w)))
    return frame_w, stats


def sample_batch_curriculum(rng, groups, W, batch, mu, sd, frame_w, hard_mix, avgc_idx, avgc_frac):
    """Dataset-balanced (or AVGC-frac) windows; within-ds mix uniform vs hardness."""
    C = groups[0]["trials"][0][0].shape[1]
    Xo = np.zeros((batch, W, C), np.float32)
    yo = np.zeros((batch, W), np.float32)
    mo = np.zeros((batch, W), np.float32)
    n_env = len(groups)

    if avgc_idx is not None and avgc_frac is not None and n_env > 1:
        n_avgc = int(round(batch * float(avgc_frac)))
        n_avgc = min(max(n_avgc, 1), batch - (n_env - 1))
        rem = batch - n_avgc
        others = [i for i in range(n_env) if i != avgc_idx]
        base = rem // len(others)
        extra = rem - base * len(others)
        counts = np.zeros(n_env, np.int64)
        counts[avgc_idx] = n_avgc
        for j, oi in enumerate(others):
            counts[oi] = base + (1 if j < extra else 0)
        ds_of = np.concatenate([np.full(int(counts[i]), i, np.int64) for i in range(n_env)])
        rng.shuffle(ds_of)
    else:
        ds_of = np.arange(batch) % n_env

    for g_i, g in enumerate(groups):
        rows = np.flatnonzero(ds_of == g_i)
        if len(rows) == 0:
            continue
        n = len(rows)
        n_hard = int(round(n * float(hard_mix)))
        n_hard = min(max(n_hard, 0), n)
        picks = np.empty(n, np.int64)
        if n_hard > 0 and frame_w is not None:
            w = frame_w[g_i]
            p = w / w.sum()
            picks[:n_hard] = rng.choice(g["total"], size=n_hard, replace=True, p=p)
        if n_hard < n:
            picks[n_hard:] = rng.integers(0, g["total"], size=n - n_hard)
        which = np.searchsorted(g["ends"], picks, side="right")
        local = picks - (g["ends"] - g["lengths"])[which]
        trials = g["trials"]
        for row, ti, t in zip(rows, which, local):
            x, y = trials[int(ti)]
            t = int(t)
            a = t - W + 1
            if a >= 0:
                Xo[row] = x[a:t + 1]
                yo[row] = y[a:t + 1]
                mo[row] = 1.0
            else:
                nn = t + 1
                Xo[row, -nn:] = x[:nn]
                yo[row, -nn:] = y[:nn]
                mo[row, -nn:] = 1.0
    Xo -= mu
    Xo /= sd
    return Xo, yo, mo


def schedule(step, total, warm_frac, hard_mix_max, max_avgc_frac, n_env, avgc_idx):
    warm = int(total * warm_frac)
    base_frac = 1.0 / max(n_env, 1)
    if step < warm:
        return 0.0, (base_frac if avgc_idx is not None else None)
    p = (step - warm) / max(total - warm, 1)
    # cosine ramp 0→1
    ramp = 0.5 * (1.0 - math.cos(math.pi * p))
    hard_mix = hard_mix_max * ramp
    if avgc_idx is None:
        avgc_frac = None
    else:
        avgc_frac = base_frac + (max_avgc_frac - base_frac) * ramp
        avgc_frac = min(max(avgc_frac, base_frac), 0.7)
    return hard_mix, avgc_frac


def weighted_bce(logit, y, m, pos_weight):
    sw = V.switch_weights(y, m)
    bce = F.binary_cross_entropy_with_logits(logit, y, pos_weight=pos_weight, reduction="none")
    denom = (sw * m).sum().clamp_min(1.0)
    loss = (bce * sw * m).sum() / denom
    dv = (logit[:, 1:] - logit[:, :-1]).abs()
    m2 = m[:, 1:] * m[:, :-1]
    loss = loss + 0.05 * (dv * m2).sum() / m2.sum().clamp_min(1.0)
    return loss


def set_lr(opt, step, total, base):
    warm = min(40, max(total // 10, 1))
    if step < warm:
        lr = base * (step + 1) / warm
    else:
        p = (step - warm) / max(total - warm, 1)
        lr = 1e-5 + 0.5 * (base - 1e-5) * (1.0 + math.cos(math.pi * p))
    for group in opt.param_groups:
        group["lr"] = lr
    return lr


def train_curriculum(model, groups, train_ds, y_all, mu, sd, frame_w, device, steps, batch, tag,
                     hard_kind, abort_after=50):
    pos = float(y_all.mean())
    pos_weight = torch.tensor([(1.0 - pos) / max(pos, 1e-3)], device=device)
    opt = torch.optim.AdamW(model.parameters(), lr=LR, weight_decay=1e-4)
    rng = np.random.default_rng(V.TRAIN_SEED)
    ema = copy.deepcopy(model).eval()
    model.train()
    n_env = len(groups)
    avgc_idx = next((i for i, d in enumerate(train_ds) if d == "AVGC"), None)
    t0 = time.time()
    last = None
    early_losses = []
    last_hm = last_af = 0.0
    for step in range(steps):
        set_lr(opt, step, steps, LR)
        hard_mix, avgc_frac = schedule(
            step, steps, WARM_FRAC, HARD_MIX_MAX, MAX_AVGC_FRAC, n_env, avgc_idx)
        last_hm, last_af = hard_mix, (avgc_frac if avgc_frac is not None else -1.0)
        xb, yb, mb = sample_batch_curriculum(
            rng, groups, WINDOW, batch, mu, sd, frame_w, hard_mix, avgc_idx, avgc_frac)
        x = torch.from_numpy(xb).to(device, non_blocking=True)
        y = torch.from_numpy(yb).to(device, non_blocking=True)
        m = torch.from_numpy(mb).to(device, non_blocking=True)
        x = V.augment(x)
        opt.zero_grad(set_to_none=True)
        loss = weighted_bce(model(x), y, m, pos_weight)
        if not torch.isfinite(loss):
            raise RuntimeError(f"non-finite loss at step {step} for {tag}")
        loss.backward()
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        opt.step()
        V.ema_update(model, ema, EMA_DECAY)
        last = float(loss.item())
        if step < abort_after:
            early_losses.append(last)
            if step + 1 == abort_after:
                mean_early = float(np.mean(early_losses))
                log_line(f"    {tag} early-abort check mean_loss@{abort_after}={mean_early:.4f}")
                if mean_early >= CHANCE_LOSS:
                    raise RuntimeError(
                        f"early abort {tag}: mean loss {mean_early:.4f} >= chance {CHANCE_LOSS}")
        if step == 0 or (step + 1) % 200 == 0 or step + 1 == steps:
            af_s = f"{avgc_frac:.3f}" if avgc_frac is not None else "na"
            log_line(
                f"    {tag} step {step + 1}/{steps} loss {last:.4f} "
                f"hard_mix {hard_mix:.3f} avgc_frac {af_s} kind {hard_kind}")
    ema.eval()
    acc = V.train_frame_acc(ema, groups, WINDOW, mu, sd, device)
    log_line(
        f"  trained {tag} loss {last:.4f} frame-acc {acc:.3f} kind {hard_kind} "
        f"avgc_in_train={avgc_idx is not None} ({time.time() - t0:.0f}s)")
    return ema, dict(
        loss=last, frame_acc=acc, hard_kind=hard_kind,
        avgc_in_train=avgc_idx is not None,
        final_hard_mix=float(last_hm), final_avgc_frac=float(last_af),
        hard_mix_max=HARD_MIX_MAX, max_avgc_frac=MAX_AVGC_FRAC,
        warm_frac=WARM_FRAC, teacher_w=TEACHER_W)


def fit_with_oom(init_path, groups, train_ds, y_all, frame_w, device, steps, batch, tag,
                 hard_kind, halved=False):
    model, mu, sd, _train = load_stem(init_path, device)
    try:
        ema, stats = train_curriculum(
            model, groups, train_ds, y_all, mu, sd, frame_w, device, steps, batch, tag, hard_kind)
        return ema, mu, sd, batch, stats
    except torch.cuda.OutOfMemoryError:
        if halved:
            raise
        log_line(f"  OOM on {tag} at batch {batch}; halving once")
        del model
        if device.type == "cuda":
            torch.cuda.empty_cache()
        return fit_with_oom(
            init_path, groups, train_ds, y_all, frame_w, device, steps, batch // 2, tag,
            hard_kind, halved=True)


def hp30_stream(stream):
    out = {}
    for key, d in stream.items():
        out[key] = V4.highpass(np.asarray(d, np.float64)[:, None]).reshape(-1).astype(np.float64)
    return out


def score_lodo(model, feat, mu, sd, held, device):
    streams = V.infer_all(model, feat, mu, sd, WINDOW, device)
    ev = N.to_ev(streams)
    off = N.official(held, ev, P_STAY, SCALE)
    hp = {ds: hp30_stream(streams[ds]) for ds in streams}
    ev_hp = N.to_ev(hp)
    off_hp = N.official(held, ev_hp, P_STAY, SCALE)
    log_line(f"LODO {held} p {P_STAY} sc {SCALE}  H {off['H']:.4f} Acc {off['Acc']:.2f} "
             f"L {off['L']:.3f} SDT {off['SDT']:.2f}")
    log_line(f"LODO_hp30 {held} p {P_STAY} sc {SCALE}  H {off_hp['H']:.4f} Acc {off_hp['Acc']:.2f}")
    return dict(curr=off, hp30=off_hp), streams


def score_external(model, feat_ext, meta, mu_in, sd_in, corpus_streams, corpus_att, device):
    parts = []
    for ds, table in corpus_streams.items():
        labels = corpus_att[ds]
        for key, d in table.items():
            parts.append(np.asarray(d, np.float64) * np.where(labels[key] == 0, 1.0, -1.0))
    mu_e = float(np.concatenate(parts).mean())
    sd_e = float(np.concatenate(parts).std())
    log_line(f"  external emissions mu {mu_e:.4f} sd {sd_e:.4f} scale {SCALE}")

    hp_corpus = {ds: hp30_stream(corpus_streams[ds]) for ds in corpus_streams}
    parts_hp = []
    for ds, table in hp_corpus.items():
        labels = corpus_att[ds]
        for key, d in table.items():
            parts_hp.append(np.asarray(d, np.float64) * np.where(labels[key] == 0, 1.0, -1.0))
    mu_hp = float(np.concatenate(parts_hp).mean())
    sd_hp = float(np.concatenate(parts_hp).std())
    log_line(f"  external hp30 emissions mu {mu_hp:.4f} sd {sd_hp:.4f} scale {SCALE}")

    rows = {}
    for ds in TEST_NAMES:
        stream = V.infer_all(model, {ds: feat_ext[ds]}, mu_in, sd_in, WINDOW, device)[ds]
        ev = {}
        for key, d in stream.items():
            meta_k = meta[ds][key]
            d = np.asarray(d, np.float64)
            ev[key] = dict(r0=d, r1=np.zeros(len(d)), att=meta_k["att"], t_end=meta_k["t_end"],
                           att128=meta_k["att128"], n128=meta_k["n128"], subject=meta_k["subject"])
        r = E.score(ev, mu_e, sd_e * SCALE, P_STAY, 0.75)
        stream_hp = hp30_stream(stream)
        ev_hp = {}
        for key, d in stream_hp.items():
            meta_k = meta[ds][key]
            d = np.asarray(d, np.float64)
            ev_hp[key] = dict(r0=d, r1=np.zeros(len(d)), att=meta_k["att"], t_end=meta_k["t_end"],
                              att128=meta_k["att128"], n128=meta_k["n128"], subject=meta_k["subject"])
        r_hp = E.score(ev_hp, mu_hp, sd_hp * SCALE, P_STAY, 0.75)
        rows[ds] = dict(curr=r, hp30=r_hp)
        log_line(f"EXT {ds} p {P_STAY} sc {SCALE}  H {r['H']:.4f} Acc {r['Acc']:.2f} "
                 f"L {r['L']:.3f} SDT {r['SDT']:.2f}")
        log_line(f"EXT_hp30 {ds} p {P_STAY} sc {SCALE}  H {r_hp['H']:.4f} Acc {r_hp['Acc']:.2f}")
    return rows


def load_external():
    E.CONF.update(prep="eaP_n5_t60", targets=["env", "onset", "dom"])
    logits, meta = {}, {}
    for ds in TEST_NAMES:
        evs = []
        for i in range(4):
            path = os.path.join(EVID, f"ext_clf_{ds}_w10b3sw2P_evidence_s{i}.pkl")
            with open(path, "rb") as fh:
                evs.append(pickle.load(fh))
        logits[ds], meta[ds] = {}, {}
        for key in evs[0]:
            cols = [(ev[key]["r0"] - ev[key]["r1"]).astype(np.float32) for ev in evs]
            e0 = evs[0][key]
            logits[ds][key] = np.stack(cols, 1)
            meta[ds][key] = dict(
                att=np.asarray(e0["att"]), t_end=np.asarray(e0["t_end"], np.float64),
                att128=np.asarray(e0["att128"]), n128=int(e0["n128"]), subject=int(e0["subject"]))
        log_line(f"  external {ds}: {len(logits[ds])} trials")
    return logits, meta


def build_teacher_for_fold(held, feat_all, device, hard_kind):
    """Frozen IRM↔synth teacher on the same held-* ckpts; hardness over train datasets."""
    train_ds = [d for d in DS_KEYS if d != held] if held is not None else list(DS_KEYS)
    tag = f"held-{held}" if held is not None else "all5"
    irm_path = os.path.join(IRM_DIR, f"{tag}.pt")
    synth_path = os.path.join(SYNTH_DIR, f"{tag}.pt")
    if not os.path.isfile(irm_path):
        raise FileNotFoundError(irm_path)
    if not os.path.isfile(synth_path):
        raise FileNotFoundError(synth_path)

    irm, mu_i, sd_i, _ = load_stem(irm_path, device)
    log_line(f"  teacher IRM {irm_path}")
    irm_log = teacher_frame_logits(irm, {d: feat_all[d] for d in train_ds}, mu_i, sd_i, device)
    del irm
    if device.type == "cuda":
        torch.cuda.empty_cache()

    synth, mu_s, sd_s, _ = load_stem(synth_path, device)
    log_line(f"  teacher synth {synth_path}")
    # use IRM mu/sd for synth scoring consistency with student ms features; synth ckpt
    # mu/sd are the same family (ms) from same recipe — prefer synth's own stats for its net
    synth_log = teacher_frame_logits(synth, {d: feat_all[d] for d in train_ds}, mu_s, sd_s, device)
    del synth
    if device.type == "cuda":
        torch.cuda.empty_cache()

    blended = blend_logits(irm_log, synth_log, TEACHER_W)
    groups, y_all = V.trial_groups(feat_all, train_ds)
    # Align trial keys: trial_groups iterates feat[ds].items() — pass same feat subset
    feat_sub = {d: feat_all[d] for d in train_ds}
    frame_w, hstats = build_group_hardness(groups, train_ds, feat_sub, blended, hard_kind)
    log_line(f"  hardness[{hard_kind}] " + ", ".join(
        f"{d}:p90={hstats[d]['p90']:.3f}" for d in train_ds))
    return groups, y_all, frame_w, hstats, train_ds


def smoke_kind(feat_all, device):
    held = "UHD"  # AVGC in train
    candidates = [x.strip() for x in os.environ.get("SMOKE_KINDS", "bce,abs_logit").split(",") if x.strip()]
    smoke_steps = int(os.environ.get("SMOKE_STEPS", "40"))
    results = []
    for kind in candidates:
        log_line(f"SMOKE HARD_KIND={kind} steps={smoke_steps} held={held}")
        try:
            groups, y_all, frame_w, hstats, train_ds = build_teacher_for_fold(
                held, feat_all, device, kind)
            path = os.path.join(INIT_DIR, f"held-{held}.pt")
            ema, mu, sd, used, stats = fit_with_oom(
                path, groups, train_ds, y_all, frame_w, device, smoke_steps, BATCH,
                f"smoke-{kind}", kind)
            del ema
            if device.type == "cuda":
                torch.cuda.empty_cache()
            ok = (stats["loss"] is not None and math.isfinite(stats["loss"])
                  and stats["frame_acc"] > 0.55 and stats["avgc_in_train"])
            row = dict(ok=ok, batch=used, hardness=hstats, **stats)
            results.append(row)
            log_line(f"SMOKE HARD_KIND={kind} ok={ok} loss={stats['loss']:.4f} "
                     f"frame={stats['frame_acc']:.3f}")
        except Exception as exc:
            log_line(f"SMOKE HARD_KIND={kind} FAILED: {exc}")
            results.append(dict(hard_kind=kind, ok=False, error=str(exc)))
            if device.type == "cuda":
                torch.cuda.empty_cache()
    ok_rows = [r for r in results if r.get("ok")]
    if not ok_rows:
        raise RuntimeError(f"all smoke kinds failed: {results}")
    # Prefer higher frame_acc; tie-break toward bce
    best = max(ok_rows, key=lambda r: (r["frame_acc"], 0 if r["hard_kind"] == "bce" else -1))
    log_line(f"SMOKE chose HARD_KIND={best['hard_kind']} frame={best['frame_acc']:.3f} "
             f"loss={best['loss']:.4f}")
    return best["hard_kind"], results


def main():
    os.makedirs(os.path.dirname(LOG), exist_ok=True)
    open(LOG, "w").close()
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    log_line(
        f"device {device} steps {STEPS} batch {BATCH} HARD_KIND {HARD_KIND} "
        f"teacher_w {TEACHER_W} hard_mix_max {HARD_MIX_MAX} max_avgc_frac {MAX_AVGC_FRAC} "
        f"warm {WARM_FRAC} init {INIT_DIR} smoke={SMOKE} tune={TUNE}")
    log_line(f"fuse tip AVGC {FUSE_TIP['H']:.4f}/{FUSE_TIP['Acc']:.2f} (IRM↔synth w=0.5)")
    torch.backends.cuda.matmul.allow_tf32 = True
    torch.backends.cudnn.allow_tf32 = True
    V.assert_causal(device)
    N.load_all()
    feat_all = V.build_features()["ms"]
    os.makedirs(CKPT_DIR, exist_ok=True)

    hard_kind = HARD_KIND
    smoke_info = None
    if SMOKE or TUNE:
        hard_kind, smoke_info = smoke_kind(feat_all, device)
        if SMOKE:
            out = dict(smoke=smoke_info, chosen_hard_kind=hard_kind, init_dir=INIT_DIR,
                       teacher_w=TEACHER_W)
            os.makedirs(os.path.dirname(OUT), exist_ok=True)
            with open(OUT.replace(".json", "_smoke.json"), "w") as fh:
                json.dump(out, fh, indent=1, default=float)
            log_line("SMOKE done; exiting without full score")
            return

    per = {}
    for held in DS_KEYS:
        path = os.path.join(INIT_DIR, f"held-{held}.pt")
        if not os.path.isfile(path):
            raise FileNotFoundError(path)
        log_line(f"fold held-out={held} init {path} kind={hard_kind}")
        groups, y_all, frame_w, hstats, train_ds = build_teacher_for_fold(
            held, feat_all, device, hard_kind)
        ema, mu, sd, used, stats = fit_with_oom(
            path, groups, train_ds, y_all, frame_w, device, STEPS, BATCH,
            f"held-{held}", hard_kind)
        torch.save({"state": ema.state_dict(), "mu": mu, "sd": sd, "init": path,
                    "train": train_ds, "steps": STEPS, "batch": used,
                    "hard_kind": hard_kind, "stats": stats, "hardness": hstats,
                    "cfg": CFG, "teacher_w": TEACHER_W},
                   os.path.join(CKPT_DIR, f"held-{held}.pt"))
        scores, _streams = score_lodo(ema, feat_all, mu, sd, held, device)
        per[held] = dict(scores=scores, stats=stats, batch=used, hardness=hstats)
        del ema
        if device.type == "cuda":
            torch.cuda.empty_cache()

    log_line(f"all-5 AVGC-curriculum fine-tune for AASD/Carta, init {INIT_DIR}/all5.pt")
    groups, y_all, frame_w, hstats, train_ds = build_teacher_for_fold(
        None, feat_all, device, hard_kind)
    all_path = os.path.join(INIT_DIR, "all5.pt")
    ema, mu, sd, used, stats = fit_with_oom(
        all_path, groups, train_ds, y_all, frame_w, device, STEPS, BATCH, "all5", hard_kind)
    torch.save({"state": ema.state_dict(), "mu": mu, "sd": sd, "init": all_path,
                "train": train_ds, "steps": STEPS, "batch": used,
                "hard_kind": hard_kind, "stats": stats, "hardness": hstats,
                "cfg": CFG, "teacher_w": TEACHER_W},
               os.path.join(CKPT_DIR, "all5.pt"))
    corpus_streams = V.infer_all(ema, feat_all, mu, sd, WINDOW, device)
    ext_logits, ext_meta = load_external()
    ext_feat = V4.build_features_from(ext_logits)["ms"]
    ext = score_external(ema, ext_feat, ext_meta, mu, sd, corpus_streams, N.G["att"], device)

    mean_h = float(np.mean([per[d]["scores"]["curr"]["H"] for d in DS_KEYS]))
    mean_acc = float(np.mean([per[d]["scores"]["curr"]["Acc"] for d in DS_KEYS]))
    avgc_h = float(per["AVGC"]["scores"]["curr"]["H"])
    avgc_a = float(per["AVGC"]["scores"]["curr"]["Acc"])
    log_line(f"LODO mean H {mean_h:.4f} Acc {mean_acc:.2f}")
    log_line(
        f"AVGC H {avgc_h:.4f} Acc {avgc_a:.2f} vs fuse tip "
        f"{FUSE_TIP['H']:.4f}/{FUSE_TIP['Acc']:.2f} "
        f"ΔH {avgc_h - FUSE_TIP['H']:+.4f} ΔAcc {avgc_a - FUSE_TIP['Acc']:+.2f}")

    out = dict(
        p_stay=P_STAY, scale=SCALE, steps=STEPS, batch=BATCH,
        hard_kind=hard_kind, teacher_w=TEACHER_W,
        hard_mix_max=HARD_MIX_MAX, max_avgc_frac=MAX_AVGC_FRAC, warm_frac=WARM_FRAC,
        init_dir=INIT_DIR, smoke=smoke_info, fuse_tip_avgc=FUSE_TIP,
        lodo={d: per[d]["scores"] for d in DS_KEYS},
        lodo_stats={d: per[d]["stats"] for d in DS_KEYS},
        lodo_mean=dict(H=mean_h, Acc=mean_acc),
        avgc=dict(H=avgc_h, Acc=avgc_a),
        external=ext,
        all5_stats=stats,
    )
    os.makedirs(os.path.dirname(OUT), exist_ok=True)
    with open(OUT, "w") as fh:
        json.dump(out, fh, indent=1, default=float)
    log_line(f"wrote {OUT}")


if __name__ == "__main__":
    main()
