Skip to content

Raise InternalError for non-CPU constant storage in the emitter - #24202

Open
AxelNoun wants to merge 3 commits into
pytorch:mainfrom
AxelNoun:fix/21545-emit-guard-non-cpu
Open

AxelNoun wants to merge 3 commits into
pytorch:mainfrom
AxelNoun:fix/21545-emit-guard-non-cpu

Conversation

@AxelNoun

@AxelNoun AxelNoun commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

_tensor_spec_to_evalue reads a constant's bytes by casting spec.storage.data_ptr() to a host pointer. When the storage is on CUDA, that pointer is a device address, so to_executorch() segfaults (exit 139) with no Python exception. This PR checks the storage device first and raises an InternalError, as @shoumikhin suggested in #21545, that names the device and says how to fix the export. As discussed in the issue, lowering a CUDA module onto the portable path is not supported; only the way it fails changes. The check applies only to non-empty constants that the emitter serializes itself and whose storage is not on CPU, which are exactly the cases that reach the host read of a device pointer.

Fixes #21545

Test plan

New test test_emit_non_cpu_constant_raises in exir/emit/test/test_emit.py. It uses meta storage, which is not host-readable either and needs no accelerator. Without the guard, that case already fails with a wrapped "NULL pointer access" InternalError, so the test matches on the device name in the message.

Built and installed from this branch after merging main at d6abe23 (pip install . --no-build-isolation on Ubuntu 24.04, Python 3.12, torch 2.14.0+cpu):

pytest exir/emit/test/test_emit.py

70 passed, 2 skipped, as before the merge. With _emitter.py replaced by main's, the new test fails on the "NULL pointer access" message. lintrunner --skip MYPY reports no issues on the two files.

I also ran the reproducer from #21545 on CUDA (RTX 3060 under WSL), with the same package and torch replaced by the cu130 build of the same version (2.14.0+cu130). With main's _emitter.py it segfaults in _tensor_spec_to_evalue (exit 139). With this branch it raises InternalError: ... Constant tensor storage is on device 'cuda:0', ... (exit 1).

I used Claude Code to write this PR.

cc @JacobSzwejbka @angelayi @nil-is-all @Gasoonjia @digantdesai

AxelNoun and others added 2 commits October 27, 2026 16:56
_tensor_spec_to_evalue reads a constant's bytes by casting
spec.storage.data_ptr() to a host pointer. For CUDA storage that is a
device address, so to_executorch() segfaults with no Python exception
(pytorch#21545). Check the storage device first and raise an InternalError that
names the device and asks the user to move the constants to CPU.
Constants consumed by a delegate do not reach this check, so telling
every user to move the model to CPU is wrong for flows that delegate
to a device backend.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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/24202

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

⚠️ 16 Awaiting Approval

As of commit 64b7fb9 with merge base d6abe23 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Oct 27, 2026
@nil-is-all nil-is-all added module: exir Issues related to Export IR and the code under exir/ module: cuda Issues related to the AOTI CUDA backend labels Oct 27, 2026

This branch has not been deployed

No deployments
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. community: contribution PRs coming from community (excluding hardware partners) module: cuda Issues related to the AOTI CUDA backend module: exir Issues related to Export IR and the code under exir/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_executorch segfaults when constant tensors are in device memory

5 participants