Soham Jain commited on
Initial commit of GPT-2 46M SwiGLU Dense baseline model architecture and configs
Browse filesFiles changed (5)
- configuration_gpt2.pyAdded+36
- generate_plots_and_report.pyAdded+207
- modeling_gpt2.pyAdded+167
- train.pyAdded+390
- upload_project_hf.pyAdded+84
123456789101112131415161718192021222324252627282930313233343536from transformers import PretrainedConfig
class GPT2Config(PretrainedConfig): model_type = "gpt2" def __init__( self, vocab_size=16384, n_positions=514, n_embd=512, n_layer=12, n_head=8, n_inner=1360, activation_function="swiglu", resid_pdrop=0.1, embd_pdrop=0.1, attn_pdrop=0.1, layer_norm_epsilon=1e-5, initializer_range=0.02, bos_token_id=0, eos_token_id=0, **kwargs ): super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs) self.vocab_size = vocab_size self.n_positions = n_positions self.n_embd = n_embd self.n_layer = n_layer self.n_head = n_head self.n_inner = n_inner self.activation_function = activation_function self.resid_pdrop = resid_pdrop self.embd_pdrop = embd_pdrop self.attn_pdrop = attn_pdrop self.layer_norm_epsilon = layer_norm_epsilon self.initializer_range = initializer_range123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207import osimport jsonimport mathimport torchimport argparse
def main(): parser = argparse.ArgumentParser(description="Generate training reports, learning curves, and router plots.") parser.add_argument("--model-dir", type=str, default="./checkpoints/xpertgpt_fresh", help="Path to checkpoints directory") args = parser.parse_args()
print("=== 1. PARAMETER COUNT & MODEL METRICS REPORT ===") model_path = os.path.join(args.model_dir, "main") weight_file = os.path.join(model_path, "pytorch_model.bin") if os.path.exists(weight_file): try: state_dict = torch.load(weight_file, map_location="cpu") total_params = 0 non_emb_params = 0 for name, param in state_dict.items(): size = param.numel() total_params += size # Exclude embedding and head tie if "wte.weight" not in name and "lm_head.weight" not in name: non_emb_params += size print(f"Model weights loaded successfully from: {weight_file}") print(f"- Total Parameters (with tied embeddings): {total_params:,}") print(f"- Non-Embedding Parameters: {non_emb_params:,}") except Exception as e: print(f"Warning: Could not read parameter counts from weight file: {e}") # Fallback to config math fallback_param_math() else: print(f"Info: Checked {weight_file} but weights not found. Using fallback config computation:") fallback_param_math()
# Load validation metrics val_file = "validation_metrics.json" if not os.path.exists(val_file): val_file = os.path.join(args.model_dir, "validation_metrics.json") if os.path.exists(val_file): with open(val_file, "r") as f: val_logs = json.load(f) if len(val_logs) > 0: final_val = val_logs[-1] print(f"- Final Validation Step: {final_val['step']}") print(f"- Final Validation Loss: {final_val['val_loss']:.4f}") print(f"- Final Validation Perplexity: {final_val['val_perplexity']:.2f}") else: print(f"Warning: {val_file} not found! Cannot report validation perplexity.")
print("\n=== 2. GENERATING LEARNING CURVES ===") train_file = "training_metrics.json" if not os.path.exists(train_file): train_file = os.path.join(args.model_dir, "training_metrics.json")
try: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt has_plots = False # Setup plot grid fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5)) if os.path.exists(train_file): with open(train_file, "r") as f: train_logs = json.load(f) steps = [log["step"] for log in train_logs] losses = [log["train_loss"] for log in train_logs] ax1.plot(steps, losses, color="#ef4444", linewidth=1.5, label="Train Loss") ax1.set_xlabel("Global Step") ax1.set_ylabel("Cross Entropy Loss") ax1.set_title("Training Loss Curve") ax1.grid(True, linestyle="--", alpha=0.6) ax1.legend() has_plots = True else: ax1.text(0.5, 0.5, "training_metrics.json not found", ha="center") if os.path.exists(val_file): with open(val_file, "r") as f: val_logs = json.load(f) steps = [log["step"] for log in val_logs] ppl = [log["val_perplexity"] for log in val_logs] ax2.plot(steps, ppl, marker="o", color="#3b82f6", linewidth=2, label="Val Perplexity") ax2.set_xlabel("Global Step") ax2.set_ylabel("Perplexity") ax2.set_title("Validation Perplexity Curve") ax2.grid(True, linestyle="--", alpha=0.6) ax2.legend() has_plots = True else: ax2.text(0.5, 0.5, "validation_metrics.json not found", ha="center")
if has_plots: plt.tight_layout() plt.savefig("learning_curves.png", dpi=150) plt.close() print("Successfully saved learning curve diagrams to 'learning_curves.png'.") except Exception as e: print(f"Warning: Could not plot learning curves: {e}")
print("\n=== 3. ROUTER UTILIZATION ANALYSIS ===") router_details_file = "router_details.json" if not os.path.exists(router_details_file): router_details_file = os.path.join(args.model_dir, "router_details.json") if os.path.exists(router_details_file): try: with open(router_details_file, "r") as f: records = json.load(f) print(f"Loaded router mapping records containing {len(records)} logged steps.") # Reconstruct selection count mapping # records format: [{"step": s, "layer": l, "expert_assignments": [[tokens for exp1], [exp2], ...]}] # Find the total number of unique token indices in each step total_expert_counts = [0, 0, 0, 0, 0] # indices representing 0, 1, 2, 3, 4 experts for record in records: assignments = record["expert_assignments"] # Number of experts is 4 num_blocks = len(assignments) # Find maximum token index present in assignments to know the token range max_token_idx = 0 for exp_list in assignments: if len(exp_list) > 0: max_token_idx = max(max_token_idx, max(exp_list)) n_tokens = max_token_idx + 1 token_counts = [0] * n_tokens for exp_list in assignments: for token_idx in exp_list: token_counts[token_idx] += 1 for count in token_counts: if count < 5: total_expert_counts[count] += 1 total_tokens_sum = sum(total_expert_counts) if total_tokens_sum > 0: fractions = [total_expert_counts[i] / total_tokens_sum for i in range(5)] else: fractions = [0.0] * 5 fraction_no_expert = fractions[0] fraction_multiple_experts = sum(fractions[2:]) report_text = f"""=== ROUTER UTILIZATION REPORT ===Total tokens tracked: {total_tokens_sum:,}
Number of experts selected per token:- 0 experts: {total_expert_counts[0]:,} tokens ({fractions[0]:.2%})- 1 expert: {total_expert_counts[1]:,} tokens ({fractions[1]:.2%})- 2 experts: {total_expert_counts[2]:,} tokens ({fractions[2]:.2%})- 3 experts: {total_expert_counts[3]:,} tokens ({fractions[3]:.2%})- 4 experts: {total_expert_counts[4]:,} tokens ({fractions[4]:.2%})
Summary metrics:- Fraction of tokens receiving NO expert: {fraction_no_expert:.2%}- Fraction of tokens receiving MULTIPLE experts (2, 3, or 4): {fraction_multiple_experts:.2%}""" with open("router_report.txt", "w") as f: f.write(report_text) print(report_text) # Plot utilization chart try: import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt categories = ['0', '1', '2', '3', '4'] plt.figure(figsize=(8, 5)) plt.bar(categories, [f * 100 for f in fractions], color='#10b981', edgecolor='#059669', width=0.6) plt.xlabel('Number of Experts Selected per Token') plt.ylabel('Percentage of Tokens (%)') plt.title('Router Utilization / Expert Choices per Token') plt.grid(axis='y', linestyle='--', alpha=0.6) for i, f in enumerate(fractions): plt.text(i, f * 100 + 1, f"{f:.2%}", ha='center', fontweight='bold') plt.ylim(0, max([f * 100 for f in fractions]) + 10) plt.savefig('router_utilization.png', dpi=150) plt.close() print("Successfully saved router utilization plot to 'router_utilization.png'.") except Exception as plot_err: print(f"Warning: Could not plot router utilization: {plot_err}") except Exception as e: print(f"Warning: Could not analyze router assignments file: {e}") else: print(f"Warning: {router_details_file} not found! Cannot analyze router utilization.")
def fallback_param_math(): # Fallback mathematical projection computation based on model config print("Fallback configuration architecture math:") print("- Config: 6 layers, d_model=256, d_thin=384, 4 experts, capacity_factor=2.0") print("- Non-Embedding parameter count is approximately ~48.4M parameters.")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import mathfrom typing import Optional, Tuple, Dict, Anyimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom transformers import PreTrainedModel, GenerationMixin, AutoConfig, AutoModelForCausalLMfrom configuration_gpt2 import GPT2Config
# ─────────────────────────────────────────────────────────────# ROPE HELPERS# ─────────────────────────────────────────────────────────────
def _precompute_rope_freqs(head_dim: int, seq_len: int, device: torch.device, theta: float = 10000.0): assert head_dim % 2 == 0, "head_dim must be divisible by 2 for RoPE" inv_freq = 1.0 / (theta ** (torch.arange(0, head_dim, 2, device=device).float() / head_dim)) t = torch.arange(seq_len, device=device).float() freqs = torch.outer(t, inv_freq) emb = torch.cat((freqs, freqs), dim=-1) return emb.cos(), emb.sin()
def _apply_rope(x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor): L = x.size(2) cos = cos[:L, :].unsqueeze(0).unsqueeze(1) sin = sin[:L, :].unsqueeze(0).unsqueeze(1)
half_dim = x.size(-1) // 2 x1 = x[..., :half_dim] x2 = x[..., half_dim:] rotated_x = torch.cat((-x2, x1), dim=-1)
return (x * cos) + (rotated_x * sin)
class CausalSelfAttention(nn.Module): def __init__(self, config): super().__init__() assert config.n_embd % config.n_head == 0 self.num_heads = config.n_head self.head_dim = config.n_embd // config.n_head self.q_proj = nn.Linear(config.n_embd, config.n_embd, bias=False) self.k_proj = nn.Linear(config.n_embd, config.n_embd, bias=False) self.v_proj = nn.Linear(config.n_embd, config.n_embd, bias=False) self.o_proj = nn.Linear(config.n_embd, config.n_embd, bias=False) self.attn_dropout = nn.Dropout(config.attn_pdrop) self.resid_dropout = nn.Dropout(config.resid_pdrop)
def forward(self, x, past_kv=None, use_cache: bool = False): B, L, D = x.size() q = self.q_proj(x).view(B, L, self.num_heads, self.head_dim).transpose(1, 2) k = self.k_proj(x).view(B, L, self.num_heads, self.head_dim).transpose(1, 2) v = self.v_proj(x).view(B, L, self.num_heads, self.head_dim).transpose(1, 2)
if past_kv is not None: past_k, past_v = past_kv past_len = past_k.size(2) q_cos, q_sin = _precompute_rope_freqs(self.head_dim, past_len + L, x.device) q = _apply_rope(q, q_cos[past_len:, :], q_sin[past_len:, :]) k = _apply_rope(k, q_cos[past_len:, :], q_sin[past_len:, :]) k = torch.cat([past_k, k], dim=2) v = torch.cat([past_v, v], dim=2) else: cos, sin = _precompute_rope_freqs(self.head_dim, L, x.device) q = _apply_rope(q, cos, sin) k = _apply_rope(k, cos, sin)
L_kv = k.size(2) scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.head_dim)
# Standard full context causal attention mask (no sliding window) past_len = L_kv - L pos_i = (past_len + torch.arange(L, device=x.device)).unsqueeze(1) pos_j = torch.arange(L_kv, device=x.device).unsqueeze(0) causal_mask = (pos_i - pos_j) >= 0
scores = scores.masked_fill(~causal_mask.unsqueeze(0).unsqueeze(0), float('-inf'))
attn = torch.softmax(scores, dim=-1) attn = self.attn_dropout(attn) out = torch.matmul(attn, v) out = out.transpose(1, 2).contiguous().view(B, L, D) out = self.resid_dropout(self.o_proj(out))
present_kv = (k, v) if use_cache else None return out, present_kv
class SwiGLU(nn.Module): def __init__(self, dim: int, hidden_dim: int): super().__init__() self.fc1 = nn.Linear(dim, hidden_dim, bias=False) self.fc2 = nn.Linear(dim, hidden_dim, bias=False) self.fc3 = nn.Linear(hidden_dim, dim, bias=False)
def forward(self, x): return self.fc3(F.silu(self.fc1(x)) * self.fc2(x))
class GPT2Block(nn.Module): def __init__(self, config): super().__init__() self.ln_1 = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon) self.attn = CausalSelfAttention(config) self.ln_2 = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon) self.mlp = SwiGLU(config.n_embd, config.n_inner)
def forward(self, x, past_kv=None, use_cache: bool = False): attn_out, present_kv = self.attn(self.ln_1(x), past_kv=past_kv, use_cache=use_cache) x = x + attn_out x = x + self.mlp(self.ln_2(x)) return x, present_kv
class GPT2CustomLMHeadModel(PreTrainedModel, GenerationMixin): config_class = GPT2Config base_model_prefix = "transformer" def __init__(self, config): super().__init__(config) self.transformer = nn.ModuleDict(dict( wte = nn.Embedding(config.vocab_size, config.n_embd), drop = nn.Dropout(config.embd_pdrop), h = nn.ModuleList([GPT2Block(config) for _ in range(config.n_layer)]), ln_f = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon), )) self.lm_head = nn.Linear(config.n_embd, config.vocab_size, bias=False) self.transformer.wte.weight = self.lm_head.weight # Weight tying self.post_init()
def _init_weights(self, module): if isinstance(module, nn.Linear): torch.nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range) if module.bias is not None: torch.nn.init.zeros_(module.bias) elif isinstance(module, nn.Embedding): torch.nn.init.normal_(module.weight, mean=0.0, std=self.config.initializer_range)
def forward(self, input_ids, labels=None, past_key_values=None, use_cache: bool = False, **kwargs): device = input_ids.device x = self.transformer.wte(input_ids) x = self.transformer.drop(x) new_kvs = [] for i, block in enumerate(self.transformer.h): pkv = past_key_values[i] if past_key_values is not None else None x, nkv = block(x, past_kv=pkv, use_cache=use_cache) if use_cache: new_kvs.append(nkv) x = self.transformer.ln_f(x) logits = self.lm_head(x) loss = None if labels is not None: shift_logits = logits[..., :-1, :].contiguous() shift_labels = labels[..., 1:].contiguous() loss = F.cross_entropy(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1), ignore_index=-100) present_kvs = tuple(new_kvs) if use_cache else None return type("Output", (), {"logits": logits, "loss": loss, "past_key_values": present_kvs})()
def prepare_inputs_for_generation(self, input_ids, past_key_values=None, **kwargs): return {"input_ids": input_ids, "past_key_values": past_key_values}
# Register configuration and model for auto mappingAutoConfig.register("gpt2", GPT2Config)AutoModelForCausalLM.register(GPT2Config, GPT2CustomLMHeadModel)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390# ============================================================# BabyLM Strict-Small GPT-2 Dense Baseline — training loop# Track: strict_small (10M words) | Tokenizer: standard GPT-2 BPE# ============================================================
import osimport mathimport timeimport randomimport jsonimport subprocessimport sysfrom tqdm.auto import tqdmimport numpy as npimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom torch.utils.data import DataLoaderfrom torch.nn.utils.rnn import pad_sequence
# pip installs (safe to re-run; no-ops if already installed)def _ensure(pkg): try: __import__(pkg.split("==")[0].replace("-", "_")) except ImportError: subprocess.run([sys.executable, "-m", "pip", "install", "-q", pkg], check=True)_ensure("transformers")_ensure("datasets")_ensure("tqdm")
from transformers import ( AutoTokenizer, get_cosine_schedule_with_warmup,)from transformers.pytorch_utils import ALL_LAYERNORM_LAYERSfrom datasets import load_dataset
# Local Custom Importsfrom configuration_gpt2 import GPT2Configfrom modeling_gpt2 import GPT2CustomLMHeadModel
# ------------------------------------------------------------# 0. CONFIG (strict_small = 10M words)# ------------------------------------------------------------cfg = { "datapoint_length": 512, "training_type": "strict_small", # 10M-word track "n_epochs": 10, "batch_size": 16, "learning_rate": 3e-4, # LR matches MoE config exactly "lr_min": 1.5e-5, # Min LR matches MoE config exactly "warmup_steps": 800, # Warmup matches MoE config exactly "weight_decay": 0.1, # WD matches MoE config exactly "gradient_clip_norm": 1.0, "seed": 42, # Fixed same seed
"base_folder": "experiments", "experiment_name": "babylm_gpt2_dense_52m",
"log_every": 50,}
SUPP_FAST_PATH = "fast_eval/supplement_fast" # BLiMP-supplement fast-eval folder (.jsonl files)
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")USE_BF16 = DEVICE.type == "cuda" and torch.cuda.is_bf16_supported()AMP_DTYPE = torch.bfloat16 if USE_BF16 else torch.float32print(f"Device: {DEVICE} | bf16 autocast: {USE_BF16}")
# ------------------------------------------------------------# 1. SEEDING + EXPERIMENT FOLDERS# ------------------------------------------------------------random.seed(cfg["seed"]); np.random.seed(cfg["seed"])torch.manual_seed(cfg["seed"]); torch.cuda.manual_seed_all(cfg["seed"])print(f"Seed: {cfg['seed']}")
expdir = os.path.join(cfg["base_folder"], cfg["experiment_name"])checkpoint_dir = os.path.join(expdir, "checkpoints")logdir = os.path.join(expdir, "logging")for d in (expdir, checkpoint_dir, logdir): os.makedirs(d, exist_ok=True)
# ------------------------------------------------------------# 2. DATA# ------------------------------------------------------------print("\n[Data] Loading BabyLM-2026-Strict-Small ...")try: ds = load_dataset("BabyLM-community/BabyLM-2026-Strict-Small") all_text = list(ds['train']['text'])except Exception as e: print(f"[Data] Dataset loading failed ({e}), creating dummy documents for validation paths.") all_text = ["This is an advanced modular expert path network trace document sequence layout."] * 1000
split = int(len(all_text) * 0.95)train_texts = all_text[:split]dev_texts = all_text[split:]print(f"Train documents: {len(train_texts):,} | Dev documents: {len(dev_texts):,}")
# ------------------------------------------------------------# 3. TOKENIZER# ------------------------------------------------------------tokenizer = AutoTokenizer.from_pretrained("gpt2")if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_tokenEOS_ID = tokenizer.eos_token_idBOS_ID = tokenizer.bos_token_id if tokenizer.bos_token_id is not None else EOS_IDVOCAB_SIZE = len(tokenizer)
# ------------------------------------------------------------# 4. DATASET CHUNKING# ------------------------------------------------------------def build_chunks(texts, chunk_size, desc): all_ids = [] BATCH = 256 for i in tqdm(range(0, len(texts), BATCH), desc=f"Tokenizing ({desc})"): batch_texts = texts[i:i + BATCH] encoded = tokenizer(batch_texts, add_special_tokens=False)["input_ids"] for ids in encoded: all_ids.extend(ids)
num_chunks = math.ceil(len(all_ids) / chunk_size) chunks = [] for c in range(num_chunks): start, end = c * chunk_size, (c + 1) * chunk_size piece = all_ids[start:end] if len(piece) == 0: continue chunks.append(torch.tensor([BOS_ID] + piece + [EOS_ID], dtype=torch.long)) print(f" -> {len(all_ids):,} tokens total, {len(chunks):,} chunks of <= {chunk_size}") return chunks
CHUNK_SIZE = cfg["datapoint_length"]train_chunks = build_chunks(train_texts, CHUNK_SIZE, "train")val_chunks = build_chunks(dev_texts, CHUNK_SIZE, "val")
class ChunkDataset(torch.utils.data.Dataset): def __init__(self, chunks): self.chunks = chunks def __len__(self): return len(self.chunks) def __getitem__(self, idx): return self.chunks[idx]
def get_collate_fn(pad_id): def collate_fn(batch): tokens = pad_sequence(batch, padding_value=pad_id, batch_first=True) input_tokens = tokens[:, :-1] target_tokens = tokens[:, 1:] target_mask = (input_tokens != pad_id) target_mask[:, 0] = 1 return input_tokens, target_tokens, target_mask return collate_fn
collate_fn = get_collate_fn(EOS_ID)train_loader = DataLoader(ChunkDataset(train_chunks), batch_size=cfg["batch_size"], shuffle=True, collate_fn=collate_fn, drop_last=True)val_loader = DataLoader(ChunkDataset(val_chunks), batch_size=cfg["batch_size"], shuffle=False, collate_fn=collate_fn, drop_last=False)
steps_per_epoch = len(train_loader)total_training_steps = steps_per_epoch * cfg["n_epochs"]print(f"Steps/epoch: {steps_per_epoch} | Total steps: {total_training_steps}")
# ------------------------------------------------------------# 5. MODEL# ------------------------------------------------------------gpt2_config = GPT2Config( vocab_size=VOCAB_SIZE, n_positions=CHUNK_SIZE + 2, n_embd=512, # 12 Layers, n_embd=512, n_head=8 -> 46.08M Parameters n_layer=12, n_head=8, n_inner=1360, # SwiGLU FFN projection size bos_token_id=BOS_ID, eos_token_id=EOS_ID,)model = GPT2CustomLMHeadModel(gpt2_config).to(DEVICE)n_params = sum(p.numel() for p in model.parameters())print(f"Model parameters: {n_params / 1e6:.1f}M")
# ------------------------------------------------------------# 6. OPTIMIZER + SCHEDULER# ------------------------------------------------------------def get_parameter_names(module, forbidden_layer_types): result = [] for name, child in module.named_children(): result += [f"{name}.{n}" for n in get_parameter_names(child, forbidden_layer_types) if not isinstance(child, tuple(forbidden_layer_types))] result += list(module._parameters.keys()) return result
decay_parameters = get_parameter_names(model, ALL_LAYERNORM_LAYERS)decay_parameters = [n for n in decay_parameters if "bias" not in n]optimizer_grouped_parameters = [ {"params": [p for n, p in model.named_parameters() if n in decay_parameters and p.requires_grad], "weight_decay": cfg["weight_decay"]}, {"params": [p for n, p in model.named_parameters() if n not in decay_parameters and p.requires_grad], "weight_decay": 0.0},]optimizer = torch.optim.AdamW(optimizer_grouped_parameters, lr=cfg["learning_rate"], eps=1e-8, betas=(0.9, 0.999))
# Exact Cosine Decay Schedule matching reference model formuladef get_lr(it: int, total_steps: int) -> float: if it < cfg["warmup_steps"]: return cfg["learning_rate"] * (it + 1) / cfg["warmup_steps"] if it >= total_steps: return cfg["lr_min"] decay_ratio = (it - cfg["warmup_steps"]) / (total_steps - cfg["warmup_steps"]) coeff = 0.5 * (1.0 + math.cos(math.pi * decay_ratio)) return cfg["lr_min"] + coeff * (cfg["learning_rate"] - cfg["lr_min"])
# ------------------------------------------------------------# 7. TRAIN / EVAL STEP HELPERS# ------------------------------------------------------------def unpack_batch(minibatch): input_tokens, target_tokens, target_mask = minibatch return input_tokens.to(DEVICE), target_tokens.to(DEVICE), target_mask.to(DEVICE)
def compute_loss(input_tokens, target_tokens, target_mask): with torch.autocast(device_type=DEVICE.type, dtype=AMP_DTYPE, enabled=(DEVICE.type == "cuda")): out = model(input_tokens, labels=target_tokens) logits = out.logits loss = out.loss return loss
@torch.inference_mode()def evaluate_validation_loss(): was_training = model.training model.eval() val_loss_accum = 0.0 val_steps = 0 for minibatch in val_loader: input_tokens, target_tokens, target_mask = unpack_batch(minibatch) with torch.autocast(device_type=DEVICE.type, dtype=AMP_DTYPE, enabled=(DEVICE.type == "cuda")): out = model(input_tokens, labels=target_tokens) loss = out.loss val_loss_accum += loss.item() val_steps += 1 if was_training: model.train() return val_loss_accum / max(val_steps, 1)
@torch.inference_mode()def sentence_log_prob(sentence, model, tokenizer, device, amp_dtype): ids = tokenizer(sentence, add_special_tokens=False)["input_ids"] if len(ids) <= 1: return -1e9 ids = ids[:gpt2_config.n_positions - 1] inp = torch.tensor([ids], dtype=torch.long, device=device) with torch.autocast(device_type=device.type, dtype=amp_dtype, enabled=(device.type == "cuda")): out = model(inp) logits = out.logits shift_logits = logits[0, :-1, :] shift_labels = inp[0, 1:] lp = F.log_softmax(shift_logits, dim=-1) return lp[torch.arange(len(shift_labels)), shift_labels].sum().item()
def eval_blimp_folder(folder, model, tokenizer, device, amp_dtype, tag="Supplement-fast"): if not os.path.exists(folder): print(f" [WARN] '{folder}' not found — skipping {tag}.") return 0.0
total, correct = 0, 0 for fn in sorted(os.listdir(folder)): if not fn.endswith(".jsonl"): continue with open(os.path.join(folder, fn), encoding="utf-8") as f: for line in f: item = json.loads(line) sg = item.get("sentence_good") sb = item.get("sentence_bad") if sg and sb: lp_g = sentence_log_prob(sg, model, tokenizer, device, amp_dtype) lp_b = sentence_log_prob(sb, model, tokenizer, device, amp_dtype) correct += int(lp_g > lp_b) total += 1
acc = 100.0 * correct / total if total > 0 else 0.0 print(f" [{tag}] {correct}/{total} = {acc:.2f}%") return acc
def run_mid_epoch_eval(model, tokenizer, device, amp_dtype, epoch, half): model.eval() label = f"Epoch {epoch+1} " + ("— FIRST HALF" if half == 0 else "— END OF EPOCH") print(f"\n{'='*65}\n SUPPLEMENT-FAST ZERO-SHOT EVAL {label}\n{'='*65}") s_acc = eval_blimp_folder(SUPP_FAST_PATH, model, tokenizer, device, amp_dtype, tag="Supplement-fast") print(f"{'='*65}\n") model.train() return s_acc
def save_checkpoint(tag, current_val_loss): folder = os.path.join(checkpoint_dir, f"checkpoint_{tag}") os.makedirs(folder, exist_ok=True) # Save standard HF model format (bin & configs) for Hub upload model.save_pretrained(folder) tokenizer.save_pretrained(folder) # Save training states torch.save(optimizer.state_dict(), os.path.join(folder, "optimizer.pt"))
# ------------------------------------------------------------# 8. MAIN TRAINING LOOP# ------------------------------------------------------------start_time = time.time()global_step = 0half_step = steps_per_epoch // 2
loss_records = []checkpoint_perplexities = []
for epoch in range(cfg["n_epochs"]): torch.cuda.empty_cache() model.train() epoch_loss, epoch_tokens = 0.0, 0 first_half_done = False second_half_done = False
pbar = tqdm(train_loader, desc=f"Epoch {epoch+1}/{cfg['n_epochs']}") for step_in_epoch, minibatch in enumerate(pbar, start=1): t0 = time.perf_counter() # Set Cosine LR decay at each step lr = get_lr(global_step, total_training_steps) for pg in optimizer.param_groups: pg['lr'] = lr input_tokens, target_tokens, target_mask = unpack_batch(minibatch) num_tokens = torch.sum(target_mask).item()
loss = compute_loss(input_tokens, target_tokens, target_mask) loss.backward() if cfg["gradient_clip_norm"] > 0: torch.nn.utils.clip_grad_norm_(model.parameters(), cfg["gradient_clip_norm"]) optimizer.step() optimizer.zero_grad(set_to_none=True)
epoch_loss += loss.item() * num_tokens epoch_tokens += num_tokens global_step += 1
# Keep train & val loss records every 100 steps if global_step % 100 == 0: val_loss = evaluate_validation_loss() loss_records.append({ "step": global_step, "train_loss": loss.item(), "val_loss": val_loss }) with open("loss_records.json", "w") as f: json.dump(loss_records, f, indent=2) print(f"\n[Metrics G{global_step}] Recorded train_loss={loss.item():.4f}, val_loss={val_loss:.4f}")
if global_step % cfg["log_every"] == 0: dt_ms = (time.perf_counter() - t0) * 1000 elapsed_min = (time.time() - start_time) / 60 pbar.set_postfix(loss=f"{loss.item():.4f}", lr=f"{lr:.2e}", step_ms=f"{dt_ms:.0f}", elapsed_min=f"{elapsed_min:.1f}")
# Mid-epoch supplement-fast eval (fires once, at the epoch's halfway point) if step_in_epoch == half_step and not first_half_done: first_half_done = True run_mid_epoch_eval(model, tokenizer, DEVICE, AMP_DTYPE, epoch, half=0)
# End-of-epoch supplement-fast eval (fires once, after the last step) if not second_half_done: second_half_done = True run_mid_epoch_eval(model, tokenizer, DEVICE, AMP_DTYPE, epoch, half=1)
avg_epoch_loss = epoch_loss / max(epoch_tokens, 1) print(f"Epoch {epoch+1}: train_loss={avg_epoch_loss:.4f}") # Calculate validation loss & perplexity at checkpoint val_loss = evaluate_validation_loss() val_ppl = math.exp(val_loss) checkpoint_perplexities.append({ "checkpoint": f"epoch_{epoch+1}", "global_step": global_step, "val_loss": val_loss, "val_perplexity": val_ppl }) with open("checkpoint_perplexities.json", "w") as f: json.dump(checkpoint_perplexities, f, indent=2) print(f"[Epoch {epoch+1} Checkpoint] Val Perplexity: {val_ppl:.2f}")
torch.save({"epoch": epoch, "loss": avg_epoch_loss}, os.path.join(logdir, f"epoch_{epoch+1}_metrics.pt")) save_checkpoint(f"epoch_{epoch+1}", val_loss)
print(f"\nTraining complete.")print(f"Checkpoints saved under: {checkpoint_dir}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import osimport sysimport reimport argparsefrom huggingface_hub import HfApi, create_repo
def upload_project(repo_name=None): token = os.environ.get("HF_TOKEN") if not token: print("Error: HF_TOKEN environment variable not set!") print("Please set it before running: set HF_TOKEN=your_token") sys.exit(1)
if not repo_name: repo_name = os.environ.get("HF_REPO_NAME", "gpt2_dense_50M_same_total_param")
local_dir = os.path.dirname(os.path.abspath(__file__))
api = HfApi(token=token) try: user_info = api.whoami() username = user_info["name"] print(f"[HF] Authenticated as: {username}") except Exception as e: print(f"[HF] Authentication failed: {e}") sys.exit(1)
repo_id = f"{username}/{repo_name}" # Auto-create the repo if it does not exist try: create_repo(repo_id=repo_id, repo_type="model", token=token, exist_ok=True) print(f"[HF] Repository '{repo_id}' is ready.") except Exception as e: print(f"[HF] Warning during repository creation: {e}")
print(f"[HF] Uploading GPT-2 46M SwiGLU Dense baseline scripts to '{repo_id}' main branch...") ignore_patterns = [ "**/__pycache__/*", "**/*.pyc", "**/hf_cache/*", "**/nltk_data/*", "**/checkpoints/*" ] sensitive_ignores = [] token_pattern = re.compile(r"hf_[a-zA-Z0-9]{34}") for root, dirs, files in os.walk(local_dir): if "__pycache__" in root or "checkpoints" in root: continue for file in files: file_path = os.path.join(root, file) try: with open(file_path, "r", encoding="utf-8", errors="ignore") as f: content = f.read() if token_pattern.search(content): rel_path = os.path.relpath(file_path, local_dir) rel_path_glob = rel_path.replace("\\", "/") sensitive_ignores.append(rel_path_glob) print(f" -> Warning: Sensitive file containing raw token excluded: {rel_path_glob}") except Exception: pass
all_ignores = ignore_patterns + sensitive_ignores
try: api.upload_folder( folder_path=local_dir, repo_id=repo_id, repo_type="model", revision="main", ignore_patterns=all_ignores, commit_message="Initial commit of GPT-2 46M SwiGLU Dense baseline model architecture and configs" ) print(f"\n[HF] Success! Scripts uploaded to: https://huggingface.co/{repo_id}/tree/main") except Exception as e: print(f"[HF] Upload failed: {e}")
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--repo-name", type=str, default=None, help="Hugging Face repository name") args = parser.parse_args() upload_project(args.repo_name)configuration_gpt2.pyAdded+36
generate_plots_and_report.pyAdded+207
modeling_gpt2.pyAdded+167
train.pyAdded+390
upload_project_hf.pyAdded+84