Skip to content

Use the backend's copy hook in single-backend to_backend - #23339

Merged
shoumikhin merged 1 commit into
pytorch:mainfrom
shoumikhin:to-backend-use-copy-hook
Oct 2, 2026
Merged

shoumikhin merged 1 commit into
pytorch:mainfrom
shoumikhin:to-backend-use-copy-hook

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

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_preprocess lets a backend decide how that copy is made. The default is copy.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, which to_edge_transform_and_lower also uses, already calls the hook. But two other paths do not:

  • to_backend(backend_id, edge_program, compile_specs) calls copy.deepcopy directly.
  • 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_backend now calls cls.copy_exported_program_for_preprocess(edge_program, compile_specs) instead of copy.deepcopy(edge_program).

copied_edge_program = cls.copy_exported_program_for_preprocess(
    edge_program, compile_specs
)

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

  • New unit test in test_lowered_backend_module.py: to_backend calls the backend's copy hook once with the program and the compile specs, and passes what the hook returns to preprocess. It fails before this change (the hook is never called) and passes after it.
  • The other tests in that file give the same result before and after. One end-to-end test fails both ways in my environment, because the installed runtime does not include the test-only demo backend.

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.
Copilot AI balanced review requested due to automatic review settings October 2, 2026 00:20
@shoumikhin shoumikhin added the release notes: exir Changes to any dialects and passes on these dialects, such as memory planning label Oct 2, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

🔗 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 Failures

As of commit 335964b with merge base 7c80907 (image):

NEW FAILURES - The following jobs have failed:

  • Cadence Build & Test / Resolve CI docker image / resolve (gh)
    ##[error]Refusing to check out fork pull request code from a 'pull_request_target' workflow. This workflow runs with the base repository's GITHUB_TOKEN, secrets, default-branch cache scope, and runner access. Fetching and executing a fork's code in that trusted context commonly leads to "pwn request" vulnerabilities. To opt in, review the risks at https://gh.io/securely-using-pull_request_target and set 'allow-unsafe-pr-checkout: true' on the actions/checkout step.
  • pull / test-arm-backend-no-driver (test_pytest_models_tosa) / linux-job (gh)
    RuntimeError: Command bash /__w/_temp/exec_script failed with exit code 1

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 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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. release notes: exir Changes to any dialects and passes on these dialects, such as memory planning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants