Delete main.py
Browse files
main.py
DELETED
|
@@ -1,114 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from torch.utils.data import DataLoader
|
| 3 |
-
from adria import Adria, DatasetLoader, Trainer
|
| 4 |
-
import tiktoken
|
| 5 |
-
|
| 6 |
-
# THIS, however, IS Gemini slop code. All it does is just run a train and generate loop.
|
| 7 |
-
# As of this example train/generate file, the model still uses tiktoken and is trained on TinyStories with ~17.5M params.
|
| 8 |
-
|
| 9 |
-
# --- CONFIGURATION ---
|
| 10 |
-
BATCH_SIZE = 16 # Keep small for laptop GPU
|
| 11 |
-
ACCUM_STEPS = 16 # Effective batch = 64
|
| 12 |
-
MAX_LEN = 256
|
| 13 |
-
TOTAL_STEPS = 1
|
| 14 |
-
|
| 15 |
-
def train():
|
| 16 |
-
# 1. Initialize Data Loader
|
| 17 |
-
# Using 'gpt2' tokenizer for efficiency (50k vocab) vs o200k (200k vocab)
|
| 18 |
-
# Using 'roneneldan/TinyStories' as the dataset. (You can train it on other datasets if you want)
|
| 19 |
-
dataset = DatasetLoader("roneneldan/TinyStories", tokenizer_name="gpt2", max_length=MAX_LEN)
|
| 20 |
-
loader = DataLoader(dataset, batch_size=BATCH_SIZE)
|
| 21 |
-
|
| 22 |
-
# 2. Initialize Model
|
| 23 |
-
# Crucial: pass the vocab size FROM the dataset to the model
|
| 24 |
-
print(f">> Initializing Adria with vocab size: {dataset.vocab_size}")
|
| 25 |
-
|
| 26 |
-
model = Adria(
|
| 27 |
-
vocab_size=dataset.vocab_size,
|
| 28 |
-
dim_model=256,
|
| 29 |
-
head_amount=8,
|
| 30 |
-
layer_num=6,
|
| 31 |
-
dim_hidden_layer=1024,
|
| 32 |
-
max_len=256
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
# 3. Initialize Trainer (Also set that save_dir to a folder you want to save the .pth files to)
|
| 36 |
-
trainer = Trainer(model, loader, accum_steps=ACCUM_STEPS, save_dir="adria_checkpoints")
|
| 37 |
-
|
| 38 |
-
# 4. Run
|
| 39 |
-
trainer.train(total_steps=(TOTAL_STEPS * ACCUM_STEPS))
|
| 40 |
-
|
| 41 |
-
def generate():
|
| 42 |
-
|
| 43 |
-
# Load model for inference
|
| 44 |
-
tokenizer = tiktoken.get_encoding("gpt2")
|
| 45 |
-
vocab_size = tokenizer.n_vocab
|
| 46 |
-
|
| 47 |
-
# 1. Setup Device
|
| 48 |
-
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 49 |
-
print(f"Running inference on: {device}")
|
| 50 |
-
|
| 51 |
-
# 2. Match Training Hyperparameters (As we fixed before)
|
| 52 |
-
model = Adria(
|
| 53 |
-
vocab_size=vocab_size,
|
| 54 |
-
dim_model=256,
|
| 55 |
-
head_amount=8,
|
| 56 |
-
layer_num=6,
|
| 57 |
-
dim_hidden_layer=1024,
|
| 58 |
-
max_len=256
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# Load weights
|
| 62 |
-
try:
|
| 63 |
-
model.load_state_dict(torch.load("adria_checkpoints/adria_step_9000.pth"))
|
| 64 |
-
print("Model weights loaded.")
|
| 65 |
-
except FileNotFoundError:
|
| 66 |
-
print("No checkpoint found, generating with random weights.")
|
| 67 |
-
|
| 68 |
-
# 3. Move Model to GPU
|
| 69 |
-
model.to(device)
|
| 70 |
-
model.eval() # Good practice to set eval mode
|
| 71 |
-
|
| 72 |
-
prompt = input("Enter a prompt: ")
|
| 73 |
-
|
| 74 |
-
# 4. CRITICAL FIX: Move the input tensor to the device!
|
| 75 |
-
start_ids = torch.tensor(tokenizer.encode(prompt), dtype=torch.long).unsqueeze(0).to(device)
|
| 76 |
-
|
| 77 |
-
print(f"Prompt: {prompt}")
|
| 78 |
-
|
| 79 |
-
# Run generation
|
| 80 |
-
# We use torch.no_grad() to save memory since we aren't training
|
| 81 |
-
with torch.no_grad():
|
| 82 |
-
out_ids = model.generate(start_ids, max_new_tokens=100, temperature=0.8, top_k=40)
|
| 83 |
-
|
| 84 |
-
print(tokenizer.decode(out_ids[0].tolist()))
|
| 85 |
-
|
| 86 |
-
def print_model_params(model):
|
| 87 |
-
|
| 88 |
-
# 1. Count all parameters (including shared ones counted twice)
|
| 89 |
-
logical_params = sum(p.numel() for p in model.parameters())
|
| 90 |
-
|
| 91 |
-
# 2. Count only UNIQUE parameters (handles weight tying correctly)
|
| 92 |
-
unique_params = sum(p.numel() for p in set(model.parameters()))
|
| 93 |
-
|
| 94 |
-
print(f"Logical Parameters (if no tying): {logical_params:,}")
|
| 95 |
-
print(f"Actual Unique Parameters: {unique_params:,}")
|
| 96 |
-
|
| 97 |
-
# Estimate size in RAM (Float32 = 4 bytes per param)
|
| 98 |
-
print(f"Model Size in VRAM (Float32): {unique_params * 4 / 1024**2:.2f} MB")
|
| 99 |
-
|
| 100 |
-
if __name__ == "__main__":
|
| 101 |
-
|
| 102 |
-
model = Adria(
|
| 103 |
-
vocab_size=(tiktoken.get_encoding("gpt2")).n_vocab,
|
| 104 |
-
dim_model=256,
|
| 105 |
-
head_amount=8,
|
| 106 |
-
layer_num=6,
|
| 107 |
-
dim_hidden_layer=1024,
|
| 108 |
-
max_len=256
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
# Uncomment the mode you want to run
|
| 112 |
-
# train()
|
| 113 |
-
generate()
|
| 114 |
-
# print_model_params(model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|