-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Migrate nvtext/edit_distance APIs to pylibcudf
- Loading branch information
Showing
13 changed files
with
161 additions
and
25 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
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/nvtext/edit_distance.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 @@ | ||
============= | ||
edit_distance | ||
============= | ||
|
||
.. automodule:: pylibcudf.nvtext.edit_distance | ||
:members: |
5 changes: 5 additions & 0 deletions
5
docs/cudf/source/user_guide/api_docs/pylibcudf/nvtext/index.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,5 @@ | ||
nvtext | ||
====== | ||
.. toctree:: | ||
:maxdepth: 1 | ||
edit_distance |
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# ============================================================================= | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
# | ||
# 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 edit_distance.pyx) | ||
|
||
set(linked_libraries cudf::cudf) | ||
rapids_cython_create_modules( | ||
CXX | ||
SOURCE_FILES "${cython_sources}" | ||
LINKED_LIBRARIES "${linked_libraries}" MODULE_PREFIX pylibcudf_nvtext_ ASSOCIATED_TARGETS cudf | ||
) |
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,7 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . cimport edit_distance | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
] |
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,7 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . import edit_distance | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
] |
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,8 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
|
||
|
||
cpdef Column edit_distance(Column input, Column targets) | ||
|
||
cpdef Column edit_distance_matrix(Column input) |
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,63 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.column.column_view cimport column_view | ||
from pylibcudf.libcudf.nvtext.edit_distance cimport ( | ||
edit_distance as cpp_edit_distance, | ||
edit_distance_matrix as cpp_edit_distance_matrix, | ||
) | ||
|
||
|
||
cpdef Column edit_distance(Column input, Column targets): | ||
""" | ||
Returns the edit distance between individual strings in two strings columns | ||
For details, see :cpp:func:`edit_distance` | ||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
targets : Column | ||
Strings to compute edit distance against | ||
Returns | ||
------- | ||
Column | ||
New column of edit distance values | ||
""" | ||
cdef column_view c_strings = input.view() | ||
cdef column_view c_targets = targets.view() | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_edit_distance(c_strings, c_targets)) | ||
|
||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column edit_distance_matrix(Column input): | ||
""" | ||
Returns the edit distance between all strings in the input strings column | ||
For details, see :cpp:func:`edit_distance_matrix` | ||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
Returns | ||
------- | ||
Column | ||
New column of edit distance values | ||
""" | ||
cdef column_view c_strings = input.view() | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_edit_distance_matrix(c_strings)) | ||
|
||
return Column.from_libcudf(move(c_result)) |
27 changes: 27 additions & 0 deletions
27
python/pylibcudf/pylibcudf/tests/test_nvtext_edit_distance.py
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,27 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
from utils import assert_column_eq | ||
|
||
|
||
def test_edit_distance(): | ||
input_arr = pa.array(["hallo", "goodbye", "world"]) | ||
targets = pa.array(["hello", "", "world"]) | ||
result = plc.nvtext.edit_distance.edit_distance( | ||
plc.interop.from_arrow(input_arr), | ||
plc.interop.from_arrow(targets), | ||
) | ||
expected = pa.array([1, 7, 0], type=pa.int32()) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_edit_distance_matrix(): | ||
input_arr = pa.array(["hallo", "goodbye", "world"]) | ||
result = plc.nvtext.edit_distance.edit_distance_matrix( | ||
plc.interop.from_arrow(input_arr), | ||
) | ||
expected = pa.array( | ||
[[0, 7, 4], [7, 0, 6], [4, 6, 0]], type=pa.list_(pa.int32()) | ||
) | ||
assert_column_eq(expected, result) |