11.5 Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing
The seq2seq model of the previous chapter, from Encoder-decoder with teacher forcing through Luong attention, reads a sentence one token at a time through a GRU. Training cannot run the positions in parallel, and everything the decoder knows about the source passes through attention over states that were themselves built sequentially. The three parts that remove the recurrence are now in place: attention with heads, masks, and positions.
Concepts from other sections (16)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Gated recurrent unit (GRU): a recurrent cell with one state and two gates, . Read more: 9.3 GRU (torch gate order)
- exact match: the fraction of hypotheses equal to their reference after stripping white space at both ends. Read more: 10.5 Sequence metrics: exact match, BLEU, chrF
- LayerNorm: per-row normalization by the population mean and variance with inside the square root, then an affine map. Read more: 6.5 Module system and basic layers
- safetensors: a checkpoint format of an 8-byte header length, a JSON header, and raw little-endian row-major tensor bytes. Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- beam search: keeping the best prefixes by summed log-probability, ranking all extensions together at each step. Read more: 10.4 Beam search, generic over a step function
- embedding table: the matrix whose row is the learned vector for token , shared across every window. Read more: 8.2 Bengio’s neural probabilistic language model
- Self-attention: a layer in which each position computes a weighted average of all positions’ values, weighted by query-key similarity. Read more: Chapter 2: Deep Learning
- causal mask: the rule ; each position reads itself and the past. Read more: 11.2 Masks: causal, padding, sliding window, additive
- teacher forcing: feeding the true previous target token at every training step. Read more: 10.1 Encoder-decoder with teacher forcing
- Residual stream: the running sum a Transformer block adds its attention and MLP outputs to. Read more: 13.4 Variance propagation and initialization in Mathematics
- Padding mask: a per-step boolean that freezes a finished sequence’s state and zeros its output. Read more: 9.2 LSTM (torch gate order)
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- Dropout: randomly zeroing activations during training to approximate an ensemble of subnetworks. Read more: Chapter 2: Deep Learning
- KV cache: per-layer storage of past keys and values, so each decode step processes only the new token. Read more: 21.2 The KV cache, incremental decoding, and generate
- RMSNorm: divides a vector by its root mean square plus eps (inside the root) and multiplies by a learned gain; no mean subtraction, no bias. Read more: 13.1 Pre-LN and RMSNorm
- Warmup-stable-decay (WSD): warmup, a long constant phase at the peak, and a short final decay. Read more: 16.4 Learning-rate schedules (cosine, WSD, Noam) and gradient clipping in Mathematics
This section assembles them into the 2017 architecture, trains it with the paper’s recipe, and decodes it with the beam search you already built. The task is three-digit addition, which needs exact attention to two positions per output digit and carries between them. An RNN encoder-decoder of the same size learns it slowly; the transformer reaches 0.98 exact match on unseen sums in a few thousand updates. The same build also exposes the first architectural decision every modern LLM made differently from the paper: where to put the LayerNorm. You will see post-LN fail without warmup while pre-LN trains.
Besides the three sections above, this one uses most of the foundations chapter: the Tensor, the op library, cross_entropy from Losses with a fused backward, the layers of Module system and basic layers, DataLoader and train_step from Training loop and the autograd bigram, and safetensors from Safetensors for every dtype, atomic checkpoints, and the token stream. From Mathematics it uses Adam from Adam and AdamW, noam from Learning-rate schedules and gradient clipping, normal_init from Variance propagation and initialization, and PCG32 from Modular arithmetic, hashing, and PCG32. The full solution is python/tinyllm/xfmr/transformer.py, its contract is course/contracts/py/tinyllm/xfmr/transformer.pyi, and the model oracle tests are in test_transformer.py.
Learning objectives
- Assemble encoder and decoder stacks from attention, masks, positions, and a feed-forward network, with all three kinds of attention wired correctly.
- Explain why post-LN needs warmup and pre-LN does not, and why pre-LN needs one final norm per stack.
- Compute label-smoothed loss and the Noam rate by hand, including the pad rule and the step count.
- Show that step-by-step decoding reproduces the full forward pass, and run beam search on top of it.
The notation
Section titled “The notation”| Symbol | Meaning | Type / shape |
|---|---|---|
| source states; source length | [B, S, d] | |
| target states; target length | [B, T, d] | |
| model width, heads, feed-forward width | int | |
| encoder and decoder layers | int | |
| the shared embedding table | float32[V, d] | |
| LayerNorm over the last axis | ||
| position-wise feed-forward | [.., d] -> [.., d] | |
| vocabulary size | int | |
| label smoothing | float in | |
| update index, 0 for the first update | int | |
| warmup updates | int |
The idea, from first principles
Section titled “The idea, from first principles”The task is a conditional distribution . The source is fully known, so it can be read once, in parallel, by a stack of self-attention layers that may look everywhere except padding. That stack is the encoder.
The target must be generated left to right at inference, but at training time all of is known. If position is forbidden from reading positions , one parallel pass over (bos, ) computes exactly what sequential steps would. That is the causal mask, and it is what makes teacher forcing a single forward pass.
The decoder still needs the source. A second attention whose queries come from the decoder and whose keys and values come from the encoder output does that: cross-attention, masked by source padding only, since there is no “future” in the source. Each sublayer is added to a residual stream so depth does not destroy the signal, and normalized so the scale stays fixed. Where the normalization goes turns out to be a free choice with consequences.
The stacks
Section titled “The stacks”Input. , and the same for the target with the same table (the paper shares one matrix between the two embeddings and the output layer). starts at , so has unit scale, the scale of the unit-circle pairs of , and neither swamps the other.
Encoder layer (post-LN): , then . Self-attention may read every real source position.
Decoder layer (post-LN): with the causal and target-padding mask, then where is the encoder’s output and the mask is the source padding, then .
Output. . With tie_embeddings and equal vocabularies, src_emb, tgt_emb, and out are one tensor, listed once in the state_dict.
Where the LayerNorm goes
Section titled “Where the LayerNorm goes”Post-LN computes . Every layer’s output is normalized, but the gradient that reaches early layers passes through every LN on the residual path, and at initialization the gradients of the last layers are large. With Adam at a fixed learning rate the first updates are too big and training stalls. Xiong et al. show this, and that the fix the paper used, a learning-rate warmup, is what makes post-LN trainable.
Pre-LN computes . The residual path is the identity, gradients reach every layer undamped, and no warmup is needed. Because nothing normalizes the stream any more, a pre-LN stack ends with one more LN (enc_norm, dec_norm). torch’s norm_first=True is this, and GPT and every model of the modern block use it.
The difference is easy to see. The LayerNorm demo trains a 6 + 6 layer model at a constant rate with no warmup: post-LN keeps about 91% of its first loss after 150 updates, pre-LN about 54%.
Three kinds of attention
Section titled “Three kinds of attention”The model uses multi-head attention in three roles, each with its own sources and mask:
| Sublayer | Queries | Keys and values | Mask |
|---|---|---|---|
| encoder self | source | source | source padding [B, 1, 1, S] |
| decoder self | target | target | causal [T, T] AND target padding [B, 1, 1, T] (via combine) |
| decoder cross | target | encoder output | source padding |
The caller passes padding masks only (src != pad, tgt_in != pad). The model builds these attention masks itself, so it cannot forget causality.
The training recipe
Section titled “The training recipe”Teacher forcing. A target is fed as tgt_in = (bos, ) and predicted as , every position at once. The causal mask is what makes this one forward pass equal to sequential steps.
Label smoothing. With the smoothed target is and the loss is , with gradient . It stops the model from pushing one logit to infinity. The foundations’ cross_entropy(label_smoothing=) already implements it; label_smoothed_loss adds the padding rule: a target equal to the pad id is not a prediction and counts for nothing, not even in the mean.
Noam schedule. : a linear warmup for steps, then a decay as . The paper counts steps from , so update (counting from 0) runs at . Evaluating at gives rate 0 to the first update.
Optimizer. Adam with , , and no weight decay.
Working it by hand
Section titled “Working it by hand”Two pieces of the recipe are easy to get subtly wrong, so work both. First, label smoothing with , logits , target 0, and . The softmax is , so and
The smoothed target is , and the gradient is . A second row whose target is the pad id contributes no loss, no gradient, and does not count in the mean.
Second, Noam with , , , so :
| update | rate | ||
|---|---|---|---|
| 0 | 1 | 0.03125 | |
| 3 | 4 | 0.125 (peak) | |
| 15 | 16 | 0.0625 |
The first update learns at 0.03125, and the peak falls exactly at the last warmup update.
Designing the API
Section titled “Designing the API”@dataclassclass TransformerConfig: src_vocab: int; tgt_vocab: int; d_model: int = 512; n_heads: int = 8; d_ff: int = 2048 n_enc: int = 6; n_dec: int = 6; dropout: float = 0.1; norm: Literal['post','pre'] = 'post' tie_embeddings: bool = True; max_len: int = 256; ln_eps: float = 1e-5class Transformer(Module): def encode(self, src, src_mask) -> Tensor # [B, S, d] def decode(self, tgt_in, memory, src_mask, tgt_mask) -> Tensor # logits [B, T, V] def forward(self, src, tgt_in, src_mask, tgt_mask) -> Tensor def init_state(self, src_mask) -> DecodeState def decode_step(self, y_prev, memory, state) -> tuple[Tensor, DecodeState] # last position's logitsdef label_smoothed_loss(logits, targets, eps, pad_id) -> Tensordef noam_rate(step, d_model, warmup, factor=1.0) -> floatdef fit(model, src, tgt, steps, batch, pad_id, warmup=4000, factor=1.0, smoothing=0.1, lr=None, ...) -> list[float]def translate(model, src, bos, eos, pad_id, max_len, beam_size=1, length_penalty=1.0) -> list[list[int]]def save_transformer(model, dir) / load_transformer(dir)The masks passed in are padding masks, bool [B, S] and [B, T], True = a real token. Decoding reuses decode on the growing prefix and keeps the last row. That is slow ( per sequence) but correct by construction; the KV cache adds the speed later. Beam search needs only that step function.
Building it step by step
Section titled “Building it step by step”The layers
Section titled “The layers”Both placements live in one class; the branch is the whole difference.
class EncoderLayer(Module): # ... self_attn, linear1, linear2, norm1, norm2, drop ...
def ff(self, x: Tensor) -> Tensor: return self.linear2(self.drop(F.relu(self.linear1(x))))
def forward(self, x: Tensor, mask: NDArray) -> Tensor: if self.pre: # Pre-LN: x + F(LN(x)). The residual path is the identity. h = self.norm1(x) x = x + self.drop(self.self_attn(h, h, mask)) return x + self.drop(self.ff(self.norm2(x))) # Post-LN: LN(x + F(x)). Every layer's output is normalized. x = self.norm1(x + self.drop(self.self_attn(x, x, mask))) return self.norm2(x + self.drop(self.ff(x)))The decoder layer adds the cross-attention sublayer between the two:
def forward(self, y, memory, self_mask, cross_mask): if self.pre: h = self.norm1(y) y = y + self.drop(self.self_attn(h, h, self_mask)) # Only the query side is normalized; memory is already the encoder's output. y = y + self.drop(self.cross_attn(self.norm2(y), memory, cross_mask)) return y + self.drop(self.ff(self.norm3(y))) y = self.norm1(y + self.drop(self.self_attn(y, y, self_mask))) # Queries from the decoder, keys and values from the encoder. y = self.norm2(y + self.drop(self.cross_attn(y, memory, cross_mask))) return self.norm3(y + self.drop(self.ff(y)))Construction: scaled, tied embeddings and the final norms
Section titled “Construction: scaled, tied embeddings and the final norms” # Embeddings start at std d^-1/2 so that emb * sqrt(d) has unit scale. self.src_emb = Embedding(cfg.src_vocab, d, rng=r) self.src_emb.weight.data = normal_init((cfg.src_vocab, d), d**-0.5, r) if cfg.tie_embeddings and cfg.src_vocab == cfg.tgt_vocab: self.tgt_emb = Embedding(cfg.tgt_vocab, d, rng=r) self.tgt_emb.weight = self.src_emb.weight # one table, listed once else: # ... a separate target table, same init ... self.pos = SinusoidalPE(cfg.max_len, d) self.encoder = ModuleList([EncoderLayer(cfg, r) for _ in range(cfg.n_enc)]) self.decoder = ModuleList([DecoderLayer(cfg, r) for _ in range(cfg.n_dec)]) if cfg.norm == "pre": # Pre-LN leaves the residual stream unnormalized: one last LN per stack. self.enc_norm = LayerNorm(d, cfg.ln_eps) self.dec_norm = LayerNorm(d, cfg.ln_eps) self.out = Linear(d, cfg.tgt_vocab, bias=False, rng=r) if cfg.tie_embeddings: self.out.weight = self.tgt_emb.weight # logits = y E^TAssigning the same Tensor object (not a copy) is what ties: one gradient accumulates from all three uses, and the state_dict lists it once. enc_norm and dec_norm exist only for pre-LN, so a post-LN checkpoint has exactly the paper’s parameter names.
Embedding, encode, decode
Section titled “Embedding, encode, decode” def embed(self, ids, table, offset=0): x = table(ids) * math.sqrt(self.cfg.d_model) return self.drop(self.pos(x, offset))
def encode(self, src, src_mask): # ... shape and dtype checks ... m = _key_mask(src_mask) # bool [B, S] -> [B, 1, 1, S] x = self.embed(src, self.src_emb) for layer in self.encoder: x = layer(x, m) return self.enc_norm(x) if self.cfg.norm == "pre" else x
def decode(self, tgt_in, memory, src_mask, tgt_mask): # ... shape and dtype checks ... T = tgt_in.shape[1] tm = _key_mask(tgt_mask) # Position t may read targets 0..t only: causal AND not padding. self_mask = combine(causal_mask(T), tm) cross_mask = _key_mask(src_mask) # cross-attention: source padding only y = self.embed(tgt_in, self.tgt_emb) for layer in self.decoder: y = layer(y, memory, self_mask, cross_mask) if self.cfg.norm == "pre": y = self.dec_norm(y) return self.out(y)_key_mask refuses anything but a bool [B, T] array. An int mask of 0/1 would be easy to pass by mistake and would broadcast silently with the wrong meaning.
One decoding step
Section titled “One decoding step” def init_state(self, src_mask): m = np.asarray(src_mask) return DecodeState(np.zeros((m.shape[0], 0), dtype=np.int64), m)
def decode_step(self, y_prev, memory, state): y = np.asarray(y_prev, dtype=np.int64).reshape(-1, 1) prefix = np.concatenate([state.prefix, y], axis=1) # ... batch check ... # No cache yet (L8.2 adds one): recompute the whole prefix, keep the last row. logits = self.decode( prefix, memory, state.src_mask, np.ones(prefix.shape, dtype=bool) ) return logits[:, -1, :], DecodeState(prefix, state.src_mask)Because the decoder is causal, row of decode(prefix) does not depend on anything after , so this equals the full forward pass row for row. The state is immutable (a new DecodeState each step), which is what beam search needs when it forks hypotheses.
The recipe
Section titled “The recipe”def label_smoothed_loss(logits, targets, eps, pad_id): t = np.asarray(targets).astype(np.int64) # Padding positions are not predictions: no loss, no gradient, not counted. t = np.where(t == pad_id, -100, t) return cross_entropy(logits, t, ignore_index=-100, label_smoothing=eps)
def noam_rate(step, d_model, warmup, factor=1.0): # Update number `step` (0 first) is the paper's step_num = step + 1. return factor * noam(step + 1, d_model, warmup)
def make_optimizer(model): # lr is overwritten every step by fit; 0.0 is a placeholder. return Adam(list(model.parameters()), lr=0.0, betas=(0.9, 0.98), eps=1e-9)fit sets the rate before every update and feeds shifted targets:
def loss_fn(m, b): s, t = b["src"], b["tgt"] # Teacher forcing: read bos y_1 .. y_{T-1}, predict y_1 .. y_T. logits = m(s, t[:, :-1], s != pad_id, t[:, :-1] != pad_id) return label_smoothed_loss(logits, t[:, 1:], smoothing, pad_id)
# ... while step < steps: for b in loader: # reshuffles each epoch from the seeded stream if step >= steps: break # A constant lr (for the no-warmup demo) or the Noam rate. opt.lr = lr if lr is not None else noam_rate(step, model.cfg.d_model, warmup, factor) stats = train_step(model, b, loss_fn, opt, clip) losses.append(stats["loss"]) # ... step += 1Translating
Section titled “Translating”translate switches to eval mode (no dropout), encodes each row once, and hands beam_search a step function whose state is (memory, DecodeState):
was = model.training model.eval() try: with no_grad(): for row in src: s = row[None, :] mask = s != pad_id memory = model.encode(s, mask)
def step(st, y): mem, ds = st logits, ds2 = model.decode_step(y, mem, ds) return logits.data, (mem, ds2)
hyps = beam_search(step, (memory, model.init_state(mask)), bos, eos, beam_size, max_len, length_penalty) toks = hyps[0].tokens # The answer is the tokens before eos, not eos itself. out.append(toks[:-1] if hyps[0].finished else toks) finally: model.train(was) # restore the caller's mode even on errorsave_transformer writes a config.json with tl_arch = "transformer" and the norm placement, plus model.safetensors; load_transformer rebuilds the config with dropout 0 and loads the weights. The solution’s entry point (cli_xfmr.py) wraps these as train-transformer (with --norm, --warmup, --lr for the LayerNorm comparison) and translate --beam. The full solution is transformer.py.
Why this design
Section titled “Why this design”One training forward costs per sequence, all positions in parallel. Decoding by recomputing the prefix costs decoder passes in total; a KV cache makes it . Correctness comes first, then speed: the cache must reproduce exactly these logits.
The masks are built inside the model rather than passed in, which removes one way to train a non-causal decoder that looks perfect in training. One class with a pre flag, rather than two classes, keeps the same parameter names (plus enc_norm, dec_norm) and the same code path, so torch’s norm_first is matched by flipping one flag. Using ignore_index for pads, rather than masking the loss after the fact, takes the mean over real targets only, so a batch with short targets is not diluted and the smoothing mean never includes pad positions.
One tradeoff is accepted: post-LN is the default because it is the paper’s model, even though it needs warmup. Pre-LN is one flag away, and everything from GPT onward uses it.
What can go wrong
Section titled “What can go wrong”A correct implementation must reproduce both hand examples: the label-smoothed loss of 0.618111 with its gradient and the ignored pad row, and the Noam rates 0.03125, 0.125, and 0.0625, so the first update actually learns. Against torch, post-LN and pre-LN models with copied weights must match on logits, smoothed loss, and every gradient.
The properties follow from the design. Changing targets after must leave logits bitwise equal (the decoder is causal, so teacher forcing is honest). Ids in padded source positions must change nothing, so batches of different lengths behave. A different source must change the logits, proving cross-attention is wired to the encoder. Step-by-step decoding must equal the full forward pass, so beam search scores the right logits.
The structural checks: embeddings are with one tied table listed once (the paper’s section 3.4); pre-LN adds exactly enc_norm and dec_norm, and decoder layers have cross_attn and norm3, which the model zoo checkpoints depend on; and Adam uses betas (0.9, 0.98) and eps 1e-9. fit must take the exact step count, give the same losses for the same seed, and honour a constant lr (needed for the post-LN vs pre-LN comparison). Greedy translate with beam 1 must equal a hand argmax loop, drop eos, restore the caller’s mode, and survive save/load. An unknown norm, non-bool masks, and a batch mismatch must raise. Finally, it must learn: 600 updates on two-digit reversed addition reach a reference held-out loss.
The model suite test_transformer.py rewrites the forward pass in float64 numpy from the state_dict (one layer, padding on both sides, both norm orders) and compares against it.
The pitfalls, several of which produce a model that trains happily and is still wrong:
| Pitfall | Symptom | How the solution avoids it |
|---|---|---|
| post-LN written as pre-LN (normalizing the sublayer’s input) | a different model from the paper’s; torch disagrees | the post branch is norm(x + sublayer(x)) |
| pre-LN without the final LayerNorm | the stack’s output is unnormalized; logits blow up with depth | enc_norm / dec_norm created and applied when norm == "pre" |
| no causal mask in the decoder | training loss near zero, generation garbage | decode always builds combine(causal_mask(T), tm) itself |
| cross-attention reading the decoder instead of the encoder | trains as a language model of the targets, never learns the task | cross_attn(y, memory, ...): keys and values are memory |
| embeddings not multiplied by | positions swamp the tokens at init | embed multiplies by math.sqrt(d_model) before adding PE |
| source padding readable in cross-attention | outputs depend on how long the batch’s other sentences are | cross_mask = _key_mask(src_mask) in every decoder layer |
| label smoothing counting pad targets | short targets in a batch pull every logit toward the pad id | pads mapped to ignore_index = -100 before cross_entropy |
| Noam at | the first update has rate 0, the peak is one late | noam(step + 1, ...) |
| the output layer not tied | twice the parameters; the zoo’s keys differ | self.out.weight = self.tgt_emb.weight |
decode_step returning the first position | beam search scores the wrong token | returns logits[:, -1, :] |
translate keeping eos | exact match is 0 | toks[:-1] if hyps[0].finished |
Warning: a missing causal mask is the most expensive bug on this list, because it looks like success. Training loss drops to nearly zero, and only generation reveals that the model learned to copy.
Where this leads
Section titled “Where this leads”The next chapter splits this model in two. GPT keeps only the decoder, in pre-LN; BERT keeps only the encoder, in post-LN. The model zoo loads tl_arch = transformer checkpoints and reports exact match with beam search on the addition task.
The production equivalents show where each piece goes next:
| Solution piece | Production equivalent | What it adds | Where to look |
|---|---|---|---|
Transformer | torch nn.Transformer, fairseq TransformerModel | fused attention, norm_first, incremental state caching | torch/nn/modules/transformer.py; fairseq/models/transformer/ |
| post-LN vs pre-LN | DeepNorm, Sandwich-LN, RMSNorm pre-norm | train 1000-layer post-LN; modern LLMs use pre-RMSNorm (see Pre-LN and RMSNorm) | Wang et al., “DeepNet” (2022) |
noam_rate | linear warmup + cosine, WSD | the schedules LLM training uses (see Learning-rate schedules and the capstone) | HF get_cosine_schedule_with_warmup |
decode_step recomputing the prefix | the KV cache | instead of work per token | KV cache and generate; HF past_key_values |
Further reading
Section titled “Further reading”- Vaswani et al., “Attention Is All You Need” (2017), sections 3 and 5.
- Xiong et al., “On Layer Normalization in the Transformer Architecture” (ICML 2020).
- Szegedy et al., “Rethinking the Inception Architecture” (2016), section 7 (label smoothing).
- Rush, The Annotated Transformer.
Key terms
Section titled “Key terms”- encoder: a stack of self-attention and feed-forward layers that reads the whole source once.
- decoder: a stack with masked self-attention, cross-attention into the encoder output, and a feed-forward network.
- cross-attention: attention with queries from the decoder and keys and values from the encoder output.
- post-LN: , the paper’s placement; needs warmup.
- pre-LN: with one final LN per stack; trains without warmup.
- label smoothing: training against instead of a one-hot target.
- Noam schedule: , linear warmup then inverse-square-root decay.
Summary
Section titled “Summary”- The encoder reads the whole source once; the decoder writes one token at a time with three sublayers: masked self-attention, cross-attention into the encoder’s output, and a feed-forward network.
- Two masks keep it honest: the decoder may not read the future, and nobody may read padding.
- Where the LayerNorm sits decides trainability: post-LN (the paper) normalizes after the residual sum and needs warmup; pre-LN normalizes the sublayer’s input, needs one final norm per stack, and trains without warmup.
- The paper’s recipe is three numbers you can check by hand: embeddings times , the Noam rate with update at step , and label smoothing that moves of the target mass onto every id.
- Decoding one token at a time must give the logits of the full forward pass; beam search runs on top of that step.
Review questions
Section titled “Review questions”- Why does pre-LN need
enc_normanddec_normbut post-LN does not?Answer
In post-LN the last operation of every layer is a LayerNorm, so the stack's output is already normalized. In pre-LN each layer adds an unnormalized sublayer output to the stream, so after $N$ layers the stream has grown; one final LN brings it back to the scale the output projection (and, for the encoder, cross-attention) expects. - Why must
noam_rateevaluate the schedule atstep + 1?Answer
The Noam formula has a factor $n \cdot w^{-3/2}$ in warmup, which is 0 at $n = 0$. Counting updates from 0, evaluating at $n = s$ would waste the first update and shift the peak one update late. - Why does cross-attention use only the source padding mask and no causal mask?
Answer
The source is entirely known before decoding starts, so there is no future to hide. The only illegal keys are padding positions, which are not real tokens. - Why can
decode_stepreturnlogits[:, -1, :]of a fulldecodecall and still match the full forward pass at every position?Answer
The causal mask makes row $t$ a function of rows $0..t$ only. Recomputing the prefix of length $t + 1$ gives exactly row $t$ of the full pass; later tokens cannot change it. - What would go wrong if the embeddings were not multiplied by ?
Answer
The table starts at std $d^{-1/2}$, so each token vector has norm about 1 while each PE vector has norm $\sqrt{d/2}$. Without the scale, position dominates the input at init and the model has to learn to amplify token identity first.