-
Notifications
You must be signed in to change notification settings - Fork 113
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
Add support for safely creating universal functions in Rust #400
base: main
Are you sure you want to change the base?
Conversation
@mhostetter Would you be interested and able to test this branch? Would the functionality available here suffice to handle your use case? EDIT: The |
5658032
to
2276b54
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks quite solid as an initial implementation! I'll review again after you complete the PR.
I can try, but I fear my knowledge is too limited to be effective. I've only done an example or two with Py03 and To summarize my goals in words... Write a NumPy ufunc in Rust. This function takes |
I think to test this branch, you would only need to change your numpy = "0.20" by numpy = { git = "https://github.com/PyO3/rust-numpy.git", branch = "ufunc" }
To my understanding, universal functions always take 1-dimensional vectors as inputs (so they have a chance of vectorizing the inner-most loop) and take their outputs explicitly. So if your modulus is basically fixed, you could inject by capturing it in the closure that defines your ufunc, e.g. let m = ...;
let add_mod_m = move |[x, y]: [ArrayView1<'_, u64>; 2], [z]: [ArrayViewMut1<'_, u64>; 1]| {
azip!((x in x, y in y, z in z) *z = (*x + *y) % m);
});
let add_mod_m = numpy::ufunc::from_func(py, CString::new("add_mod_m").unwrap(), numpy::ufunc::Identity::Zero, add_mod_m);
module.add("add_mod_m", add_mod_m).unwrap(); If you want to vary |
This does not support polymorphic universal functions, but the numbers of inputs and outputs are arbitrary.
Closes #399