Report a clear error instead of crashing on a device-planned copy - #21960
Open
shoumikhin wants to merge 1 commit into
Open
Report a clear error instead of crashing on a device-planned copy#21960shoumikhin wants to merge 1 commit into
shoumikhin wants to merge 1 commit into
Conversation
shoumikhin
requested review from
JacobSzwejbka and
kirklandsign
as code owners
August 20, 2026 01:10
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21960
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 62498f8 with merge base 3caaaca ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
When the runtime fills a memory-planned tensor it copies the caller's data into
the buffer it reserved, with a plain host copy. That is correct for main memory.
If the destination lives on an accelerator, a host copy into it is undefined and
the process dies with a segmentation fault and no message. The last thing a user
sees is unrelated:
[cuda_backend.cpp:429] Created new CUDA stream 0x10ae920 for method
Segmentation fault (core dumped)
Nothing points at the cause, which is a program whose activations live on a
device being exported so that the runtime also reserves its own buffer for them.
Check the assumption the copy makes and return an error naming the fix. Both the
ATen and portable variants of copy_tensor_data get the same guard, so the
behaviour does not depend on which runtime is built. The message is kept short
enough to survive the runtime's 256-character log buffer, since the part that
tells a user what to change is the part worth keeping.
The new test builds tensors on CPU and on CUDA device tags and checks that a
host-to-host copy still succeeds and moves the data, while a copy with either
side on a device is refused. Each tensor owns its own storage, so the
host-to-host case has a distinct destination and the assertion would fail if the
copy were removed.
shoumikhin
force-pushed
the
device-planned-copy-error
branch
from
August 20, 2026 01:14
5fb6682 to
62498f8
Compare
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.
Supersedes #21704, which was stacked and did not build. Same change, rebuilt on
mainwith the compile error fixed.
What is broken
When the runtime fills a memory-planned tensor it copies the caller's data into the
buffer it reserved, using a plain host copy:
std::memcpy(dst_data_ptr, t_src.const_data_ptr(), t_src.nbytes());That is correct for main memory. If the destination lives on an accelerator, a host copy
into it is undefined and the process dies with a segmentation fault and no message. The
last thing a user sees is unrelated to the cause:
Nothing points at what actually went wrong, which is a program whose activations live on
a device being exported so the runtime also reserves its own buffer for them.
The change
Check the assumption the copy makes, and return an error that names the fix:
Both the ATen and portable variants of
copy_tensor_dataget the same guard, so thebehaviour does not depend on which runtime is built.
The message is deliberately short. The runtime logs through a 256-character buffer
(
runtime/platform/log.cpp), so a longer message is silently cut off, and the part thattells a user what to change is the part worth keeping.
Test plan
A new test builds tensors tagged CPU and CUDA and checks three cases:
Error::NotSupported,Each tensor owns its own storage, so the host-to-host case has a distinct destination.
Deleting the copy makes that assertion fail, which is the point: a test that shares one
buffer between source and destination would pass whether or not the copy happened.
Verified by compiling the test against the portable headers and running the host-to-host
case with the copy present and then removed:
Known gap
The guard in the ATen variant has no test coverage. The new test is registered outside
the
get_aten_mode_options()loop that the neighbouringtensor_util_testuses, and itnames
executorch::runtime::internal::, which isexecutorch::runtime::aten::internal::in ATen mode. The portable guard is covered; the ATen one is not. Worth fixing, and left
out of this change to keep it to one concern.