zziang commited on
Commit
b4fa0f8
·
verified ·
1 Parent(s): f4b6f21

director: 160 training scripts + fixed block lookup

Browse files
Files changed (1) hide show
  1. kaggle/director_sft.py +8 -4
kaggle/director_sft.py CHANGED
@@ -35,11 +35,13 @@ def find_blocks(model):
35
  """Longest ModuleList of decoder blocks, whatever it is nested under."""
36
  import torch.nn as nn
37
 
38
- best = (0, None)
39
  for name, mod in model.named_modules():
40
  if isinstance(mod, nn.ModuleList) and len(mod) > best[0]:
41
- best = (len(mod), (name, mod))
42
- return best[1]
 
 
43
 
44
 
45
  def build_example(tok, topic: str, lines: list[dict]) -> dict:
@@ -67,7 +69,7 @@ def train(rows: list[dict]) -> str:
67
  tok = AutoTokenizer.from_pretrained(MODEL_ID)
68
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype=torch.float32,
69
  device_map="cuda:0")
70
- n_layers, (lname, blocks) = find_blocks(model)
71
  log(f"blocks: {lname} x{n_layers}; training the last {TRAIN_LAYERS}")
72
 
73
  for p in model.parameters():
@@ -115,6 +117,8 @@ def train(rows: list[dict]) -> str:
115
  def main() -> None:
116
  OUT.mkdir(parents=True, exist_ok=True)
117
  path = OUT / "sft.jsonl"
 
 
118
  rows = [json.loads(l) for l in path.read_text().splitlines() if l.strip()]
119
  log(f"{len(rows)} training scripts from {path}")
120
  if not rows:
 
35
  """Longest ModuleList of decoder blocks, whatever it is nested under."""
36
  import torch.nn as nn
37
 
38
+ best = (0, "", None)
39
  for name, mod in model.named_modules():
40
  if isinstance(mod, nn.ModuleList) and len(mod) > best[0]:
41
+ best = (len(mod), name, mod)
42
+ if best[2] is None:
43
+ raise RuntimeError("no decoder block list found")
44
+ return best
45
 
46
 
47
  def build_example(tok, topic: str, lines: list[dict]) -> dict:
 
69
  tok = AutoTokenizer.from_pretrained(MODEL_ID)
70
  model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype=torch.float32,
71
  device_map="cuda:0")
72
+ n_layers, lname, blocks = find_blocks(model)
73
  log(f"blocks: {lname} x{n_layers}; training the last {TRAIN_LAYERS}")
74
 
75
  for p in model.parameters():
 
117
  def main() -> None:
118
  OUT.mkdir(parents=True, exist_ok=True)
119
  path = OUT / "sft.jsonl"
120
+ if not path.exists(): # reuse a set collected by an earlier run
121
+ path = ROOT / "webfm-eval" / "director" / "sft.jsonl"
122
  rows = [json.loads(l) for l in path.read_text().splitlines() if l.strip()]
123
  log(f"{len(rows)} training scripts from {path}")
124
  if not rows: