pytorch - 💡(How to fix) Fix torch::stable::Device(const std::string&) linker error [3 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#180492Fetched 2026-04-17 08:22:02
View on GitHub
Comments
3
Participants
2
Timeline
32
Reactions
0
Author
Participants
Assignees
Timeline (top)
mentioned ×10subscribed ×10labeled ×7commented ×3

Error Message

/usr/bin/ld: stabledevice.o: in function make_device_broken(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': stabledevice.cpp:25: undefined reference to torch::stable::Device::Device(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' /usr/bin/ld: build/lib.linux-x86_64-cpython-312/stabledevice.cpython-312-x86_64-linux-gnu.so: hidden symbol `_ZN5torch6stable6DeviceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE' isn't defined /usr/bin/ld: final link failed: bad value collect2: error: ld returned 1 exit status

Code Example

/usr/bin/ld: stabledevice.o: in function `make_device_broken(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
stabledevice.cpp:25: undefined reference to `torch::stable::Device::Device(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: build/lib.linux-x86_64-cpython-312/stabledevice.cpython-312-x86_64-linux-gnu.so: hidden symbol `_ZN5torch6stable6DeviceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE' isn't defined
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

---

// stabledevice.cpp
#include <string>
#include <torch/csrc/stable/device_struct.h>
#include <torch/headeronly/core/DeviceType.h>

torch::stable::Device make_device_ok() {
  return torch::stable::Device(torch::headeronly::kCUDA, 0);
}

torch::stable::Device make_device_broken(const std::string &s) {
  return torch::stable::Device(s);
}

---

# setup.py
from setuptools import setup

from torch.utils.cpp_extension import BuildExtension, CUDAExtension, include_paths, library_paths

setup(
    name="stabledevice",
    ext_modules=[
        CUDAExtension(
            "stabledevice",
            ["stabledevice.cpp"],
            include_dirs=include_paths(),
            library_dirs=library_paths(),
            libraries=["torch", "torch_cpu", "cuda"],
        ),
    ],
    cmdclass={"build_ext": BuildExtension},
)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

I'm trying to port a Torch extension to stable ABI and running into a problem. The torch::stable::Device(const std::string&) constructor cannot be used from out-of-tree extensions.

/usr/bin/ld: stabledevice.o: in function `make_device_broken(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
stabledevice.cpp:25: undefined reference to `torch::stable::Device::Device(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: build/lib.linux-x86_64-cpython-312/stabledevice.cpython-312-x86_64-linux-gnu.so: hidden symbol `_ZN5torch6stable6DeviceC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE' isn't defined
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status

minimal reproducer

// stabledevice.cpp
#include <string>
#include <torch/csrc/stable/device_struct.h>
#include <torch/headeronly/core/DeviceType.h>

torch::stable::Device make_device_ok() {
  return torch::stable::Device(torch::headeronly::kCUDA, 0);
}

torch::stable::Device make_device_broken(const std::string &s) {
  return torch::stable::Device(s);
}
# setup.py
from setuptools import setup

from torch.utils.cpp_extension import BuildExtension, CUDAExtension, include_paths, library_paths

setup(
    name="stabledevice",
    ext_modules=[
        CUDAExtension(
            "stabledevice",
            ["stabledevice.cpp"],
            include_dirs=include_paths(),
            library_dirs=library_paths(),
            libraries=["torch", "torch_cpu", "cuda"],
        ),
    ],
    cmdclass={"build_ext": BuildExtension},
)

Torch implementation

https://github.com/pytorch/pytorch/blob/70d99e998b4955e0049d13a98d77ae1b14db1f45/torch/csrc/stable/device_inl.h#L25-L37

Versions

Torch 2.11.0 Python 3.12 Linux x86_64

cc @ezyang @gchanan @kadeng @msaroufim @janeyx99

extent analysis

TL;DR

The issue can be resolved by using the torch::stable::Device constructor that takes a torch::DeviceType and an index, instead of the constructor that takes a std::string.

Guidance

  • The error message indicates an undefined reference to the torch::stable::Device constructor that takes a std::string, suggesting that this constructor is not available for use in out-of-tree extensions.
  • The Torch implementation of torch::stable::Device provides a constructor that takes a torch::DeviceType and an index, which can be used instead of the constructor that takes a std::string.
  • To fix the issue, modify the make_device_broken function to use the available constructor, for example by passing torch::headeronly::kCUDA and an index.
  • Verify that the modified code compiles and links successfully.

Example

torch::stable::Device make_device_broken() {
  return torch::stable::Device(torch::headeronly::kCUDA, 0);
}

Notes

  • The issue is specific to Torch 2.11.0 and may not apply to other versions.
  • The availability of certain constructors may vary depending on the Torch version and the type of extension being built.

Recommendation

Apply workaround: The recommended solution is to modify the code to use the available constructor, as shown in the example above. This workaround allows the code to compile and link successfully, while avoiding the use of the unavailable constructor.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING