feat: add PDF batch processing and merge results to prevent OOM - #86
Open
tiankuolu wants to merge 1 commit into
Open
feat: add PDF batch processing and merge results to prevent OOM#86tiankuolu wants to merge 1 commit into
tiankuolu wants to merge 1 commit into
Conversation
tiankuolu
force-pushed
the
pdf-batch-and-merge
branch
3 times, most recently
from
July 29, 2026 15:56
5f6c264 to
11e6128
Compare
tiankuolu
force-pushed
the
pdf-batch-and-merge
branch
from
July 29, 2026 16:00
11e6128 to
6c4aa13
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
中文说明
问题
当前 Transformers PDF 示例会先渲染全部页面,并将所有页面一起传给模型。对于页数较多的 PDF,页面图片和 OCR 输出可能占用大量内存,最终导致 OOM。
改动
infer_transformers.py--pages-per-batch控制批次大小,默认每批 1 页--resume跳过已经完成的批次merge_results.pymerged.mdocr_output/加入.gitignore内存行为
内存占用由
--pages-per-batch和最大单批输出决定,不再随 PDF 总页数持续积累。单个批次仍然需要能够放入可用内存。较小的批次也可能减少批次边界处的跨页上下文。
用法
python infer_transformers.py your_doc.pdf \ --output-dir ./ocr_output \ --pages-per-batch 1中断后继续:
python infer_transformers.py your_doc.pdf \ --output-dir ./ocr_output \ --pages-per-batch 1 \ --resume重新合并已有结果:
测试
测试覆盖:
English
Problem
The current Transformers PDF example renders all pages before passing them to
the model. For PDFs with many pages, rendered images and OCR outputs may consume
significant memory and eventually cause an out-of-memory error.
Changes
infer_transformers.py--pages-per-batch, defaulting to one page--resumemerge_results.pymerged.mdatomicallyocr_output/directoryMemory behavior
Memory usage is bounded by
--pages-per-batchand the largest batch outputinstead of growing with the total number of PDF pages.
A single batch must still fit in available memory. Smaller batches may also
reduce cross-page context across batch boundaries.
Usage
python infer_transformers.py your_doc.pdf \ --output-dir ./ocr_output \ --pages-per-batch 1Resume an interrupted run:
python infer_transformers.py your_doc.pdf \ --output-dir ./ocr_output \ --pages-per-batch 1 \ --resumeMerge existing results again:
Tests
The tests cover:
Related work
PR #36 focuses on resumability and resource cleanup for the SGLang batch
runner. This PR focuses on the Transformers backend and incremental result
merging.