Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/upcoming_changes/9417.cuda.rst
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``.
5 changes: 5 additions & 0 deletions numba/cuda/cudadecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
register_numpy_ufunc,
trigonometric_functions,
comparison_functions,
math_operations,
bit_twiddling_functions)
from numba.core.typing.templates import (AttributeTemplate, ConcreteTemplate,
AbstractTemplate, CallableTemplate,
Expand Down Expand Up @@ -799,3 +800,7 @@ def resolve_local(self, mod):

for func in bit_twiddling_functions:
register_numpy_ufunc(func, register_global)

for func in ('log', 'log2', 'log10'):
if func in math_operations:

Copy link
Copy Markdown
Member

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:

Suggested change
for func in ('log', 'log2', 'log10'):
if func in math_operations:
for func in math_operations:
if func in ('log', 'log2', 'log10'):

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 if condition 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.

register_numpy_ufunc(func, register_global)
12 changes: 12 additions & 0 deletions numba/cuda/tests/cudapy/test_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I print the data that was generated in basic_ufunc_test and could see a few complex arrays there.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The ufunc tests in numba.tests.test_ufuncs don't check values in the complex domain, but the CUDA ones have extra inputs added:

# The basic ufunc test does not set up complex inputs, so we'll add
# some here for testing with CUDA.
self.inputs.extend([
(np.complex64(-0.5 - 0.5j), types.complex64),
(np.complex64(0.0), types.complex64),
(np.complex64(0.5 + 0.5j), types.complex64),
(np.complex128(-0.5 - 0.5j), types.complex128),
(np.complex128(0.0), types.complex128),
(np.complex128(0.5 + 0.5j), types.complex128),
(np.array([-0.5 - 0.5j, 0.0, 0.5 + 0.5j], dtype='c8'),
types.Array(types.complex64, 1, 'C')),
(np.array([-0.5 - 0.5j, 0.0, 0.5 + 0.5j], dtype='c16'),
types.Array(types.complex128, 1, 'C')),
])



if __name__ == '__main__':
unittest.main()
31 changes: 31 additions & 0 deletions numba/cuda/ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 math and cmath implementations, but am not sure how they will interact with CUDA math.

@guilhermeleobas guilhermeleobas Mar 13, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

np.log(complex_array) is implemented using a combination of three libdevice functions: hypotf, logf and atan2f:

...
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These libdevice functions are used because of the lowerings of math.log, math.hypot, and math.atan2 in mathimpl.py.

}

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