Skip to content
Baidu Unlimited OCR Explanation: Why long document OCR should design KV cache, resolution, and regression verification boundaries before model size
← Back to blog

Baidu Unlimited OCR Explanation: Why long document OCR should design KV cache, resolution, and regression verification boundaries before model size

Development·13 min read

Baidu Unlimited OCR is an open source OCR model that attempts to handle dozens of page documents with a single inference by suppressing the KV cache growth of long outputs with R-SWA. The key is how to match memory, resolution, repetition suppression, and validation sets in a real document pipeline rather than benchmark scores.

OCR may seem like an old technology, but it's still tricky in real-world work. When processing long documents such as contracts, papers, manuals, and scanned PDFs, problems with page splitting, table/formula restoration, small text, GPU memory, and processing time arise all at once. Unlimited OCR released by Baidu approaches this problem not by using a “larger model” but by changing the structure of the KV cache that continues to grow over long outputs.

Baidu Unlimited OCR Explanation: Why long document OCR should design KV cache, resolution, and regression verification boundaries before model size
Unlimited The key to OCR is managing KV cache and verification boundaries together in long document output.

1. One-line problem definition

Key one line: The bottleneck of long document OCR is not a single recognition model, but the memory that grows and the generation loop that gets slower as the output gets longer.

For novice developers, the KV cache is the working memory that the model holds to refer back to what it has just created. A typical LLM decoder continues to increase this memory as the output tokens become longer. When OCR attempts to extract text from dozens of pages of documents at once, GPU memory and speed become bottlenecks.

This article is intended for developers who are building PDF OCR pipelines, running text extractors for in-house document searches, or examining VLM-based document parsing models. The scope is Unlimited OCR's R-SWA structure, implementation method, and adoption judgment. If you only process one simple receipt or a few structured forms, this model may be overkill.

2. First, conclusion

Key line: Unlimited OCR should be viewed as “a design that keeps memory usage constant even during long outputs” rather than “a new OCR accuracy record”.

Baidu researchers released Unlimited OCR on arXiv on June 22, 2026. Based on DeepSeek OCR, all attention of the decoder was replaced with Reference Sliding Window Attention, or R-SWA for short, and it was explained that tens of pages of documents can be transcribed in a single forward pass within the standard maximum length of 32K.

AI Times introduced the following figures in an article on June 27, 2026: OmniDocBench v1.5 93.23 points, v1.6 93.92 points, basic mode 5580 TPS, processing performance about 35% higher than before when generating 6000 tokens, and edit distance 0.11 or less in documents over 40 pages. These numbers are interesting, but product adoption requires revalidation with 50 to 100 documents of your own.

3. Decomposition of core structure

Key line: Unlimited OCR works by combining image compression, R-SWA decoder, repetition suppression, and PDF preprocessing flows.

The first layer is the image encoder. Unlimited OCR leverages DeepSeek OCR's DeepEncoder family architecture to compress document images into a small number of vision tokens. The number of tokens at the input stage must be reduced to allow room for multiple pages to be inserted at once.

The second layer is R-SWA. R-SWA retains reference tokens such as images and input prompts, but does not retain any output tokens it has already generated. Keeps only a portion of recent output as a window. When a person copies a book, it is similar to the way they only check the original and the few lines they just wrote.

The third layer is repetition suppression. The GitHub and Hugging Face examples use values ​​like no_repeat_ngram_size=35, single image ngram_window=128, and multi-page ngram_window=1024. OCR results are ruined if repeated phrases occur when creating long documents, so memory optimization and repetition suppression are both necessary.

The fourth layer is execution mode. The document states that single images can use gundam or base mode, while multi-page/PDFs can only use base mode. In other words, “making it a long document” means converting the original PDF into image pages of appropriate resolution, managing both the page count and the 32K output limit.

4. Description of design intent

Key line: The design intent is to reliably continue long copy operations without breaking the entire document into pages.

Existing OCR pipelines were often divided into document detection, layout analysis, character recognition, and post-processing. Modern VLM-based OCR simplifies the structure by generating text directly from document images. Instead, the LLM decoder produces long outputs, bearing the cost of an ever-growing KV cache.

Unlimited The choice of OCR varies here. Rather than growing the entire model, we utilize DeepSeek OCR-based checkpoints and change the decoder attention structure. The researchers performed 4,000 steps of continual training with 2 million document data, and explain that 90% were single-page documents and 10% were multi-page linked documents.

The benefits of this approach are clear. This can help reduce the problem of memory usage and token generation time on long documents growing linearly with output length. There is also giving up. Because it reduces direct references to the entire past output, separate verification is required for tasks that require tight alignment of distant context.

5. Evidence and Comparison

Key one-liners: Unlimited OCR targets the operational cost issue between traditional OCR, per-page VLM OCR, and long-context LLM OCR.

ApproachCore methodAdvantagesLimitSuitable situation
Traditional OCR pipelineStep-by-step processing of layout analysis and character recognitionOperation is predictable and possible on CPU/light GPURecovery quality of complex tables, formulas, and unstructured documents may be lowStructured forms, bulk scanning, cost-sensitive tasks
VLM OCR by pageInsert each page image separately and create textEasy to introduce and good for separating failed pagesEasy to miss context and table continuity between pagesDocument with high page independence
Long Context LLM OCRProcess long input and long output at onceEasy to maintain overall document flowKV cache and creation time grow with output lengthHigh-value tasks with few documents and priority on accuracy
Unlimited OCRKeep only reference tokens and recent outputs with R-SWASuppresses memory growth on long outputs and aims for parsing a single inference documentLow-resolution small text, 32K length, GPU environment, burden of reproduction verificationLong PDFs, documents containing tables/formulas, VLM OCR cost bottleneck improvement

arXiv Abstract explains that R-SWA keeps the KV cache constant throughout the entire decoding process. Examples of transformers execution and SGLang server execution examples are available on GitHub and Hugging Face. This is meaningful in that it is not a simple announcement, but a form that developers can actually download and verify.

However, the benchmark numbers in the AI ​​Times article do not directly constitute the product SLA. OmniDocBench scores, TPS, and edit distance are the result of specific datasets and execution conditions. If in-house documents include low-resolution scans, stamps/signatures, double-sided scans, handwriting, or complex tables, separate standards must be set.

6. Actual operation flow / step-by-step execution method

Key one-liners: First, you need to record setup, conversion, inference, quality scoring, and cost measurement all at once in a small set of PDFs.

Hugging Face example is presented based on Python 3.12.3, CUDA 12.9, PyTorch 2.10.0, transformers 4.57.1, and PyMuPDF 1.27.2.2 environment. It is safer to start in a separate GPU experiment environment rather than uploading directly to the production server.

import os
import tempfile
import fitz
import torch
from transformers import AutoModel, AutoTokenizer

model_name = "baidu/Unlimited-OCR"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(
    model_name,
    trust_remote_code=True,
    use_safetensors=True,
    torch_dtype=torch.bfloat16,
).eval().cuda()

def pdf_to_images(pdf_path, dpi=300):
    doc = fitz.open(pdf_path)
    tmp_dir = tempfile.mkdtemp(prefix="pdf_ocr_")
    mat = fitz.Matrix(dpi / 72, dpi / 72)
    paths = for i, page in enumerate(doc):
        out = os.path.join(tmp_dir, f"page_{i+1:04d}.png")
        page.get_pixmap(matrix=mat).save(out)
        paths.append(out)
    doc.close()
    return paths

model.infer_multi(
    tokenizer,
    prompt="<image>Multi page parsing.",
    image_files=pdf_to_images("sample.pdf", dpi=300),
    output_path="./outputs",
    image_size=1024,
    max_length=32768,
    no_repeat_ngram_size=35,
    ngram_window=1024,
    save_results=True,
)

It is recommended to leave an experiment log as below.

{
  "doc_id": "contract-001",
  "pages": 18,
  "dpi": 300,
  "image_size": 1024,
  "max_length": 32768,
  "ngram_window": 1024,
  "gpu": "A100-80GB",
  "latency_sec": 0,
  "peak_vram_gb": 0,
  "edit_distance_sample": 0,
  "table_reconstruction_pass": false
}

This value must be present to compare existing OCR, VLM OCR per page, and Unlimited OCR on the same basis. In particular, max_length=32768 does not mean that it is an infinite document. You must find the point where the results are truncated or the table collapses.

7. Pitfalls

Key one-liners: Long document OCR fails more in input quality and verification loops than in model installation.

  1. Pitfall: Image the PDF at low DPI.
    Prevention:Start at 300 DPI and use small print for documents. Raise the resolution for each sample to compare.
    Recovery: Save the error page with the original image to isolate whether it is a low resolution issue or a model issue.
  2. Pitfall: Misunderstand the 32K length as virtually unlimited.
    Prevention: Increase the number of pages, average number of tokens, and table/formula weighting. Set the upper limit on document length based on
    Recovery: If truncation occurs, fallback to division by chapter/section and post-processing merging by page.
  3. Pitfall: Replace the existing OCR by looking only at the benchmark score.
    Prevention: Edit distance of 50-100 internal documents; Measure table restoration, formula restoration, processing time, and VRAM simultaneously.
    Recovery: Set routing policy like traditional OCR for structured documents and VLM OCR for complex documents.
  4. Pitfall: Do not record repeat suppression parameters
    Prevention: no_repeat_ngram_size Save ngram_window with the results.
    Recovery: For documents with repetitive phrases, change the parameters to the same image and retest.
  5. Plot: Trust_remote_code is inadvertently turned on on the production server.
    Prevention: Fix model code. Review as commit, run in isolated container.
    Recovery: Apply SBOM, network block, read-only volume, and minimize permissions before operational reflection.

8. Strengths and Limitations

Key line: Its strength is that it changes the cost structure of long output, its limitation is that it is not a substitute for actual document quality and operational verification.

The strengths are clear. It opens up the possibility of processing long documents without breaking them into pages. It can produce better results than traditional methods in end-to-end OCR, including tables, formulas, and complex layouts. It is also important that the code and weights are public, so they can be verified directly with internal documents.

The limit is also realistic. First, parsing a single inference document requires GPU memory and model loading costs. Second, small text or low-resolution scanning is not a problem that R-SWA solves. Third, processing an entire long document at once can make it more difficult to track which page the error occurred in the event of a failure.

From my perspective, the introduction judgment is conservative. There is little reason to change existing structured documents that perform well in OCR. On the other hand, if tables are broken due to page-by-page processing, or if it is important for the team to restore the structure of long papers/reports, it is worth creating a separate experiment queue.

9. Points to study more deeply

Key line: Don't just look at R-SWA, you should also look at document image preprocessing, repetition suppression, benchmark standards, and security implementation.

It is a good idea to order additional learning like this. First, we check the KV cache issue and R-SWA intent in the arXiv abstract. Next, read the example infer_multi on GitHub and see the input image conversion process. Finally, create a sample internal document and record any failures in tables/formulas/small text/long pages.

10. Action Checklist + Author's Perspective

Key line: The completion criteria for introducing Unlimited OCR is not “return” but “proved in which document group it is better than the existing pipeline.”

  • More than 50 internal representative documents were collected by level of difficulty.
  • Conventional OCR, page-by-page VLM OCR, and Unlimited OCR were compared on the same document.
  • Logs page count, DPI, image size, max_length, ngram_window, GPU, VRAM, latency.
  • Tables, formulas, body, header/footer, and page numbers were scored separately.
  • Confirmed the document type where truncation occurs at the 32K output limit.
  • isolated trust_remote_code execution and fixed model code/weight version.
  • In case of failure, a routing was created that falls back to existing OCR or page split OCR.
  • Storage location and log masking policy were set when processing personal information/contract documents.

Definition of Done: In the internal verification set, if Unlimited OCR clearly lowers the error rate or processing cost of the target document group compared to the existing pipeline and passes the failed document fallback and security execution criteria, the first introduction is considered completed.

From the author's perspective, this technology is not so much a “model to replace all OCR” as it is an example of a rethinking of the bottlenecks of long document OCR pipelines. Recommended targets are teams that require heavy post-processing of existing OCR due to long PDFs, tables/formulas, and multi-page contexts. Conversely, teams that reliably process only a few types of structured documents are better off adding verification automation and routing to existing OCR.

11. Reference

Share this article

Related articles

Take the AQ test

See your AI capability in three minutes. Assess recognition, utilization, verification, integration, and ethics at once, then receive practical insights.

Start the free AQ test