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

Migrate string case operations to pylibcudf #15489

Merged
merged 7 commits into from
Apr 11, 2024
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
8 changes: 7 additions & 1 deletion python/cudf/cudf/_lib/cpp/strings/case.pxd
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Copyright (c) 2020, NVIDIA CORPORATION.
# Copyright (c) 2020-2024, NVIDIA CORPORATION.
from libcpp.memory cimport unique_ptr

from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.column.column_view cimport column_view


cdef extern from "cudf/strings/case.hpp" namespace "cudf::strings" nogil:
cdef unique_ptr[column] capitalize(
const column_view & input) except +

cdef unique_ptr[column] is_title(
const column_view & input) except +

cdef unique_ptr[column] to_lower(
const column_view & strings) except +

Expand Down
2 changes: 2 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ rapids_cython_create_modules(
LINKED_LIBRARIES "${linked_libraries}" MODULE_PREFIX pylibcudf_ ASSOCIATED_TARGETS cudf
)
link_to_pyarrow_headers(pylibcudf_interop)

add_subdirectory(strings)
2 changes: 2 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/__init__.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from . cimport (
search,
sorting,
stream_compaction,
strings,
types,
unary,
)
Expand Down Expand Up @@ -48,6 +49,7 @@ __all__ = [
"rolling",
"search",
"stream_compaction",
"strings",
"sorting",
"types",
"unary",
Expand Down
2 changes: 2 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
search,
sorting,
stream_compaction,
strings,
types,
unary,
)
Expand Down Expand Up @@ -48,6 +49,7 @@
"rolling",
"search",
"stream_compaction",
"strings",
"sorting",
"types",
"unary",
Expand Down
21 changes: 21 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# =============================================================================
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing permissions and limitations under
# the License.
# =============================================================================

set(cython_sources case.pyx)
set(linked_libraries cudf::cudf)
rapids_cython_create_modules(
CXX
SOURCE_FILES "${cython_sources}"
LINKED_LIBRARIES "${linked_libraries}" MODULE_PREFIX pylibcudf_ ASSOCIATED_TARGETS cudf
)
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
8 changes: 8 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/strings/case.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

from cudf._lib.pylibcudf.column cimport Column


cpdef Column to_lower(Column input)
cpdef Column to_upper(Column input)
cpdef Column swapcase(Column input)
30 changes: 30 additions & 0 deletions python/cudf/cudf/_lib/pylibcudf/strings/case.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

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

from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.strings cimport case as cpp_case
from cudf._lib.pylibcudf.column cimport Column


cpdef Column to_lower(Column input):
cdef unique_ptr[column] c_result
with nogil:
c_result = cpp_case.to_lower(input.view())

return Column.from_libcudf(move(c_result))

cpdef Column to_upper(Column input):
cdef unique_ptr[column] c_result
with nogil:
c_result = cpp_case.to_upper(input.view())

return Column.from_libcudf(move(c_result))

cpdef Column swapcase(Column input):
cdef unique_ptr[column] c_result
with nogil:
c_result = cpp_case.swapcase(input.view())

return Column.from_libcudf(move(c_result))
50 changes: 18 additions & 32 deletions python/cudf/cudf/_lib/strings/case.pyx
Original file line number Diff line number Diff line change
@@ -1,48 +1,34 @@
# Copyright (c) 2018-2022, NVIDIA CORPORATION.
# Copyright (c) 2018-2024, NVIDIA CORPORATION.

from cudf.core.buffer import acquire_spill_lock

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

from cudf._lib.column cimport Column
from cudf._lib.cpp.column.column cimport column
from cudf._lib.cpp.column.column_view cimport column_view
from cudf._lib.cpp.strings.case cimport (
swapcase as cpp_swapcase,
to_lower as cpp_to_lower,
to_upper as cpp_to_upper,
)

from cudf._lib.pylibcudf.strings import case


@acquire_spill_lock()
def to_upper(Column source_strings):
cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_to_upper(source_view))

return Column.from_unique_ptr(move(c_result))
return Column.from_pylibcudf(
case.to_upper(
source_strings.to_pylibcudf(mode='read')
)
)


@acquire_spill_lock()
def to_lower(Column source_strings):
cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_to_lower(source_view))

return Column.from_unique_ptr(move(c_result))
return Column.from_pylibcudf(
case.to_lower(
source_strings.to_pylibcudf(mode='read')
)
)


@acquire_spill_lock()
def swapcase(Column source_strings):
cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_swapcase(source_view))

return Column.from_unique_ptr(move(c_result))
return Column.from_pylibcudf(
case.swapcase(
source_strings.to_pylibcudf(mode='read')
)
)
60 changes: 60 additions & 0 deletions python/cudf/cudf/pylibcudf_tests/test_string_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

import pyarrow as pa
import pytest
from utils import assert_column_eq
vyasr marked this conversation as resolved.
Show resolved Hide resolved

import cudf._lib.pylibcudf as plc
from cudf._lib.pylibcudf.strings import case
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved


@pytest.fixture(scope="module")
def string_col():
return pa.array(
["AbC", "de", "FGHI", "j", "kLm", "nOPq", None, "RsT", None, "uVw"]
)


def wrap_nulls(func):
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
def wrapper(x):
if x is None:
return None
return func(x)

return wrapper


def test_to_upper(string_col):
plc_col = plc.interop.from_arrow(string_col)
got = case.to_upper(plc_col)

@wrap_nulls
def to_upper(x):
return x.upper()

expected = pa.Array.from_pandas(string_col.to_pandas().apply(to_upper))
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
assert_column_eq(got, expected)


def test_to_lower(string_col):
plc_col = plc.interop.from_arrow(string_col)
got = case.to_lower(plc_col)

@wrap_nulls
def to_lower(x):
return x.lower()

expected = pa.Array.from_pandas(string_col.to_pandas().apply(to_lower))
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
assert_column_eq(got, expected)


def test_swapcase(string_col):
plc_col = plc.interop.from_arrow(string_col)
got = case.swapcase(plc_col)

@wrap_nulls
def swapcase(x):
return x.swapcase()

expected = pa.Array.from_pandas(string_col.to_pandas().apply(swapcase))
brandon-b-miller marked this conversation as resolved.
Show resolved Hide resolved
assert_column_eq(got, expected)
Loading