Skip to content

File-backed mmap for XNNPACK packed weights (#19862)#19862

Draft
doggeral wants to merge 1 commit into
pytorch:mainfrom
doggeral:export-D106673663
Draft

File-backed mmap for XNNPACK packed weights (#19862)#19862
doggeral wants to merge 1 commit into
pytorch:mainfrom
doggeral:export-D106673663

Conversation

@doggeral
Copy link
Copy Markdown

@doggeral doggeral commented May 28, 2026

Summary:

Add file-backed mmap support to XNNWeightsCache so that packed weight allocations go to a MAP_SHARED file instead of dirty heap. After msync(MS_ASYNC), pages become clean file-backed and drop out of iOS phys_footprint.

How it works

  1. set_packed_cache_path() configures the cache file path via BackendOptions
  2. initialize_for_runtime() opens the cache file
  3. Each reserve_space() call extends the file via ftruncate and creates a MAP_SHARED mmap region — XNNPACK packs weights directly into file-backed pages
  4. finalize_for_runtime() calls msync(MS_ASYNC) on newly added regions only (incremental sync), making pages clean
  5. On Windows, mmap is unavailable — all code paths fall back to heap allocation automatically (packed_file_fd_ stays -1)

Expected savings

~400 MB packed weights move from dirty heap to clean file-backed pages (0 phys_footprint on iOS).

Differential Revision: D106673663

@doggeral doggeral requested a review from digantdesai as a code owner May 28, 2026 22:03
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 28, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19862

Note: Links to docs will display an error until the docs builds have been completed.

❗ 2 Active SEVs

There are 2 currently active SEVs. If your PR is affected, please view them below:

❌ 2 New Failures, 2 Unrelated Failures

As of commit 24af41f with merge base 4de16d0 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented May 28, 2026

CLA Missing ID

  • ❌ The email address for the commit (24af41f) is not linked to the GitHub account, preventing the EasyCLA check. Consult this Help Article and GitHub Help to resolve. (To view the commit's email address, add .patch at the end of this PR page's URL.) For further assistance with EasyCLA, please visit our EasyCLA portal and chat with our support bot.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 28, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 28, 2026

@doggeral has exported this pull request. If you are a Meta employee, you can view the originating Diff in D106673663.

@github-actions
Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@doggeral doggeral marked this pull request as draft May 28, 2026 22:20
Summary:

Add file-backed mmap support to `XNNWeightsCache` so that packed weight allocations go to a `MAP_SHARED` file instead of dirty heap. After `msync(MS_ASYNC)`, pages become clean file-backed and drop out of iOS `phys_footprint`.

## How it works

1. `set_packed_cache_path()` configures the cache file path via `BackendOptions`
2. `initialize_for_runtime()` opens the cache file
3. Each `reserve_space()` call extends the file via `ftruncate` and creates a `MAP_SHARED` mmap region — XNNPACK packs weights directly into file-backed pages
4. `finalize_for_runtime()` calls `msync(MS_ASYNC)` on newly added regions only (incremental sync), making pages clean
5. On Windows, mmap is unavailable — all code paths fall back to heap allocation automatically (`packed_file_fd_` stays -1)

## Expected savings

~400 MB packed weights move from dirty heap to clean file-backed pages (0 `phys_footprint` on iOS).

Differential Revision: D106673663
@meta-codesync meta-codesync Bot changed the title File-backed mmap for XNNPACK packed weights File-backed mmap for XNNPACK packed weights (#19862) May 29, 2026
@doggeral doggeral force-pushed the export-D106673663 branch from eeb479e to 24af41f Compare May 29, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant