Skip to content

Fix device placement of constants, shared inputs and delegate outputs - #24210

Open
pytorchbot wants to merge 1 commit into
release/1.6from
cherry-pick-24088-by-pytorch_bot_bot_
Open

pytorchbot wants to merge 1 commit into
release/1.6from
cherry-pick-24088-by-pytorch_bot_bot_

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

What is wrong today

When a delegate runs on a GPU, PropagateDevicePass copies its inputs to the device (_h2d_copy) and its outputs back (_d2h_copy). skip_h2d_for_method_inputs and skip_d2h_for_method_outputs remove those copies at the method's edges.

  1. A transposed parameter is stored contiguous, but a delegate reading it is compiled for its original strides. to_edge does this silently.
  2. When a host op writes a delegate output in place, the next delegate on that device reads the old value. Such a relu_ makes memory planning fail.
  3. A lowered module returning one tensor, called from another module, gets no _d2h_copy.
  4. With the output skip flag, a returned output that something else reads comes back on the host.
  5. A constant that feeds a device delegate makes to_executorch fail. With the input skip flag, it arrives as host memory marked as device memory.
  6. With the input skip flag, an input with more than one reader is refused.
  7. A delegate targeting the CPU gets copies that fail at run time.

What this change does

Numbered as above:

  1. to_edge makes such a parameter contiguous before partitioning, and warns for each tensor whose layout it changes.
  2. The host op writes the host copy of the output. The copies stay when it writes between two delegates, also through a view.
  3. The output gets its _d2h_copy.
  4. Returned outputs stay on the device. One that a host op writes in place is returned on the host, with a warning.
  5. The constant gets a device copy, with a warning. One neither contiguous nor channels-last is refused.
  6. The input stays on the device of its first delegate. Shape-only ops read it there. Other readers share one _d2h_copy of it, with a warning. An input written in place on the host is refused.
  7. The delegate gets no copies.

Also:

  • A tensor that several delegates on one device read is copied there once, unless written in between.
  • New option PropagateDeviceConfig(error_on_per_call_copies=True) turns the warnings in 4 to 6 into errors when the matching skip flag is set.

What was tested

  • Device pass tests and new exir/tests/test_passes.py tests pass. All new tests but one fail without their fix.
  • backends/cuda/tests/test_device_placement.py matches eager PyTorch with real CUDA delegates on an H100. The unittest-cuda job now runs when these passes change, and fails if that test skips.

…#24088)

## What is wrong today

When a delegate runs on a GPU, `PropagateDevicePass` copies its inputs
to the device (`_h2d_copy`) and its outputs back (`_d2h_copy`).
`skip_h2d_for_method_inputs` and `skip_d2h_for_method_outputs` remove
those copies at the method's edges.

1. A transposed parameter is stored contiguous, but a delegate reading
it is compiled for its original strides. `to_edge` does this silently.
2. When a host op writes a delegate output in place, the next delegate
on that device reads the old value. Such a `relu_` makes memory planning
fail.
3. A lowered module returning one tensor, called from another module,
gets no `_d2h_copy`.
4. With the output skip flag, a returned output that something else
reads comes back on the host.
5. A constant that feeds a device delegate makes `to_executorch` fail.
With the input skip flag, it arrives as host memory marked as device
memory.
6. With the input skip flag, an input with more than one reader is
refused.
7. A delegate targeting the CPU gets copies that fail at run time.

## What this change does

Numbered as above:

1. `to_edge` makes such a parameter contiguous before partitioning, and
warns for each tensor whose layout it changes.
2. The host op writes the host copy of the output. The copies stay when
it writes between two delegates, also through a view.
3. The output gets its `_d2h_copy`.
4. Returned outputs stay on the device. One that a host op writes in
place is returned on the host, with a warning.
5. The constant gets a device copy, with a warning. One neither
contiguous nor channels-last is refused.
6. The input stays on the device of its first delegate. Shape-only ops
read it there. Other readers share one `_d2h_copy` of it, with a
warning. An input written in place on the host is refused.
7. The delegate gets no copies.

Also:

- A tensor that several delegates on one device read is copied there
once, unless written in between.
- New option `PropagateDeviceConfig(error_on_per_call_copies=True)`
turns the warnings in 4 to 6 into errors when the matching skip flag is
set.

## What was tested

- Device pass tests and new `exir/tests/test_passes.py` tests pass. All
new tests but one fail without their fix.
- `backends/cuda/tests/test_device_placement.py` matches eager PyTorch
with real CUDA delegates on an H100. The `unittest-cuda` job now runs
when these passes change, and fails if that test skips.

(cherry picked from commit cc4b514)
@pytorch-bot

pytorch-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 3 New Failures, 8 Pending, 7 Unclassified Failures

As of commit 24a83cc with merge base 4db778e (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

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

This branch was previously deployed

1 inactive deployment
cadence — 24a83ccd Deployed Oct 27, 2026 by pytorchbot via hifi-op-test / hifi4 #34880
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.

2 participants