core/nn/gpt2_hf_loader library
Loads HuggingFace gpt2 / gpt2-medium / gpt2-large / gpt2-xl
weights into a GPT module.
The expected on-disk layout is a HuggingFace safetensors file
produced by e.g.:
from transformers import GPT2LMHeadModel
m = GPT2LMHeadModel.from_pretrained("gpt2")
m.save_pretrained("gpt2", safe_serialization=True)
# -> gpt2/model.safetensors
or downloaded straight from the HF Hub (model.safetensors).
Weight conventions (HF → dart_pytorch):
wte.weight[V, D]→tokenEmb.weightwpe.weight[maxCtx, D]→posEmb.table.weighth.{i}.ln_1.weight/bias[D]→block.ln1.gamma/betah.{i}.attn.c_attn.weight[D, 3D]→ block QKV per-head weightsh.{i}.attn.c_attn.bias[3D]→ block QKV per-head biasesh.{i}.attn.c_proj.weight[D, D]→ blockmha.wo.weighth.{i}.attn.c_proj.bias[D]→ blockmha.wo.biash.{i}.ln_2.weight/bias[D]→block.ln2.gamma/betah.{i}.mlp.c_fc.weight[D, 4D]→block.ffn1.weighth.{i}.mlp.c_fc.bias[4D]→block.ffn1.biash.{i}.mlp.c_proj.weight[4D, D]→block.ffn2.weighth.{i}.mlp.c_proj.bias[D]→block.ffn2.biasln_f.weight/bias[D]→encoder.finalNorm.gamma/beta
GPT-2 uses Conv1D layers (weight shape [in, out]), so every
_.weight matrix is transposed on load.
The lm_head.weight is tied to wte.weight in HF; when the target
GPT has tieWeights: true (the default) it is loaded implicitly
via wte. When untied, the loader falls back to wte for the
head as well (HF does not save a separate head).
GPT-2 uses GELU (tanh approx) and biased attention projections;
callers must build the target GPT with
attnBias: true, activation: Activation.geluTanh or the outputs
will diverge from the reference model.
Classes
- GPT2HFLoader
- GPT2LoadReport
-
Diagnostic summary of a single GPT2HFLoader.loadFile /
loadMapcall.unusedKeystypically contains extra HF conveniences likeattn.bias(the causal mask buffer) ormasked_bias; these are safe to ignore because we build the causal mask on the fly.