import numpy as np, torch, torch.nn.functional as F from model import GPT, TEXT_VOCAB, IMAGE_OFFSET, IMAGE_VOCAB from train import get_batch_rows raw = np.memmap("../data_mm/val.bin", dtype=np.uint16, mode="r").reshape(-1,320) for p in ["out_mm2/ckpt_iter2000.pt","out_mm2/ckpt_iter6000.pt"]: ck = torch.load(p, map_location="cuda", weights_only=False) m = GPT(ck["cfg"]).cuda(); m.load_state_dict(ck["model"]); m.eval() st = {"txt_n":0,"txt_l":0.0,"img_n":0,"img_l":0.0} with torch.no_grad(): for _ in range(20): x,y,_ = get_batch_rows(raw, 320, 32, "cuda", False) with torch.amp.autocast("cuda", dtype=torch.bfloat16): logits = m(x)["logits"].float() l = F.cross_entropy(logits.view(-1,logits.size(-1)), y.reshape(-1), ignore_index=-100, reduction="none") valid = (y.reshape(-1) != -100) tg = y.reshape(-1) is_img = valid & (tg >= IMAGE_OFFSET) & (tg < IMAGE_OFFSET+IMAGE_VOCAB) is_txt = valid & (tg < TEXT_VOCAB) st["img_n"] += is_img.sum().item(); st["img_l"] += l[is_img].sum().item() st["txt_n"] += is_txt.sum().item(); st["txt_l"] += l[is_txt].sum().item() print("%s text %.4f (ppl %.1f) image %.4f (ppl %.1f)" % ( p.split("/")[-1], st["txt_l"]/st["txt_n"], np.exp(st["txt_l"]/st["txt_n"]), st["img_l"]/st["img_n"], np.exp(st["img_l"]/st["img_n"])))