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

オープン
#2,689 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
numpy, python

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Python
スター
654
フォーク
258
平均マージ
2日 7時間
マージ済み PR(30日)
39

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pymc-devs/pytensor のほかの issue

pymc-devs/pytensor の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。