Skip to content

ENH: add vecdot and longdouble matmul - #124

Open
SwayamInSync wants to merge 14 commits into
numpy:mainfrom
SwayamInSync:enh-vecdot-v1.1
Open

SwayamInSync wants to merge 14 commits into
numpy:mainfrom
SwayamInSync:enh-vecdot-v1.1

Conversation

@SwayamInSync

Copy link
Copy Markdown
Member

Adds a np.vecdot loop for both backends and makes np.matmul work for the longdouble backend, needed by NumPy's dot-family fallback for user DTypes.

  • quad_dot in matmul.cpp is the shared dot-product path: QuadBLAS for aligned SLEEF operands with BLAS-compatible strides, otherwise a strided loop (SLEEF fma, or a long double sum).
  • matmul uses the strided loop for the longdouble backend.
  • vecdot loops quad_dot over the outer dimensions; quad_add_dot_family_loop registers it so matvec/vecmat can reuse it.
  • One resolver for the dot family, with the elementwise binary ufuncs' backend rule: operands of different backends run in longdouble.
  • Errors inside loops acquire the GIL first, since gufunc loops can run without it (a failed buffer allocation in matmul used to segfault).
  • New tests/test_vecdot.py, longdouble and mixed-backend matmul tests, and an out-of-memory test.
  • "Linear Algebra" section in docs/api/functions.md.

Known limitation, to handle after the release: np.result_type picks SLEEF for differing backends, while the ufuncs pick longdouble. NumPy's np.dot/np.inner fallback casts to np.result_type before calling matmul, so np.dot of a longdouble array with a float64 or SLEEF array returns a SLEEF result rounded through float64, whereas np.matmul keeps longdouble precision.

@SwayamInSync

Copy link
Copy Markdown
Member Author

Requires #123 to be merged first to clear the CI issues here

@SwayamInSync SwayamInSync added enhancement New feature or request v1.1 labels Nov 24, 2026
Comment thread tests/test_dot.py
numpy_quaddtype.get_quadblas_version()
except NotImplementedError:
pytest.skip("without QuadBLAS the naive loop runs instead")
n = 2**23

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than trying to make a multi-terabyte allocation, you can use resource.RLIMIT_AS to limit the address space of a subprocess and use a much smaller allocation:

import os
import resource
from pathlib import Path

import numpy as np
import pytest
from numpy_quaddtype import QuadPrecDType

dtype = QuadPrecDType()
n = 2048
a = np.broadcast_to(np.ones((), dtype=dtype), (n, n))
x = np.ones(n, dtype=dtype)
out = np.empty_like(x)

pages = int(Path("/proc/self/statm").read_text().split()[0])
limit = pages * os.sysconf("SC_PAGE_SIZE") + 8 * 1024**2
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
if soft != resource.RLIM_INFINITY:
    limit = min(limit, soft)
resource.setrlimit(resource.RLIMIT_AS, (limit, hard))

with pytest.raises(MemoryError):
    np.matmul(a, x, out=out)

I recently added a similar helper for memory limit tests like this in NumPy if you want more examples.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the one nitpick about the tests, otherwise this looks good to me.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request v1.1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants