Repository navigation
Use the backend's copy hook in single-backend to_backend - #23339
Merged
Merged
Conversation
BackendDetails.copy_exported_program_for_preprocess lets a backend decide how a program is copied before preprocess. A backend uses it to share large constants instead of copying them; the CUDA backend does this in its low-memory mode. The multi-method lowering path already calls the hook. The single-backend to_backend(backend_id, edge_program, compile_specs) still called copy.deepcopy directly, and to_backend(edge_program, partitioner) reaches that function for every partition. On those paths an override was ignored and every constant was copied. Call the hook there too. The default hook is copy.deepcopy, so nothing changes for backends that do not override it.
shoumikhin
requested review from
JacobSzwejbka and
larryliu0820
as code owners
October 2, 2026 00:20
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23339
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New FailuresAs of commit 335964b with merge base 7c80907 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The focused implementation matches existing lowering behavior and is adequately covered by the new regression test.
Review effort: Balanced
Findings: None
What changed in this PR
Routes single-backend lowering through the backend-specific program-copy hook, enabling memory-efficient copying.
Changes:
- Replaces direct deep-copying with
copy_exported_program_for_preprocess. - Adds a unit test verifying hook invocation and preprocessing input.
| File | Description |
|---|---|
exir/backend/backend_api.py |
Uses the backend copy hook before preprocessing. |
exir/backend/test/test_lowered_backend_module.py |
Tests hook forwarding behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
mergennachin
approved these changes
Oct 2, 2026
3 of 7 tasks
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.
What is wrong today
Before ExecuTorch calls a backend's
preprocess, it copies the program, so a backend that edits the graph cannot change the caller's program.BackendDetails.copy_exported_program_for_preprocesslets a backend decide how that copy is made. The default iscopy.deepcopy. A backend can override it to share large constant tensors instead of copying them. The CUDA backend does that in its low-memory mode, because copying model weights costs as much memory as the weights themselves.EdgeProgramManager.to_backend, whichto_edge_transform_and_loweralso uses, already calls the hook. But two other paths do not:to_backend(backend_id, edge_program, compile_specs)callscopy.deepcopydirectly.to_backend(edge_program, partitioner)calls that same function for every partition.On those paths a backend's override is ignored, and every constant is copied. For a large delegate payload that can be gigabytes of extra memory during lowering.
What this change does
The single-backend
to_backendnow callscls.copy_exported_program_for_preprocess(edge_program, compile_specs)instead ofcopy.deepcopy(edge_program).The default hook is still
copy.deepcopy, so nothing changes for a backend that does not override it. Only backends that opted in see a difference, and they now get the same behavior on every lowering path.What was tested
test_lowered_backend_module.py:to_backendcalls the backend's copy hook once with the program and the compile specs, and passes what the hook returns topreprocess. It fails before this change (the hook is never called) and passes after it.