"""Probe what fits and what is fast on this GPU. Two safety rules learned the hard way: 1. NEVER instantiate a large model on CPU. Build it inside `with torch.device('cuda')` so parameters land on the GPU from the start. Building a 2.7B model on CPU needs ~11GB of host RAM, and building it twice back-to-back hangs a 30GB machine via swap thrash. 2. Count parameters on the `meta` device, which allocates nothing. Also estimates memory before trying, and skips runs that clearly will not fit. """ import gc import time import torch from model import GPT, MMConfig PEAK_TFLOPS = 168.0 # measured bf16 matmul peak on this card def make(n_layer, n_embd, n_head, n_kv, n_stream, block, vocab=32391): return MMConfig( block_size=block, vocab_size=vocab, n_layer=n_layer, n_embd=n_embd, n_head=n_head, n_kv_head=n_kv, n_stream=n_stream, use_mtp=False, ) def count_params(cfg): with torch.device("meta"): m = GPT(cfg) return sum(p.numel() for p in m.parameters()) def estimate_gb(cfg, batch, bytes_per_param=14.0): """Rough memory estimate: optimizer state + activations.""" n = count_params(cfg) state = n * bytes_per_param / 1e9 tokens = batch * cfg.block_size act = tokens * cfg.n_layer * cfg.n_embd * 12 * 2 * max(1, cfg.n_stream) / 1e9 return state, act def try_run(cfg, batch, device="cuda", iters=6): gc.collect() torch.cuda.empty_cache() torch.cuda.reset_peak_memory_stats() m = opt = x = y = None try: with torch.device(device): m = GPT(cfg) opt = torch.optim.AdamW(m.parameters(), lr=1e-4) x = torch.randint(0, cfg.vocab_size, (batch, cfg.block_size), device=device) y = torch.randint(0, cfg.vocab_size, (batch, cfg.block_size), device=device) for _ in range(2): with torch.amp.autocast("cuda", dtype=torch.bfloat16): loss = m(x, y)["loss"] loss.backward() opt.step() opt.zero_grad(set_to_none=True) torch.cuda.synchronize() t = time.time() for _ in range(iters): with torch.amp.autocast("cuda", dtype=torch.bfloat16): loss = m(x, y)["loss"] loss.backward() opt.step() opt.zero_grad(set_to_none=True) torch.cuda.synchronize() dt = (time.time() - t) / iters return dict(ok=True, mem=torch.cuda.max_memory_allocated() / 1e9, dt=dt, tok_s=batch * cfg.block_size / dt) except torch.cuda.OutOfMemoryError: return dict(ok=False) finally: del m, opt, x, y gc.collect() torch.cuda.empty_cache() def main(): torch.backends.cuda.matmul.allow_tf32 = True free_gb = torch.cuda.mem_get_info()[0] / 1e9 print(f"GPU free: {free_gb:.1f} GB, peak measured {PEAK_TFLOPS:.0f} TFLOPS bf16\n") sizes = [ ("124M", make(12, 768, 12, 4, 2, 512)), ("350M", make(24, 1024, 16, 4, 2, 512)), ("760M", make(24, 1536, 16, 4, 1, 512)), ("1.3B", make(24, 2048, 16, 4, 1, 512)), ("2.7B", make(32, 2560, 20, 4, 1, 512)), ] print(f"{'size':>6} {'params':>9} {'batch':>6} {'mem GB':>8} {'tok/s':>9} " f"{'TFLOPS':>8} {'MFU':>6} {'chinchilla':>12}") for name, cfg in sizes: n = count_params(cfg) state, act1 = estimate_gb(cfg, 1) if state > free_gb * 0.75: print(f"{name:>6} {n/1e6:8.1f}M {'--':>6} {'SKIP':>8} " f"optimizer state {state:.1f}GB exceeds {free_gb*0.75:.1f}GB budget") continue best = None for b in (64, 32, 16, 8, 4, 2, 1): if state + act1 * b > free_gb * 0.92: continue r = try_run(cfg, b) if r["ok"]: best = (b, r) break if best is None: print(f"{name:>6} {n/1e6:8.1f}M {'--':>6} {'OOM':>8}") continue b, r = best tf = 6 * n * r["tok_s"] / 1e12 hrs = (6 * n * 20 * n) / (tf * 1e12) / 3600 if tf > 0 else 0 print(f"{name:>6} {n/1e6:8.1f}M {b:6d} {r['mem']:8.2f} {r['tok_s']:9.0f} {tf:8.1f} " f"{tf/PEAK_TFLOPS*100:5.1f}% {hrs:9.1f} h") if __name__ == "__main__": main()