Reputation: 11
I am attempting to pass a float16
ndarray from Python to C++ using pybind11. While the compilation succeeds, running the Python code results in an error regarding an undefined symbol. Below are the relevant code snippets and environment details.
Error Message:
check_dtype.cpython-310-x86_64-linux-gnu.so: undefined symbol: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE15_M_replace_coldEPcmPKcmm
(demangles as std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace_cold(char*, unsigned long, char const*, unsigned long, unsigned long)
)
C++ Code:
#include <iostream>
#include <stdfloat>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
void check_array(py::array_t<std::float16_t> arr) {
auto r = arr.unchecked<1>(); // Read-only access to NumPy array
for (ssize_t i = 0; i < r.shape(0); ++i) {
std::cout << "Element " << i << " has a size of " << sizeof(r(i)) << " bytes." << std::endl;
}
}
PYBIND11_MODULE(check_dtype, m) {
m.def("check_array", &check_array, "A function to check the byte size of array elements.");
}
Python Code:
import numpy as np
import check_dtype
a = np.array([1.0, 2.0, 3.0], dtype=np.float16)
check_dtype.check_array(a)
Environment:
Has anyone encountered this issue before or has insights on how to resolve the undefined symbol error? Any help would be greatly appreciated!
I attempted to pass a float16 ndarray from Python to a C++ extension module using pybind11. Given the successful compilation, I expected the program to run smoothly and print the size of each element in the array in bytes.
Upvotes: 1
Views: 74