-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckpoint.py
More file actions
31 lines (25 loc) · 1.35 KB
/
Copy pathcheckpoint.py
File metadata and controls
31 lines (25 loc) · 1.35 KB
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
import json
import math
import struct
import torch
from model import TinyGpt2
WEIGHT_NAMES = ["token", "position", "query", "key", "value", "attention", "attention_bias",
"expand", "expand_bias", "contract", "contract_bias", "head", "head_bias",
"norm1_scale", "norm1_bias", "norm2_scale", "norm2_bias", "norm_final_scale", "norm_final_bias"]
def load_checkpoint(path):
record = json.loads(path.read_text())
if record["format"] != "leanexe-tiny-gpt2-checkpoint-v1":
raise ValueError("Unsupported checkpoint format")
model = TinyGpt2(record["architecture"]["context"])
if set(record["weights"]) != set(model.weights):
raise ValueError("Checkpoint tensor names differ from the model")
with torch.no_grad():
for name, parameter in model.weights.items():
tensor = record["weights"][name]
if tensor["shape"] != list(parameter.shape) or len(tensor["bits"]) != parameter.numel():
raise ValueError(f"Invalid checkpoint shape: {name}")
values = [struct.unpack(">d", bytes.fromhex(word))[0] for word in tensor["bits"]]
if not all(math.isfinite(value) for value in values):
raise ValueError(f"Nonfinite checkpoint value: {name}")
parameter.copy_(torch.tensor(values).reshape(parameter.shape))
return model, record