-
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
Allow setting malloc heap size in string udfs #12094
Allow setting malloc heap size in string udfs #12094
Conversation
Codecov ReportBase: 87.47% // Head: 88.22% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## branch-22.12 #12094 +/- ##
================================================
+ Coverage 87.47% 88.22% +0.75%
================================================
Files 133 137 +4
Lines 21826 22571 +745
================================================
+ Hits 19093 19914 +821
+ Misses 2733 2657 -76
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
@@ -87,6 +88,26 @@ def _get_ptx_file(): | |||
return regular_result[1] | |||
|
|||
|
|||
default_heap_size = int(2e6) |
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 feel like this needs a big comment on why this is the default. This number seems a bit high considering 10M rows of 100 bytes each would only be 1GB. I suppose it makes sense since it is the largest strings column in bytes possible.
Would it be too strange to make this configurable through an environment variable perhaps?
At least for this release until we can come up with a good metric for setting it.
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 introduced an environment variable to control this. I'm still open to discussion here as to what to set the value as. Indeed I did base it on the size of a strings column.
Co-authored-by: Bradley Dice <[email protected]>
… into fea-stringudf-heap
@@ -87,6 +88,29 @@ def _get_ptx_file(): | |||
return regular_result[1] | |||
|
|||
|
|||
# Maximum size of a string column is 2gb | |||
STRINGS_UDF_DEFAULT_HEAP_SIZE = 2**31 |
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.
Should these names be module-scope public or private?
heap_size = 0 | ||
|
||
|
||
def set_malloc_heap_size(size=STRINGS_UDF_HEAP_SIZE): |
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.
Why should this have a default, rather than reading the env var when it is lazily allocated?
I’m not convinced STRINGS_UDF_HEAP_SIZE
needs to be defined.
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.
The pool is very small if unset - something like 8mb I believe. Perhaps it'd be better if it said something like _STRINGS_UDF_INITIAL_HEAP_SIZE
?
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 @bdice is just asking why there's a default function parameter at all if this is set by an environment variable. This sort of relates to the other question about why we call this lazily rather than at initialization. If it needs to be done lazily for a good reason, then perhaps a better alternative would be to have the default size=None
and read the environment variable inside the function if size==None
instead of storing it in a module-scope function. That shouldn't lead to repeated reads since after the first invocation with the current state of the code heap_size
will always be equal to size
so it won't go into the conditional.
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.
The lazy initialization is kind of a placeholder in case we have an algorithm that can help guess the size based on the input size for example. I cannot speak to the env var access since I'm not familiar with the scoping here as it relates to udf kernel launches. Ideally if it could be set before each kernel launch? Though I understand the env var may not have changed.
Also, it may not be possible to make CUDA API calls during initialization (before the CUDA context is created) as we have found before with other CUDA calls.
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.
+1 on what @davidwendt mentioned about initialization. For the logic discussion, what do you think of the changes in af7cd9f ?
@@ -47,7 +47,14 @@ | |||
utils.JIT_SUPPORTED_TYPES |= STRING_TYPES | |||
_supported_masked_types |= {string_view, udf_string} | |||
|
|||
utils.launch_arg_getters[cudf_str_dtype] = column_to_string_view_array | |||
def column_to_string_view_array_init_heap(col): | |||
# lazily allocate heap only when a string needs to be returned |
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.
The structure of this code makes it look like idempotency is its main goal, not laziness.
strings_udf.set_malloc_heap_size()
should only ever be called once, I think?
Maybe this function or module in cudf (and not strings_udf) should own the “is it allocated already?” logic via a cache or global of some kind, and then the strings_udf logic doesn’t need to read env vars for the allocation size, etc.
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.
This is not an allocation. There is currently a malloc-heap-size by default (set to about 8MB on the GPUs I've tested on). This updates the heap size to a new value so "already allocated" is not the goal.
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.
Is set_malloc_heap_size
intended to be a public API that can be called by a user, or is it an internal function? It's not entirely clear what the public API is here because this file doesn't define an __all__
. If the function is just for internal use, why does it need to be called every time someone accesses a string column rather than just calling cudaDeviceSetLimit
once when the package is imported (or after we can guarantee that the CUDA context/etc is initialized if that is the requirement that we're currently addressing by doing this "lazily")?
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 what we want to happen is:
- The first time a user runs a string_udf, set the heap size to either default or what is retrieved from their environment.
- The next time a user runs a string_udf, do not set the heap size. Or at least early return.
- Reset the heap the next time the user runs a string_udf iff the user sets the value of
STRING_UDF_HEAP_SIZE
to something else during the session.
Does this seems reasonable?
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.
Seems we have 2 comment chains asking about the same thing.
#12094 (comment)
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.
- The first time a user runs a string_udf, set the heap size to either default or what is retrieved from their environment.
Yes.
- The next time a user runs a string_udf, do not set the heap size. Or at least early return.
The "set" function should not be called. Avoid designs that try to set but exit early.
- Reset the heap the next time the user runs a string_udf iff the user sets the value of
STRING_UDF_HEAP_SIZE
to something else during the session.
No. The STRING_UDF_HEAP_SIZE
should not be modifiable (I argued separately that that variable should not exist at all). If the user needs to be able to set a specific size, then the "set" function should be public and the user should be able to call it explicitly with immediate effect (not waiting until the next UDF execution, because memory demands can change in the meantime).
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.
With the changes in af7cd9f I still have heap_size
available as a module level attribute of strings_udf
, what do you think about checking if heap_size == 0
within the getter to determine if the function need be called or not?
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.
You could use a cached function for this instead of a global
that tracks whether the function has already been run. Following ideas from https://mail.python.org/archives/list/[email protected]/thread/5OR3LJO7LOL6SC4OOGKFIVNNH4KADBPG/
from functools import lru_cache
# Only executes the body of the function on the first call
once = lru_cache(maxsize=None)
@once
def set_initial_malloc_heap_size():
strings_udf.set_malloc_heap_size()
def column_to_string_view_array_init_heap(col):
set_initial_malloc_heap_size()
return column_to_string_view_array(col)
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 this worked perfectly!
I realized halfway through my review that my screen hadn't refreshed to include @bdice's latest comments, which largely overlapped with mine. I'll resolve my comments for now so that we just focus on his, I can post again if I have questions about the resolution. |
""" | ||
global heap_size | ||
if size == None: | ||
size = os.environ.get( |
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.
Generally environment variables take effect at load time. I don't think we should allow/encourage the user to modify the environment of the process while it is running in order to change this.
I think you want to define it on import rather than in the body of this function:
_STRINGS_UDF_DEFAULT_HEAP_SIZE = os.environ.get("STRINGS_UDF_HEAP_SIZE", 2**31)
@@ -47,7 +47,14 @@ | |||
utils.JIT_SUPPORTED_TYPES |= STRING_TYPES | |||
_supported_masked_types |= {string_view, udf_string} | |||
|
|||
utils.launch_arg_getters[cudf_str_dtype] = column_to_string_view_array | |||
def column_to_string_view_array_init_heap(col): | |||
# lazily allocate heap only when a string needs to be returned |
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.
You could use a cached function for this instead of a global
that tracks whether the function has already been run. Following ideas from https://mail.python.org/archives/list/[email protected]/thread/5OR3LJO7LOL6SC4OOGKFIVNNH4KADBPG/
from functools import lru_cache
# Only executes the body of the function on the first call
once = lru_cache(maxsize=None)
@once
def set_initial_malloc_heap_size():
strings_udf.set_malloc_heap_size()
def column_to_string_view_array_init_heap(col):
set_initial_malloc_heap_size()
return column_to_string_view_array(col)
rerun tests |
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.
Looks like a function is defined twice. Otherwise LGTM.
Co-authored-by: Bradley Dice <[email protected]>
@gpucibot merge |
Adds a mechanism for setting the default cuda malloc heap size for string UDFs, with 2gb default.