Upload train.py with huggingface_hub
Browse files127 unmodified lines12812913013113213313413513613713813914010 unmodified lines15115215315415515615716 unmodified lines1741751761771781796 unmodified lines1861871881891901911926 unmodified lines199200201202203204205206207208209210211127 unmodified linesif not os.path.exists(p):ds = load_dataset("nick007x/github-code-2025", split="train", streaming=True)arr = np.array(_encode_stream(ds, t, "content", cfg.max_tokens_code), dtype=np.uint32)assert arr.max() < V, f"code 越界: max={arr.max()}, V={V}"np.save(p, arr)p = os.path.join(cfg.data_dir, "math.npy")if not os.path.exists(p):ds = load_dataset("open-web-math/open-web-math", split="train", streaming=True)arr = np.array(_encode_stream(ds, t, "text", cfg.max_tokens_math), dtype=np.uint32)assert arr.max() < V, f"math 越界: max={arr.max()}, V={V}"np.save(p, arr)p = os.path.join(cfg.data_dir, "general.npy")if not os.path.exists(p):10 unmodified linesif any(i < 0 or i >= V for i in ids): continueall_ids.extend(ids); total += len(ids)arr = np.array(all_ids, dtype=np.uint32)assert arr.max() < V, f"general 越界: max={arr.max()}, V={V}"np.save(p, arr)class TokenDataset(Dataset):16 unmodified linesprog = (step-cfg.warmup_steps)/max(1,cfg.steps-cfg.warmup_steps)return cfg.lr*(0.1+0.9*0.5*(1+math.cos(math.pi*prog)))def train(cfg):set_seed(cfg.seed)_t = AutoTokenizer.from_pretrained(cfg.tokenizer_name, trust_remote_code=True)6 unmodified linesm_ids = np.load(os.path.join(cfg.data_dir, "math.npy"))for name, ids in [("general", g_ids), ("code", c_ids), ("math", m_ids)]:print(f"{name}: {len(ids)} tokens, max={ids.max()}")assert ids.max() < cfg.vocab_size, f"{name} 越界"ld = lambda ids, sh: DataLoader(TokenDataset(ids, cfg.max_seq_len, cfg.vocab_size),batch_size=cfg.batch_size, shuffle=sh, drop_last=True)g_it, c_it, m_it = _cycle(ld(g_ids, True)), _cycle(ld(c_ids, True)), _cycle(ld(m_ids, True))6 unmodified linesif mod in (0,2): x,y=next(g_it); is_g=Trueelif mod==1: x,y=next(c_it); is_g=Falseelse: x,y=next(m_it); is_g=Falsex,y = x.to(device), y.to(device); y = y.clamp(0, cfg.vocab_size - 1); B=x.shape[0]lg_no,lg_fb,rl = model.forward_both(x)def chunked_ce(logits,targets):V_=logits.shape[-1]; logits=logits.view(-1,V_); targets=targets.reshape(-1).clamp(0,V_-1)total=0.0for i in range(0,V_,16384): total=total+F.cross_entropy(logits[:,i:i+16384],targets,reduction='sum')return total/targets.numel()l_no=chunked_ce(lg_no,y); l_fb=chunked_ce(lg_fb,y)with torch.no_grad():l_no_per=F.cross_entropy(lg_no.view(-1,cfg.vocab_size),y.view(-1),reduction='none').view(B,-1).mean(1)127 unmodified lines12812913013113213313413513613713813914010 unmodified lines15115215315415515615716 unmodified lines1741751761771781791801811821831841856 unmodified lines1921931941951961971986 unmodified lines205206207208209210211212213214127 unmodified linesif not os.path.exists(p):ds = load_dataset("nick007x/github-code-2025", split="train", streaming=True)arr = np.array(_encode_stream(ds, t, "content", cfg.max_tokens_code), dtype=np.uint32)assert arr.max() < Vnp.save(p, arr)p = os.path.join(cfg.data_dir, "math.npy")if not os.path.exists(p):ds = load_dataset("open-web-math/open-web-math", split="train", streaming=True)arr = np.array(_encode_stream(ds, t, "text", cfg.max_tokens_math), dtype=np.uint32)assert arr.max() < Vnp.save(p, arr)p = os.path.join(cfg.data_dir, "general.npy")if not os.path.exists(p):10 unmodified linesif any(i < 0 or i >= V for i in ids): continueall_ids.extend(ids); total += len(ids)arr = np.array(all_ids, dtype=np.uint32)assert arr.max() < Vnp.save(p, arr)class TokenDataset(Dataset):16 unmodified linesprog = (step-cfg.warmup_steps)/max(1,cfg.steps-cfg.warmup_steps)return cfg.lr*(0.1+0.9*0.5*(1+math.cos(math.pi*prog)))def chunked_ce(logits, targets):V_ = logits.shape[-1]logits = logits.view(-1, V_)targets = targets.reshape(-1).clamp(0, V_-1)return F.cross_entropy(logits, targets, reduction='mean')def train(cfg):set_seed(cfg.seed)_t = AutoTokenizer.from_pretrained(cfg.tokenizer_name, trust_remote_code=True)6 unmodified linesm_ids = np.load(os.path.join(cfg.data_dir, "math.npy"))for name, ids in [("general", g_ids), ("code", c_ids), ("math", m_ids)]:print(f"{name}: {len(ids)} tokens, max={ids.max()}")assert ids.max() < cfg.vocab_sizeld = lambda ids, sh: DataLoader(TokenDataset(ids, cfg.max_seq_len, cfg.vocab_size),batch_size=cfg.batch_size, shuffle=sh, drop_last=True)g_it, c_it, m_it = _cycle(ld(g_ids, True)), _cycle(ld(c_ids, True)), _cycle(ld(m_ids, True))6 unmodified linesif mod in (0,2): x,y=next(g_it); is_g=Trueelif mod==1: x,y=next(c_it); is_g=Falseelse: x,y=next(m_it); is_g=Falsex,y = x.to(device), y.to(device)y = y.clamp(0, cfg.vocab_size - 1)B=x.shape[0]lg_no,lg_fb,rl = model.forward_both(x)l_no=chunked_ce(lg_no,y); l_fb=chunked_ce(lg_fb,y)with torch.no_grad():l_no_per=F.cross_entropy(lg_no.view(-1,cfg.vocab_size),y.view(-1),reduction='none').view(B,-1).mean(1)
train.py+13-10