Spaces:
Sleeping
Sleeping
Download train.py from ShubhWorking/StoicGPT: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/spaces/ShubhWorking/StoicGPT/resolve/main/train.py
- Command line
-
hf download hf://spaces/ShubhWorking/StoicGPT/train.py
-
curl -L -o train.py https://huggingface.co/spaces/ShubhWorking/StoicGPT/resolve/main/train.py
5.55 kB
| import time | |
| import torch | |
| import tiktoken | |
| from data import load_text, create_dataloader_v1 | |
| from model import GPTModel | |
| GPT_CONFIG = { | |
| "vocab_size": 50257, | |
| "context_length": 256, | |
| "emb_dim": 384, | |
| "n_heads": 6, | |
| "n_layers": 6, | |
| "drop_rate": 0.1, | |
| "qkv_bias": False, | |
| } | |
| SETTINGS = { | |
| "learning_rate": 5e-4, | |
| "num_epochs": 10, | |
| "batch_size": 2, | |
| "weight_decay": 0.1, | |
| } | |
| def text_to_token_ids(text, tokenizer): | |
| return torch.tensor(tokenizer.encode(text)).unsqueeze(0) | |
| def token_ids_to_text(token_ids, tokenizer): | |
| return tokenizer.decode(token_ids.squeeze(0).tolist()) | |
| def calc_loss_batch(input_batch, target_batch, model, device): | |
| input_batch, target_batch = input_batch.to(device), target_batch.to(device) | |
| logits, _ = model(input_batch) | |
| return torch.nn.functional.cross_entropy(logits.flatten(0, 1), target_batch.flatten()) | |
| def calc_loss_loader(data_loader, model, device, num_batches=None): | |
| total_loss = 0.0 | |
| if len(data_loader) == 0: | |
| return float("nan") | |
| num_batches = min(num_batches or len(data_loader), len(data_loader)) | |
| for i, (x, y) in enumerate(data_loader): | |
| if i >= num_batches: | |
| break | |
| total_loss += calc_loss_batch(x, y, model, device).item() | |
| return total_loss / num_batches | |
| def evaluate_model(model, train_loader, val_loader, device, eval_iter): | |
| model.eval() | |
| with torch.no_grad(): | |
| train_loss = calc_loss_loader(train_loader, model, device, eval_iter) | |
| val_loss = calc_loss_loader(val_loader, model, device, eval_iter) | |
| model.train() | |
| return train_loss, val_loss | |
| def generate_text_simple(model, idx, max_new_tokens, context_size): | |
| for _ in range(max_new_tokens): | |
| idx_cond = idx[:, -context_size:] | |
| with torch.no_grad(): | |
| logits, _ = model(idx_cond) | |
| idx_next = torch.argmax(logits[:, -1, :], dim=-1, keepdim=True) | |
| idx = torch.cat((idx, idx_next), dim=1) | |
| return idx | |
| def generate_and_print_sample(model, tokenizer, device, start_context): | |
| model.eval() | |
| encoded = text_to_token_ids(start_context, tokenizer).to(device) | |
| with torch.no_grad(): | |
| token_ids = generate_text_simple(model, encoded, | |
| max_new_tokens=50, | |
| context_size=model.pos_emb.weight.shape[0]) | |
| print(token_ids_to_text(token_ids, tokenizer).replace("\n", " ")) | |
| model.train() | |
| def train_model_simple(model, train_loader, val_loader, optimizer, device, | |
| num_epochs, eval_freq, eval_iter, start_context, tokenizer): | |
| train_losses, val_losses, track_tokens_seen = [], [], [] | |
| tokens_seen, global_step = 0, -1 | |
| for epoch in range(num_epochs): | |
| model.train() | |
| for input_batch, target_batch in train_loader: | |
| optimizer.zero_grad() | |
| loss = calc_loss_batch(input_batch, target_batch, model, device) | |
| loss.backward() | |
| optimizer.step() | |
| tokens_seen += input_batch.numel() | |
| global_step += 1 | |
| if global_step % eval_freq == 0: | |
| train_loss, val_loss = evaluate_model( | |
| model, train_loader, val_loader, device, eval_iter) | |
| train_losses.append(train_loss) | |
| val_losses.append(val_loss) | |
| track_tokens_seen.append(tokens_seen) | |
| print(f"Ep {epoch+1} (Step {global_step:06d}): " | |
| f"Train loss {train_loss:.3f}, Val loss {val_loss:.3f}") | |
| generate_and_print_sample(model, tokenizer, device, start_context) | |
| return train_losses, val_losses, track_tokens_seen | |
| if __name__ == "__main__": | |
| torch.manual_seed(123) | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| print(f"Using device: {device}") | |
| text_data = load_text() | |
| print(f"Total characters: {len(text_data):,}") | |
| split_idx = int(0.90 * len(text_data)) | |
| train_loader = create_dataloader_v1( | |
| text_data[:split_idx], | |
| batch_size=SETTINGS["batch_size"], | |
| max_length=GPT_CONFIG["context_length"], | |
| stride=GPT_CONFIG["context_length"], | |
| drop_last=True, shuffle=True, num_workers=4, | |
| ) | |
| val_loader = create_dataloader_v1( | |
| text_data[split_idx:], | |
| batch_size=SETTINGS["batch_size"], | |
| max_length=GPT_CONFIG["context_length"], | |
| stride=GPT_CONFIG["context_length"], | |
| drop_last=False, shuffle=False, num_workers=4, | |
| ) | |
| model = GPTModel(GPT_CONFIG).to(device) | |
| optimizer = torch.optim.AdamW( | |
| model.parameters(), | |
| lr=SETTINGS["learning_rate"], | |
| weight_decay=SETTINGS["weight_decay"], | |
| ) | |
| tokenizer = tiktoken.get_encoding("gpt2") | |
| total_params = sum(p.numel() for p in model.parameters()) | |
| print(f"Model parameters: {total_params:,}") | |
| print(f"Training for {SETTINGS['num_epochs']} epochs — estimated 2–4 hrs on CPU. Go grab a coffee.\n") | |
| start_time = time.time() | |
| train_losses, val_losses, tokens_seen = train_model_simple( | |
| model, train_loader, val_loader, optimizer, device, | |
| num_epochs=SETTINGS["num_epochs"], | |
| eval_freq=5, | |
| eval_iter=1, | |
| start_context="The obstacle is the way", | |
| tokenizer=tokenizer, | |
| ) | |
| elapsed = time.time() - start_time | |
| hours, rem = divmod(int(elapsed), 3600) | |
| minutes, seconds = divmod(rem, 60) | |
| print(f"\nTraining complete in {hours}h {minutes}m {seconds}s") | |
| torch.save(model.state_dict(), "model.pth") | |
| print("Model saved to model.pth") | |