Reciprocal.impl computes np.float32(1.0) / x: float32 precision for float64 scalars under NEP 50 (Python linker)
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
- #2431 de @Ai-Eastern — cerrado sin fusionar
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 70/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- numpy, python
- Área
- ai-infra-agents, backend
Línea de trabajo
Examine la función scalar.basic.Reciprocal.impl en el código de PyTensor. El problema es que usa np.float32(1.0) / x, lo que bajo las reglas de promoción NEP 50 de NumPy 2 provoca pérdida de precisión para escalares float64. Cambie la implementación a 1.0 / x o np.reciprocal(x). Pruebe ejecutando el script de reproducción proporcionado con el enlazador de Python para verificar que la corrección elimina el error de 2.3e-8.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 654
- Forks
- 258
- Merge medio
- 2 d 7 h
- PR fusionados (30 d)
- 39
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de pymc-devs/pytensor
-
Docs of mode still mentions C/CVM as the default with FAST_RUNPosiblemente ocupada @ADITYASINGH1206 la tomó hace 8 días. Abiertodocs
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
pymc-devs/pytensor#2641 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Update conditions page in docsAbiertodocs
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
BUG: geomspace returns wrong sign for the endpoints when start and stop are negativePosiblemente ocupada @mayuriphad la tomó hace 27 días. Abierto
Dificultad 2/5 Menos de una hora Aptitud para principiantes 85/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
pymc-devs/pytensor#2428 · 5 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
Todos los issues de pymc-devs/pytensor
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
dgunning/edgartools#2019 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
topoteretes/cognee#6818 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
source_verification_agent: no outcome when the publisher confirms a work but its DOI resolves to a different recordPosiblemente ocupada @pinjun99 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Imbad0202/academic-research-skills#1159 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[Bug]: DoubleMLData.from_arrays checks the treatment d with force_all_x_finite instead of force_all_d_finitePosiblemente ocupada @leninworld la tomó hoy. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
DoubleML/doubleml-for-py#471 ·
Los mantenedores suelen responder en 1 día