LayoutLM

Also known as: LayoutLMv3

On this page 7

LayoutLM is a family of pre-trained Transformer models from Microsoft Research that understand documents by combining the text of each word with its position on the page and, in later versions, with the page image itself. In Document AI, it became the reference architecture for layout-aware language modeling and is widely used for form extraction, key information extraction, document classification and document question answering. The latest widely used version is LayoutLMv3.

Ordinary language models read text as a flat sequence and lose the visual structure that gives a form its meaning. LayoutLM restores that structure by encoding two-dimensional coordinates for every token, so the model learns that a value printed to the right of a label usually belongs to that label.

How LayoutLM encodes text and layout

LayoutLM encodes a document as a sequence of OCR tokens enriched with spatial information. An OCR engine first extracts words and their bounding boxes; the model then adds embeddings for the box coordinates to the usual word and position embeddings of a BERT-style encoder.

  • Text embedding: the word piece itself, as in BERT.
  • One-dimensional position: the index of the token in reading order.
  • Two-dimensional position: the x and y coordinates of the box corners, normalized to a 0 to 1,000 grid, plus width and height.
  • Visual features: in the first version added only at fine-tuning time, in later versions learned jointly during pre-training.

Pre-training uses large collections of scanned documents, notably the IIT-CDIP collection of about 11 million pages. The central objective is a masked visual-language model: some words are hidden, but their layout positions remain, so the model must predict the missing text from both the surrounding words and their spatial arrangement.

LayoutLM versions and their differences

The LayoutLM family evolved through three main generations, each integrating the page image more deeply. The table below compares their architecture and pre-training objectives.

VersionYearVisual inputKey pre-training objectives
LayoutLM2020Optional image features at fine-tuningMasked visual-language model, document classification
LayoutLMv22021CNN backbone producing visual tokensMasked modeling, text-image alignment, text-image matching
LayoutXLM2021Same as LayoutLMv2Multilingual pre-training across many languages
LayoutLMv32022Linear image patch embeddings, no CNNUnified text and image masking, word-patch alignment
Main LayoutLM versions

LayoutLMv3 simplified the design by replacing the convolutional backbone with image patches similar to a Vision Transformer. This made the model lighter and allowed it to perform well on both text-centric tasks, such as form understanding, and image-centric tasks, such as document layout analysis. LayoutLMv2 also introduced spatial-aware self-attention, which lets attention weights depend on the relative positions of tokens.

Tasks and benchmarks

LayoutLM models are fine-tuned for specific document tasks by adding a small task head on top of the encoder. Results are usually reported on a standard set of public benchmarks, which the following table maps to their tasks.

TaskTypical outputCommon benchmark
Form understandingEntity labels and links between fieldsFUNSD
Receipt understandingStore, date, items and totalsSROIE, CORD
Document image classificationDocument type labelRVL-CDIP
Document question answeringAnswer span from the pageDocVQA
Document layout analysisRegions such as titles, tables and figuresPubLayNet
Typical LayoutLM tasks and benchmarks

For extraction tasks, the most common setup is token classification with BIO labels, where each token is tagged as the beginning, inside or outside of a field. Downstream code then groups tagged tokens into fields such as invoice number or total amount.

For relation extraction, a second head predicts links between entities, for example connecting a question field with its answer field on a form. Document question answering instead uses an extractive span head that predicts the start and end tokens of the answer, in the same way as reading comprehension models for plain text. Because all heads share one encoder, a single pre-trained checkpoint can serve several tasks after separate fine-tuning runs.

LayoutLM compared with other document models

LayoutLM belongs to the OCR-based family of document models, which depend on an external text recognizer. Newer approaches remove OCR entirely or use general multimodal language models, and each design involves clear trade-offs.

ApproachExample modelsStrengthWeakness
Layout-aware encoderLayoutLM familyPrecise token-level extraction, modest computeDepends on OCR quality
OCR-free understandingDonut, Pix2StructNo OCR errors or OCR costWeaker on dense small text
LLM-based extractionMultimodal language modelsFlexible prompts, little labeled dataHigher cost, less predictable output
Template-based extractionZonal OCR rulesSimple for fixed layoutsBreaks on new layouts
LayoutLM and alternative approaches

In production, LayoutLM remains attractive when an organization has labeled data for a stable set of fields and needs fast, inexpensive and auditable extraction. Generative models are often preferred for open-ended questions or highly varied documents.

Limitations and practical considerations

The main practical limitation of LayoutLM is its dependence on upstream OCR. Recognition errors, wrong bounding boxes or a poor reading order propagate directly into the model and cannot be corrected by fine-tuning alone.

  • Sequence length: the standard models accept 512 tokens, so dense multi-page documents must be split into windows.
  • Licensing: the original LayoutLM uses a permissive license, while LayoutLMv2 and LayoutLMv3 weights are licensed for non-commercial use, which affects commercial deployment.
  • Language coverage: the English models need LayoutXLM or other multilingual variants for non-Latin scripts.
  • Labeled data: fine-tuning usually requires several hundred annotated pages per document type for reliable results.
  • Coordinate scaling: boxes must be normalized to the 0 to 1,000 grid; forgetting this step silently degrades accuracy.

A plain-language summary

When people read a form, they use more than words. They notice where each word sits. A number next to the word Total is probably the total. A name under the word Customer is probably the customer.

LayoutLM teaches a computer to read the same way. It looks at each word and also at its place on the page. Later versions also look at the picture of the page.

This makes the model good at pulling out facts such as names, dates and amounts. The key idea is simple: position on the page carries meaning.

An example from practice

A logistics company receives about 20,000 delivery notes per month from dozens of carriers, each with its own layout. Its earlier system used fixed templates and failed whenever a carrier changed its form. The team annotated 600 scanned notes with twelve fields, including shipment number, consignee, weight and delivery date.

Engineers ran each page through an OCR engine, normalized the word boxes and fine-tuned a LayoutLM base model for token classification. Fields with low confidence scores were routed to human reviewers. After deployment, the model extracted the twelve fields with field-level accuracy above 95 percent on held-out carriers, and manual data entry fell by roughly three quarters. New carrier layouts no longer required new templates, only occasional additional training examples.