Skip to content

Commit

Permalink
Migrate unary operations to pylibcudf (#14850)
Browse files Browse the repository at this point in the history
This PR migrates the unary operations in cuDF Python to pylibcudf.

Authors:
  - Vyas Ramasubramani (https://github.com/vyasr)
  - Bradley Dice (https://github.com/bdice)

Approvers:
  - Ashwin Srinath (https://github.com/shwina)

URL: #14850
  • Loading branch information
vyasr authored Feb 5, 2024
1 parent 9e9f2b9 commit fc83eff
Showing 14 changed files with 257 additions and 128 deletions.
1 change: 1 addition & 0 deletions docs/cudf/source/user_guide/api_docs/pylibcudf/index.rst
Original file line number Diff line number Diff line change
@@ -17,3 +17,4 @@ This page provides API documentation for pylibcudf.
scalar
table
types
unary
6 changes: 6 additions & 0 deletions docs/cudf/source/user_guide/api_docs/pylibcudf/unary.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=====
unary
=====

.. automodule:: cudf._lib.pylibcudf.unary
:members:
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
# the License.
# =============================================================================

set(cython_sources aggregation.pyx binaryop.pyx copying.pyx types.pyx)
set(cython_sources aggregation.pyx binaryop.pyx copying.pyx types.pyx unary.pyx)

set(linked_libraries cudf::cudf)

50 changes: 24 additions & 26 deletions python/cudf/cudf/_lib/cpp/unary.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

from libc.stdint cimport int32_t
from libcpp.memory cimport unique_ptr
@@ -7,34 +7,32 @@ from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.column.column_view cimport column_view
from cudf._lib.cpp.types cimport data_type

ctypedef int32_t underlying_type_t_unary_op


cdef extern from "cudf/unary.hpp" namespace "cudf" nogil:

ctypedef enum unary_operator:
SIN "cudf::unary_operator::SIN"
COS "cudf::unary_operator::COS"
TAN "cudf::unary_operator::TAN"
ARCSIN "cudf::unary_operator::ARCSIN"
ARCCOS "cudf::unary_operator::ARCCOS"
ARCTAN "cudf::unary_operator::ARCTAN"
SINH "cudf::unary_operator::SINH"
COSH "cudf::unary_operator::COSH"
TANH "cudf::unary_operator::TANH"
ARCSINH "cudf::unary_operator::ARCSINH"
ARCCOSH "cudf::unary_operator::ARCCOSH"
ARCTANH "cudf::unary_operator::ARCTANH"
EXP "cudf::unary_operator::EXP"
LOG "cudf::unary_operator::LOG"
SQRT "cudf::unary_operator::SQRT"
CBRT "cudf::unary_operator::CBRT"
CEIL "cudf::unary_operator::CEIL"
FLOOR "cudf::unary_operator::FLOOR"
ABS "cudf::unary_operator::ABS"
RINT "cudf::unary_operator::RINT"
BIT_INVERT "cudf::unary_operator::BIT_INVERT"
NOT "cudf::unary_operator::NOT"
cpdef enum class unary_operator(int32_t):
SIN
COS
TAN
ARCSIN
ARCCOS
ARCTAN
SINH
COSH
TANH
ARCSINH
ARCCOSH
ARCTANH
EXP
LOG
SQRT
CBRT
CEIL
FLOOR
ABS
RINT
BIT_INVERT
NOT

cdef extern unique_ptr[column] unary_operation(
column_view input,
Empty file.
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/pylibcudf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
# =============================================================================

set(cython_sources aggregation.pyx binaryop.pyx column.pyx copying.pyx gpumemoryview.pyx
groupby.pyx interop.pyx scalar.pyx table.pyx types.pyx utils.pyx
groupby.pyx interop.pyx scalar.pyx table.pyx types.pyx unary.pyx utils.pyx
)
set(linked_libraries cudf::cudf)
rapids_cython_create_modules(
3 changes: 2 additions & 1 deletion python/cudf/cudf/_lib/pylibcudf/__init__.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.

# TODO: Verify consistent usage of relative/absolute imports in pylibcudf.
from . cimport aggregation, binaryop, copying, groupby, interop
from . cimport aggregation, binaryop, copying, groupby, interop, unary
from .column cimport Column
from .gpumemoryview cimport gpumemoryview
from .scalar cimport Scalar
@@ -21,5 +21,6 @@ __all__ = [
"gpumemoryview",
"groupby",
"interop",
"unary",
"types",
]
3 changes: 2 additions & 1 deletion python/cudf/cudf/_lib/pylibcudf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.

from . import aggregation, binaryop, copying, groupby, interop
from . import aggregation, binaryop, copying, groupby, interop, unary
from .column import Column
from .gpumemoryview import gpumemoryview
from .scalar import Scalar
@@ -19,5 +19,6 @@
"gpumemoryview",
"groupby",
"interop",
"unary",
"types",
]
2 changes: 1 addition & 1 deletion python/cudf/cudf/_lib/pylibcudf/binaryop.pxd
Original file line number Diff line number Diff line change
@@ -10,5 +10,5 @@ cpdef Column binary_operation(
object lhs,
object rhs,
binary_operator op,
DataType data_type
DataType output_type
)
12 changes: 6 additions & 6 deletions python/cudf/cudf/_lib/pylibcudf/binaryop.pyx
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ cpdef Column binary_operation(
object lhs,
object rhs,
binary_operator op,
DataType data_type
DataType output_type
):
"""Perform a binary operation between a column and another column or scalar.
@@ -40,8 +40,8 @@ cpdef Column binary_operation(
The right hand side argument.
op : BinaryOperator
The operation to perform.
data_type : DataType
The output to use for the output.
output_type : DataType
The data type to use for the output.
Returns
-------
@@ -57,7 +57,7 @@ cpdef Column binary_operation(
(<Column> lhs).view(),
(<Column> rhs).view(),
op,
data_type.c_obj
output_type.c_obj
)
)
elif isinstance(lhs, Column) and isinstance(rhs, Scalar):
@@ -67,7 +67,7 @@ cpdef Column binary_operation(
(<Column> lhs).view(),
dereference((<Scalar> rhs).c_obj),
op,
data_type.c_obj
output_type.c_obj
)
)
elif isinstance(lhs, Scalar) and isinstance(rhs, Column):
@@ -77,7 +77,7 @@ cpdef Column binary_operation(
dereference((<Scalar> lhs).c_obj),
(<Column> rhs).view(),
op,
data_type.c_obj
output_type.c_obj
)
)
else:
19 changes: 19 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/unary.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

from cudf._lib.cpp.unary cimport unary_operator

from .column cimport Column
from .types cimport DataType


cpdef Column unary_operation(Column input, unary_operator op)

cpdef Column is_null(Column input)

cpdef Column is_valid(Column input)

cpdef Column cast(Column input, DataType data_type)

cpdef Column is_nan(Column input)

cpdef Column is_not_nan(Column input)
156 changes: 156 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/unary.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf._lib.cpp cimport unary as cpp_unary
from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.unary cimport unary_operator

from cudf._lib.cpp.unary import \
unary_operator as UnaryOperator # no-cython-lint

from .column cimport Column
from .types cimport DataType


cpdef Column unary_operation(Column input, unary_operator op):
"""Perform a unary operation on a column.
For details, see :cpp:func:`unary_operation`.
Parameters
----------
input : Column
The column to operate on.
op : UnaryOperator
The operation to perform.
Returns
-------
pylibcudf.Column
The result of the unary operation
"""
cdef unique_ptr[column] result

with nogil:
result = move(cpp_unary.unary_operation(input.view(), op))

return Column.from_libcudf(move(result))


cpdef Column is_null(Column input):
"""Check whether elements of a column are null.
For details, see :cpp:func:`is_null`.
Parameters
----------
input : Column
The column to check.
Returns
-------
pylibcudf.Column
A boolean column with ``True`` representing null values.
"""
cdef unique_ptr[column] result

with nogil:
result = move(cpp_unary.is_null(input.view()))

return Column.from_libcudf(move(result))


cpdef Column is_valid(Column input):
"""Check whether elements of a column are valid.
For details, see :cpp:func:`is_valid`.
Parameters
----------
input : Column
The column to check.
Returns
-------
pylibcudf.Column
A boolean column with ``True`` representing valid values.
"""
cdef unique_ptr[column] result

with nogil:
result = move(cpp_unary.is_valid(input.view()))

return Column.from_libcudf(move(result))


cpdef Column cast(Column input, DataType data_type):
"""Cast a column to a different data type.
For details, see :cpp:func:`cast`.
Parameters
----------
input : Column
The column to check.
data_type : DataType
The data type to cast to.
Returns
-------
pylibcudf.Column
A boolean column with ``True`` representing null values.
"""
cdef unique_ptr[column] result

with nogil:
result = move(cpp_unary.cast(input.view(), data_type.c_obj))

return Column.from_libcudf(move(result))


cpdef Column is_nan(Column input):
"""Check whether elements of a column are nan.
For details, see :cpp:func:`is_nan`.
Parameters
----------
input : Column
The column to check.
Returns
-------
pylibcudf.Column
A boolean column with ``True`` representing nan values.
"""
cdef unique_ptr[column] result

with nogil:
result = move(cpp_unary.is_nan(input.view()))

return Column.from_libcudf(move(result))


cpdef Column is_not_nan(Column input):
"""Check whether elements of a column are not nan.
For details, see :cpp:func:`is_not_nan`.
Parameters
----------
input : Column
The column to check.
Returns
-------
pylibcudf.Column
A boolean column with ``True`` representing non-nan values.
"""
cdef unique_ptr[column] result

with nogil:
result = move(cpp_unary.is_not_nan(input.view()))

return Column.from_libcudf(move(result))
Loading

0 comments on commit fc83eff

Please sign in to comment.