Reciprocal.impl computes np.float32(1.0) / x: float32 precision for float64 scalars under NEP 50 (Python linker)

Đang mở Phù hợp với người mới
#2,430 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pymc-devs/pytensor

Tất cả issue của pymc-devs/pytensor

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.