`as_tensor_variable` / `constant` ignore `floatX` for lists and tuples of Python floats

Abierto
#2,689 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
numpy, python

Línea de trabajo

Start in pytensor/scalar/basic.py at ps.convert, then inspect the sequence branch and autocast_float to understand how floatX and cast_policy="numpy+floatX" apply. Run the scalar and core tensor tests under both floatX settings, as described in the issue. Done means lists and tuples of Python floats follow the configured policy while arrays and NumPy scalars keep their dtype; check the behavior for NumPy scalars inside sequences.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Description

With floatX="float32", a Python float scalar is autocast to floatX, but a list or tuple of Python floats is not. It comes out as float64.

import numpy as np
import pytensor
import pytensor.tensor as pt

pytensor.config.floatX = "float32"

pt.as_tensor_variable(1.0).dtype             # float32
pt.as_tensor_variable([1.0, 2.0]).dtype      # float64  <- surprising
pt.as_tensor_variable((1.0, 2.0)).dtype      # float64  <- surprising
pt.as_tensor_variable(np.array([1.0, 2.0])).dtype  # float64 (expected, array has its own dtype)
pt.as_tensor_variable([]).dtype              # float32 (empty list already uses floatX)

Cause

ps.convert (pytensor/scalar/basic.py) sends Python float through autocast_float, which respects floatX under the default cast_policy="numpy+floatX". Lists and tuples fall into the final else branch, which only calls np.asarray(x). NumPy infers float64 and nothing downcasts it. The only special case there is the empty one (x_.size == 0), which already uses floatX.

Why it matters

A list of Python floats has no dtype of its own, so it is just as untyped as a bare 1.0. The two paths disagree, and the list/tuple path can silently upcast a float32 graph to float64.

Related

#1073 and #1913 cover the same problem for scalars under the custom cast policy. This is the same gap for lists and tuples.

Proposed fix

Under cast_policy="numpy+floatX", have the sequence branch of ps.convert cast a float64 result to floatX when the input has no dtype attribute. Arrays and numpy scalars keep their dtype. A prototype passes the scalar and core tensor tests under both floatX settings. I haven't run the full suite.

Open question: should a list containing numpy float64 scalars, e.g. [np.float64(1.), 2.], also be downcast? The list has no .dtype, so the prototype would downcast it.

Workaround

pt.as_tensor_variable(x, dtype="floatX")

Lenguaje dominante
Python
Estrellas
654
Forks
258
Merge medio
2 d 7 h
PR fusionados (30 d)
39

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de pymc-devs/pytensor

Todos los issues de pymc-devs/pytensor

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.