An encoder block
You will learn
What one transformer encoder block does to its input, and what moving the norms in front changes.
A transformer layer is two sublayers, each wrapped in a residual add and a layer norm: self-attention mixes the positions, then a feed-forward net of width 8 works on each position alone. In the post-norm order of the original paper the block ends with a norm, so every output row has mean 0 and variance 1.000. Pre-norm puts the norms in front of each sublayer and ends on a residual add, so its rows come out at variances 2.73, 3.33 and 0.96; deep stacks train more easily that way, with one norm added at the very top. The spec encoder_block.t27 runs one block on 3 positions, d_model 4 and 2 heads.