Skip to content

Add skip_h2d_for_constants to keep constants in device memory (#24144) - #24269

Closed
shoumikhin wants to merge 1 commit into
pytorch:release/1.6from
shoumikhin:pick-24144-release-1.6
Closed

shoumikhin wants to merge 1 commit into
pytorch:release/1.6from
shoumikhin:pick-24144-release-1.6

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

Cherry-pick of #24144 onto release/1.6.

With CUDA device memory planning, which is new in 1.6, a constant read only by a GPU delegate was still copied from the host on every call. The new opt-in skip_h2d_for_constants keeps it in device memory after one copy at load. Programs that do not set it are unchanged.

It depends on #24210 (device placement of constants), which has now landed on release/1.6. The bot's earlier pick conflicted because #24210 was not there yet. This is the same commit applied by hand: it applies cleanly and the diff matches the commit on main line for line.

…h#24144)

## What is wrong today

With `skip_h2d_for_method_inputs`, the inputs of a device delegate
arrive in device memory. Constants (weights and read-only buffers) do
not. Their data is stored on the host, so a constant that a delegate
reads from outside is copied to the device on every call. The CUDA
partitioner already moves constants into its delegate, so this hits
constants left outside, for example with a custom partitioner.

## What this change does

A new opt-in option keeps such a constant in device memory. The runtime
copies it there once, when the method loads.

```python
ExecutorchBackendConfig(
    propagate_device_config=PropagateDeviceConfig(skip_h2d_for_constants=True),
)
```

- `PropagateDevicePass` tags a constant that only delegates on one
device read with that device.
- Memory planning gives it device memory for the whole method. The
emitter stores its data as the initial value of that memory.
- The tensor parser loads such data on the host and copies it with the
registered `DeviceAllocator`. New `DeviceAllocator::synchronize()` waits
for the copy, so the host buffer can be freed and the first run reads
the right data. On CUDA it waits for the stream the copy used, on the
right device.
- An empty tensor with initial data no longer fails to load, also in
host memory.

Limits:

- Each method that reads the constant keeps its own device copy while it
is loaded.
- A constant that anything else also reads, or that is changed on the
host first (a view, a transpose), is still copied on every call.
- `share_mutable_buffers` does not work with it.

Cost: the loading code is in every runtime build. In CI, the stripped
Arm bare-metal `size_test` grows by 560 bytes, to 117884 (limit 118000).
On x86 its code grows by 727 bytes with GCC and 607 with Clang, and the
stripped file size does not change.

## What was tested

- `test_propagate_device_pass.py`: only such constants skip the copy and
get device memory; warnings and errors.
- `tensor_parser_device_test.cpp`: such a constant loads from the
program and from a `.ptd` file into device 1 of a mock CUDA device, and
is waited for.
- `test_emit.py`: a method whose initial data is empty loads.
- `test_cuda_allocator.cpp`: `synchronize()` waits for the right stream,
also with another GPU current (needs two GPUs; ran on H100s).
- `test_device_placement.py`: such a program runs twice on an H100 and
matches eager PyTorch.

Each new test fails without its change, and each commit passes on its
own.

(cherry picked from commit 92721a8)
@pytorch-bot

pytorch-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⏳ No Failures, 6 Pending

As of commit 41c57d7 with merge base 4db778e (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 29, 2026
@shoumikhin

Copy link
Copy Markdown
Contributor Author

Closing in favor of the pytorchbot cherry-pick #24274, which has the same diff.

@shoumikhin shoumikhin closed this Oct 29, 2026
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