-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces pylibcudf string `slice` APIs and migrates the cuDF cython to use them. Part of #15162 Authors: - https://github.com/brandon-b-miller - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Lawrence Mitchell (https://github.com/wence-) URL: #15988
- Loading branch information
1 parent
bfaddd3
commit e0b8ab0
Showing
10 changed files
with
270 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ strings | |
|
||
contains | ||
replace | ||
slice |
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/strings/slice.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
===== | ||
slice | ||
===== | ||
|
||
.. automodule:: cudf._lib.pylibcudf.strings.slice | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ from . cimport ( | |
regex_flags, | ||
regex_program, | ||
replace, | ||
slice, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ | |
regex_flags, | ||
regex_program, | ||
replace, | ||
slice, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from cudf._lib.pylibcudf.column cimport Column | ||
from cudf._lib.pylibcudf.scalar cimport Scalar | ||
|
||
ctypedef fused ColumnOrScalar: | ||
Column | ||
Scalar | ||
|
||
cpdef Column slice_strings( | ||
Column input, | ||
ColumnOrScalar start=*, | ||
ColumnOrScalar stop=*, | ||
Scalar step=* | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
|
||
from cudf._lib.pylibcudf.column cimport Column | ||
from cudf._lib.pylibcudf.libcudf.column.column cimport column | ||
from cudf._lib.pylibcudf.libcudf.scalar.scalar cimport numeric_scalar | ||
from cudf._lib.pylibcudf.libcudf.scalar.scalar_factories cimport ( | ||
make_fixed_width_scalar as cpp_make_fixed_width_scalar, | ||
) | ||
from cudf._lib.pylibcudf.libcudf.strings cimport substring as cpp_slice | ||
from cudf._lib.pylibcudf.libcudf.types cimport size_type | ||
from cudf._lib.pylibcudf.scalar cimport Scalar | ||
|
||
from cython.operator import dereference | ||
|
||
|
||
cpdef Column slice_strings( | ||
Column input, | ||
ColumnOrScalar start=None, | ||
ColumnOrScalar stop=None, | ||
Scalar step=None | ||
): | ||
"""Perform a slice operation on a strings column. | ||
``start`` and ``stop`` may be a | ||
:py:class:`~cudf._lib.pylibcudf.column.Column` or a | ||
:py:class:`~cudf._lib.pylibcudf.scalar.Scalar`. But ``step`` must be a | ||
:py:class:`~cudf._lib.pylibcudf.scalar.Scalar`. | ||
For details, see :cpp:func:`cudf::strings::slice_strings`. | ||
Parameters | ||
---------- | ||
input : Column | ||
Strings column for this operation | ||
start : Union[Column, Scalar] | ||
The start character position or positions. | ||
stop : Union[Column, Scalar] | ||
The end character position or positions | ||
step : Scalar | ||
Distance between input characters retrieved | ||
Returns | ||
------- | ||
pylibcudf.Column | ||
The result of the slice operation | ||
""" | ||
cdef unique_ptr[column] c_result | ||
cdef numeric_scalar[size_type]* cpp_start | ||
cdef numeric_scalar[size_type]* cpp_stop | ||
cdef numeric_scalar[size_type]* cpp_step | ||
|
||
if input is None: | ||
raise ValueError("input cannot be None") | ||
|
||
if ColumnOrScalar is Column: | ||
if step is not None: | ||
raise ValueError("Column-wise slice does not support step") | ||
|
||
if start is None or stop is None: | ||
raise ValueError( | ||
"start and stop must be provided for Column-wise slice" | ||
) | ||
|
||
with nogil: | ||
c_result = cpp_slice.slice_strings( | ||
input.view(), | ||
start.view(), | ||
stop.view() | ||
) | ||
|
||
elif ColumnOrScalar is Scalar: | ||
if start is None: | ||
start = Scalar.from_libcudf( | ||
cpp_make_fixed_width_scalar(0) | ||
) | ||
if stop is None: | ||
stop = Scalar.from_libcudf( | ||
cpp_make_fixed_width_scalar(0) | ||
) | ||
if step is None: | ||
step = Scalar.from_libcudf( | ||
cpp_make_fixed_width_scalar(1) | ||
) | ||
|
||
cpp_start = <numeric_scalar[size_type]*>start.c_obj.get() | ||
cpp_stop = <numeric_scalar[size_type]*>stop.c_obj.get() | ||
cpp_step = <numeric_scalar[size_type]*>step.c_obj.get() | ||
|
||
with nogil: | ||
c_result = cpp_slice.slice_strings( | ||
input.view(), | ||
dereference(cpp_start), | ||
dereference(cpp_stop), | ||
dereference(cpp_step) | ||
) | ||
else: | ||
raise ValueError("start, stop, and step must be either Column or Scalar") | ||
|
||
return Column.from_libcudf(move(c_result)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.