musabc commited on
Commit
9d142b9
·
verified ·
1 Parent(s): c2aa5b6

Upload sft_03_train.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. sft_03_train.py +30 -2
sft_03_train.py CHANGED
@@ -24,6 +24,10 @@ import sys
24
  import time
25
  from pathlib import Path
26
 
 
 
 
 
27
  import numpy as np
28
  import torch
29
  import torch.nn.functional as F
@@ -249,13 +253,37 @@ def main():
249
  model = GPTV5(cfg).to(DEVICE)
250
  ckpt = torch.load(args.base, map_location=DEVICE, weights_only=False)
251
  if "model" in ckpt:
252
- model.load_state_dict(ckpt["model"])
253
  log(f" Base step: {ckpt.get('step', '?')} best_val: "
254
  f"{ckpt.get('best_val', '?')}")
255
  else:
256
- model.load_state_dict(ckpt)
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  log(f" Params: {sum(p.numel() for p in model.parameters())/1e6:.1f}M")
258
 
 
 
 
 
 
 
 
 
 
 
 
259
  # Resume?
260
  start_step = 0
261
  sft_latest = out_dir / "sft_latest.pt"
 
24
  import time
25
  from pathlib import Path
26
 
27
+ # Liger CE training'de logits'i in-place modifiye ettigi icin SFT'de devre disi
28
+ # Bu IMPORT'tan ONCE set edilmeli
29
+ os.environ["NANOGPT_NO_LIGER"] = "1"
30
+
31
  import numpy as np
32
  import torch
33
  import torch.nn.functional as F
 
253
  model = GPTV5(cfg).to(DEVICE)
254
  ckpt = torch.load(args.base, map_location=DEVICE, weights_only=False)
255
  if "model" in ckpt:
256
+ sd = ckpt["model"]
257
  log(f" Base step: {ckpt.get('step', '?')} best_val: "
258
  f"{ckpt.get('best_val', '?')}")
259
  else:
260
+ sd = ckpt
261
+
262
+ # _orig_mod. prefix'i kaldir (compile ile kaydedilmis ise)
263
+ has_prefix = any(k.startswith("_orig_mod.") for k in sd.keys())
264
+ if has_prefix:
265
+ log(f" ! _orig_mod. prefix tespit edildi, kaldiriliyor")
266
+ sd = {k.replace("_orig_mod.", "", 1): v for k, v in sd.items()}
267
+
268
+ missing, unexpected = model.load_state_dict(sd, strict=False)
269
+ log(f" Yuklendi: {len(sd) - len(unexpected)}/{len(sd)} key")
270
+ if missing:
271
+ log(f" ! Missing keys ({len(missing)}): {missing[:3]}...")
272
+ if unexpected:
273
+ log(f" ! Unexpected keys ({len(unexpected)}): {unexpected[:3]}...")
274
  log(f" Params: {sum(p.numel() for p in model.parameters())/1e6:.1f}M")
275
 
276
+ # Sanity check — random tokens uzerinde forward, ortalama top-1 olasilik
277
+ with torch.no_grad():
278
+ with torch.amp.autocast(device_type="cuda", dtype=DTYPE):
279
+ test_x = torch.randint(0, cfg.vocab_size, (2, 64), device=DEVICE)
280
+ test_logits, _ = model(test_x, test_x)
281
+ probs = F.softmax(test_logits[0, 0].float(), dim=-1)
282
+ top1_p = probs.max().item()
283
+ entropy = -(probs * torch.log(probs + 1e-12)).sum().item()
284
+ log(f" Sanity: top1_p={top1_p:.4f}, entropy={entropy:.3f} "
285
+ f"(uniform ~= {math.log(cfg.vocab_size):.2f}, trained <~ 5)")
286
+
287
  # Resume?
288
  start_step = 0
289
  sft_latest = out_dir / "sft_latest.pt"