< Academy

Attention Reloaded: How R-SWA Helps AI Read Longer Documents

Research
Steven Manangu
ML Engineer

What happens when an AI system has to read a document that keeps getting longer?

‍

For many AI models, processing a document is not simply a case of reading the pages and producing an answer. As the model generates text, it also needs to keep track of what it has already written.

‍

This creates a memory problem: the longer the output, the more information the model has to store and manage.

‍

In our latest Passion Academy, Steven Manangu explores a research paper from Baidu titled Unlimited OCR Works. The paper introduces a technique called Reference Sliding Window Attention (R-SWA). This technique aims to make long document processing more efficient by changing how an AI model manages its attention and memory.

‍

‍

The problem with reading long documents

‍

This problem becomes particularly challenging for Optical Character Recognition (OCR), where AI systems convert text from images and scanned documents into machine readable text. When processing a long document, the model must retain access to the original pages while continuing to generate its transcription.

‍

A typical document OCR model has two main components. First, a vision encoder processes the pages and converts their visual information into a sequence of tokens. These tokens represent the content of the document in a format the language model can work with.

‍

Next, a decoder generates the transcription, one token at a time.

‍

The challenge comes as the transcription grows. When the model generates its first few words, there is relatively little previous output to keep track of. But as it works through dozens of pages, that history can become thousands of tokens long.

‍

Most transformer models use something called a Key Value (KV) cache to store information needed by the attention mechanism. Rather than calculating everything again for every new token, the model can reuse these stored representations.

‍

However, with full attention, the cache continues to grow as more tokens are generated. This means memory requirements increase alongside the length of the output.

‍

For a system expected to process long documents in one continuous run, this can become a significant bottleneck.

‍

‍

A simple way to think about the solution

‍

Imagine copying a book by hand.

‍

To write the next sentence, you need access to the original book and enough of your recent writing to understand where you are and maintain continuity.

‍

But do you really need to keep every sentence you have already written in front of you?

‍

This is the intuition behind R-SWA.

‍

Instead of requiring the model to retain its entire generated history, the technique separates two types of information:

‍

  1. ‍The source document: The original visual tokens remain accessible throughout the process.‍
  2. The recent output: The model only needs to retain a limited window of its most recently generated tokens.

‍

This allows the model to continue referring to the original document without carrying an ever growing record of everything it has already produced.

‍

‍

What is Reference Sliding Window Attention?

‍

R-SWA stands for Reference Sliding Window Attention. It builds on an existing technique called Sliding Window Attention (SWA), but makes an important distinction between the original input and the output generated by the model.

‍

There are three approaches worth comparing.

‍

Full causal attention

‍

The model can access the original reference and all previously generated tokens. This provides a complete history, but the KV cache grows as the output gets longer.

‍

Plain Sliding Window Attention

‍

The model only attends to a fixed window of recent tokens. However, this means older information eventually disappears from its attention window, including information that may still be important.

‍

Reference Sliding Window Attention

‍

The model always retains access to the original reference, while applying a sliding window to the generated output. In the paper’s default configuration, this output window contains 128 tokens.

‍

The distinction is important. The model does not forget the document it is transcribing. It simply limits how much of its own previous output it needs to keep in active attention.

‍

‍

As shown in the diagram, the reference tokens remain available for every new prediction, while the output window moves forward as more text is generated.

‍

Why does this make a difference to memory?

‍

The potential memory saving comes from preventing the generated output history from growing indefinitely.

‍

With full attention, the number of cached token positions can be represented as:

‍

Reference tokens + generated output tokens

‍

With R-SWA, it becomes:

‍

Reference tokens + fixed output window

‍

This means the output portion of the cache remains bounded, even as the transcription becomes much longer.

‍

‍

Above is an example with 10,000 reference tokens and a 128 token output window. Under full attention, the cache continues growing with every generated token. With R-SWA, the number of cached token positions remains at approximately 10,128.

‍

This does not mean the model’s entire memory usage becomes constant. The original reference still has to be processed and retained, and the model still needs to attend to it. However, the memory associated with generated output no longer grows continuously.

‍

‍

Inside Unlimited OCR

‍

The paper applies R-SWA to an OCR system called Unlimited OCR, building on the DeepSeek OCR architecture.

‍

The original system uses a vision encoder called DeepEncoder, which processes a page at a resolution of 1024 × 1024. In its Base configuration, each page is represented by 256 visual tokens.

‍

Its decoder is a 3 billion parameter Mixture of Experts model, with approximately 0.5 billion active parameters.

‍

The main change introduced by the research is the application of R-SWA across every decoder layer.

‍

The researchers continued training the decoder while keeping the vision encoder frozen. Their training process used:

‍

  • 2 million document samples
  • 32,000 training sequences
  • 4,000 training steps
  • A mixture of single page and synthetic multipage examples, in a 9:1 ratio

‍

The aim was to teach the decoder to work with the new attention mechanism while preserving the model’s ability to process and transcribe document content.

‍

‍

What did the researchers find?

‍

The paper evaluates Unlimited OCR using several benchmarks, including OmniDocBench, which measures how accurately document content is extracted and represented.

‍

On the OmniDocBench v1.5 evaluation, Unlimited OCR achieved an overall score of 93.23, compared with 87.01 for the DeepSeek OCR baseline.

‍

The results also showed improvements across several individual metrics:

‍

‍

‍

For text edit distance, a lower score is better because it indicates fewer differences between the generated transcription and the reference text. For the formula and table metrics, higher scores indicate better performance.

‍

These results suggest that Unlimited OCR can deliver strong document recognition while using a much shorter output attention window.

‍

However, there is an important caveat. The updated model also received additional document training. Because the research changes the attention mechanism and continues training at the same time, the benchmark comparison does not isolate how much of the improvement comes specifically from R-SWA.

‍

A matched comparison against a full attention model with the same additional training would help establish that more clearly.

‍

‍

Does performance hold up across longer documents?

‍

One of the most interesting parts of the research is its evaluation of longer documents.

‍

The researchers tested the system on an internal collection containing novels, research papers and other documents, measuring how transcription quality changed as the number of input pages increased.

‍

‍

‍

The Distinct 35 metric is an indicator of repetition, rather than transcription accuracy. The text edit distance is therefore more useful when assessing how closely the output matches the original document.

‍

The results show that the model can process documents of 40 pages or more in one continuous run. However, transcription accuracy decreases as document length increases. This suggests that limiting the output memory solves only part of the challenge of processing longer documents.

‍

‍

What about decoding speed?

‍

The researchers also investigated whether limiting the output cache could improve throughput as generated sequences become longer.

‍

‍

These figures demonstrate the potential efficiency gains from keeping the output cache bounded. However, the 34.8% result is based on an idealised setup rather than an end to end latency measurement for processing a large PDF. The distinction matters when considering how these improvements might translate into real world applications.

‍

‍

What are the limitations?

‍

Despite the promising results, the paper does not suggest that long document processing is now a solved problem.

‍

There are two important limitations to consider.

‍

The original input still grows

‍

R-SWA limits the amount of generated output stored in the attention cache. However, every additional page still contributes reference tokens.

‍

The model must still process the source document and attend to its content. This means the technique addresses one part of the memory problem rather than removing the cost of processing long inputs altogether.

‍

The impact of R-SWA needs further isolation

‍

As mentioned earlier, the updated model received additional document training alongside the introduction of R-SWA. This makes it difficult to attribute the improvement in

OCR benchmark scores to the attention mechanism alone.

‍

A comparison against a full attention model that receives exactly the same additional training would provide a clearer picture of the technique’s individual contribution.

‍

The research also raises an interesting question about what comes next. Rather than keeping the entire source document available in attention, could a model retrieve specific parts of the original document when it needs them?

‍

The paper proposes reference chunk retrieval as a potential direction for future work, alongside possible extensions to automatic speech recognition and translation.

‍

‍

The bigger picture

‍

The interesting contribution of R-SWA is not simply that it makes an OCR model faster or more efficient. It demonstrates a different way of thinking about how AI systems manage information over long sequences.

‍

Not every piece of information needs to be treated in the same way. The original source may need to remain accessible throughout a task, while the model’s own generated history can be managed using a much smaller window.

‍

This distinction could be useful beyond document transcription, particularly in tasks where a model needs to produce long outputs while continually referring back to an original source.

‍

The broader question is how we can build AI systems that retain access to the information that matters without requiring them to remember every step they have taken along the way.

‍

The source stays. The window moves. Attention keeps up.

‍

‍

References

‍

Yin, Y. et al. (2026). Unlimited OCR Works. Baidu. arXiv:2606.23050.

‍

Read the research paper here

‍

< back to academy
< previous
Next >