Repository navigation
Add skip_h2d_for_constants to keep constants in device memory (#24144) - #24269
Closed
shoumikhin wants to merge 1 commit into
Closed
shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
…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)
shoumikhin
requested review from
JacobSzwejbka and
larryliu0820
as code owners
October 29, 2026 01:31
🔗 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 PendingAs of commit 41c57d7 with merge base 4db778e ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Contributor
Author
|
Closing in favor of the pytorchbot cherry-pick #24274, which has the same diff. |
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.
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_constantskeeps 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.