Skip to content

Report a clear error instead of crashing on a device-planned copy - #21960

Open
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:device-planned-copy-error
Open

Report a clear error instead of crashing on a device-planned copy#21960
shoumikhin wants to merge 1 commit into
pytorch:mainfrom
shoumikhin:device-planned-copy-error

Conversation

@shoumikhin

Copy link
Copy Markdown
Contributor

Supersedes #21704, which was stacked and did not build. Same change, rebuilt on main
with 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:

[cuda_backend.cpp:429] Created new CUDA stream 0x10ae920 for method
Segmentation fault (core dumped)

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:

Planned-buffer copy needs host memory on both sides: dst device CUDA, src device CPU.
Export with MemoryPlanningPass(alloc_graph_input=False) to share the caller's memory.

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 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 that
tells 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:

  • host to host succeeds and the data arrives,
  • a device destination is refused with Error::NotSupported,
  • a device source is refused the same way.

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:

copy present -> pass
copy removed -> fail (4 assertions)

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 neighbouring tensor_util_test uses, and it
names executorch::runtime::internal::, which is executorch::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.

Copilot AI lite review requested due to automatic review settings August 20, 2026 01:10
@pytorch-bot

pytorch-bot Bot commented Aug 20, 2026

Copy link
Copy Markdown

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

As of commit 62498f8 with merge base 3caaaca (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 Aug 20, 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 was unable to review this pull request because the user who requested the review has reached their quota limit.

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.
Copilot AI review requested due to automatic review settings August 20, 2026 01:14
@shoumikhin
shoumikhin force-pushed the device-planned-copy-error branch from 5fb6682 to 62498f8 Compare August 20, 2026 01:14

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 was unable to review this pull request because the user who requested the review has reached their quota limit.

@shoumikhin shoumikhin added the release notes: runtime Changes related to the core runtime which loads the program methods, initializes delegates, and runs label Aug 20, 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. release notes: runtime Changes related to the core runtime which loads the program methods, initializes delegates, and runs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants