Context Compression

DeepSeek-OCR

Contexts Optical Compression: How to squeeze 6,000+ text tokens into just 100-800 visual tokens while maintaining 97% accuracy. The key to practical, production-scale document AI.

This paper presents DeepEncoder—a vision encoder that combines SAM's local perception with CLIP's global understanding, connected by a 16× convolutional compressor. The result: OCR at 200,000+ pages per day on a single A100 GPU.

DeepSeek AIOctober 2025arXiv:2510.1823497% @ 10× compression200k pages/day
The Core Question
How Much Can You Compress Visual Context?
A typical document page might produce 6,000+ text tokens. Traditional OCR pipelines pass this directly to LLMs. But what if you could encode the visual information into just 100-800 tokens that the LLM can decode? This paper proves it's possible—with 97% accuracy.

Imagine you have a 50-page PDF document. You want an LLM to answer questions about it. What are your options?

Option 1: Raw Text Extraction

  • • OCR each page → text
  • • Concatenate all text
  • • Feed to LLM
Token count for 50 pages:
~300,000 tokens

Exceeds most LLM context windows. Even if it fits, costs ~$6 per query with GPT-4.

Option 2: Visual Compression

  • • Encode each page as image
  • • Compress to visual tokens
  • • LLM decodes visual tokens
Token count for 50 pages:
~15,000 tokens

20× compression. Fits in context. Costs ~$0.30 per query. And it works!

The Key Insight

Text has incredible redundancy when represented as characters/tokens. The word "information" is 11 characters, but visually it's a single recognizable unit. A well-trained visual encoder can capture this semantic density directly from pixels.

Text tokens
information
11+ tokens
Visual patch
[word image]
~0.25 tokens
Compression
44×
per word

Of course, compression has limits. If you compress too aggressively, information is lost. The question is: what's the optimal compression ratio? This paper provides the first systematic study of this tradeoff.

Before diving into architecture, let's understand the real-world impact. Document AI is a massive market with specific operational requirements.

Production Requirements

Throughput

Enterprises process millions of documents daily. A legal firm might ingest 100,000 contracts per month.

Cost

At $0.03/1K tokens, processing 100K pages with raw text costs ~$180K/month. With compression, it's ~$9K.

Latency

Users expect real-time responses. 300K token context = 30+ second inference. 15K tokens = 3 seconds.

Accuracy

Financial documents require 99%+ accuracy. Medical records even higher. Compression must preserve critical information.

DeepSeek-OCR Production Numbers

200K+
Pages/day (single A100)
33M
Pages/day (20-node cluster)
97%
Accuracy @ 10× compression
$0.002
Per page (compute only)

The Business Case

At 33M pages/day with 20 nodes, the cost per page is approximately $0.002 in compute. Traditional OCR pipelines cost 10-100× more when you factor in the LLM inference costs for the extracted text. This makes previously impossible use cases economically viable.

DeepEncoder is the heart of the system—a 380M parameter vision encoder that achieves extreme compression while preserving semantic content.

Architecture Overview

Stage 1: SAM-base80M params

Window attention for local perception. Processes high-resolution patches without quadratic memory cost.

Input
1024×1024
Patches
64×64 = 4,096
Attention
Window (7×7)
↓ 4,096 visual tokens
Stage 2: Convolutional Compressor16× compression

Two conv layers with stride 2 reduce spatial dimensions by 4× in each dimension, achieving 16× total token reduction.

Input tokens
4,096
Output tokens
256
Compression
16×
↓ 256 visual tokens
Stage 3: CLIP-large300M params

Global attention for semantic knowledge. Can now afford dense attention because tokens are compressed.

Input tokens
256
Attention
Full global
Output
256 + semantics
~385M
Total Encoder Params
16×
Token Compression
97%+
Accuracy Retained

Why Two Encoders?

SAM excels at low-level visual features (edges, shapes, spatial structure) while CLIP excels at semantic understanding (what objects mean, text-image alignment). The compressor sits between them, learning which SAM features matter for CLIP's semantic task. This two-stage design captures both local detail and global meaning.

Why This Design?

The key insight is that local and global processing have different computational needs:

  • Local (SAM): Processing 4,096 tokens with window attention is O(4096 × 49) = O(200K) operations per layer.
  • Global (CLIP): Processing 256 tokens with full attention is O(256²) = O(65K) operations per layer.
  • Combined: This hybrid is 10× more efficient than full attention on 4,096 tokens, which would be O(16M) operations.

The choice of SAM and CLIP isn't arbitrary—each brings specific strengths that complement the other perfectly.

SAM (Segment Anything Model)

Facebook's SAM was trained to segment any object in any image. This gives it:

  • Fine-grained perception: Can distinguish individual letters, table cells, diagram elements
  • Edge detection: Knows where text ends and background begins
  • Window attention: Efficient at high resolution without memory explosion
Best at:
Local structure, boundaries, small details

CLIP (Contrastive Language-Image)

OpenAI's CLIP was trained on 400M image-text pairs. This gives it:

  • Semantic understanding: Knows what text means, not just what it looks like
  • Layout comprehension: Understands document structure (headers, body, etc.)
  • Global context: Can relate elements across the entire image
Best at:
Global meaning, relationships, document type

The Synergy

Neither model alone would work well for OCR:

SAM alone:

Knows shape of letters but not meaning. Would output "Q" as similar to "O" because shapes are similar.

CLIP alone:

Good at overall understanding but lacks precision. Would recognize "invoice" but might miss specific numbers.

SAM + CLIP:

SAM extracts precise local features, compressor reduces dimensionality, CLIP adds semantic context. The decoder gets both precision and understanding.

Transfer Learning Advantage

Both SAM and CLIP come with excellent pre-trained weights. DeepEncoder fine-tunes them for OCR rather than training from scratch. This gives:

  • • Faster training (2 epochs vs. typical 10+)
  • • Better generalization to unseen document types
  • • Lower risk of overfitting to training data

The compressor is the bridge between SAM's fine-grained output and CLIP's semantic processing. It's deceptively simple but critically important.

Compressor Architecture

# Two convolutional layers
Conv2d(256, 512, kernel=3, stride=2, padding=1) # 4096 → 1024
GELU()
Conv2d(512, 1024, kernel=3, stride=2, padding=1) # 1024 → 256
GELU()
LayerNorm(1024)
Input
64×64×256
After Conv1
32×32×512
After Conv2
16×16×1024
Flattened
256×1024

Why Convolutions, Not Attention?

The compressor could have used attention-based pooling (like Perceiver), but convolutions have specific advantages here:

Locality preservation

Each output token has a fixed 4×4 receptive field. Spatial relationships are preserved deterministically.

Computation efficiency

O(N) vs O(N²) for attention. At 4,096 tokens, this is 16× faster.

Trainable locality

The 3×3 kernel learns what local patterns to combine. Different filters for text vs. images vs. tables.

Translation equivariance

Same text processed the same way regardless of position. Important for document generalization.

The 16× Number

Why 16× and not 8× or 32×? The paper experimented with different ratios:

  • : 1,024 tokens—CLIP attention too expensive, marginal accuracy gain
  • 16×: 256 tokens—sweet spot for accuracy vs. compute
  • 64×: 64 tokens—too aggressive, loses fine details

The decoder reconstructs text from compressed visual tokens. DeepSeek uses their 3B MoE (Mixture of Experts) architecture—a clever choice for OCR.

DeepSeek-3B-MoE Architecture

Total Parameters
3B

Full model size including all experts

Activated Parameters
570M

Per token during inference

Expert Configuration
64 routed experts
2 shared experts (always active)
6 experts activated per token
Efficiency
5× parameter efficiency
(3B knowledge, 570M compute)

Why MoE for OCR?

OCR involves diverse "skills": recognizing Latin letters, Chinese characters, numbers, mathematical symbols, tables, etc. MoE naturally specializes:

Text Experts
Specialize in character recognition
Layout Experts
Handle document structure
Language Experts
Generate coherent output

The router learns to activate different expert combinations for different document types. A Chinese financial report routes differently than an English research paper.

The Reconstruction Objective

The decoder is trained with standard language modeling:

L = -Σ log P(texti | visual_tokens, text1...i-1)

Given compressed visual tokens, predict the original document text autoregressively. The compression ratio (visual tokens / text tokens) determines difficulty.

DeepSeek-OCR supports multiple resolution modes, letting you trade accuracy for speed based on document complexity.

Resolution Modes

ModeResolutionTokensBest For
Tiny512×51264Simple forms, receipts
Small640×640100Standard documents
Base1024×1024256Detailed documents
Large1280×1280400Small text, tables
GundamDynamic~795Complex multi-column
Gundam-MDynamic+~1,120Dense newspapers

Click to select primary • Right-click to compare

Token Count Comparison

Tiny
64
Small
100
Medium
256
Large
576
Gundam-M
1120

Accuracy Trade-off

Tiny
92%
Small
97%
Medium
98.5%
Large
99.2%
Gundam-M
99.8%

Amber line: 97% production threshold

Small Mode

100 tokens

The sweet spot for most applications. Excellent accuracy-efficiency tradeoff for invoices, contracts, and general documents.

Resolution
640×640
Patches
40×40 → 10×10
Accuracy
97%
Speed
Fast
Best For
Standard document OCR

Choosing the Right Mode

Start with Small (100 tokens)

The 97% accuracy sweet spot. Only scale up if you see errors in your specific document types.

When to use Tiny (64 tokens)

High-volume batch jobs where occasional errors are acceptable. Receipts, simple forms.

When to use Large/Gundam-M

Legal, medical, financial documents where errors have high cost. Worth the extra tokens.

Dynamic resolution

Use document classifiers to route different types to appropriate resolutions automatically.

Dynamic Resolution (Gundam Mode)

For complex documents, Gundam mode dynamically tiles the image:

# Gundam mode formula
n × (640×640 tiles) + 1 × (1024×1024 overview)
= n × 100 tokens + 256 tokens
# Example: n=5 tiles
5 × 100 + 256 = 756 tokens

The model automatically determines n (2-9) based on document aspect ratio and text density.

When to Use Which Mode

  • Tiny/Small: High-volume batch processing
  • Base: General-purpose default
  • Large: Financial statements, contracts
  • Gundam: Newspapers, dense layouts

Automatic Mode Selection

In production, you can let the model auto-select based on a quick analysis of text density. This typically adds <10ms latency but optimizes token usage.

Training DeepSeek-OCR required massive data curation and careful multi-stage optimization.

Training Data Composition

OCR 1.0 Data~60M samples
• 30M PDF pages (~100 languages)
• 25M Chinese pages
• 5M other language pages
• 4M fine-annotated pages
• 3M Word documents
• 20M scene OCR samples
OCR 2.0 Data~16M samples
• 10M synthetic charts
• 5M chemical formulas (SMILES)
• 1M geometry diagrams
Supporting Data30% of total
• 20% general vision (captions, grounding)
• 10% text-only (8K sequences)

Two-Stage Training

Stage 1: Encoder Pre-training
• Dataset: All OCR + 100M LAION
• Batch size: 1,280
• Learning rate: 5e-5
• Epochs: 2
• Optimizer: AdamW + cosine decay
• Sequence length: 4,096
Stage 2: Full Model
• Infrastructure: 160× A100-40G
• Pipeline parallel: 4 stages
• Data parallel: 40
• Global batch: 640
• Learning rate: 3e-5
• Throughput: 70B tokens/day

Pipeline Parallelism Layout

PP Stage 0: SAM-base + Convolutional Compressor
PP Stage 1: CLIP-large
PP Stage 2: Decoder layers 1-6
PP Stage 3: Decoder layers 7-12

This layout balances compute across stages. Encoder stages are memory-bound, decoder stages are compute-bound.

The central question: how much can you compress before losing critical information? This paper provides the first systematic study.

Compression Ratio vs. Accuracy

100%80%60%40%
10×15×20×
10×
100 vision tokens (640×640)
64 vision tokens (512×512)
High accuracy zone (90%+)
Danger zone (<70%)

The 10× Threshold

Notice the steep drop after 10× compression. Below 10×, accuracy stays above 90%. Beyond 10×, each additional 2× compression costs roughly 10% accuracy. This is the fundamental information-theoretic limit of visual encoding.

Compression Ratio Results

CompressionVision TokensText TokensAccuracy
~7×100600-70098.5%
~10×100900-100096.8%
~13×64800-90083.8%
~17×641000-110079.3%
~20×641200-130059.1%

The 10× Sweet Spot

The data reveals a clear threshold: below 10× compression, accuracy stays above 95%. Beyond 10×, it degrades rapidly.

Practical Implication

For most documents with <1000 words per page, use Small mode (100 tokens). You'll get 97%+ accuracy.

Dense Documents

For 2000+ word pages (newspapers, legal), use Gundam mode (795 tokens) to stay under 10× compression.

Why 20× Fails

At 20× compression, each visual token must encode ~20 text tokens worth of information. This exceeds the information capacity of the encoder. You're literally asking 64 numbers to represent 1,300 characters—the math doesn't work.

Let's analyze performance on OmniDocBench, the most comprehensive document understanding benchmark.

OmniDocBench Results (Edit Distance - Lower is Better)

ModelTokens/PageOverallEnglishChinese
GOT-OCR 2.02560.2870.2870.411
MinerU 2.06,7900.1330.1330.238
DeepSeek-OCR Small1000.2210.2210.284
DeepSeek-OCR Base2560.1370.1370.240
DeepSeek-OCR Gundam7950.1270.1270.181
68×
Fewer tokens than MinerU
100 vs 6,790
4.5%
Better than MinerU
At Gundam resolution
24%
Better Chinese OCR
0.181 vs 0.238

Document Category Analysis

Excels At:
  • Financial reports: 0.022 (near perfect)
  • Books: 0.037 (excellent)
  • Slides: 0.085 (clean layouts)
  • Academic papers: 0.091
Challenges:
  • Newspapers: 0.122 (needs Gundam mode)
  • Dense tables: 0.098 (many small cells)
  • Handwritten: 0.185 (not the focus)

DeepSeek-OCR is designed for production from day one. Here's how to deploy it.

Quick Start

from transformers import AutoModel, AutoTokenizer
import torch

# Load model
model = AutoModel.from_pretrained(
    'deepseek-ai/DeepSeek-OCR',
    trust_remote_code=True
).eval().cuda().to(torch.bfloat16)

# OCR a document
result = model.ocr('document.pdf', mode='base')

Hardware Requirements

  • Minimum: 16GB VRAM (RTX 4080)
  • Recommended: 40GB VRAM (A100)
  • BF16: ~6GB for inference
  • FP32: ~12GB for inference

Throughput by Hardware

  • RTX 4090: ~50K pages/day
  • A100-40G: ~200K pages/day
  • 8× A100: ~1.6M pages/day
  • 160× A100: ~33M pages/day

vLLM Deployment (Recommended)

# Start vLLM server for high throughput
python -m vllm.entrypoints.openai.api_server \
  --model deepseek-ai/DeepSeek-OCR \
  --trust-remote-code \
  --tensor-parallel-size 1 \
  --max-model-len 8192 \
  --gpu-memory-utilization 0.9

vLLM provides 3-5× throughput improvement through continuous batching and PagedAttention.

Common Issues

  • OOM errors: Reduce batch size or use BF16
  • Slow inference: Enable Flash Attention 2
  • Poor accuracy: Check resolution mode for document type
  • CUDA errors: Requires CUDA 11.8+ and PyTorch 2.0+

Every system has limitations. Understanding them helps you use the tool effectively.

Current Limitations

Handwritten Text

Performance on handwritten documents is weaker. The model was trained primarily on printed/digital text.

Extreme Compression

Beyond 20× compression, accuracy drops sharply. Very dense documents may need to be split into sections.

Non-Latin Scripts

Strong on Chinese and English, but performance on Arabic, Thai, and other scripts may be degraded.

Complex Layouts

Highly artistic layouts, overlapping text, and extreme rotations can confuse the model.

Future Directions

Higher Compression Ratios

Can we push to 50× with new architectures? Techniques like learned tokenization and hierarchical compression show promise.

Multi-Page Context

Currently processes pages independently. Future work could maintain context across pages for better document understanding.

Video Document Understanding

Extend to video documents (presentations, lectures) where temporal context provides additional signal.

Key Takeaways

The Problem
Traditional OCR produces thousands of tokens per page, overwhelming LLMs.
The Solution
DeepEncoder compresses visual information to 100-800 tokens while preserving semantics.
The Architecture
SAM (local) → 16× Compressor → CLIP (global) → MoE Decoder
The Result
97% accuracy @ 10× compression, 200K+ pages/day on single GPU.

The breakthrough: Visual information is fundamentally more compressible than text tokens. By encoding documents as images first, we can achieve massive efficiency gains that make production-scale document AI economically viable.