Reciprocal.impl computes np.float32(1.0) / x: float32 precision for float64 scalars under NEP 50 (Python linker)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
- #2431 của @Ai-Eastern — đã đóng, không merge
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 70/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- numpy, python
- Lĩnh vực
- ai-infra-agents, backend
Hướng nghiên cứu
Xem xét hàm scalar.basic.Reciprocal.impl trong codebase PyTensor. Vấn đề là nó sử dụng np.float32(1.0) / x, điều này theo quy tắc thăng hạng NEP 50 của NumPy 2 sẽ gây mất độ chính xác cho các scalar float64. Thay đổi cách triển khai thành 1.0 / x hoặc np.reciprocal(x). Kiểm tra bằng cách chạy script tái tạo được cung cấp với trình liên kết Python để xác minh bản sửa lỗi loại bỏ lỗi 2.3e-8.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Summary
scalar.basic.Reciprocal.impl is np.float32(1.0) / x. Under NumPy 2's promotion rules (NEP 50) a NumPy float32 scalar divided by a Python float gives a float32, so the Python implementation of the reciprocal of a float64 scalar carries float32 precision. The C implementation and np.reciprocal (used for arrays through nfunc_spec) are exact, so this shows only where the scalar Python impl runs — the Python linker, and anywhere scalar constants are folded through impl.
Reproduce
pytensor 3.3.0, numpy 2.4.6, Python 3.12, macOS arm64.
import numpy as np, pytensor, pytensor.tensor as pt
a = pt.dscalar("a")
f = pytensor.function([a], [1 / a, pt.reciprocal(a), a ** -1.0, 2.0 / a], mode="FAST_RUN") # with PYTENSOR_FLAGS=linker=py
for name, v in zip(["1 / a", "reciprocal(a)", "a ** -1.0", "2.0 / a"], f(0.657)):
print(name, repr(float(v)), abs(float(v) - eval(name.replace("reciprocal(a)", "1 / a").replace("a", "0.657"))) / (1 / 0.657))
1 / a 1.522070050239563 2.3e-08
reciprocal(a) 1.522070050239563 2.3e-08
a ** -1.0 1.522070050239563 2.3e-08
2.0 / a 3.0441400304414 0.0
The first three are rewritten to Reciprocal (pytensor.dprint shows the single node) and come back 2.3e-8 off; 2.0 / a is a true_div and exact. Directly in NumPy:
>>> np.float32(1.0) / 0.657
np.float32(1.52207)
>>> np.float32(1.0) / np.float64(0.657)
np.float64(1.5220700152207)
Fix
return 1.0 / x (or np.reciprocal(x)) in Reciprocal.impl. np.float32(1.0) was presumably there to keep float32 inputs float32 under the old value-based casting; with NEP 50 it now downcasts Python floats instead.
Where it was noticed
pymc-marketing's BinomialAdstock (1 / alpha - 1) and the half-life parameterisation of its GeometricAdstock (0.5 ** (1.0 / halflife)), and any InverseGamma prior (beta / x), evaluated under the Python linker: the log density of a whole MMM is off by 6e-7 and its gradient by 4e-8 relative, against a float64 reference that NumPy and an independent implementation agree on.
- Ngôn ngữ chính
- Python
- Star
- 654
- Fork
- 258
- Merge trung bình
- 3 ngày 41 phút
- Pull request đã merge (30 ngày)
- 42
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
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 pymc-devs/pytensor
-
Docs of mode still mentions C/CVM as the default with FAST_RUNCó thể đã có người làm @ADITYASINGH1206 đã nhận 8 ngày trước. Đang mởdocs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
pymc-devs/pytensor#2641 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Update conditions page in docsĐang mởdocs
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
BUG: geomspace returns wrong sign for the endpoints when start and stop are negativeCó thể đã có người làm @mayuriphad đã nhận 27 ngày trước. Đang mở
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
pymc-devs/pytensor#2428 · 5 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của pymc-devs/pytensor
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
dgunning/edgartools#2019 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
topoteretes/cognee#6818 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
source_verification_agent: no outcome when the publisher confirms a work but its DOI resolves to a different recordCó thể đã có người làm @pinjun99 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Imbad0202/academic-research-skills#1159 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: DoubleMLData.from_arrays checks the treatment d with force_all_x_finite instead of force_all_d_finiteCó thể đã có người làm @leninworld đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
DoubleML/doubleml-for-py#471 ·
Maintainer thường phản hồi trong vòng 1 ngày