[executorch][native] Map .ptn packages instead of copying them - #22532
[executorch][native] Map .ptn packages instead of copying them#22532SS-JIA wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22532
Note: Links to docs will display an error until the docs builds have been completed. ❌ 3 New FailuresAs of commit 6073d95 with merge base 2c1da32 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
digantdesai
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Stack from ghstack (oldest at bottom):
Packageread the whole.ptninto astd::vector<uint8_t>, so every constantwas heap-resident for the package's lifetime whether or not anything touched it.
Fine for a 14 MB
mobilenet_v2; disqualifying for a model whose weights are thereason constants ship out-of-line in the first place.
Adds
OwnedBytes: owning, read-only bytes that are either a heap buffer or aread-only file mapping, behind one
span().Packageholds one of these insteadof a vector, so the backing becomes a load-time argument rather than a property
of the type.
Package::load(path)maps by default; passuse_mmap=falseto readinto the heap.
load_fileandmap_filecollapse into that one entry point, andthe
read_filethat was duplicated inPackage.cppandptn_inspector.cppcollapses into
OwnedBytes::from_file.The mapping covers the whole file from offset 0, which is deliberately simpler
than ExecuTorch's
MmapDataLoader: mapping a sub-range needs page-rounding on theway in and again in the unmap callback, since only the interior pointer survives
in the buffer, whereas a whole-file map is already page-aligned. That also means
alignment is unchanged from the heap path -- a page-aligned base is more aligned
than
new[]'s, so every span keeps the alignment its file offset implies. Thecaveat in
SafeTensorsReader.habout unpadded safetensors payloads appliesequally to both modes; removing it is an AOT change.
MAP_SHARED, so two processes running the same model share physical pages, theway
MmapDataLoaderdoes. A zero-length file yields empty heap bytes, sincemmaprejects a zero length. Platforms withoutmmapthrow rather than failingto compile.
ptn_inspectormaps by default and takes--nommapto opt out. It cannot usePackage::load(path), because it also accepts a bare.ptgand so must inspectthe leading bytes before it knows whether to build a package at all;
re-acquiring the file inside the path overload would read it twice under
--nommap. That overload therefore has no caller yet -- it is the entry pointthe runtime will use -- though both halves it forwards to are exercised here.
One behaviour difference worth knowing: a mapping sees later edits to the file on
disk, a heap read does not.
Differential Revision: D118480655