-
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.
[FEA] Migrate nvtext/edit_distance APIs to pylibcudf (#16957)
Apart of #15162. This PR migrates `edit_distance.pxd` to pylibcudf Authors: - Matthew Murray (https://github.com/Matt711) Approvers: - Matthew Roeschke (https://github.com/mroeschke) - Yunsong Wang (https://github.com/PointKernel) - David Wendt (https://github.com/davidwendt) URL: #16957
- Loading branch information
Showing
14 changed files
with
171 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
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 |
---|---|---|
|
@@ -49,3 +49,4 @@ This page provides API documentation for pylibcudf. | |
|
||
io/index.rst | ||
strings/index.rst | ||
nvtext/index.rst |
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: |
7 changes: 7 additions & 0 deletions
7
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,7 @@ | ||
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)) |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def edit_distance_data(): | ||
arr1 = ["hallo", "goodbye", "world"] | ||
arr2 = ["hello", "", "world"] | ||
return pa.array(arr1), pa.array(arr2) | ||
|
||
|
||
def test_edit_distance(edit_distance_data): | ||
input_col, targets = edit_distance_data | ||
result = plc.nvtext.edit_distance.edit_distance( | ||
plc.interop.from_arrow(input_col), | ||
plc.interop.from_arrow(targets), | ||
) | ||
expected = pa.array([1, 7, 0], type=pa.int32()) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
def test_edit_distance_matrix(edit_distance_data): | ||
input_col, _ = edit_distance_data | ||
result = plc.nvtext.edit_distance.edit_distance_matrix( | ||
plc.interop.from_arrow(input_col) | ||
) | ||
expected = pa.array( | ||
[[0, 7, 4], [7, 0, 6], [4, 6, 0]], type=pa.list_(pa.int32()) | ||
) | ||
assert_column_eq(expected, result) |