Accessing numpy fft ufuncs from numba

I’ve just released a small package on pypi called numba-ufunc-hooks:

It may be of interest as it contains a strategy for accessing internal numpy ufunc code. It only contains overloads for np.fft.{r,i,ir}fft at present, so it’s not very comprehensive and probably will never aim to be as this would supporting full ufunc generality (promotion etc.).

Briefly, the strategy gain’s access to the internal function pointers of numpy ufuncs using a (minimal) ctypes struct mirroring the C PyUfuncObject struct. Then, the function is invoked from an intrinsic with the appropriate arguments. To support caching, the ctypes ufunc objects are stored in a global variable accessible to LLVM at process startup.

Related prior discussions:

2 Likes

A version/ABI guard around the mirrored PyUFuncObject layout would make this safer to distribute, with a clear fallback when NumPy’s internal fields do not match. It may also be worth documenting that the cached ctypes ufunc objects need initialization before cached compiled functions are loaded, so cache reuse across processes does not retain stale pointers.