Position and encoder decoder architecture
Quick answer
Transformer blocks use attention, position information, feed-forward layers, residual paths and normalization. Encoders and decoders differ in how they access sequence context. Examples are static Python snippets tested locally on CPU; PyTorch is required for selected examples. ByteMind execution is unverified.
Positionless encoder symmetry
import torchtorch.manual_seed(4)m=torch.nn.TransformerEncoderLayer(4,2,8,dropout=0.,batch_first=True).eval()x=torch.tensor([[[1.,2.,3.,4.],[4.,1.,2.,3.],[3.,4.,1.,2.]]])with torch.no_grad(): a=m(x).mean(1);b=m(x.flip(1)).mean(1)print(bool(torch.allclose(a,b,atol=1e-6)))Expected output (locally tested): True. No positional information or causal mask; pooling erases the permutation.
How it works
Without position information or an order-dependent mask, self-attention and tokenwise feed-forward layers are permutation equivariant: permuting valid tokens permutes their outputs. Mean pooling then makes an encoder insensitive to token order. Learned absolute positions add a trainable vector per position (bounded by the table); sinusoidal encodings use sin(pos/10000^(2i/d)) and cos(pos/10000^(2i/d)) in paired dimensions for even d. Relative position biases and rotary methods instead change attention scores or projected queries/keys; they are not identical to absolute addition. A typical encoder block mixes source tokens bidirectionally, then applies a positionwise MLP. Residual additions preserve model width. Post-norm computes LayerNorm(x+sublayer(x)); pre-norm computes x+sublayer(LayerNorm(x)). The original Transformer used post-norm; modern variants often differ. LayerNorm operates over feature coordinates for each token, not across the batch. A decoder-only model uses causal self-attention for next-token prediction; an encoder-decoder decoder also cross-attends to encoder memory. Training shifts target inputs; future target tokens must not be visible through masks or features. Padding keys and invalid query loss positions are treated separately. Positionless causal decoders are not covered by the permutation argument because their mask supplies order information.
Sinusoidal positions
import mathd=4p=[[f(t/10000**(2*i/d)) for i in range(d//2) for f in (math.sin,math.cos)] for t in (0,1)]print([[round(v,4) for v in row] for row in p])Expected output (locally tested): [[0.0, 1.0, 0.0, 1.0], [0.8415, 0.5403, 0.01, 1.0]]. Paired frequencies encode position with no learned table.
Common mistakes
Do not claim all Transformers use the same normalization ordering, assume a positionless bidirectional pooled model distinguishes permutations, or give decoder training access to the current prediction target.
Practice and next steps
Compare two reversed token sequences with a positionless encoder and masked mean pooling. Add explicit position embeddings and explain which symmetry is broken. Draw encoder memory feeding decoder cross-attention.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Position and encoder decoder architecture — knowledge check
Apply position and encoder decoder architecture to small examples and debugging scenarios.
Why add positional information to a bidirectional encoder?
What is pre-norm residual form?
Where do decoder cross-attention keys and values come from?
Why shift targets for decoder inputs?
Which positions should contribute to token loss?