File size: 2,097 Bytes
3afc63f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | """Score a finished `train_same_data.py` run on another test set.
Rebuilds the codec and generator from the run's `config.yaml`, `tokenizer.pt`
and `generator.pt`, then applies the trainer's own `evaluate` (same decoding
settings, same metrics) to `<data>/test_puz.npy` and `<data>/test_sol.npy`.
python ncm/eval_same_data.py --upstream /path/to/HazyResearch-ncm \
--run runs/ncm-same-data-s0-resumed --data data/sudoku_c30 \
--out ncm/results/ncm_same_data_c30.json
"""
import argparse
import json
import sys
from pathlib import Path
import torch
from omegaconf import OmegaConf
sys.path.insert(0, str(Path(__file__).resolve().parent))
from train_same_data import evaluate # noqa: E402
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument('--upstream', type=Path, required=True)
ap.add_argument('--run', type=Path, required=True)
ap.add_argument('--data', type=Path, required=True)
ap.add_argument('--out', type=Path, required=True)
ap.add_argument('--batch', type=int, default=64)
ap.add_argument('--device', default='cuda')
a = ap.parse_args()
sys.path.insert(0, str(a.upstream.resolve()))
from models.vqvae import VQVAE
from models.next_concept import NextConceptTransformer
config = OmegaConf.load(a.run / 'config.yaml')
codec = VQVAE(**config.tokenizer.vqvae).to(a.device)
codec.load_state_dict(torch.load(a.run / 'tokenizer.pt', map_location=a.device, weights_only=False)['model'])
codec.eval().requires_grad_(False)
gen = NextConceptTransformer(codec, config.model).to(a.device)
ck = torch.load(a.run / 'generator.pt', map_location=a.device, weights_only=False)
gen.load_state_dict(ck['model'])
result = evaluate(codec, gen, a.data, a.batch, a.device)
result.pop('predictions')
result.update({'run': a.run.name, 'generator_step': ck['step'], 'test_set': a.data.name})
a.out.parent.mkdir(parents=True, exist_ok=True)
a.out.write_text(json.dumps(result, indent=2))
print(json.dumps(result), flush=True)
if __name__ == '__main__':
main()
|