提交 f359c0c4 authored 作者: Brendan Murphy's avatar Brendan Murphy 提交者: Ricardo Vieira

Changes due to new numpy scalar promotion rules

1. Changed autocaster due to new promotion rules With "weak promotion" of python types in Numpy 2.0, the statement `1.1 == np.asarray(1.1).astype('float32')` is True, whereas in Numpy 1.26, it was false. However, in numpy 1.26, `1.1 == np.asarray([1.1]).astype('float32')` was true, so the scalar behavior and array behavior are the same in Numpy 2.0, while they were different in numpy 1.26. Essentially, in Numpy 2.0, if python floats are used in operations with numpy floats or arrays, then the type of the numpy object will be used (i.e. the python value will be treated as the type of the numpy objects). To preserve the behavior of `NumpyAutocaster` from numpy <= 1.26, I've added an explicit conversion of the value to be converted to a numpy type using `np.asarray` during the check that decides what dtype to cast to. 2. Updates due to new numpy conversion rules for out-of-bounds python ints In numpy 2.0, out of bounds python ints will not be automatically converted, and will raise an `OverflowError` instead. For instance, converting 255 to int8 will raise an error, instead of returning -1. To explicitly force conversion, we must use `np.asarray(value).astype(dtype)`, rather than `np.asarray(value, dtype=dtype)`. The code in `TensorType.filter` has been changed to the new recommended way to downcast, and the error type caught by some tests has been changed to OverflowError from TypeError
上级 24efd1b1
......@@ -183,7 +183,9 @@ class NumpyAutocaster:
for dtype in try_dtypes:
x_ = np.asarray(x).astype(dtype=dtype)
if np.all(x == x_):
if np.all(
np.asarray(x) == x_
): # use np.asarray(x) to match TensorType.filter
break
# returns either an exact x_==x, or the last cast x_
return x_
......
......@@ -178,7 +178,7 @@ class TensorType(CType[np.ndarray], HasDataType, HasShape):
else:
if allow_downcast:
# Convert to self.dtype, regardless of the type of data
data = np.asarray(data, dtype=self.dtype)
data = np.asarray(data).astype(self.dtype)
# TODO: consider to pad shape with ones to make it consistent
# with self.broadcastable... like vector->row type thing
else:
......
......@@ -335,6 +335,7 @@ class TestPfunc:
h = pfunc([a, b, c], (a + b + c)) # Default: allow_input_downcast=None
# Everything here should behave like with False
assert np.all(h([3], [6], 0) == 9)
with pytest.raises(TypeError):
h([3], np.array([6], dtype="int16"), 0)
......
......@@ -3198,7 +3198,6 @@ def test_autocast_custom():
assert (dvector() + 1.1).dtype == "float64"
assert (fvector() + np.float32(1.1)).dtype == "float32"
assert (fvector() + np.float64(1.1)).dtype == "float64"
assert (fvector() + 1.1).dtype == config.floatX
assert (lvector() + np.int64(1)).dtype == "int64"
assert (lvector() + np.int32(1)).dtype == "int64"
assert (lvector() + np.int16(1)).dtype == "int64"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论