chess-sim: RL pilot (ReinFlow-style PPO on SmolVLA), stopped after round 7: checks, logs, round-4 checkpoint
e534973 verified Download chess-sim/experiments/rl_pilot/scripts/step_test.py from Machanize/playful: direct link, hf CLI and curl.
- Browser
- Download file 2.16 kB
-
https://huggingface.co/datasets/Machanize/playful/resolve/main/chess-sim/experiments/rl_pilot/scripts/step_test.py
- Command line
-
hf download hf://datasets/Machanize/playful/chess-sim/experiments/rl_pilot/scripts/step_test.py
-
curl -L -o step_test.py https://huggingface.co/datasets/Machanize/playful/resolve/main/chess-sim/experiments/rl_pilot/scripts/step_test.py
2.16 kB
| import argparse, copy, sys, time | |
| import numpy as np, torch | |
| sys.path.insert(0, "sim") | |
| torch.set_num_threads(2) | |
| from episode import load_config | |
| from rl_env import VecChessEnv | |
| from rl_policy import FlowRL | |
| from rl_train import run_episodes, gae, ppo_update, summarize | |
| if __name__ == "__main__": | |
| cfg = load_config() | |
| pol = FlowRL("models/baseline", cfg["dataset"]["instruction"], 0.2, 0.002) | |
| venv = VecChessEnv(10, cfg) | |
| t0 = time.time() | |
| eps = run_episodes(pol, venv, 7_700_003, list(range(20)), noise=True, early_stop=True, check_expert=False, record=True) | |
| print(f"20 episodes on 10 envs: {time.time()-t0:.1f}s, parts {dict((k, round(v, 1)) for k, v in run_episodes.timing.items())}", flush=True) | |
| venv.close() | |
| gae(eps, 0.99, 0.95) | |
| dec = [d for e in eps for d in e["decisions"]] | |
| for d in dec: | |
| d["adv"] = float(np.random.randn()) | |
| start = {n: p.detach().clone() for n, p in pol.model.named_parameters() if p.requires_grad} | |
| for lr in (3e-7, 1e-6): | |
| with torch.no_grad(): | |
| for n, p in pol.model.named_parameters(): | |
| if n in start: | |
| p.copy_(start[n]) | |
| opt = torch.optim.AdamW([dict(params=pol.policy_params(), lr=lr), dict(params=list(pol.critic.parameters()), lr=3e-4)]) | |
| ns = argparse.Namespace(epochs=1, minibatch=16, clip=0.2, vf_coef=0.5, max_grad_norm=1.0, target_kl=1e9) | |
| t0 = time.time() | |
| out = ppo_update(pol, opt, dec, ns, policy_on=True) | |
| with torch.no_grad(): | |
| new, _ = pol.evaluate(None, torch.stack([d["chain"] for d in dec[:32]]).to(pol.device), | |
| prefix=pol.stack_prefix([d["prefix"] for d in dec[:32]])) | |
| old = torch.stack([d["logp"] for d in dec[:32]]).to(pol.device) | |
| lr_ = (new - old) | |
| kl_el = ((lr_.exp() - 1) - lr_).mean().item() | |
| per_step = lr_.abs().mean(dim=(0, 2, 3)).cpu().numpy().round(3) | |
| print(f"lr {lr}: {len(dec)} decisions, one epoch in {time.time()-t0:.1f}s; during: {out}; after the epoch: per-element KL {kl_el:.4f}, " | |
| f"mean |log-ratio| per denoising step {per_step}", flush=True) | |