BHARGAV REDDY commited on
Commit
1f3081c
Β·
verified Β·
1 Parent(s): 5296727

Upload train_300m.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. train_300m.py +32 -18
train_300m.py CHANGED
@@ -256,7 +256,22 @@ def probe_max_batch(model, device, dtype, seq_len, vocab_size, max_search=4096,
256
  tmp_opt.zero_grad(set_to_none=True)
257
  hi = mid - 1
258
  except RuntimeError as e:
259
- if "out of memory" in str(e).lower():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  try: del x, t, loss
261
  except: pass
262
  torch.cuda.empty_cache()
@@ -536,28 +551,13 @@ def train(cfg: dict):
536
  unique_params = model.num_params
537
  print(f" Parameters : {actual_params:,} total ({unique_params:,} unique, tied embeddings)")
538
 
539
- # ── torch.compile ─────────────────────────────────────────────────────────
540
- compiled_model = False
541
- if cfg.get("compile", True) and sys.platform == "linux" and device.type == "cuda":
542
- try:
543
- print(" Compiling model (torch.compile)...")
544
- model = torch.compile(model)
545
- compiled_model = True
546
- print(" torch.compile: enabled")
547
- except Exception as e:
548
- print(f" torch.compile: failed ({e}), continuing without")
549
- else:
550
- reason = "non-linux" if sys.platform != "linux" else "disabled"
551
- print(f" torch.compile: skipped ({reason})")
552
-
553
- # ── Batch sizing ──────────────────────────────────────────────────────────
554
  if cfg["auto_config"] and device.type == "cuda":
555
  print(f"\n Probing max micro_batch_size (VRAM search)...")
556
  max_mbs = probe_max_batch(
557
  model, device, dtype, cfg["seq_len"], cfg["vocab_size"]
558
  )
559
- raw_model = model._orig_mod if hasattr(model, "_orig_mod") else model
560
- raw_model.apply(raw_model._init_weights)
561
  torch.cuda.empty_cache(); gc.collect()
562
  grad_accum = max(1, math.ceil(cfg["global_batch"] / max_mbs))
563
  effective_batch = max_mbs * grad_accum
@@ -573,6 +573,20 @@ def train(cfg: dict):
573
  tokens_per_step = effective_batch * cfg["seq_len"]
574
  print(f" Tokens/step : {tokens_per_step:,}")
575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576
  # ── Optional init from existing weights ───────────────────────────────────
577
  init_model_path = cfg.get("init_model_path")
578
  if init_model_path:
 
256
  tmp_opt.zero_grad(set_to_none=True)
257
  hi = mid - 1
258
  except RuntimeError as e:
259
+ err_str = str(e).lower()
260
+ if ("out of memory" in err_str
261
+ or "no valid triton configs" in err_str
262
+ or "out of resource" in err_str
263
+ or "inductorerror" in type(e).__name__.lower()):
264
+ try: del x, t, loss
265
+ except: pass
266
+ torch.cuda.empty_cache()
267
+ tmp_opt.zero_grad(set_to_none=True)
268
+ hi = mid - 1
269
+ else:
270
+ raise
271
+ except Exception as e:
272
+ err_str = str(e).lower()
273
+ if ("out of resource" in err_str
274
+ or "no valid triton configs" in err_str):
275
  try: del x, t, loss
276
  except: pass
277
  torch.cuda.empty_cache()
 
551
  unique_params = model.num_params
552
  print(f" Parameters : {actual_params:,} total ({unique_params:,} unique, tied embeddings)")
553
 
554
+ # ── Batch sizing (BEFORE torch.compile to avoid Triton kernel issues) ────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
555
  if cfg["auto_config"] and device.type == "cuda":
556
  print(f"\n Probing max micro_batch_size (VRAM search)...")
557
  max_mbs = probe_max_batch(
558
  model, device, dtype, cfg["seq_len"], cfg["vocab_size"]
559
  )
560
+ model.apply(model._init_weights)
 
561
  torch.cuda.empty_cache(); gc.collect()
562
  grad_accum = max(1, math.ceil(cfg["global_batch"] / max_mbs))
563
  effective_batch = max_mbs * grad_accum
 
573
  tokens_per_step = effective_batch * cfg["seq_len"]
574
  print(f" Tokens/step : {tokens_per_step:,}")
575
 
576
+ # ── torch.compile (after batch probe to avoid Triton shared-memory errors)
577
+ compiled_model = False
578
+ if cfg.get("compile", True) and sys.platform == "linux" and device.type == "cuda":
579
+ try:
580
+ print(" Compiling model (torch.compile)...")
581
+ model = torch.compile(model)
582
+ compiled_model = True
583
+ print(" torch.compile: enabled")
584
+ except Exception as e:
585
+ print(f" torch.compile: failed ({e}), continuing without")
586
+ else:
587
+ reason = "non-linux" if sys.platform != "linux" else "disabled"
588
+ print(f" torch.compile: skipped ({reason})")
589
+
590
  # ── Optional init from existing weights ───────────────────────────────────
591
  init_model_path = cfg.get("init_model_path")
592
  if init_model_path: