numpy 2.x scalar types np.float32 and others are no longer convertible
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 42/100
Hướng nghiên cứu
Bắt đầu với reproducer trong test_module.cpp và các ví dụ chuyển đổi scalar Python được liệt kê, sau đó lần theo các bộ chuyển đổi rvalue tích hợp sẵn của Boost.Python cho double, float, int và long, đặc biệt là các nhánh PyFloat_Check và PyLong_Check của chúng. Công việc được xem là hoàn tất khi reproducer chấp nhận các kiểu scalar NumPy được mô tả trong issue mà không yêu cầu chuyển đổi Python rõ ràng, đồng thời vẫn giữ nguyên hành vi hiện có đối với các giá trị tích hợp sẵn.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
In NumPy 2.0, scalar types such as np.float32, np.int32, and np.int64 no longer subclass Python's built-in float or int (per
NEP 51). Boost.Python's built-in rvalue converters for C++ double, float, int, and long use PyFloat_Check / PyLong_Check, which return false for these NumPy scalars. The result is ArgumentError: Python argument types did not match C++ signature for any Boost.Python-wrapped function that previously accepted NumPy scalars via implicit conversion.
np.float64 is unaffected because it still subclasses Python float.
Reproducing
Put the following in test_module.cpp:
#include <boost/python.hpp>
double take_double(double x) { return x; }
float take_float(float x) { return x; }
int take_int(int x) { return x; }
long take_long(long x) { return x; }
BOOST_PYTHON_MODULE(test_module)
{
using namespace boost::python;
def("take_double", take_double);
def("take_float", take_float);
def("take_int", take_int);
def("take_long", take_long);
}
Then do:
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh -b -p $PWD/mc3
source mc3/etc/profile.d/conda.sh
conda create -y -p $PWD/env -c conda-forge python=3.11 "numpy>=2.0" libboost-python-devel gxx_linux-64
conda activate $PWD/env
$CXX -shared -fPIC -o test_module.so test_module.cpp -Ienv/include/python3.11 -Ienv/include -Lenv/lib -lboost_python311 -Wl,-rpath,env/lib
python -c "import numpy as np; import test_module; test_module.take_double(np.float32(1.5))"
The python script at the bottom of the issue gives some more types that don't convert.
Workaround
Users can explicitly convert before passing to Boost.Python:
func(float(np.float32(1.5))) # works
func(int(np.int32(42))) # works
func(np.float32(1.5).item()) # also works
I have implemented this in a few places and I'm sure many others have as well. See https://github.com/cctbx/cctbx_project/issues/1084
Longer python reproducer
import numpy as np
import test_module
# Show which numpy scalars still subclass builtins (the root cause)
for np_type in [np.float64, np.float32, np.float16,
np.int64, np.int32, np.int16, np.int8]:
builtin = float if np.issubdtype(np_type, np.floating) else int
print("issubclass(%s, %s): %s" % (
np_type.__name__, builtin.__name__, issubclass(np_type, builtin)))
print()
scalar_values = [
np.float64(1.5),
np.float32(1.5),
np.float16(1.5),
np.int64(42),
np.int32(42),
np.int16(42),
np.int8(42),
np.uint64(42),
np.uint32(42),
]
functions = [
(test_module.take_double, "take_double"),
(test_module.take_float, "take_float"),
(test_module.take_int, "take_int"),
(test_module.take_long, "take_long"),
]
for func, func_name in functions:
for scalar in scalar_values:
try:
result = func(scalar)
print("Pass", func_name, type(scalar))
except Exception as e:
print("Fail", func_name, type(scalar))
- Ngôn ngữ chính
- C++
- Star
- 540
- Fork
- 224
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của boostorg/python
-
numpy::from_data calls vector::front and crashes (or has UB) when called with a scalar objectĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
-
Windows CI does not fail if one or more tests failCó thể đã có người làm @stephengtuggy đã nhận 148 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
-
BoostDetectToolset-1.90.0.cmake file not found in an include() call in boost_python-config.cmakeĐang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
-
Windows CI: vcpkg dependency caching not workingCó thể đã có người làm @stephengtuggy đã nhận 148 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
Tất cả issue của boostorg/python
Issue tương tự
-
bot-triaged module: correctness (silent) module: fft triage review
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
patrickjquinn/Marathon-Shell#84 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
sav-oss-org/ptusa_main#1610 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Command gate: one shared constant for the just-below-1e-9 MoveAxis stop epsilon (ZWO, Bisque)Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
open-astro/AlpacaBridge#2606 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Refactor force_torque_system integration test to use EcmScopeCó thể đã có người làm @patelshivani2283-lab đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày