Post-Norm Transformer Sublayers #
This file packages the LayerNorm theorem at the interface used by post-norm Transformer blocks.
The preceding files prove the smooth residual components:
x ↦ x + MultiHeadSelfAttention(x);x ↦ x + W₂ GELU(W₁x + b₁) + b₂.
This module proves the LayerNorm composition:
residual_stream ↦ LayerNorm(residual_stream, gamma, beta).
That is the exact post-norm sublayer shape used by classical Transformer encoder blocks
(LayerNorm(x + Sublayer(x))). Attention and feed-forward sublayers reach the same boundary, so
postNorm_backpropVec_eq_adjoint_fderiv_at serves both of them. The single-graph theorems
mhaPostNorm_backpropVec_eq_adjoint_fderiv_at and seqFfnPostNorm_backpropVec_eq_adjoint_fderiv_at
additionally append LayerNorm to each residual prefix in one SSA graph. The attention prefix
uses DirectReshapeAttention, not the executable head layout. These are exact-real graph results,
not correspondence proofs for a runtime Transformer or fused kernels.
We deliberately keep this proof factored at the residual-stream interface. It avoids treating
LayerNorm's pointwise domain hypotheses as globally smooth, and it gives full-block proofs a clean
seam: compose a globally smooth residual graph with this pointwise post-norm graph, threading the
unused LayerNorm parameters through the residual prefix with DGraph.weakenContext.
References:
- Vaswani et al., "Attention Is All You Need", NeurIPS 2017.
- PyTorch
torch.nn.TransformerEncoderLayer. https://pytorch.org/docs/stable/generated/torch.nn.TransformerEncoderLayer.html - PyTorch
torch.nn.LayerNorm. https://pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html
A post-norm Transformer stage normalizes a sequence-shaped residual stream.
Instances For
Saved tensors for the LayerNorm part of a post-norm Transformer stage.
Instances For
MHA context extended with the affine parameters for the following LayerNorm.
Instances For
Residual-MHA plus one whole-node post-norm output.
Instances For
Gamma parameter for the post-norm LayerNorm, weakened through any saved tensors.
Instances For
Beta parameter for the post-norm LayerNorm, weakened through any saved tensors.
Instances For
The residual stream x + MHA(x) after the residual-attention prefix graph.
Instances For
LayerNorm input triple after the residual-MHA prefix has run.
Instances For
Residual-MHA graph while carrying the following LayerNorm's affine parameters.
The attention result does not depend on the carried gamma/beta. DGraph.weakenContext
preserves them in the full context so the appended LayerNorm can read them.
Instances For
Single SSA graph for the first post-norm Transformer encoder sublayer:
LayerNorm(x + MultiHeadSelfAttention(x), gamma, beta).
LayerNorm is appended as a whole pointwise node backed by the detailed LayerNorm graph theorem. The attention prefix retains its direct-reshape head split.
Instances For
Pointwise correctness for the single-graph residual-MHA post-norm sublayer.
Instances For
End-to-end VJP theorem for the single-graph residual-MHA post-norm sublayer.
Sequence feed-forward plus post-norm #
This is the second sublayer shape in a post-norm Transformer encoder block:
LayerNorm(X + FFN(X), gamma, beta).
The FFN affine maps and biases are supplied over flattened sequence tensors and remain fixed during differentiation. A shared position-wise implementation can provide block-diagonal maps; runtime or fused-kernel correspondence is a separate obligation.
Sequence-FFN context extended with the affine parameters for the following LayerNorm.
Instances For
Sequence-FFN residual plus one whole-node post-norm output.
Instances For
Gamma parameter for the FFN post-norm LayerNorm.
Instances For
Beta parameter for the FFN post-norm LayerNorm.
Instances For
The residual stream X + FFN(X) after the sequence-FFN prefix graph.
Instances For
LayerNorm input triple after the residual-FFN prefix has run.
Instances For
Sequence-FFN graph while carrying the following LayerNorm's affine parameters.
Instances For
Single SSA graph for the second post-norm Transformer encoder sublayer:
LayerNorm(X + FFN(X), gamma, beta).
Instances For
Pointwise correctness for the single-graph residual-FFN post-norm sublayer.
Instances For
End-to-end VJP theorem for the single-graph residual-FFN post-norm sublayer.
The post-norm graph itself.
The context is [residual_stream, gamma, beta]. For attention this residual stream is
x + MHA(x); for the feed-forward half it is x + FFN(x). The residual computation is proved in
ResidualAttention/FeedForward; this graph proves the LayerNorm boundary that follows it.
Instances For
Pointwise correctness for the post-norm Transformer boundary.
The only hypothesis is 0 < ε. At the evaluation point, it implies LayerNorm's differentiability
conditions: positive variance-plus-epsilon and nonzero standard deviation.
Instances For
VJP theorem for the post-norm Transformer boundary.
This is the model-level theorem used after either residual attention or a residual feed-forward block has produced its sequence-shaped residual stream.
Calculus bridge for a residual block followed by post-norm LayerNorm.
This is the theorem we use to move from separately proved pieces to a whole Transformer sublayer. Suppose some residual-producing map
residualPack : E → [residual_stream, gamma, beta]
is differentiable at x. It may come from residual attention, residual feed-forward, or any future
block that produces the same LayerNorm context. If 0 < ε, then the composed post-norm map
x ↦ LayerNorm(residualPack x)
is differentiable, with derivative given by the usual chain rule.
This is more general than MHA: the same theorem covers Transformer, ViT, GPT-style blocks, and future residual modules once they expose the residual stream plus affine LayerNorm parameters.
Calculus bridge for a full two-sublayer post-norm Transformer encoder block.
This theorem is deliberately stated at the map level rather than for one concrete SSA graph. A post-norm encoder block has two domain-sensitive LayerNorms:
attnPackbuilds[x + MHA(x), gamma₁, beta₁];- after the first LayerNorm evaluation,
ffnPackbuilds[norm₁ + FFN(norm₁), gamma₂, beta₂]; - the second LayerNorm produces the block output.
The theorem says that if the two residual-pack maps are differentiable and both LayerNorm epsilons are positive, then the whole two-sublayer block is differentiable by ordinary Fréchet chain rule.
The concrete graph-level VJP theorems for each sublayer are mhaPostNorm_* and seqFfnPostNorm_*.
This bridge composes the supplied maps; it does not construct their attention or FFN semantics.