Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added overload methods for Numpy's ufunc.reduce #7524

Closed
wants to merge 4 commits into from

Conversation

kc611
Copy link
Collaborator

@kc611 kc611 commented Nov 1, 2021

Reference an existing issue

Resolves #6313 #2751

This PR aims to implement the numpy.ufunc.METHOD as a callable inside njit-ed functions so that we can do stuff like:

import numba as nb
import numpy as np

@nb.njit
def add_twice(x, y):
    return 2*x + 2*y

custom_vectorize = nb.vectorize([], identity=None, target="cpu")
vec_add = custom_vectorize(add_twice)

@nb.njit
def test_fn(x, axis):
    return vec_add.reduce(x, axis) 

print(test_fn(np.ones((10, 20)), 0))

)

fn = cgutils.get_or_insert_function(
builder.module, fnty, "dufunc_reduce_direct"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The current issue I was facing is that this call cannot detect the implemented dufunc_reduce_direct method in C code. Is it a type issue ?

Copy link
Member

Choose a reason for hiding this comment

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

You need to add the symbol - try using llvmlite.binding.add_symbol('dufunc_reduce_direct', <addr of dufunc_reduce_direct>)

@@ -344,6 +344,29 @@ init_ufunc_dispatch(void)
return result;
}


NUMBA_EXPORT_FUNC(PyObject)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm also not too familiar with this C function declarations of Numba so I might be doing something wrong here.
(Looking at the failing build)

Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need to wrap the return type in NUMBA_EXPORT_FUNC (at least for now) - can you try without it?

Copy link
Member

Choose a reason for hiding this comment

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

Also, I think this should be returning a PyObject* since, that's what a PyCFunctionWithKeywords returns: https://docs.python.org/3/c-api/structures.html#c.PyCFunctionWithKeywords

@gmarkall gmarkall added this to the Numba 0.56 RC milestone Nov 1, 2021
kc611 and others added 2 commits November 4, 2021 16:42
…ded tests

Co-authored-by: Brandon Willard <brandonwillard@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request] Implement numpy.ufunc.METHOD
3 participants