TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
model package provides the GPT struct which assembles all transformer layers into a trainable and serializable language model. It implements the optimizer.Model interface from github.com/itsubaki/autograd, making it compatible with AdamW and other autograd optimizers out of the box.
GPT Struct
GPT holds the architecture hyperparameters as exported fields alongside an embedded model.Model from the autograd library. The embedded model manages the named layer registry and the full parameter map.
| Field | Description |
|---|---|
VocabSize | Number of tokens in the vocabulary |
MaxContextLen | Maximum sequence length for KV cache allocation and RoPE precomputation |
EmbedDim | Hidden and embedding dimension shared across all layers |
NumOfHeads | Number of multi-head attention heads; head dimension = EmbedDim / NumOfHeads |
NumOfBlocks | Number of transformer blocks stacked in sequence |
Theta | RoPE base frequency (standard default: 10000) |
NewGPT()
NewGPT constructs and initializes a GPT model with randomly initialized weights.
Size of the token vocabulary. Must match the tokenizer’s vocabulary. Example:
1000.Maximum sequence length used for KV cache allocation and RoPE frequency precomputation. Example:
256.Embedding and hidden dimension used consistently across all layers. Example:
192.Number of attention heads. The per-head dimension is computed as
embedDim / numOfHeads. Example: 6.Number of transformer blocks stacked in the model. Example:
6.Base frequency for Rotary Position Embedding (RoPE). The conventional default is
10000.Optional variadic flag. Pass
true to enable KV caching in all attention layers. Required for efficient autoregressive generation; typically omitted during training.Layer composition
NewGPT registers the following named layers in order:
| Key | Type | Description |
|---|---|---|
"embed" | Embeddings | Token embedding lookup table — shape (VocabSize, EmbedDim) |
"block[i]" | BlockT | Transformer block i containing norm1 (RMSNorm) → attn (MultiHeadAttention with RoPE) → residual → norm2 (RMSNorm) → ffn (SwiGLU) → residual |
"norm" | RMSNorm | Final layer normalization before unembedding |
"unembed" | Linear | Linear projection from EmbedDim → VocabSize (no bias) |
function.RoPE(theta, embedDim, maxContextLen) and shared across all blocks.
The
embedDim value in the test suite uses 394 (not 192) to produce integer head dimensions with numOfHeads=6. Always ensure embedDim is evenly divisible by numOfHeads.NewGPTFrom()
NewGPTFrom loads a gob-encoded model checkpoint from disk, reconstructs the architecture from the saved hyperparameters, and copies all saved parameter values into the freshly built model.
Filesystem path to a
.gob file previously written by GPT.Save().Optional variadic flag. Pass
true to enable KV caching in the reconstructed model, typically required for inference.(*GPT, error) — a fully initialized model on success, or a non-nil error if the file cannot be opened, decoded, or if the parameter shapes do not match.
Forward()
Forward runs a full forward pass through the model and returns raw logits.
Token ID tensor with shape
(B, C) where B is the batch size and C is the sequence length. Token values must be integers in the range [0, VocabSize), stored as float64.*variable.Variable — logit tensor with shape (B, C, VocabSize). Each position holds unnormalized log-probabilities over the full vocabulary.
Data flow:
Forward with a shape-(1, 1) input for each new token. The attention layers read cached keys and values from all prior steps automatically.
During training,
Forward is called with the full (B, C) sequence and gradients are tracked. During generation, wrap calls inside variable.Nograd() (done automatically by GenerateChan) to skip gradient allocation.Save() and Load()
Save()
Save encodes the entire GPT struct — hyperparameters and all parameter tensors — to a gob file at the given path.
Destination file path. The file is created (or truncated) by
os.Create.error — non-nil if the file cannot be created or if gob encoding fails.
Load()
Load copies parameter data from a layer.Parameters map (keyed by dotted path strings such as "block[0].attn.Wq.w") into the live model’s parameters in-place.
A
map[string]*layer.Parameter typically obtained from a previously decoded GPT via savedModel.Params(). Every key in params must exist in the current model; an unknown key returns an error.error — non-nil if any key in params is not found in the model.
Named parameter keys follow the pattern <layer>.<sublayer>.<weight>. For a 6-block model the full parameter set includes:
ClearCache()
ClearCache resets the KV cache stored in every transformer block’s attention layer.