Spaces:
Running on Zero
Running on Zero
Delete files src/train.py with huggingface_hub
Browse files- src/train.py +0 -172
src/train.py
DELETED
|
@@ -1,172 +0,0 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
from src.infra.tokenizer import CharacterTokenizer
|
| 6 |
-
from src.infra.dataset import AutoRegressiveDataset, split_tokens
|
| 7 |
-
|
| 8 |
-
from src.task.config import ModelConfig,TrainingConfig
|
| 9 |
-
|
| 10 |
-
from src.task.model import TinyTransformerLM
|
| 11 |
-
from src.task.training import train, evaluate
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def separator(title: str) -> None:
|
| 16 |
-
print("\n" + "=" * 60)
|
| 17 |
-
print(title)
|
| 18 |
-
print("=" * 60)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# --------------------------------------------------
|
| 22 |
-
# Configuration
|
| 23 |
-
# --------------------------------------------------
|
| 24 |
-
|
| 25 |
-
separator("Configuration")
|
| 26 |
-
|
| 27 |
-
torch.manual_seed(42)
|
| 28 |
-
|
| 29 |
-
model_config = ModelConfig()
|
| 30 |
-
training_config = TrainingConfig()
|
| 31 |
-
|
| 32 |
-
print(model_config)
|
| 33 |
-
print(training_config)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# --------------------------------------------------
|
| 37 |
-
# Load corpus
|
| 38 |
-
# --------------------------------------------------
|
| 39 |
-
|
| 40 |
-
separator("Corpus")
|
| 41 |
-
|
| 42 |
-
data_path = Path("data/tiny_shakespear.txt")
|
| 43 |
-
|
| 44 |
-
text = data_path.read_text(
|
| 45 |
-
encoding="utf-8",
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
print(f"Corpus length: {len(text):,} characters")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# --------------------------------------------------
|
| 52 |
-
# Tokenization
|
| 53 |
-
# --------------------------------------------------
|
| 54 |
-
|
| 55 |
-
separator("Tokenization")
|
| 56 |
-
|
| 57 |
-
tokenizer = CharacterTokenizer(text)
|
| 58 |
-
|
| 59 |
-
tokens = torch.tensor(
|
| 60 |
-
tokenizer.encode(text),
|
| 61 |
-
dtype=torch.long,
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
print(f"Vocabulary size: {tokenizer.vocab_size}")
|
| 65 |
-
print(f"Token count: {len(tokens):,}")
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# --------------------------------------------------
|
| 69 |
-
# Train / validation / test split
|
| 70 |
-
# --------------------------------------------------
|
| 71 |
-
|
| 72 |
-
separator("Data Split")
|
| 73 |
-
|
| 74 |
-
train_tokens, val_tokens, test_tokens = split_tokens(
|
| 75 |
-
tokens,
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
print(f"Train tokens: {len(train_tokens):,}")
|
| 79 |
-
print(f"Val tokens: {len(val_tokens):,}")
|
| 80 |
-
print(f"Test tokens: {len(test_tokens):,}")
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# --------------------------------------------------
|
| 84 |
-
# Datasets
|
| 85 |
-
# --------------------------------------------------
|
| 86 |
-
|
| 87 |
-
separator("Datasets")
|
| 88 |
-
|
| 89 |
-
train_dataset = AutoRegressiveDataset(
|
| 90 |
-
tokens=train_tokens,
|
| 91 |
-
context_length=model_config.context_length,
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
val_dataset = AutoRegressiveDataset(
|
| 95 |
-
tokens=val_tokens,
|
| 96 |
-
context_length=model_config.context_length,
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
test_dataset = AutoRegressiveDataset(
|
| 100 |
-
tokens=test_tokens,
|
| 101 |
-
context_length=model_config.context_length,
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
print(f"Train examples: {len(train_dataset):,}")
|
| 105 |
-
print(f"Val examples: {len(val_dataset):,}")
|
| 106 |
-
print(f"Test examples: {len(test_dataset):,}")
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
# --------------------------------------------------
|
| 110 |
-
# Model
|
| 111 |
-
# --------------------------------------------------
|
| 112 |
-
|
| 113 |
-
separator("Model")
|
| 114 |
-
|
| 115 |
-
model = TinyTransformerLM(
|
| 116 |
-
config=model_config,
|
| 117 |
-
vocab_size=tokenizer.vocab_size,
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
print(model)
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# --------------------------------------------------
|
| 124 |
-
# Training
|
| 125 |
-
# --------------------------------------------------
|
| 126 |
-
|
| 127 |
-
separator("Training")
|
| 128 |
-
|
| 129 |
-
train(
|
| 130 |
-
model=model,
|
| 131 |
-
train_dataset=train_dataset,
|
| 132 |
-
val_dataset=val_dataset,
|
| 133 |
-
model_config=model_config,
|
| 134 |
-
training_config=training_config,
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# --------------------------------------------------
|
| 139 |
-
# Final test evaluation
|
| 140 |
-
# --------------------------------------------------
|
| 141 |
-
|
| 142 |
-
separator("Test Evaluation")
|
| 143 |
-
|
| 144 |
-
test_loss = evaluate(
|
| 145 |
-
model=model,
|
| 146 |
-
dataset=test_dataset,
|
| 147 |
-
model_config=model_config,
|
| 148 |
-
training_config=training_config,
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
print(f"Test loss: {test_loss:.4f}")
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
# --------------------------------------------------
|
| 155 |
-
# Save checkpoint
|
| 156 |
-
# --------------------------------------------------
|
| 157 |
-
|
| 158 |
-
separator("Saving")
|
| 159 |
-
|
| 160 |
-
checkpoint_dir = Path("checkpoints")
|
| 161 |
-
checkpoint_dir.mkdir(parents=True, exist_ok=True)
|
| 162 |
-
|
| 163 |
-
checkpoint_path = (
|
| 164 |
-
checkpoint_dir / "tiny-sentence-transformer.pt"
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
torch.save(
|
| 168 |
-
model.state_dict(),
|
| 169 |
-
checkpoint_path,
|
|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