BHARGAV REDDY commited on
Upload train_300m.py with huggingface_hub
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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:
|