Skip to content

Commit e45f588

Browse files
committed
BUG: keep dot's own 0-D path for DTypes without a dotfunc
`dot` has always handled a 0-D operand with `multiply`, for every dtype, after casting both operands to their common dtype. Diverting DTypes without a dotfunc into the fallback skipped that cast, which exposed `multiply`'s heterogeneous loops where `dot` never allowed them: np.dot(np.array('ab'), 3, out=np.empty((), 'U6')) returned 'ababab' instead of raising, and the same for bytes. Move the dotfunc check below the existing 0-D block so those operands stay on dot's own code path, and let the fallback handle only the matmul case. Checked against a build with the fallback compiled out over 8752 dot/vdot/inner cases (dtype x shape grids, mixed dtypes, out=, subclasses, Fortran and strided layouts, empty arrays): no case where one succeeds and the other raises, and no differing results.
1 parent 82e3bf3 commit e45f588

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎numpy/_core/numeric.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,9 @@ def _dot_fallback(a, b, out=None):
12351235
"'a.ndim >= 2 and b.ndim >= 3' for user-defined dtypes; "
12361236
"use 'numpy.tensordot' instead."
12371237
)
1238+
# ``dot`` itself handles 0-D operands with ``multiply`` before it ever
1239+
# reaches this fallback, so in practice only the ``matmul`` branch is
1240+
# used; the 0-D one is kept so the helper stands on its own.
12381241
op = np.multiply if a_ndim == 0 or b_ndim == 0 else np.matmul
12391242
try:
12401243
return op(a, b, out=out)

‎numpy/_core/src/multiarray/multiarraymodule.c‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,18 +1014,6 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out)
10141014
}
10151015
Py_SETREF(typec, NPY_DT_CALL_ensure_canonical(typec));
10161016

1017-
if (PyDataType_GetArrFuncs(typec)->dotfunc == NULL) {
1018-
Py_DECREF(typec);
1019-
if (npy_cache_import_runtime(
1020-
"numpy._core.numeric", "_dot_fallback",
1021-
&_npy_module_state->runtime_imports._dot_fallback) == -1) {
1022-
return NULL;
1023-
}
1024-
return PyObject_CallFunctionObjArgs(
1025-
_npy_module_state->runtime_imports._dot_fallback, op1, op2,
1026-
out != NULL ? (PyObject *)out : Py_None, NULL);
1027-
}
1028-
10291017
Py_INCREF(typec);
10301018
ap1 = (PyArrayObject *)PyArray_FromAny(op1, typec, 0, 0,
10311019
NPY_ARRAY_ALIGNED, NULL);
@@ -1057,6 +1045,29 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out)
10571045
Py_DECREF(ap2);
10581046
return mul_res;
10591047
}
1048+
1049+
if (PyDataType_GetArrFuncs(typec)->dotfunc == NULL) {
1050+
/*
1051+
* DTypes without a legacy dotfunc (i.e. all new-style user DTypes)
1052+
* are served by the matmul gufunc instead. This is deliberately
1053+
* checked *after* the 0-D case above: `dot` has always handled 0-D
1054+
* operands with `multiply` for every dtype, and it casts both to
1055+
* their common dtype first. Diverting them into the fallback would
1056+
* skip that cast and so expose `multiply`'s heterogeneous loops
1057+
* (e.g. string repetition) where `dot` never allowed them.
1058+
*/
1059+
Py_DECREF(ap1);
1060+
Py_DECREF(ap2);
1061+
Py_DECREF(typec);
1062+
if (npy_cache_import_runtime(
1063+
"numpy._core.numeric", "_dot_fallback",
1064+
&_npy_module_state->runtime_imports._dot_fallback) == -1) {
1065+
return NULL;
1066+
}
1067+
return PyObject_CallFunctionObjArgs(
1068+
_npy_module_state->runtime_imports._dot_fallback, op1, op2,
1069+
out != NULL ? (PyObject *)out : Py_None, NULL);
1070+
}
10601071
l = PyArray_DIMS(ap1)[PyArray_NDIM(ap1) - 1];
10611072
if (PyArray_NDIM(ap2) > 1) {
10621073
matchDim = PyArray_NDIM(ap2) - 2;

‎numpy/_core/tests/test_multiarray.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import numpy as np
3232
import numpy._core._multiarray_tests as _multiarray_tests
33+
from numpy._core._exceptions import _UFuncNoLoopError
3334
from numpy._core._rational_tests import rational, rational2
3435
from numpy._core.multiarray import _get_ndarray_c_version, dot
3536
from numpy._core.numeric import _dot_fallback, _vdot_fallback
@@ -8829,6 +8830,35 @@ def test_dot_fallback_unsupported_type(self):
88298830
assert_raises(ValueError, _vdot_fallback, s, s)
88308831
assert_raises(ValueError, np.vdot, s, s)
88318832

8833+
def test_dot_scalar_operands_bypass_fallback(self):
8834+
# `dot` handles a 0-D operand with `multiply` for every dtype, and
8835+
# casts both operands to their common dtype first. That must keep
8836+
# happening for dtypes without a dotfunc: routing them through the
8837+
# fallback would skip the cast and reach `multiply`'s heterogeneous
8838+
# loops, so `np.dot` would start repeating strings instead of
8839+
# raising. See the `out=` cases below.
8840+
s = np.array("ab")
8841+
assert_raises(_UFuncNoLoopError, np.dot, s, 3)
8842+
assert_raises(_UFuncNoLoopError, np.dot, 3, s)
8843+
assert_raises(_UFuncNoLoopError, np.dot, s, 3, np.empty((), "U6"))
8844+
assert_raises(_UFuncNoLoopError, np.dot, 3, s, np.empty((), "U6"))
8845+
8846+
b = np.array(b"ab")
8847+
assert_raises(_UFuncNoLoopError, np.dot, b, 3, np.empty((), "S6"))
8848+
8849+
# a dtype that does have a dotfunc behaves the same way, which is
8850+
# the point: the 0-D path is not special-cased per dtype
8851+
td = np.array(1, dtype="m8[s]")
8852+
assert_raises(_UFuncNoLoopError, np.dot, td, td)
8853+
8854+
def test_dot_fallback_unsupported_type_is_uniform(self):
8855+
# A dtype with no dotfunc and no matmul loop reports the same legacy
8856+
# ValueError whatever the shapes are.
8857+
s1, s2 = np.array(["a", "b"]), np.array([["a", "b"], ["c", "d"]])
8858+
for x, y in [(s1, s1), (s1, s2), (s2, s1), (s2, s2)]:
8859+
assert_raises(ValueError, _dot_fallback, x, y)
8860+
assert_raises(ValueError, np.dot, x, y)
8861+
88328862
def test_dot_fallback_noncontiguous_inputs(self):
88338863
a = np.arange(12, dtype=np.float64).reshape(4, 3)
88348864
b = np.arange(15, dtype=np.float64).reshape(3, 5)

0 commit comments

Comments
 (0)