Skip to content

Save a lowered program whose named data is file-backed - #23693

Draft
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:serde-file-backed-named-data
Draft

shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:serde-file-backed-named-data

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

Problem

A backend can give ExecuTorch its named data as FileBackedData instead of bytes. The data then waits in a file on disk until the .pte or .ptd is written, instead of sitting in memory a second time. The CUDA backend stores its weights this way, and NamedDataStore, to_executorch() and write_tensor_data_to_file() all handle it.

Saving such a lowered program with exir.save() fails, though:

TypeError: Object of type FileBackedData is not JSON serializable

The serializer writes the lowered module's named data as JSON, and its encoder only knows bytes.

Change

Before encoding, the serializer reads each file-backed buffer into bytes. It works on a copy of the named data, so the lowered module itself keeps its file-backed buffers. The saved file is the same as if the backend had given bytes, and, as for any buffer in this JSON format, the data is held in memory while saving. Loading did not need a change: it already decodes every buffer back to bytes.

Test plan

A new test in exir/tests/test_serde.py lowers a small graph with the named-data test backend, replaces its buffer with a FileBackedData holding distinct bytes, then saves and loads the program and checks the loaded buffer. It fails without this change with the error above, and passes with it.

Ran exir/tests/test_serde.py on Linux aarch64 with Python 3.12, using this branch's exir package and schema over an installed nightly: 16 passed with the change, and 15 passed on main (all tests except the new one).

A backend can hand its named data to ExecuTorch as FileBackedData instead of
bytes, so a large blob waits on disk rather than as a second copy in memory.
The CUDA backend stores its weights this way. NamedDataStore, to_executorch()
and write_tensor_data_to_file() all handle it.

exir.save() of a program that contains such a lowered module fails, though:

    TypeError: Object of type FileBackedData is not JSON serializable

The serializer turns the lowered module's named data into JSON with an
encoder that knows only bytes. Loading already reads every buffer back as
bytes.

The serializer now reads file-backed buffers into bytes before encoding them,
on a copy of the named data, so the lowered module keeps its file-backed
buffers. The saved file is the same as if the backend had given bytes. Like
any buffer in this JSON format, the data is held in memory while saving.

Tested with a new test in exir/tests/test_serde.py: it lowers a small graph
with the named-data test backend, replaces its buffer with a FileBackedData
holding distinct bytes, saves and loads the program, and checks the loaded
buffer. It fails without this change with the error above. All 16 tests in
that file pass with it.
@pytorch-bot

pytorch-bot Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 1 Pending

As of commit 1314b9f with merge base f2575be (image):
💚 Looks good so far! There are no failures yet. 💚

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

@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 Oct 12, 2026
@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.

This branch has not been deployed

No deployments
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant