File size: 3,846 Bytes
ae5a2ac | 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | import os
import time
import modal
app = modal.App("poker-ai")
vol = modal.Volume.from_name("poker-vol", create_if_missing=True)
image = (
modal.Image.debian_slim(python_version="3.12")
.pip_install("torch==2.9.1", "numpy", "huggingface_hub>=0.34")
.add_local_python_source("poker_core", "model", "train")
.add_local_file("play.py", "/root/play.py")
.add_local_file("README.md", "/root/README.md")
)
REPO = "Alisson990/poker"
GPU = os.environ.get("POKER_GPU", "B200")
def hf_push(out_dir, it, big):
from huggingface_hub import CommitOperationAdd, HfApi
api = HfApi(token=os.environ["HF_TOKEN"])
ops = [CommitOperationAdd("model.pt", os.path.join(out_dir, "model.pt")),
CommitOperationAdd("metrics.jsonl", os.path.join(out_dir, "metrics.jsonl"))]
for f in ("poker_core.py", "model.py", "train.py"):
ops.append(CommitOperationAdd(f, f"/root/{f}"))
ops.append(CommitOperationAdd("play.py", "/root/play.py"))
if big:
ops.append(CommitOperationAdd(f"checkpoints/model_it{it:05d}.pt", os.path.join(out_dir, "model.pt")))
ops.append(CommitOperationAdd("training_state/ckpt.pt", os.path.join(out_dir, "ckpt.pt")))
for attempt in range(3):
try:
api.create_commit(REPO, ops, commit_message=f"iter {it}" + (" (checkpoint)" if big else ""))
print(f"[hf] pushed iter {it} big={big}", flush=True)
return
except Exception as e: # never crash training because of HF
print(f"[hf] push failed ({attempt}): {e}", flush=True)
time.sleep(10)
@app.function(image=image, gpu=GPU, timeout=24 * 3600, volumes={"/vol": vol}, secrets=[modal.Secret.from_name("hf-poker")],
cpu=4.0, memory=32768, retries=modal.Retries(max_retries=2, initial_delay=5.0))
def train_remote(hours: float, run: str = "main", overrides: dict = None, bench_iters: int = 0, push: bool = True):
import copy
import torch
from train import DEFAULT, Trainer
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
print(torch.cuda.get_device_name(0), flush=True)
cfg = copy.deepcopy(DEFAULT)
for k, v in (overrides or {}).items():
cfg[k] = v
out = f"/vol/{run}"
tr = Trainer(cfg, "cuda", out, hf=(hf_push if push else None), log=lambda s: print(s, flush=True))
orig = tr.save_ckpt
def save_and_commit():
orig()
vol.commit()
tr.save_ckpt = save_and_commit
if os.path.exists(os.path.join(out, "ckpt.pt")):
tr.load(os.path.join(out, "ckpt.pt"))
if bench_iters:
tr.t0 = time.time()
for i in range(bench_iters):
t = time.time()
datas = []
for env in tr.envs:
t1 = time.time()
d, h, s = tr.rollout(env)
torch.cuda.synchronize()
print(f" rollout W={env.W} B={env.B} dec={d['act'].shape[0]} hands={h} steps={s} {time.time()-t1:.2f}s", flush=True)
datas.append(d)
t2 = time.time()
st = tr.update(datas, 0.0)
torch.cuda.synchronize()
print(f"iter {i}: total {time.time()-t:.2f}s update {time.time()-t2:.2f}s mem {torch.cuda.max_memory_allocated()/1e9:.1f}GB {st}", flush=True)
t = time.time()
r = tr.evaluate("tag", 6, min_hands=12000)
print("eval tag6", r, f"{time.time()-t:.1f}s", flush=True)
return
tr.train(hours, stop_file=os.path.join(out, "STOP"))
vol.commit()
@app.local_entrypoint()
def main(hours: float = 0.1, run: str = "main", bench: int = 0, push: bool = True, mb: int = 32768, compile: bool = False):
train_remote.remote(hours, run, dict(minibatch=mb, compile=compile), bench, push)
|