Skip to content
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

[ENH] Remove unnecessary as_frame conversions in cudf Column API #13565

Closed
Tracked by #14479
wence- opened this issue Jun 14, 2023 · 1 comment · Fixed by #14491
Closed
Tracked by #14479

[ENH] Remove unnecessary as_frame conversions in cudf Column API #13565

wence- opened this issue Jun 14, 2023 · 1 comment · Fixed by #14491
Labels
feature request New feature or request Performance Performance related issue

Comments

@wence-
Copy link
Contributor

wence- commented Jun 14, 2023

Is your feature request related to a problem? Please describe.

In a number of places in the implementation of Column methods, we obtain a result by first converting to a SingleColumnFrame, calling some method there (that unpacks the column, calls into libcudf, and then wraps the column back up in a frame), and then unpacking the frame back into a column.

For example, Column.argsort is implemented by calling into Frame._get_sorted_inds:

def argsort(
self, ascending: bool = True, na_position: str = "last"
) -> "cudf.core.column.NumericalColumn":
return self.as_frame()._get_sorted_inds(
ascending=ascending, na_position=na_position
)

But that is just a round-about way of calling libcudf.order_by:

def _get_sorted_inds(self, by=None, ascending=True, na_position="last"):
"""
Get the indices required to sort self according to the columns
specified in by.
"""
to_sort = [
*(
self
if by is None
else self._get_columns_by_label(list(by), downcast=False)
)._columns
]
# If given a scalar need to construct a sequence of length # of columns
if np.isscalar(ascending):
ascending = [ascending] * len(to_sort)
return libcudf.sort.order_by(to_sort, ascending, na_position)

Describe the solution you'd like

The Column API should not talk about frames at all, and call directly into libcudf, since we have all the information to hand.

For example, argsort could be implemented directly as a call to libcudf.sort.order_by.

diff --git a/python/cudf/cudf/core/column/column.py b/python/cudf/cudf/core/column/column.py
index 255ac2582a..db7aa9c3b4 100644
--- a/python/cudf/cudf/core/column/column.py
+++ b/python/cudf/cudf/core/column/column.py
@@ -1098,9 +1098,7 @@ class ColumnBase(Column, Serializable, BinaryOperand, Reducible):
     def argsort(
         self, ascending: bool = True, na_position: str = "last"
     ) -> "cudf.core.column.NumericalColumn":
-        return self.as_frame()._get_sorted_inds(
-            ascending=ascending, na_position=na_position
-        )
+        return libcudf.sort.order_by([self], [ascending], na_position)
 
     def __arrow_array__(self, type=None):
         raise TypeError(

Some minimal microbenchmarking suggests that this is ~30microseconds faster (around 1% of the runtime for a column with a million random floats on my hardware).

In [32]: x = cudf.core.column.as_column(np.random.uniform(size=1_0))

In [33]: %timeit x.argsort();
145 µs ± 82.1 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [34]: %timeit libcudf.sort.order_by([x], [True], "last")
117 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [35]: x = cudf.core.column.as_column(np.random.uniform(size=1_000))

In [36]: %timeit x.argsort();In [32]: x = cudf.core.column.as_column(np.random.uniform(size=1_0))

In [33]: %timeit x.argsort();
145 µs ± 82.1 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [34]: %timeit libcudf.sort.order_by([x], [True], "last")
117 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [35]: x = cudf.core.column.as_column(np.random.uniform(size=1_000))

In [36]: %timeit x.argsort();
192 µs ± 682 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [37]: %timeit libcudf.sort.order_by([x], [True], "last")
164 µs ± 360 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [38]: x = cudf.core.column.as_column(np.random.uniform(size=100_000))

In [39]: %timeit x.argsort();
418 µs ± 260 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [40]: %timeit libcudf.sort.order_by([x], [True], "last")
390 µs ± 247 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [41]: x = cudf.core.column.as_column(np.random.uniform(size=10_000_000))

In [42]: %timeit x.argsort();
40.3 ms ± 8.01 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [43]: %timeit libcudf.sort.order_by([x], [True], "last")
40.3 ms ± 11.8 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

192 µs ± 682 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [37]: %timeit libcudf.sort.order_by([x], [True], "last")
164 µs ± 360 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [38]: x = cudf.core.column.as_column(np.random.uniform(size=100_000))

In [39]: %timeit x.argsort();
418 µs ± 260 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [40]: %timeit libcudf.sort.order_by([x], [True], "last")
390 µs ± 247 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [41]: x = cudf.core.column.as_column(np.random.uniform(size=10_000_000))

In [42]: %timeit x.argsort();
40.3 ms ± 8.01 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [43]: %timeit libcudf.sort.order_by([x], [True], "last")
40.3 ms ± 11.8 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Describe alternatives you've considered

None

@wence- wence- added feature request New feature or request Performance Performance related issue labels Jun 14, 2023
@vyasr
Copy link
Contributor

vyasr commented Jun 21, 2023

Completely agree that we want to get rid of this. Removing ColumnBase.as_frame is mentioned somewhere in my refactoring doc but I guess it's one of the few remaining items that hadn't been converted into an issue. Thank you for documenting the perf implications so thoroughly!

copy-pr-bot bot pushed a commit that referenced this issue Nov 24, 2023
Previously a number of algorithms on Columns first converted to a
single column frame and called a frame-based algorithm (which calls
directly into libcudf using the column we first thought of). This is
unnecessary since we already have the column to hand when calling the
same algorithm at the column level. Moreover, in many cases where the
algorithm is a user-facing API, the frame-based approach does more
work (for example conversions and dtype matching).

By removing this round trip we reduce some (unnecessary) overhead, and
also make the memory footprint and behaviour of column-based methods
more transparent.

- Closes #13565
rapids-bot bot pushed a commit that referenced this issue Nov 24, 2023
Previously a number of algorithms on Columns first converted to a single column frame and called a frame-based algorithm (which calls directly into libcudf using the column we first thought of). This is unnecessary since we already have the column to hand when calling the same algorithm at the column level. Moreover, in many cases where the algorithm is a user-facing API, the frame-based approach does more work (for example conversions and dtype matching).

By removing this round trip we reduce some (unnecessary) overhead, and also make the memory footprint and behaviour of column-based methods more transparent.

- Closes #13565

Authors:
  - Lawrence Mitchell (https://github.com/wence-)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)
  - Bradley Dice (https://github.com/bdice)

URL: #14491
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request Performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants