fix(layout): bound model input copy memory#518
Open
CyMule wants to merge 2 commits into
Open
Conversation
CyMule
marked this pull request as ready for review
July 23, 2026 12:48
Contributor
There was a problem hiding this comment.
No issues found across 4 files
Shadow auto-approve: would auto-approve. Memory optimization replacing a full read with chunked copy; no behavioral change, API unchanged, regression test added. No operational tradeoff requires human judgement.
Re-trigger cubic
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.
Summary
bytesallocation.Why
process_data_with_modelcurrently callsdata.read()before writing the temporary file. This creates an additional allocation proportional to the complete input size and can increase peak memory substantially for large documents.Chunked copying bounds the temporary copy buffer without changing the resulting file, API behavior, or supported file sizes.
Impact
In a representative end-to-end partition API benchmark using a 70.4 MB two-page PDF:
The memory reduction is approximately proportional to input size. The current 1 MiB chunk size introduces a measurable throughput tradeoff and may benefit from tuning before merge.
Validation
uv run pytest test_unstructured_inference/inference/test_layout.py::test_process_data_with_model_copies_input_in_bounded_chunks -q— 1 passed.uv run ruff check unstructured_inference/inference/layout.py test_unstructured_inference/inference/test_layout.py— passed.