| import os
|
| import torch
|
| import torch.nn as nn
|
| import torch.optim as optim
|
| from torch.utils.data import IterableDataset, DataLoader
|
| from transformers import GPT2TokenizerFast
|
| from tqdm import tqdm
|
| import shutil
|
| import math
|
| from pathlib import Path
|
| import re
|
|
|
|
|
| TRAIN_SEQ_LEN = 256
|
| BATCH_SIZE = 2
|
| EPOCHS = 50
|
| LEARNING_RATE = 6e-6
|
| WEIGHT_DECAY = 0.01
|
| GRAD_CLIP = 1.0
|
| KEEP_LAST_EPOCHS = 3
|
| VAL_SPLIT_RATIO = 0.05
|
| VOCAB_SIZE = 50257
|
|
|
| BASE_MODEL_PATH = Path("models/gpt_modern_1b_class.script.pt")
|
| LAST_TRAINED_PATH = Path("models/gpt_1b_last_trained.script.pt")
|
| BACKUP_DIR = Path("models/backups")
|
| BACKUP_DIR.mkdir(exist_ok=True)
|
|
|
| RAW_PATH = Path("datasets/dialogues_text.txt")
|
| CLEAN_PATH = Path("datasets/dialogues_text_clean.txt")
|
|
|
|
|
| force_clean = False
|
| if not CLEAN_PATH.exists():
|
| print("Cleaned dataset not found. Performing initial cleaning...")
|
| force_clean = True
|
| else:
|
| try:
|
| if RAW_PATH.stat().st_mtime > CLEAN_PATH.stat().st_mtime:
|
| print("Detected changes in the raw dataset. Re-cleaning...")
|
| force_clean = True
|
| else:
|
| print(f"Using existing cleaned dataset → {CLEAN_PATH}")
|
| except FileNotFoundError:
|
| print("File system synchronization error. Performing re-cleaning for safety...")
|
| force_clean = True
|
|
|
| if force_clean:
|
| if not RAW_PATH.exists():
|
| raise FileNotFoundError(f"ERROR: Source file {RAW_PATH} not found. Check the path.")
|
|
|
| print("Cleaning up the dataset from garbage (wrong separators, extra spaces)...")
|
| text = RAW_PATH.read_text(encoding="utf-8")
|
| text = re.sub(r' {2,}', ' ', text)
|
| text = text.replace(" \n", "\n").replace("\n ", "\n")
|
| CLEAN_PATH.write_text(text, encoding="utf-8")
|
| print(f"Dataset successfully cleaned and saved → {CLEAN_PATH}")
|
|
|
| DATASET_PATH = CLEAN_PATH
|
| OUTPUT_DIR = Path("build/fine_tuning_output")
|
| MODEL_SAVE_NAME = "gpt_finetuned.script.pt"
|
|
|
| device = torch.device("cpu")
|
| print(f"Using device: {device}")
|
|
|
|
|
|
|
| class LazyTextDataset(IterableDataset):
|
| """Lazy memory-efficient dataset, splits on-the-fly into train and val."""
|
| def __init__(self, text_file, seq_len=TRAIN_SEQ_LEN, tokenizer_name="gpt2", split_type='train', val_ratio=VAL_SPLIT_RATIO):
|
| self.seq_len = seq_len
|
| self.tokenizer = GPT2TokenizerFast.from_pretrained(tokenizer_name)
|
| self.tokenizer.pad_token = self.tokenizer.eos_token
|
| self.text_file = text_file
|
| self.split_type = split_type
|
| self.val_ratio = val_ratio
|
|
|
| print(f"Loading and tokenizing text from {text_file}")
|
| with open(text_file, "r", encoding="utf-8") as f:
|
| self.data = f.read()
|
| self.tokens = self.tokenizer.encode(self.data)
|
|
|
|
|
| total_tokens = len(self.tokens) - 1
|
| total_batches = total_tokens // seq_len
|
| val_size = int(total_batches * self.val_ratio)
|
| train_size = total_batches - val_size
|
| if split_type == 'train':
|
| self.start = 0
|
| self.stop = train_size
|
| elif split_type == 'val':
|
| self.start = train_size
|
| self.stop = train_size + val_size
|
| else:
|
| raise ValueError(f"split_type should be 'train' or 'val', got {split_type}")
|
| self.total_sequences = self.stop - self.start
|
| print(f"Lazy dataset: {self.total_sequences:,} sequences for {split_type} split (from {total_batches:,} total)")
|
|
|
| def __iter__(self):
|
| for i in range(self.start * self.seq_len, self.stop * self.seq_len, self.seq_len):
|
|
|
| if i + self.seq_len + 1 > len(self.tokens):
|
| break
|
| input_seq = torch.tensor(self.tokens[i : i + self.seq_len], dtype=torch.long)
|
| label_seq = torch.tensor(self.tokens[i + 1 : i + self.seq_len + 1], dtype=torch.long)
|
| yield input_seq, label_seq
|
|
|
| def __len__(self):
|
| return self.total_sequences
|
|
|
|
|
|
|
| def get_logits_from_model(model, inputs):
|
| try:
|
| logits, _ = model(inputs)
|
| except Exception:
|
| logits = model(inputs)
|
| return logits
|
|
|
|
|
|
|
| def evaluate(model, dataloader, criterion, device):
|
| model.eval()
|
| total_loss = 0.0
|
| count = 0
|
| with torch.no_grad():
|
| for inputs, targets in dataloader:
|
| inputs, targets = inputs.to(device), targets.to(device)
|
| logits = get_logits_from_model(model, inputs)
|
| logits = logits.contiguous().view(-1, logits.size(-1))
|
| targets = targets.contiguous().view(-1)[:logits.shape[0]]
|
| loss = criterion(logits, targets)
|
| total_loss += loss.item()
|
| count += 1
|
| avg_loss = total_loss / max(count, 1)
|
| model.train()
|
| return avg_loss
|
|
|
|
|
|
|
| def cleanup_old_epochs(keep_last=KEEP_LAST_EPOCHS):
|
| epochs = sorted([p for p in OUTPUT_DIR.glob("epoch*") if p.is_dir()],
|
| key=lambda x: int(x.name.replace("epoch", "")))
|
| for old in epochs[:-keep_last]:
|
| if old.exists():
|
| shutil.rmtree(old)
|
| print(f"Old epoch deleted: {old.name}")
|
|
|
|
|
|
|
| def train():
|
| OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| print("Loading model...")
|
| model = None
|
| if LAST_TRAINED_PATH.exists():
|
| print(f"Continuing training from last JIT model: {LAST_TRAINED_PATH}")
|
| model = torch.jit.load(LAST_TRAINED_PATH, map_location=device)
|
| elif BASE_MODEL_PATH.exists():
|
| print(f"Starting from base JIT model: {BASE_MODEL_PATH}")
|
| model = torch.jit.load(BASE_MODEL_PATH, map_location=device)
|
| else:
|
| print(f"ERROR: JIT model not found. Checked paths: {BASE_MODEL_PATH} and {LAST_TRAINED_PATH}")
|
| print("Please run the JIT export script (e.g., 'model_export.py') first.")
|
| return
|
|
|
|
|
| for n, p in model.named_parameters():
|
| if torch.isnan(p).any():
|
| print(f"[FATAL] NaN in weights: {n}")
|
| exit(10)
|
| if torch.isinf(p).any():
|
| print(f"[FATAL] Inf in weights: {n}")
|
| exit(11)
|
|
|
| model.train()
|
| try:
|
| model.gradient_checkpointing_enable()
|
| print("✅ Gradient Checkpointing Enabled.")
|
| except AttributeError:
|
| print("⚠️ Warning: model.gradient_checkpointing_enable() not found on JIT model. Training will proceed without GC.")
|
|
|
| train_dataset = LazyTextDataset(DATASET_PATH, seq_len=TRAIN_SEQ_LEN, split_type='train', val_ratio=VAL_SPLIT_RATIO)
|
| val_dataset = LazyTextDataset(DATASET_PATH, seq_len=TRAIN_SEQ_LEN, split_type='val', val_ratio=VAL_SPLIT_RATIO)
|
|
|
|
|
| train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=False, drop_last=True, num_workers=0)
|
| val_dataloader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, drop_last=True, num_workers=0)
|
|
|
| optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
|
| criterion = nn.CrossEntropyLoss()
|
|
|
| total_steps = (len(train_dataset) // BATCH_SIZE) * EPOCHS
|
| print(f"\n=== BEGINNING LONG-TERM TRAINING ===")
|
| print(f"Epochs: {EPOCHS} | Steps (Train): {total_steps} | Examples (Train): {len(train_dataset)}")
|
| print(f"Batch Size (Effective): {BATCH_SIZE} | Precision: FP32")
|
|
|
| global_step = 0
|
| for epoch in range(1, EPOCHS + 1):
|
| print(f"\n--- Epoch {epoch}/{EPOCHS} ---")
|
| epoch_loss = 0.0
|
|
|
| with tqdm(train_dataloader, desc=f"Epoch {epoch} [TRAIN]", leave=False) as pbar:
|
| for inputs, targets in pbar:
|
| inputs, targets = inputs.to(device), targets.to(device)
|
| optimizer.zero_grad()
|
| logits = get_logits_from_model(model, inputs)
|
| logits = logits.contiguous().view(-1, logits.size(-1))
|
| targets = targets.contiguous().view(-1)[:logits.shape[0]]
|
| loss = criterion(logits, targets)
|
| loss.backward()
|
| torch.nn.utils.clip_grad_norm_(model.parameters(), GRAD_CLIP)
|
| optimizer.step()
|
|
|
| loss_val = loss.item()
|
| epoch_loss += loss_val
|
| global_step += 1
|
|
|
| pbar.set_postfix({
|
| "loss": f"{loss_val:.3f}",
|
| "ppl": f"{math.exp(min(loss_val, 10)):.1f}",
|
| "step": f"{global_step}"
|
| })
|
|
|
| avg_train_loss = epoch_loss / max(1, len(train_dataset) // BATCH_SIZE)
|
| print(f" [TRAIN] Average loss: {avg_train_loss:.3f} | PPL: {math.exp(avg_train_loss):.1f}")
|
|
|
| print(" [VALIDATION] Starting evaluation...")
|
| val_loss = evaluate(model, val_dataloader, criterion, device)
|
| print(f" [VALIDATION] Average loss: {val_loss:.3f} | PPL: {math.exp(val_loss):.1f}")
|
|
|
| epoch_dir = OUTPUT_DIR / f"epoch{epoch}"
|
| epoch_dir.mkdir(exist_ok=True)
|
| torch.jit.save(model, epoch_dir / MODEL_SAVE_NAME)
|
| print(f"Model saved: {epoch_dir / MODEL_SAVE_NAME}")
|
| cleanup_old_epochs()
|
|
|
| final_dir = OUTPUT_DIR / "final"
|
| final_dir.mkdir(exist_ok=True)
|
| torch.jit.save(model, final_dir / MODEL_SAVE_NAME)
|
| train_dataset.tokenizer.save_pretrained(final_dir)
|
|
|
| if LAST_TRAINED_PATH.exists():
|
| backup_path = BACKUP_DIR / f"gpt_last_trained_backup_{int(os.path.getmtime(LAST_TRAINED_PATH))}.script.pt"
|
| shutil.copy(LAST_TRAINED_PATH, backup_path)
|
| print(f"Backup of previous model created → {backup_path.name}")
|
|
|
| shutil.copy(final_dir / MODEL_SAVE_NAME, LAST_TRAINED_PATH)
|
| print(f"Last trained model saved → {LAST_TRAINED_PATH}")
|
|
|
| print(f"\nTRAINING COMPLETED! Model ready:")
|
| print(f" • For chat: {final_dir / MODEL_SAVE_NAME}")
|
| print(f" • For further fine-tuning: {LAST_TRAINED_PATH}")
|
|
|
| if __name__ == "__main__":
|
| if not RAW_PATH.exists():
|
| print(f"ERROR: No file {RAW_PATH}")
|
| print("Put your text into datasets/dialogues_text.txt")
|
| else:
|
| train() |