-
Notifications
You must be signed in to change notification settings - Fork 915
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
Reduce memory usage of as_categorical_column #14138
Merged
rapids-bot
merged 1 commit into
rapidsai:branch-23.10
from
wence-:wence/fix/categorical-mem-usage
Sep 20, 2023
Merged
Reduce memory usage of as_categorical_column #14138
rapids-bot
merged 1 commit into
rapidsai:branch-23.10
from
wence-:wence/fix/categorical-mem-usage
Sep 20, 2023
Conversation
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
The main culprit is in the way the codes returned from _label_encoding were being ordered. We were generating an int64 column for the order, gathering through the left gather map, and then argsorting, before using that ordering as a gather map for the codes. We note that gather(y, with=argsort(x)) is equivalent to sort_by_key(y, with=x) so use that instead (avoiding an unnecessary gather). Furthermore we also note that gather([0..n), with=x) is just equivalent to x, so we can avoid a gather too. This reduces the peak memory footprint of categorifying a random column of 500_000_000 int32 values where there are 100 unique values from 24.75 GiB to 11.67 GiB.
galipremsagar
approved these changes
Sep 20, 2023
wence-
added
Performance
Performance related issue
improvement
Improvement / enhancement to an existing function
non-breaking
Non-breaking change
labels
Sep 20, 2023
bdice
approved these changes
Sep 20, 2023
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.
Great! Note that this is an example of the performance antipattern discussed in #13557.
shwina
approved these changes
Sep 20, 2023
/merge |
Is performance affected? |
Yes, but positively, I run: import time
import cupy as cp
import cudf
import rmm
rmm.reinitialize(pool_allocator=True)
rng = cp.random._generator.RandomState(seed=108)
for K in [2**4, 2**10, 2**12, 2**14, 2**16]:
for N in [1_000_000, 10_000_000, 100_000_000, 250_000_000]:
col = cudf.core.column.as_column(rng.choice(cp.arange(K, dtype="uint32"), size=N, replace=True))
start = time.time()
for _ in range((reps := 1_000_000_000 // N)):
y = col.astype("category", ordered=False)
del y
end = time.time()
del col Across column sizes and number of unique values, the new code is between 25 and 30% faster. |
Excellent! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
improvement
Improvement / enhancement to an existing function
non-breaking
Non-breaking change
Performance
Performance related issue
Python
Affects Python cuDF API.
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.
Description
The main culprit is in the way the codes returned from _label_encoding were being ordered. We were generating an int64 column for the order, gathering through the left gather map, and then argsorting, before using that ordering as a gather map for the codes.
We note that gather(y, with=argsort(x)) is equivalent to sort_by_key(y, with=x) so use that instead (avoiding an unnecessary gather). Furthermore we also note that gather([0..n), with=x) is just equivalent to x, so we can avoid a gather too.
This reduces the peak memory footprint of categorifying a random column of 500_000_000 int32 values where there are 100 unique values from 24.75 GiB to 11.67 GiB.
Test code
Before
After
Checklist