-
Notifications
You must be signed in to change notification settings - Fork 32
Dispatcher/caching rewrite to address performance regression #912
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
Merged
diptorupd
merged 2 commits into
IntelPython:main
from
adarshyoga:kmeans_perf_improvements
Feb 17, 2023
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
from numba.core.types import void | ||
|
||
from numba_dpex import NdRange, Range, config | ||
from numba_dpex.core.caching import LRUCache, NullCache, build_key | ||
from numba_dpex.core.caching import LRUCache, NullCache | ||
from numba_dpex.core.descriptor import dpex_kernel_target | ||
from numba_dpex.core.exceptions import ( | ||
ComputeFollowsDataInferenceError, | ||
|
@@ -34,6 +34,11 @@ | |
from numba_dpex.core.kernel_interface.arg_pack_unpacker import Packer | ||
from numba_dpex.core.kernel_interface.spirv_kernel import SpirvKernel | ||
from numba_dpex.core.types import USMNdArray | ||
from numba_dpex.core.utils import ( | ||
build_key, | ||
create_func_hash, | ||
strip_usm_metadata, | ||
) | ||
|
||
|
||
def get_ordered_arg_access_types(pyfunc, access_types): | ||
|
@@ -85,6 +90,8 @@ def __init__( | |
self._global_range = None | ||
self._local_range = None | ||
|
||
self._func_hash = create_func_hash(pyfunc) | ||
|
||
# caching related attributes | ||
if not config.ENABLE_CACHE: | ||
self._cache = NullCache() | ||
|
@@ -151,7 +158,7 @@ def cache(self): | |
def cache_hits(self): | ||
return self._cache_hits | ||
|
||
def _compile_and_cache(self, argtypes, cache): | ||
def _compile_and_cache(self, argtypes, cache, key=None): | ||
"""Helper function to compile the Python function or Numba FunctionIR | ||
object passed to a JitKernel and store it in an internal cache. | ||
""" | ||
|
@@ -171,11 +178,13 @@ def _compile_and_cache(self, argtypes, cache): | |
device_driver_ir_module = kernel.device_driver_ir_module | ||
kernel_module_name = kernel.module_name | ||
|
||
key = build_key( | ||
tuple(argtypes), | ||
self.pyfunc, | ||
kernel.target_context.codegen(), | ||
) | ||
if not key: | ||
stripped_argtypes = strip_usm_metadata(argtypes) | ||
codegen_magic_tuple = kernel.target_context.codegen().magic_tuple() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I compared with 0.19.0, even this extra hashing on the codegen_magic_tuple may be adding an overhead. Can you try removing this and just adding the kernel to the key like we had in 0.19 |
||
key = build_key( | ||
stripped_argtypes, codegen_magic_tuple, self._func_hash | ||
) | ||
|
||
cache.put(key, (device_driver_ir_module, kernel_module_name)) | ||
|
||
return device_driver_ir_module, kernel_module_name | ||
|
@@ -604,12 +613,12 @@ def __call__(self, *args): | |
self.kernel_name, backend, JitKernel._supported_backends | ||
) | ||
|
||
# load the kernel from cache | ||
key = build_key( | ||
tuple(argtypes), | ||
self.pyfunc, | ||
dpex_kernel_target.target_context.codegen(), | ||
# Generate key used for cache lookup | ||
stripped_argtypes = strip_usm_metadata(argtypes) | ||
codegen_magic_tuple = ( | ||
dpex_kernel_target.target_context.codegen().magic_tuple() | ||
) | ||
key = build_key(stripped_argtypes, codegen_magic_tuple, self._func_hash) | ||
|
||
# If the JitKernel was specialized then raise exception if argtypes | ||
# do not match one of the specialized versions. | ||
|
@@ -630,15 +639,11 @@ def __call__(self, *args): | |
device_driver_ir_module, | ||
kernel_module_name, | ||
) = self._compile_and_cache( | ||
argtypes=argtypes, | ||
cache=self._cache, | ||
argtypes=argtypes, cache=self._cache, key=key | ||
) | ||
|
||
kernel_bundle_key = build_key( | ||
tuple(argtypes), | ||
self.pyfunc, | ||
dpex_kernel_target.target_context.codegen(), | ||
exec_queue=exec_queue, | ||
stripped_argtypes, codegen_magic_tuple, exec_queue, self._func_hash | ||
) | ||
|
||
artifact = self._kernel_bundle_cache.get(kernel_bundle_key) | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
# SPDX-FileCopyrightText: 2020 - 2022 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import hashlib | ||
|
||
from numba.core.serialize import dumps | ||
|
||
from numba_dpex.core.types import USMNdArray | ||
|
||
|
||
def build_key(*args): | ||
"""Constructs key from variable list of args | ||
|
||
Args: | ||
*args: List of components to construct key | ||
Return: | ||
Tuple of args | ||
""" | ||
return tuple(args) | ||
|
||
|
||
def create_func_hash(pyfunc): | ||
"""Creates a tuple of sha256 hashes out of code and | ||
variable bytes extracted from the compiled funtion. | ||
|
||
Args: | ||
pyfunc: Python function object | ||
Return: | ||
Tuple of hashes of code and variable bytes | ||
""" | ||
codebytes = pyfunc.__code__.co_code | ||
if pyfunc.__closure__ is not None: | ||
try: | ||
cvars = tuple([x.cell_contents for x in pyfunc.__closure__]) | ||
# Note: cloudpickle serializes a function differently depending | ||
# on how the process is launched; e.g. multiprocessing.Process | ||
cvarbytes = dumps(cvars) | ||
except: | ||
cvarbytes = b"" # a temporary solution for function template | ||
else: | ||
cvarbytes = b"" | ||
|
||
return ( | ||
hashlib.sha256(codebytes).hexdigest(), | ||
hashlib.sha256(cvarbytes).hexdigest(), | ||
) | ||
|
||
|
||
def strip_usm_metadata(argtypes): | ||
"""Convert the USMNdArray to an abridged type that disregards the | ||
usm_type, device, queue, address space attributes. | ||
|
||
Args: | ||
argtypes: List of types | ||
|
||
Return: | ||
Tuple of types after removing USM metadata from USMNdArray type | ||
""" | ||
|
||
stripped_argtypes = [] | ||
for argty in argtypes: | ||
if isinstance(argty, USMNdArray): | ||
stripped_argtypes.append((argty.ndim, argty.dtype, argty.layout)) | ||
else: | ||
stripped_argtypes.append(argty) | ||
|
||
return tuple(stripped_argtypes) |
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.
Uh oh!
There was an error while loading. Please reload this page.