beyarkay's picture
Lane re-export scripts and progress, window debugging scripts
0a3edb6 verified
Raw History Blame Contribute Delete
2.49 kB
"""Best-of-N builds of the human tasks by each model, cleared to their markers (and context)."""
import json, os, pickle, sys, time
import numpy as np, torch
from denoise import CH_PART, generate, legalize, load_denoiser, score_world, world_to_state
from factorion import Channel, Footprint
from sft import resolve_checkpoint
from training_config import RlArgs, SftArgs
tasks, CHAINS, OUT = pickle.load(open(sys.argv[1], "rb")), int(sys.argv[2]), sys.argv[3]
MODELS = {"synthetic only": "79yi1u8j", "5M human -> 5M synth": "1iurwrdh", "50% human mix": "g6yyh2oo", "human only (5M)": "1rzgjhuv"}
torch.set_num_threads(4)
args = SftArgs()
def blank(w):
b = w.copy()
free = b[Channel.FOOTPRINT.value] == Footprint.BUILDABLE.value
for c in (Channel.ENTITIES, Channel.DIRECTION, Channel.ITEMS, Channel.MISC):
b[c.value][free] = 0
return b
rows = json.load(open(OUT)) if os.path.exists(OUT) else [] # resume: one checkpoint per (variant, model)
for variant in ("world", "context"):
cond = torch.as_tensor(np.stack([world_to_state(blank(t[variant])) for t in tasks]))
for name, rid in MODELS.items():
if any(r["variant"] == variant and r["model"] == name for r in rows):
continue
t0 = time.time()
model = load_denoiser(resolve_checkpoint(rid, args.wandb_project_name, args.wandb_entity)[0], torch.device("cpu")).eval()
gen = torch.Generator().manual_seed(0)
for j in range(CHAINS):
toks, conf, _, _ = generate(model, cond, args.gen_steps, args.gen_refine, args.gen_temperature, gen)
typ, rec, part = (x.numpy() for x in toks)
for i, t in enumerate(tasks):
world = legalize(cond[i, :CH_PART].numpy().astype(np.int64), typ[i], rec[i], part[i], conf[i].float().numpy())
r = score_world(world, t["max_throughput"], RlArgs.entity_cost_scale)
rows.append({"variant": variant, "model": name, "task": i, "chain": j, "thput": r["thput"], "reward": r["score"]})
sub = [r for r in rows if r["variant"] == variant and r["model"] == name]
best = [max(r["thput"] for r in sub if r["task"] == i) for i in range(len(tasks))]
print(f"{variant:8s} {name:22s} best-of-{CHAINS} thput {np.mean(best):.3f} solved>0 {np.mean([b > 0 for b in best]):.0%} >=0.5 {np.mean([b >= 0.5 for b in best]):.0%} ({time.time() - t0:.0f}s)", flush=True)
json.dump(rows, open(OUT + ".tmp", "w"))
os.replace(OUT + ".tmp", OUT)