BHARGAV REDDY commited on
Upload train_300m.py with huggingface_hub
Browse files255 unmodified lines256257258259260261262273 unmodified lines5365375385395405415425435445455465475485495505515525535545555565575585595605615625639 unmodified lines573574575576577578255 unmodified linestmp_opt.zero_grad(set_to_none=True)hi = mid - 1except RuntimeError as e:if "out of memory" in str(e).lower():try: del x, t, lossexcept: passtorch.cuda.empty_cache()273 unmodified linesunique_params = model.num_paramsprint(f" Parameters : {actual_params:,} total ({unique_params:,} unique, tied embeddings)")# ββ torch.compile βββββββββββββββββββββββββββββββββββββββββββββββββββββββββcompiled_model = Falseif cfg.get("compile", True) and sys.platform == "linux" and device.type == "cuda":try:print(" Compiling model (torch.compile)...")model = torch.compile(model)compiled_model = Trueprint(" torch.compile: enabled")except Exception as e:print(f" torch.compile: failed ({e}), continuing without")else:reason = "non-linux" if sys.platform != "linux" else "disabled"print(f" torch.compile: skipped ({reason})")# ββ Batch sizing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββif cfg["auto_config"] and device.type == "cuda":print(f"\n Probing max micro_batch_size (VRAM search)...")max_mbs = probe_max_batch(model, device, dtype, cfg["seq_len"], cfg["vocab_size"])raw_model = model._orig_mod if hasattr(model, "_orig_mod") else modelraw_model.apply(raw_model._init_weights)torch.cuda.empty_cache(); gc.collect()grad_accum = max(1, math.ceil(cfg["global_batch"] / max_mbs))effective_batch = max_mbs * grad_accum9 unmodified linestokens_per_step = effective_batch * cfg["seq_len"]print(f" Tokens/step : {tokens_per_step:,}")# ββ Optional init from existing weights βββββββββββββββββββββββββββββββββββinit_model_path = cfg.get("init_model_path")if init_model_path:255 unmodified lines256257258259260261262263264265266267268269270271272273274275276277273 unmodified lines5515525535545555565575585595605615625639 unmodified lines573574575576577578579580581582583584585586587588589590591592255 unmodified linestmp_opt.zero_grad(set_to_none=True)hi = mid - 1except RuntimeError as e:err_str = str(e).lower()if ("out of memory" in err_stror "no valid triton configs" in err_stror "out of resource" in err_stror "inductorerror" in type(e).__name__.lower()):try: del x, t, lossexcept: passtorch.cuda.empty_cache()tmp_opt.zero_grad(set_to_none=True)hi = mid - 1else:raiseexcept Exception as e:err_str = str(e).lower()if ("out of resource" in err_stror "no valid triton configs" in err_str):try: del x, t, lossexcept: passtorch.cuda.empty_cache()273 unmodified linesunique_params = model.num_paramsprint(f" Parameters : {actual_params:,} total ({unique_params:,} unique, tied embeddings)")# ββ Batch sizing (BEFORE torch.compile to avoid Triton kernel issues) ββββif cfg["auto_config"] and device.type == "cuda":print(f"\n Probing max micro_batch_size (VRAM search)...")max_mbs = probe_max_batch(model, device, dtype, cfg["seq_len"], cfg["vocab_size"])model.apply(model._init_weights)torch.cuda.empty_cache(); gc.collect()grad_accum = max(1, math.ceil(cfg["global_batch"] / max_mbs))effective_batch = max_mbs * grad_accum9 unmodified linestokens_per_step = effective_batch * cfg["seq_len"]print(f" Tokens/step : {tokens_per_step:,}")# ββ torch.compile (after batch probe to avoid Triton shared-memory errors)compiled_model = Falseif cfg.get("compile", True) and sys.platform == "linux" and device.type == "cuda":try:print(" Compiling model (torch.compile)...")model = torch.compile(model)compiled_model = Trueprint(" torch.compile: enabled")except Exception as e:print(f" torch.compile: failed ({e}), continuing without")else:reason = "non-linux" if sys.platform != "linux" else "disabled"print(f" torch.compile: skipped ({reason})")# ββ Optional init from existing weights βββββββββββββββββββββββββββββββββββinit_model_path = cfg.get("init_model_path")if init_model_path:
train_300m.py+32-18