Closed
Description
🐛 Describe the bug
Here is a simple file:
import torch
from torch.utils.cpp_extension import load_inline
cpp_source = """
#include <torch/extension.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
// NOTE: This is an internal utility.
namespace py = pybind11;
py::object tensor_to_numpy_func(torch::Tensor tensor) {
// Use internal PyTorch utility
return py::reinterpret_steal<py::object>(torch::utils::tensor_to_numpy(tensor));
}
"""
# Load the inline extension
tensor_utils = load_inline(
name="tensor_to_numpy_internal",
cpp_sources=cpp_source,
functions=["tensor_to_numpy_func"],
with_cuda=False,
verbose=True,
extra_cflags=["-O3"]
)
# Usage
if __name__ == "__main__":
tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0]], dtype=torch.float32)
numpy_array = tensor_utils.tensor_to_numpy_func(tensor)
print(numpy_array)
print(type(numpy_array))
It works in pytorch 2.6 (cuda build, installed by pip install torch==2.6
), but errors in pytorch 2.7 (cuda build, installed by pip install torch==2.7
).
the error is:
ImportError: tensor_to_numpy_internal.so: undefined symbol: _ZN5torch5utils15tensor_to_numpyERKN2at6TensorEb
And then I inspected the so file:
$ nm -D libtorch_python.so | grep tensor_to_numpy
# no output for pytorch 2.7
# has output for pytorch 2.6
0000000000dba6d0 T _ZN5torch5utils15tensor_to_numpyERKN2at6TensorEb
seems to be some build configuration bugs
Versions
linux x86 machine