-
Notifications
You must be signed in to change notification settings - Fork 887
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
Add transpose API to pylibcudf #16749
Merged
rapids-bot
merged 15 commits into
rapidsai:branch-24.10
from
mroeschke:pylibcudf/transpose
Sep 25, 2024
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2a11f73
Add docs and initial implementation for tranpose
mroeschke 44a503f
Wrong cdef variable syntax
mroeschke 7b77ab4
Style add small test
mroeschke 28ef562
Parameterize on more cases
mroeschke 0ce3edf
Fix doc title
mroeschke e3b7a40
More unit tests, incoporate change into cudf._libs
mroeschke d01c4c1
Merge remote-tracking branch 'upstream/branch-24.10' into pylibcudf/t…
mroeschke 7f8484f
use pandas transpose as comparison
mroeschke 9c065aa
Have function just return Table
mroeschke 49a0680
Merge remote-tracking branch 'upstream/branch-24.10' into pylibcudf/t…
mroeschke 023da03
Pass through data_pointer_exposed, type owner_table
mroeschke fe0bcc3
Ensure exposed to passed to children and null_mask too
mroeschke 4c17577
typo
mroeschke 8d70df7
Add version skip
mroeschke 48a1e15
Merge branch 'branch-24.10' into pylibcudf/transpose
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
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ This page provides API documentation for pylibcudf. | |
table | ||
traits | ||
transform | ||
transpose | ||
types | ||
unary | ||
|
||
|
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 @@ | ||
========= | ||
transpose | ||
========= | ||
|
||
.. automodule:: pylibcudf.transpose | ||
: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 |
---|---|---|
@@ -1,32 +1,15 @@ | ||
# Copyright (c) 2020-2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.pair cimport pair | ||
from libcpp.utility cimport move | ||
|
||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.table.table_view cimport table_view | ||
from pylibcudf.libcudf.transpose cimport transpose as cpp_transpose | ||
import pylibcudf as plc | ||
|
||
from cudf._lib.column cimport Column | ||
from cudf._lib.utils cimport columns_from_table_view, table_view_from_columns | ||
|
||
|
||
def transpose(list source_columns): | ||
"""Transpose m n-row columns into n m-row columns | ||
""" | ||
cdef pair[unique_ptr[column], table_view] c_result | ||
cdef table_view c_input = table_view_from_columns(source_columns) | ||
|
||
with nogil: | ||
c_result = move(cpp_transpose(c_input)) | ||
|
||
# Notice, the data pointer of `result_owner` has been exposed | ||
# through `c_result.second` at this point. | ||
result_owner = Column.from_unique_ptr( | ||
move(c_result.first), data_ptr_exposed=True | ||
) | ||
return columns_from_table_view( | ||
c_result.second, | ||
owners=[result_owner] * c_result.second.num_columns() | ||
input_table = plc.table.Table( | ||
[col.to_pylibcudf(mode="read") for col in source_columns] | ||
) | ||
_, result_table = plc.transpose.transpose(input_table) | ||
return [Column.from_pylibcudf(col) for col in result_table.columns()] | ||
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 |
---|---|---|
|
@@ -43,6 +43,7 @@ set(cython_sources | |
table.pyx | ||
traits.pyx | ||
transform.pyx | ||
transpose.pyx | ||
types.pyx | ||
unary.pyx | ||
utils.pyx | ||
|
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,27 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pylibcudf as plc | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"arr", | ||
[ | ||
[], | ||
[1, 2, 3], | ||
[1, 2], | ||
[1], | ||
], | ||
) | ||
def test_transpose(arr): | ||
data = {"a": arr, "b": arr} | ||
arrow_tbl = pa.table(data) | ||
plc_tbl = plc.interop.from_arrow(arrow_tbl) | ||
_, plc_result = plc.transpose.transpose(plc_tbl) | ||
arrow_result = plc.interop.to_arrow(plc_result) | ||
if len(arr) == 0: | ||
expected = (0, 0) | ||
else: | ||
expected = (len(data), len(arr)) | ||
assert arrow_result.shape == expected | ||
Matt711 marked this conversation as resolved.
Show resolved
Hide resolved
|
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 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
from .table cimport Table | ||
|
||
|
||
cpdef tuple transpose(Table input_table) |
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,39 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
from libcpp.memory cimport unique_ptr | ||
from libcpp.pair cimport pair | ||
from libcpp.utility cimport move | ||
from pylibcudf.libcudf cimport transpose as cpp_transpose | ||
from pylibcudf.libcudf.column.column cimport column | ||
from pylibcudf.libcudf.table.table_view cimport table_view | ||
|
||
from .column cimport Column | ||
from .table cimport Table | ||
|
||
|
||
cpdef tuple transpose(Table input_table): | ||
"""Transpose a Table. | ||
|
||
For details, see :cpp:func:`transpose`. | ||
|
||
Parameters | ||
---------- | ||
input_table : Table | ||
Table to transpose | ||
|
||
Returns | ||
------- | ||
tuple[Column, Table] | ||
Two-tuple of the owner column and transposed table. | ||
""" | ||
cdef pair[unique_ptr[column], table_view] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_transpose.transpose(input_table.view())) | ||
|
||
owner_column = Column.from_libcudf(move(c_result.first)) | ||
owner_table = Table([owner_column] * c_result.second.num_columns()) | ||
|
||
return ( | ||
owner_column, | ||
Table.from_table_view(c_result.second, owner_table) | ||
) | ||
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.
@madsbk: can you remind me what it means that the
result_owner
is exposed through thetable
(c_result.second
).Is it that we have, now, two
Buffer
s that point to the same data, and therefore if we were to spill one, we would need to spill the other?I think this is right, and so yes, I think we do need (@mroeschke) to have a way of marking a column's data as exposed when we import it from pylibcudf.
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.
Yes
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.
There is a
data_ptr_exposed
keyword infrom_pylibcudf
that currently isn't implemented. I think we need to pass that parameter through to theexposed
keyword inas_buffer
?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.
Yes, sounds right
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.
It looks like this was addressed in #16760
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.
Yes, it looks like the necessary parameter was handled there so this PR should be safe to merge now.