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

Aperta
#2,689 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
numpy, python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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")

Lingua principale
Python
Stelle
654
Fork
258
Merge medio
2g 7h
PR unite (30g)
39

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pymc-devs/pytensor

Tutte le issue di pymc-devs/pytensor

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.