
Hi all,
I would like to expose more of the ufunc internals in the following PR:
https://github.com/numpy/numpy/pull/22422/
There are three new proposed functions. I hope the first one can be generally useful while the last two are very specific (and thus underscored), but will hopefully become useful e.g. for Numba or numexpr.
ufunc.resolve_dtypes(dtypes, *, signature=None, casting=None, reduction=False) -----------------------------------------------------------
Allows you to find out what dtypes NumPy's implementation will use without executing a ufunc. For the full docs, see:
https://output.circle-artifacts.com/output/job/c8f72dd5-f8fb-448c-8fd8-d6182...
Example from the docs:
>>> int32 = np.dtype("int32") >>> float32 = np.dtype("float32")
The typical ufunc call does not pass an output dtype. `np.add` has two inputs and one output, so leave the output as ``None`` (not provided):
>>> np.add.resolve_dtypes((int32, float32, None)) (dtype('float64'), dtype('float64'), dtype('float64'))
The loop found uses "float64" for all operands (including the output), the first input would be cast.
``resolve_dtypes`` supports "weak" handling for Python scalars by passing ``int``, ``float``, or ``complex``:
>>> np.add.resolve_dtypes((float32, float, None)) (dtype('float32'), dtype('float32'), dtype('float32'))
Where the Python ``float`` behaves samilar to a Python value ``0.0`` in a ufunc call. (See :ref:`NEP 50 <NEP50>` for details.)
ufunc._resolve_dtypes_and_context(dtypes, *, signature=None, casting=None, reduction=False) ------------------------------------------------------------- Identical to the above, but it additionally returns a "call_info" which allows access to the actual ufunc implmentation.
ufunc._get_loop(call_info, /, *, fixed_strides=None) ---------------------------------------------------- Second function that is passed the `call_info` from the previous one. Both would normally be called (this is because it is the way NumPy must do it internally and allows most flexibility).
After doing both calls, `call_info` can be used from C to directly access the C implementation. Flux in the C-API are expected (for now). But for example Numba already releases new versions when NumPy releases a new version.
Cheers,
Sebastian