DL0141 Visual Token Compression for OCR

How do visual token compression techniques reduce the sequence length of visual inputs without degrading fine-grained OCR performance?

Answer

The techniques that survive contact with documents all compress along the channel axis rather than the token axis: a pixel unshuffle (InternVL) or a strided convolutional reducer (DocOwl 1.5) folds each s \times s block of patch embeddings into one vector of width s^2 d and projects it back to d, so four patches become one token while the patch-to-region mapping stays bijective and no pixel is discarded. The second half of the recipe is that the token budget must keep scaling with input resolution: dynamic tiling and native-resolution patching (Qwen2-VL) give a 1275×1650 scan roughly 2,600 tokens of 28×28 pixels each, whereas a fixed-K resampler hands the same page 64 tokens no matter how many glyphs it contains. What actually kills OCR is rarely the compressor itself but the resize step in front of it: squeezing a page into 336×336 makes an 11 pt glyph thinner than one 14-pixel patch, and no downstream module can recover strokes the encoder never sampled. The useful mental model is glyph density: keep the number of glyphs covered by a single visual token near one, and compression is nearly free; push it toward ten and character-level accuracy falls off a cliff while scene-level captioning barely moves.

(1) Compress Channels, Not Positions: pixel unshuffle and conv reducers move information into the feature dimension, so a 4\times length reduction still lets every output token point at a known rectangle of the page.
(2) Keep The Budget Resolution-Dependent: a compression ratio is safe, a compression target is not; dynamic tiling plus native-resolution patching lets a dense page buy more tokens than a photo of a beach.
(3) Respect Glyph Nyquist: the binding constraint is stroke width versus patch size in the resized image, which is why 336-pixel inputs cap document accuracy regardless of the connector.
(4) Fixed-Query Resamplers Lose The Wrong Thing First: K learned queries cross-attend to all patches without a positional index, so reading order and rare characters degrade before object-level semantics do.
(5) Two-Scale Views Are Cheap: a global thumbnail supplies layout while local tiles supply glyphs, which is why AnyRes-style designs use 4 \times 576 + 576 = 2880 tokens rather than one giant grid.
(6) Prune Late And Query-Aware: dropping half the visual tokens after LLM layer 2 (FastV) saves about 45% of prefill FLOPs on scene VQA but deletes whole text lines when the question has not yet been attended to.

Three-row diagram comparing visual token compression families on the same 8 by 4 patch grid: the top row folds each 2 by 2 block of patches into one token on the channel axis and produces a 4 by 2 output grid with matching tints, the middle row sends all patches through cross-attention into four fixed learned query tokens with no positional index, and the bottom row keeps the original grid but marks half the cells as dropped by an attention score

Figure 1: Same patch grid, three compression axes. Only the top row keeps an exact mapping from output token back to page rectangle, which is what OCR decoding depends on; the middle row replaces that mapping with K content-addressed slots, and the bottom row keeps positions but deletes evidence.

Why the distinction matters becomes obvious once you count information. Natural images are locally redundant, so averaging neighbouring patches costs almost nothing; a page of text is close to the opposite, since each glyph is a high-entropy symbol whose identity cannot be inferred from its neighbours and whose position carries the reading order. A learned resampler is a query-agnostic bottleneck: it must decide what to keep before the question arrives, and a fixed 64-slot budget forces it to summarise, which is exactly the wrong operation for text. Structured merging instead makes a bounded, uniform trade, and empirically the boundary sits near one glyph per token. The design lineage of production VLMs follows that logic directly: Q-Former resamplers → pixel unshuffle with dynamic tiles → native dynamic resolution with a 2×2 patch merger, each step trading a smaller guaranteed budget for a budget that grows with how much text is actually on the page.

Mathematical Formulation:
N_p = \dfrac{HW}{p^2}
N_v = \dfrac{N_p}{s^2} = \dfrac{HW}{p^2 s^2}
A = \dfrac{H_0 W_0}{N_v}
c = \dfrac{G}{N_v}
\mathrm{prefill} = O((N_v + N_t)^2 d)

Where:

  • N_p is the patch count the vision encoder produces from a resized input of size H \times W with patch size p, and N_v is the number of tokens actually handed to the language model.
  • s is the spatial merge factor; pixel unshuffle with s=2 concatenates 4 patch embeddings into width 4d and projects back to d, so length drops 4\times with no averaging.
  • H_0 \times W_0 is the original page resolution and A the original pixels covered by one visual token, which is the honest measure of compression because the resize is itself a compressor.
  • G is the glyph count on the page and c the glyphs per visual token; c \approx 1 is the practical safety line for character-accurate reading.
  • N_t is the text prompt length and d the model width, so prefill is quadratic in the combined sequence while the KV cache grows linearly.

Worked Example, One Dense A4 Page At 150 DPI:
N_v = 4 \times 576 + 576 = 2880
c = 3200 / 2880 \approx 1.1
c = 3200 / 576 \approx 5.6
(2880 / 576)^2 = 25

Assuming about 3,200 glyphs on the page, an AnyRes layout of four 336-pixel tiles plus a thumbnail lands at roughly one glyph per token, while a single 336-pixel view lands at 5.6 and reads only headlines. The last line is the bill: those extra tokens cost 25 times the attention work in prefill, which is precisely why the compressor exists and why the interesting engineering is choosing the smallest N_v that still keeps c near 1 for the document class you serve.

Log-log line chart of glyphs covered per visual token versus visual tokens per page for a dense page of about 3200 glyphs, with a shaded horizontal band between 0.5 and 2 glyphs per token marked as the reliable OCR region, and annotated markers at 64 tokens for a fixed-query resampler, 256 tokens for an OCR-specialised encoder, 576 tokens for a single 336 pixel view, 1792 tokens for dynamic tiling with a thumbnail, and 2880 tokens for an AnyRes layout

Figure 2: Compression is only meaningful relative to glyph density. Configurations inside the band give each visual token roughly one character and read reliably; a 64-slot resampler asks one token to encode about 50 glyphs. Systems trained specifically for optical text compression can operate right of the band, but at a measured precision cost, and below the band extra tokens buy nothing.

PropertySpatial channel mergeFixed-query resamplerIn-LLM pruning or merging
MechanismPixel unshuffle or strided conv over the patch grid, then a linear projectionCross-attention from K learned queries into all patch embeddingsRank tokens by attention received in an early layer, drop or merge the tail
Budget vs resolutionGrows linearly with pixels, fixed ratio of 4x or 16xConstant at K (typically 32 to 256) whatever the input sizeGrows with pixels, then cut by a fixed keep-rate
Spatial index keptYes, one token maps to one known rectangleNo, slots are content-addressed and order must be relearnedYes for survivors, but dropped regions leave holes
Training costOne small projection, trained with the connectorA full extra transformer stage plus alignment pretrainingUsually training-free, applied at inference
Dominant failureLong context and quadratic prefill on multi-page inputsReading order and rare glyphs collapse on dense pagesWhole text lines vanish when the query is not yet visible to the scorer

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *