File size: 1,872 Bytes
020f489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""HF Jobs entry point: train 12 policies (4 games x 3 seeds) in parallel, make 2 random-init
nulls, and upload raw labelled weights + stats to the PRIVATE raw repo."""
import json, os, sys, multiprocessing as mp
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import numpy as np, torch
from huggingface_hub import HfApi

REPO = "SoulFireMage/weight-reading-probe-raw"
OUT = "/tmp/out"
STEPS = int(os.environ.get("STEPS", "0")) or None
PREFIX = os.environ.get("PREFIX", "raw")


def job(args):
    game, seed = args
    from train import train, weights_dict
    pi, stats = train(game, seed, STEPS)
    np.savez(f"{OUT}/{game}_s{seed}.npz", **weights_dict(pi))
    HfApi().upload_file(path_or_fileobj=f"{OUT}/{game}_s{seed}.npz", repo_id=REPO, repo_type="dataset",
                        path_in_repo=f"{PREFIX}/{game}_s{seed}.npz", commit_message=f"raw {game} s{seed}")
    return stats


if __name__ == "__main__":
    os.makedirs(OUT, exist_ok=True)
    from train import policy_net, weights_dict
    for i, s in enumerate([900, 901]):
        torch.manual_seed(s)
        np.savez(f"{OUT}/random_init_{i}.npz", **weights_dict(policy_net()))
    runs = [(g, s) for g in ["breakout", "pong", "pacman", "freeway"] for s in (0, 1, 2)]
    with mp.get_context("spawn").Pool(min(len(runs), os.cpu_count())) as p:
        stats = p.map(job, runs)
    json.dump(stats, open(f"{OUT}/train_stats.json", "w"), indent=1)
    api = HfApi()
    for f in ["random_init_0.npz", "random_init_1.npz", "train_stats.json"]:
        api.upload_file(path_or_fileobj=f"{OUT}/{f}", repo_id=REPO, repo_type="dataset",
                        path_in_repo=f"{PREFIX}/{f}", commit_message=f"raw {f}")
    for s in stats:
        print(s["game"], s["seed"], round(s["trained_mean"], 2), "vs random", round(s["random_mean"], 2),
              "gate", s["passes_gate"], flush=True)