-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compile_udf: Cache PTX for similar functions (#7371)
Compiling a UDF generated in a loop will result in a distinct compilation for each loop iteration, because each new definition of the UDF does not compare equal to any previous definition, and a new compilation occurs. Furthermore, each new compilation returns PTX that differs only in a trivial way (the generated code is the same but function names are different), so JITify's cache also misses. For example: ```python for data_size in range(3): data = Series([3] * (2 ** data_size), dtype=np.float64) for i in range(3): data.applymap(lambda x: x + 1) ``` results in nine compilations when one would have sufficed. This commit adds an additional cache to `compile_udf` keyed on the signature, code, and closure variables of the function. This can hit for distinct definitions of the same function. The existing `lru_cache` wrapping `compile_udf` is left in place as it is expected to be able to hash the function much more quickly, though I don't know if this has a noticeable impact on performance - perhaps it would be worth removing it for simplicity, so that there is only one level of caching. Authors: - Graham Markall (@gmarkall) Approvers: - Keith Kraus (@kkraus14) - AJ Schmidt (@ajschmidt8) URL: #7371
- Loading branch information
Showing
6 changed files
with
102 additions
and
4 deletions.
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
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
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,66 @@ | ||
# Copyright (c) 2021, NVIDIA CORPORATION. | ||
|
||
from cudf.utils import cudautils | ||
from numba import types | ||
|
||
|
||
def setup_function(): | ||
cudautils._udf_code_cache.clear() | ||
|
||
|
||
def assert_cache_size(size): | ||
assert cudautils._udf_code_cache.currsize == size | ||
|
||
|
||
def test_first_compile_sets_cache_entry(): | ||
# The first compilation should put an entry in the cache | ||
cudautils.compile_udf(lambda x: x + 1, (types.float32,)) | ||
assert_cache_size(1) | ||
|
||
|
||
def test_code_cache_same_code_different_function_hit(): | ||
# Compilation of a distinct function with the same code and signature | ||
# should reuse the cached entry | ||
|
||
cudautils.compile_udf(lambda x: x + 1, (types.float32,)) | ||
assert_cache_size(1) | ||
|
||
cudautils.compile_udf(lambda x: x + 1, (types.float32,)) | ||
assert_cache_size(1) | ||
|
||
|
||
def test_code_cache_different_types_miss(): | ||
# Compilation of a distinct function with the same code but different types | ||
# should create an additional cache entry | ||
|
||
cudautils.compile_udf(lambda x: x + 1, (types.float32,)) | ||
assert_cache_size(1) | ||
|
||
cudautils.compile_udf(lambda x: x + 1, (types.float64,)) | ||
assert_cache_size(2) | ||
|
||
|
||
def test_code_cache_different_cvars_miss(): | ||
# Compilation of a distinct function with the same types and code as an | ||
# existing entry but different closure variables should create an | ||
# additional cache entry | ||
|
||
def gen_closure(y): | ||
return lambda x: x + y | ||
|
||
cudautils.compile_udf(gen_closure(1), (types.float32,)) | ||
assert_cache_size(1) | ||
|
||
cudautils.compile_udf(gen_closure(2), (types.float32,)) | ||
assert_cache_size(2) | ||
|
||
|
||
def test_lambda_in_loop_code_cached(): | ||
# Compiling a UDF defined in a loop should result in the code cache being | ||
# reused for each loop iteration after the first. We check for this by | ||
# ensuring that there is only one entry in the code cache after the loop. | ||
|
||
for i in range(3): | ||
cudautils.compile_udf(lambda x: x + 1, (types.float32,)) | ||
|
||
assert_cache_size(1) |
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