-
Notifications
You must be signed in to change notification settings - Fork 917
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 nvtext generate_ngrams APIs to pylibcudf #17006
Merged
rapids-bot
merged 8 commits into
rapidsai:branch-24.12
from
Matt711:pylibcudf-nvtext-generate_ngrams
Oct 8, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a0e19f
Migrate nvtext generate_ngrams APIs to pylibcudf
Matt711 6e51b6a
Merge branch 'branch-24.12' into pylibcudf-nvtext-generate_ngrams
Matt711 1e0feaf
Fix typo
Matt711 c03a36d
Merge branch 'pylibcudf-nvtext-generate_ngrams' of github.com:Matt711…
Matt711 ff9fb42
remove explicit casts
Matt711 577a7fc
Merge branch 'branch-24.12' into pylibcudf-nvtext-generate_ngrams
Matt711 ecd9fc2
address review
Matt711 985bbff
address review
Matt711 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
docs/cudf/source/user_guide/api_docs/pylibcudf/nvtext/generate_ngrams.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 @@ | ||
=============== | ||
generate_ngrams | ||
=============== | ||
|
||
.. automodule:: pylibcudf.nvtext.generate_ngrams | ||
: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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ nvtext | |
:maxdepth: 1 | ||
|
||
edit_distance | ||
generate_ngrams |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . cimport edit_distance | ||
from . cimport edit_distance, generate_ngrams | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
"generate_ngrams", | ||
] |
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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from . import edit_distance | ||
from . import edit_distance, generate_ngrams | ||
|
||
__all__ = [ | ||
"edit_distance", | ||
"generate_ngrams", | ||
] |
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,12 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from pylibcudf.column cimport Column | ||
from pylibcudf.libcudf.types cimport size_type | ||
from pylibcudf.scalar cimport Scalar | ||
|
||
|
||
cpdef Column generate_ngrams(Column input, size_type ngrams, Scalar separator) | ||
|
||
cpdef Column generate_character_ngrams(Column input, size_type ngrams=*) | ||
|
||
cpdef Column hash_character_ngrams(Column input, size_type ngrams=*) |
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,111 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
from pylibcudf.column cimport Column | ||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.column.column_view cimport column_view | ||
from pylibcudf.libcudf.nvtext.generate_ngrams cimport ( | ||
generate_character_ngrams as cpp_generate_character_ngrams, | ||
generate_ngrams as cpp_generate_ngrams, | ||
hash_character_ngrams as cpp_hash_character_ngrams, | ||
) | ||
from pylibcudf.libcudf.scalar.scalar cimport string_scalar | ||
from pylibcudf.libcudf.types cimport size_type | ||
from pylibcudf.scalar cimport Scalar | ||
|
||
|
||
cpdef Column generate_ngrams(Column input, size_type ngrams, Scalar separator): | ||
""" | ||
Returns a single column of strings by generating ngrams from a strings column. | ||
|
||
For details, see :cpp:func:`generate_ngrams` | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
ngram : size_type | ||
The ngram number to generate | ||
separator : Scalar | ||
The string to use for separating ngram tokens | ||
|
||
Returns | ||
------- | ||
Column | ||
New strings columns of tokens | ||
""" | ||
cdef column_view c_strings = input.view() | ||
cdef const string_scalar* c_separator = <const string_scalar*>separator.c_obj.get() | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_generate_ngrams( | ||
c_strings, | ||
ngrams, | ||
c_separator[0] | ||
) | ||
) | ||
return Column.from_libcudf(move(c_result)) | ||
|
||
|
||
cpdef Column generate_character_ngrams(Column input, size_type ngrams = 2): | ||
""" | ||
Returns a lists column of ngrams of characters within each string. | ||
|
||
For details, see :cpp:func:`generate_character_ngrams` | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
ngram : size_type | ||
The ngram number to generate | ||
|
||
Returns | ||
------- | ||
Column | ||
Lists column of strings | ||
""" | ||
cdef column_view c_strings = input.view() | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_generate_character_ngrams( | ||
c_strings, | ||
ngrams, | ||
) | ||
) | ||
return Column.from_libcudf(move(c_result)) | ||
|
||
cpdef Column hash_character_ngrams(Column input, size_type ngrams = 2): | ||
""" | ||
Returns a lists column of hash values of the characters in each string | ||
|
||
For details, see :cpp:func:`hash_character_ngrams` | ||
|
||
Parameters | ||
---------- | ||
input : Column | ||
Input strings | ||
ngram : size_type | ||
The ngram number to generate | ||
|
||
Returns | ||
------- | ||
Column | ||
Lists column of hash values | ||
""" | ||
cdef column_view c_strings = input.view() | ||
cdef unique_ptr[column] c_result | ||
|
||
with nogil: | ||
c_result = move( | ||
cpp_hash_character_ngrams( | ||
c_strings, | ||
ngrams, | ||
) | ||
) | ||
return Column.from_libcudf(move(c_result)) |
55 changes: 55 additions & 0 deletions
55
python/pylibcudf/pylibcudf/tests/test_nvtext_generate_ngrams.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,55 @@ | ||
# 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 input_col(): | ||
arr = ["ab", "cde", "fgh"] | ||
return pa.array(arr) | ||
|
||
|
||
@pytest.mark.parametrize("ngram", [2, 3]) | ||
@pytest.mark.parametrize("sep", ["_", "**", ","]) | ||
def test_generate_ngrams(input_col, ngram, sep): | ||
result = plc.nvtext.generate_ngrams.generate_ngrams( | ||
plc.interop.from_arrow(input_col), | ||
ngram, | ||
plc.interop.from_arrow(pa.scalar(sep)), | ||
) | ||
expected = pa.array([f"ab{sep}cde", f"cde{sep}fgh"]) | ||
if ngram == 3: | ||
expected = pa.array([f"ab{sep}cde{sep}fgh"]) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("ngram", [2, 3]) | ||
def test_generate_character_ngrams(input_col, ngram): | ||
result = plc.nvtext.generate_ngrams.generate_character_ngrams( | ||
plc.interop.from_arrow(input_col), | ||
ngram, | ||
) | ||
expected = pa.array([["ab"], ["cd", "de"], ["fg", "gh"]]) | ||
if ngram == 3: | ||
expected = pa.array([[], ["cde"], ["fgh"]]) | ||
assert_column_eq(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("ngram", [2, 3]) | ||
def test_hash_character_ngrams(input_col, ngram): | ||
Matt711 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = plc.nvtext.generate_ngrams.hash_character_ngrams( | ||
plc.interop.from_arrow(input_col), | ||
ngram, | ||
) | ||
pa_result = plc.interop.to_arrow(result) | ||
if ngram == 2: | ||
assert len(pa_result[0]) == 1 | ||
assert len(pa_result[1]) == 2 | ||
assert len(pa_result[2]) == 2 | ||
else: | ||
assert len(pa_result[0]) == 0 | ||
assert len(pa_result[1]) == 1 | ||
assert len(pa_result[2]) == 1 | ||
Matt711 marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need these explicit casts. I believe Cython will implicitly perform the valid cast if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll try and build without them. I wonder--in general where you can get away with not explicitly casting?