Skip to content

Commit a5145db

Browse files
committed
MAINT: drop the unreachable multiply branch from the dot fallback
1 parent 7eee2fb commit a5145db

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

‎numpy/_core/numeric.py‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,25 +1233,18 @@ def tensordot(a, b, axes=2):
12331233

12341234

12351235
def _dot_fallback(a, b, out=None):
1236-
"""``dot`` via ``matmul``/``multiply`` for dtypes without a legacy dotfunc."""
1237-
# Do not cast the operands here -- ``multiply``/``matmul`` do that anyway,
1238-
# and leaving them untouched keeps ndarray subclasses (and other
1239-
# array-likes such as dask arrays with custom dtypes) intact.
1240-
a_ndim = np.ndim(a)
1241-
b_ndim = np.ndim(b)
1242-
1243-
if a_ndim >= 2 and b_ndim >= 3:
1236+
"""``dot`` via ``matmul`` for dtypes without a legacy dotfunc."""
1237+
# Not cast here: ``matmul`` casts anyway, and leaving the operands alone
1238+
# keeps subclasses (and array-likes such as dask arrays) intact. ``dot``
1239+
# handles 0-D itself, so only ``matmul`` is needed.
1240+
if np.ndim(a) >= 2 and np.ndim(b) >= 3:
12441241
raise ValueError(
12451242
"'dot' does not support the stacked outer-product semantics of "
12461243
"'a.ndim >= 2 and b.ndim >= 3' for user-defined dtypes; "
12471244
"use 'numpy.tensordot' instead."
12481245
)
1249-
# ``dot`` itself handles 0-D operands with ``multiply`` before it ever
1250-
# reaches this fallback, so in practice only the ``matmul`` branch is
1251-
# used; the 0-D one is kept so the helper stands on its own.
1252-
op = np.multiply if a_ndim == 0 or b_ndim == 0 else np.matmul
12531246
try:
1254-
return op(a, b, out=out)
1247+
return np.matmul(a, b, out=out)
12551248
except _UFuncNoLoopError:
12561249
# Keep the error the same as it was before we had support for dot
12571250
# for user types.

‎numpy/_core/tests/test_multiarray.py‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8918,7 +8918,7 @@ class TestDotFamilyFallback:
89188918
# when available, end-to-end through a real new-style DType (quaddtype).
89198919

89208920
@pytest.mark.parametrize("sa,sb", [
8921-
((), ()), ((), (3, 4)), ((4, 3), ()), ((5,), (5,)), ((3,), (3, 4)),
8921+
((5,), (5,)), ((3,), (3, 4)),
89228922
((4, 3), (3,)), ((4, 3), (3, 5)), ((2, 3, 4), (4,)),
89238923
((2, 3, 4), (4, 5)), ((4,), (2, 4, 5)),
89248924
((0, 3), (3, 4)), ((4, 0), (0, 5)),
@@ -8943,6 +8943,14 @@ def test_dot_fallback_rejects_stacked(self, sa, sb):
89438943
with assert_raises_regex(ValueError, "tensordot"):
89448944
_dot_fallback(a, b)
89458945

8946+
def test_dot_fallback_rejects_scalar_operands(self):
8947+
# matmul has no 0-D form; `dot` keeps 0-D on its own multiply path.
8948+
a = np.arange(6, dtype=np.float64).reshape(2, 3)
8949+
for x, y in [(np.array(3.0), a), (a, np.array(3.0)),
8950+
(np.array(3.0), np.array(4.0))]:
8951+
with assert_raises_regex(ValueError, "enough dimensions"):
8952+
_dot_fallback(x, y)
8953+
89468954
def test_dot_fallback_unsupported_type(self):
89478955
s = np.array(["a", "b", "c"])
89488956
assert_raises(ValueError, _dot_fallback, s, s)
@@ -8951,12 +8959,10 @@ def test_dot_fallback_unsupported_type(self):
89518959
assert_raises(ValueError, np.vdot, s, s)
89528960

89538961
def test_dot_scalar_operands_bypass_fallback(self):
8954-
# `dot` handles a 0-D operand with `multiply` for every dtype, and
8955-
# casts both operands to their common dtype first. That must keep
8956-
# happening for dtypes without a dotfunc: routing them through the
8957-
# fallback would skip the cast and reach `multiply`'s heterogeneous
8958-
# loops, so `np.dot` would start repeating strings instead of
8959-
# raising. See the `out=` cases below.
8962+
# `dot` handles 0-D with `multiply`, casting to the common dtype
8963+
# first. That must keep happening for dtypes without a dotfunc: the
8964+
# errors below come from `multiply`, whereas the fallback would raise
8965+
# a plain ValueError -- which is what proves 0-D never reaches it.
89608966
s = np.array("ab")
89618967
assert_raises(_UFuncNoLoopError, np.dot, s, 3)
89628968
assert_raises(_UFuncNoLoopError, np.dot, 3, s)
@@ -8983,7 +8989,6 @@ def test_dot_fallback_noncontiguous_inputs(self):
89838989
a = np.arange(12, dtype=np.float64).reshape(4, 3)
89848990
b = np.arange(15, dtype=np.float64).reshape(3, 5)
89858991
inputs = [
8986-
(np.array(3.0), np.asfortranarray(a)),
89878992
(np.asfortranarray(a), np.asfortranarray(b)),
89888993
(np.arange(24.).reshape(4, 6)[:, ::2],
89898994
np.arange(30.).reshape(6, 5)[::2]),
@@ -8992,8 +8997,7 @@ def test_dot_fallback_noncontiguous_inputs(self):
89928997
ref = np.dot(x, y)
89938998
got = _dot_fallback(x, y)
89948999
assert_array_equal(got, ref, strict=True)
8995-
if x.ndim and y.ndim:
8996-
assert got.flags["C_CONTIGUOUS"]
9000+
assert got.flags["C_CONTIGUOUS"]
89979001

89989002
def test_dot_fallback_does_not_conjugate(self):
89999003
# dot (unlike vdot) must not conjugate, even for complex input
@@ -9051,6 +9055,13 @@ def f(arr):
90519055
assert np.ndim(scalar) == 0
90529056
assert float(scalar) == np.dot(a, b)
90539057

9058+
# 0-D stays on dot's own multiply path, for user dtypes too
9059+
assert_array_equal(f(np.dot(q(3.0), q(A))), np.dot(3.0, A))
9060+
assert_array_equal(f(np.dot(q(A), q(3.0))), np.dot(A, 3.0))
9061+
scalar_out = np.empty(A.shape, dtype=qd)
9062+
assert np.dot(q(3.0), q(A), scalar_out) is scalar_out
9063+
assert_array_equal(f(scalar_out), np.dot(3.0, A))
9064+
90549065
assert_array_equal(f(np.dot(q(A), q(B))), np.dot(A, B))
90559066
assert_array_equal(f(np.inner(q(A), q(A))), np.inner(A, A))
90569067
assert float(np.vdot(q(a), q(b))) == np.vdot(a, b)

0 commit comments

Comments
 (0)