pytorch / pytorch/executorch

to_executorch segfaults when constant tensors are in device memory

Open
#21,545 3 comments 0 reactions 1 assignee View on GitHub

@Gasoonjia is already working on this.

Since Aug 2, 2026.

bug module: cuda module: exir
Dominant language
Python
Stars
5k
Forks
1.2k
Avg merge
2d 10h
Merged PRs (30d)
581

Description

Serializing a program with CUDA parameters segfaults the emitter

to_executorch() crashes with a segmentation fault when a program's constant
tensors live in device memory. The emitter reads a constant's storage through a
host pointer cast, with no check of where that storage actually is:

# exir/emit/_emitter.py, _tensor_spec_to_evalue
buffer_data = bytes(
    ctypes.cast(
        typing.cast(torch.UntypedStorage, spec.storage).data_ptr(),
        ctypes.POINTER(spec_array_type),
    ).contents
)

data_ptr() on a CUDA storage is a device address, so ctypes.cast reads device
memory from the host.

Reproducer, on a machine with a CUDA build of torch:

import torch
from torch.export import export
from executorch.exir import to_edge_transform_and_lower

model = torch.nn.Sequential(torch.nn.Linear(4, 3), torch.nn.ReLU()).eval().cuda()
example = torch.ones(2, 4).cuda()
program = to_edge_transform_and_lower(export(model, (example,))).to_executorch()

The faulthandler traceback points straight at the cast:

Fatal Python error: Segmentation fault
Current thread ...:
  File ".../executorch/exir/emit/_emitter.py", line 460 in _tensor_spec_to_evalue

The same script with host parameters succeeds, which is the workaround: keep
parameters on the host and let the partitioner see the device only through the
example input.

Suggested fix: copy the storage to the host before serializing when it is not
already there, or fail with a clear message instead of reading device memory
from the host.

Found while running an end-to-end delegate export on Linux aarch64. Not specific
to any one backend, since the crash is in the shared serialization path.

cc @JacobSzwejbka @angelayi @Gasoonjia @digantdesai

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.