Repository navigation
Add np.log* bindings for CUDA #9417
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
Changes from 4 commits
605ac85
8af4864
bd9737b
898d018
fdc423f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Support math.log, math.log2 and math.log10 in CUDA | ||
| -------------------------------------------------- | ||
|
|
||
| CUDA target now supports ``np.log``, ``np.log2`` and ``np.log10``. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -260,6 +260,18 @@ def test_bitwise_not_ufunc(self): | |||||||||||||||||||||||||||||||||
| # when the second argument is a negative. See the comment in | ||||||||||||||||||||||||||||||||||
| # numba/tests/test_ufuncs.py for more details. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| ############################################################################ | ||||||||||||||||||||||||||||||||||
| # Mathematical Functions | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def test_log_ufunc(self): | ||||||||||||||||||||||||||||||||||
| self.basic_ufunc_test(np.log, kinds='cf') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def test_log2_ufunc(self): | ||||||||||||||||||||||||||||||||||
| self.basic_ufunc_test(np.log2, kinds='cf') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def test_log10_ufunc(self): | ||||||||||||||||||||||||||||||||||
| self.basic_ufunc_test(np.log10, kinds='cf') | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+266
to
+273
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these test functions will check values in the complex domain.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I print the data that was generated in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ufunc tests in numba/numba/cuda/tests/cudapy/test_ufuncs.py Lines 33 to 48 in 03f2722
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,15 @@ def np_binary_impl(fn, context, builder, sig, args): | |
| impl = get_binary_impl_for_fn_and_ty(fn, sig.args[0]) | ||
| return impl(context, builder, sig, args) | ||
|
|
||
| def np_real_log_impl(context, builder, sig, args): | ||
| return np_unary_impl(math.log, context, builder, sig, args) | ||
|
|
||
| def np_real_log2_impl(context, builder, sig, args): | ||
| return np_unary_impl(math.log2, context, builder, sig, args) | ||
|
|
||
| def np_real_log10_impl(context, builder, sig, args): | ||
| return np_unary_impl(math.log10, context, builder, sig, args) | ||
|
|
||
| def np_real_sin_impl(context, builder, sig, args): | ||
| return np_unary_impl(math.sin, context, builder, sig, args) | ||
|
|
||
|
|
@@ -628,4 +637,26 @@ def np_real_atanh_impl(context, builder, sig, args): | |
| 'qq->q': numbers.int_shr_impl, | ||
| 'QQ->Q': numbers.int_shr_impl, | ||
| } | ||
|
|
||
| db[np.log] = { | ||
| 'f->f': np_real_log_impl, | ||
| 'd->d': np_real_log_impl, | ||
| 'F->F': npyfuncs.np_complex_log_impl, | ||
| 'D->D': npyfuncs.np_complex_log_impl, | ||
|
Comment on lines
+644
to
+645
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what will happen with complex data types, I think the calls will resolve in terms of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
...
B0:
%".25" = call float @"__nv_hypotf"(float %"arg.x", float %"arg.y")
%".26" = call float @"__nv_logf"(float %".25")
%".27" = call float @"__nv_atan2f"(float %"arg.y", float %"arg.x")
store {float, float} zeroinitializer, {float, float}* %".28"
%".31" = getelementptr inbounds {float, float}, {float, float}* %".28", i32 0, i32 0
store float %".26", float* %".31"
%".33" = getelementptr inbounds {float, float}, {float, float}* %".28", i32 0, i32 1
store float %".27", float* %".33"
%".35" = load {float, float}, {float, float}* %".28"
%"extracted.real" = extractvalue {float, float} %".35", 0
%"extracted.imag" = extractvalue {float, float} %".35", 1
%".36" = insertvalue {float, float} undef, float %"extracted.real", 0
%".37" = insertvalue {float, float} %".36", float %"extracted.imag", 1
store {float, float} %".37", {float, float}* %".ret"
ret i32 0
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These libdevice functions are used because of the lowerings of |
||
| } | ||
|
|
||
| db[np.log2] = { | ||
| 'f->f': np_real_log2_impl, | ||
| 'd->d': np_real_log2_impl, | ||
| 'F->F': npyfuncs.np_complex_log2_impl, | ||
| 'D->D': npyfuncs.np_complex_log2_impl, | ||
| } | ||
|
|
||
| db[np.log10] = { | ||
| 'f->f': np_real_log10_impl, | ||
| 'd->d': np_real_log10_impl, | ||
| 'F->F': npyfuncs.np_complex_log10_impl, | ||
| 'D->D': npyfuncs.np_complex_log10_impl, | ||
| } | ||
|
|
||
| return db | ||
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.
A minor nit, but it feels like it would be more logical to interchange the list of supported operations and all the operations:
The code as-is takes a subset and checks for its existence in the complete set, which is always going to be true, whereas the suggested version iterates over the complete set and only registers implementations for the supported subset on CUDA. Eventually the complete set and the supported set should be the same and the
ifcondition could go away, and I think it would only take for someone to set down and write all the registrations for most or all of them to work on CUDA.It's up to you whether to make this change - I don't really mind either way.