Assemble decoder-only model: forward + next-token CE loss (MULTI-1383)#6
Merged
Conversation
…-1383)
Build a Burn `Module` that turns raw tokens into a training loss: token
embedding → sinusoidal positional encoding → causal-masked
`TransformerEncoder` → final `LayerNorm` → untied LM head. Use the
encoder (not `TransformerDecoder`) because a decoder-only GPT has no
cross-attention or encoder memory; the encoder + a causal mask is the
correct primitive. The `norm_first` toggle threads through from
`ModelConfig`.
`next_token_cross_entropy` does the standard left-shift and flattens to
`CrossEntropyLoss`, so the last logit slot has no target and the first
token has no prediction.
Tests cover the four MULTI-1383 acceptance criteria:
* `gpt2_small` instantiates in the ~100M-parameter class (80M–140M),
catching slipped width / depth / vocab against the locked tokenizer
from MULTI-1379.
* Forward output is `[batch × seq × vocab]` with no NaN/Inf at init.
* Loss at init lands within ±2 nats of `ln(vocab)` — the uniform
predictor's entropy — catching missing log-softmax or wrong vocab
dim.
* Every float parameter receives a non-zero gradient after backward
on `TrainBackend`, proving the autograd graph reaches the whole
module.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Module: token embedding → sinusoidalPositionalEncoding→ causal-maskedTransformerEncoder(honoringnorm_first) → finalLayerNorm→ untied LM head. Uses the encoder (notTransformerDecoder) because decoder-only GPTs have no cross-attention or encoder memory; the encoder + a causal mask is the correct primitive.next_token_cross_entropy(logits, tokens): the standard left-shift + flatten to Burn'sCrossEntropyLoss.Test plan
cargo fmt --all --checkcargo clippy --all-targets --workspace --locked -- -D warningscargo nextest run --workspace --locked— 59 tests pass, including the four new ones:gpt2_small_lands_in_the_100m_param_class— actualnum_params()falls in 80M–140M with the locked 16k vocab.forward_returns_batch_seq_vocab_with_no_nan_or_inf— output shape[B, T, V], all finite.loss_at_init_is_near_ln_vocab— CE within ±2 nats ofln(vocab).all_parameters_receive_non_zero_gradients— backward onTrainBackendproduces a non-zero abs-sum gradient for every visited float param.Closes MULTI-1383.
🤖 Generated with Claude Code