File size: 6,388 Bytes
231a212 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | from __future__ import annotations
import copy
import json
from pathlib import Path
import numpy as np
import torch
import trackio
from data import VOCAB_SIZE, generate_selective_memory
from model import GRUControl, SelectiveSSM, parameter_count
from safetensors.torch import load_file, save_file
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
PROJECT_DIR = Path(__file__).resolve().parent
ARTIFACT_DIR = PROJECT_DIR / "artifacts" / "micro-mamba"
DATA_DIR = PROJECT_DIR / "data"
def seed_everything(seed: int) -> None:
np.random.seed(seed)
torch.manual_seed(seed)
torch.set_num_threads(1)
def loader_from(
dataset: tuple[np.ndarray, np.ndarray, np.ndarray],
batch_size: int,
shuffle: bool,
seed: int,
) -> DataLoader:
tokens, markers, targets = dataset
return DataLoader(
TensorDataset(
torch.from_numpy(tokens),
torch.from_numpy(markers),
torch.from_numpy(targets),
),
batch_size=batch_size,
shuffle=shuffle,
generator=torch.Generator().manual_seed(seed),
)
@torch.inference_mode()
def evaluate(model: nn.Module, loader: DataLoader) -> dict:
model.eval()
correct = 0
examples = 0
losses = []
criterion = nn.CrossEntropyLoss()
for tokens, markers, targets in loader:
logits = model(tokens, markers)
losses.append(float(criterion(logits, targets)))
correct += int((logits.argmax(1) == targets).sum())
examples += len(targets)
return {
"accuracy": correct / examples,
"cross_entropy": float(np.mean(losses)),
}
def train_variant(
name: str,
model: nn.Module,
train_loader: DataLoader,
validation_loader: DataLoader,
) -> tuple[nn.Module, list[dict]]:
optimizer = torch.optim.AdamW(model.parameters(), lr=2e-3, weight_decay=1e-4)
criterion = nn.CrossEntropyLoss()
best_state = copy.deepcopy(model.state_dict())
best_accuracy = -1.0
stale = 0
history = []
for epoch in range(1, 26):
model.train()
losses = []
for tokens, markers, targets in train_loader:
logits = model(tokens, markers)
loss = criterion(logits, targets)
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
losses.append(float(loss.detach()))
validation = evaluate(model, validation_loader)
record = {
"variant": name,
"epoch": epoch,
"training_loss": float(np.mean(losses)),
"validation_accuracy": validation["accuracy"],
}
history.append(record)
trackio.log(record)
if validation["accuracy"] > best_accuracy + 1e-4:
best_accuracy = validation["accuracy"]
best_state = copy.deepcopy(model.state_dict())
stale = 0
else:
stale += 1
if stale >= 8 and epoch >= 15:
break
model.load_state_dict(best_state)
return model, history
def main() -> None:
seed_everything(2043)
length = 48
train_data = generate_selective_memory(12_000, length, seed=2043)
validation_data = generate_selective_memory(2_000, length, seed=3043)
test_data = generate_selective_memory(4_000, length, seed=4043)
long_test_data = generate_selective_memory(4_000, 96, seed=5043)
train_loader = loader_from(train_data, 256, True, 2043)
validation_loader = loader_from(validation_data, 512, False, 3043)
test_loader = loader_from(test_data, 512, False, 4043)
long_test_loader = loader_from(long_test_data, 512, False, 5043)
variants = {
"selective_ssm": SelectiveSSM(VOCAB_SIZE, selective=True),
"fixed_ssm": SelectiveSSM(VOCAB_SIZE, selective=False),
"gru": GRUControl(VOCAB_SIZE),
}
trackio.init(
project="micro-mamba",
name="selective-state-space-memory-v1",
config={
"training_examples": len(train_data[0]),
"sequence_length": length,
"marked_items": 4,
"variants": {
name: parameter_count(model) for name, model in variants.items()
},
},
)
histories = {}
results = {}
ARTIFACT_DIR.mkdir(parents=True, exist_ok=True)
for name, model in variants.items():
checkpoint = ARTIFACT_DIR / f"{name}.safetensors"
if checkpoint.exists():
model.load_state_dict(load_file(checkpoint))
trained, history = model, []
else:
trained, history = train_variant(
name, model, train_loader, validation_loader
)
save_file(trained.state_dict(), checkpoint)
histories[name] = history
results[name] = {
"parameters": parameter_count(trained),
"training_epochs": 25,
"epochs_in_current_run": len(history),
"checkpoint_reused": not bool(history),
"length_48": evaluate(trained, test_loader),
"length_96_zero_shot": evaluate(trained, long_test_loader),
}
report = {
"benchmark": "Selective ordinal memory",
"training_examples": len(train_data[0]),
"training_sequence_length": length,
"test_examples_per_length": len(test_data[0]),
"results": results,
"training_history": histories,
}
(ARTIFACT_DIR / "evaluation.json").write_text(
json.dumps(report, indent=2), encoding="utf-8"
)
DATA_DIR.mkdir(parents=True, exist_ok=True)
np.savez_compressed(
DATA_DIR / "selective_memory_test.npz",
tokens=test_data[0],
markers=test_data[1],
targets=test_data[2],
)
trackio.log(
{
"selective_ssm_test_accuracy": results["selective_ssm"][
"length_48"
]["accuracy"],
"fixed_ssm_test_accuracy": results["fixed_ssm"]["length_48"][
"accuracy"
],
"gru_test_accuracy": results["gru"]["length_48"]["accuracy"],
"selective_ssm_long_accuracy": results["selective_ssm"][
"length_96_zero_shot"
]["accuracy"],
}
)
trackio.finish()
print(json.dumps(report, indent=2))
if __name__ == "__main__":
main()
|