Intelligent Document Processing models such as LayoutLM fuse three signals per text token: its word embedding, its 2D bounding-box position on the page, and (optionally) a visual patch. Self-attention then decides which tokens "belong together" — e.g. linking a form label to its value — using logits that mix content and spatial position:
logit(i,j) = (1-β)·cos(e_i, e_j)·k_c + β·exp(-d(i,j)²/2σ_p²)·k_s
a(i,j) = softmax_j( logit(i,j) / T )
H(i) = -Σ_j a(i,j)·log₂ a(i,j) (attention entropy, bits)
Here e_i is each block's content embedding (degraded by OCR noise σ before the cosine term), d(i,j) is Euclidean distance on the page, and T is the softmax temperature. Each label block's predicted partner is argmax_j a(i,j); it counts as a correct extraction only if that partner is its true value block. This 2D build uses the identical attention math as the 3D version — verified numerically (softmax rows sum to 1, entropy bounded by log2(N)) — laid flat on the page instead of extruded into a 3D scene, plus a live N×N attention-weight heatmap panel that the 3D view has no room for.
- β — 0 = pure content matching (ignores where things sit on the page); 1 = pure spatial proximity (ignores what the text says). Real IDP models sit in between, which is exactly why 2D position embeddings matter.
- T — low T sharpens attention onto the single best match; high T spreads it thin, raising entropy.
- σ (OCR noise) — simulates a noisy OCR pass corrupting recognized text, degrading content similarity and forcing the model to lean on layout instead.
- τ — the confidence floor below which an attention link is not drawn or counted as an extracted field.
- Shuffle Layout — re-randomizes block positions while keeping true label→value pairs fixed, showing how a purely spatial model breaks when the physical layout doesn't match reading order.
- Heatmap mode — toggle between row-normalized attention a(i,j) (each row is a softmax, sums to 1) and raw pre-softmax logits, to see how temperature reshapes a distribution that already existed in logit space.
Drag the page canvas to pan your view, and scroll/pinch to zoom — useful once you shuffle the layout and blocks spread toward the page edges.
Real-world relevance: this is the core mechanism behind invoice/receipt/ID-card field extraction pipelines (LayoutLMv3, Donut, Azure Form Recognizer) that combine OCR with layout-aware transformers instead of just concatenating recognized text.