Open
Description
Inner dots are computed as np.sum(a * b)
in many places. Consider replacing it with a @ b
. (See also the 3rd item in #169 (comment).)
Below is the output of ChatGPT:
===
- Under the hood:
a @ b
(ornp.dot(a, b)
) delegates to optimized BLAS libraries that implement fused multiply-add (FMA) operations, eliminating the temporary array and reducing rounding errors compared to separate multiply and sum steps. (https://stackoverflow.com/questions/18092984/numpy-difference-between-dota-b-and-ab-sum) - Performance overhead:
np.sum(a * b)
allocates an intermediate array and then sums it, incurring extra memory traffic and Python-level looping; by contrast, BLAS kernels stream data efficiently in optimized C code. (https://stackoverflow.com/questions/75556221/why-is-np-dot-so-much-faster-than-np-sum) - Numerical accuracy: BLAS libraries use hardware FMA instructions to fuse multiply and add into a single rounding operation, improving precision over two separate operations. (https://stackoverflow.com/questions/45200278/numpy-fusing-multiply-and-add-to-avoid-wasting-memory)
- Official recommendation: NumPy’s documentation advises using
np.dot(a, b)
or the@
operator (vianp.matmul
, per PEP 465) for dot products and matrix multiplication. (https://numpy.org/doc/stable/reference/generated/numpy.dot.html, https://numpy.org/doc/stable/reference/generated/numpy.matmul.html, https://peps.python.org/pep-0465/)
===
Metadata
Metadata
Assignees
Labels
No labels