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

Adding cache and specialization to func decorator #877

Merged
merged 4 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions numba_dpex/core/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,18 @@ class LRUCache(AbstractCache):
with a dictionary as a lookup table.
"""

def __init__(self, capacity=10, pyfunc=None):
def __init__(self, name="cache", capacity=10, pyfunc=None):
"""Constructor for LRUCache.

Args:
name (str, optional): The name of the cache, useful for
debugging.
capacity (int, optional): The max capacity of the cache.
Defaults to 10.
pyfunc (NoneType, optional): A python function to be cached.
Defaults to None.
"""
self._name = name
self._capacity = capacity
self._lookup = {}
self._evicted = {}
Expand Down Expand Up @@ -432,8 +435,8 @@ def get(self, key):
value = self._cache_file.load(key)
if config.DEBUG_CACHE:
print(
"[cache]: unpickled an evicted artifact, "
"key: {0:s}.".format(str(key))
"[{0:s}]: unpickled an evicted artifact, "
"key: {1:s}.".format(self._name, str(key))
)
else:
value = self._evicted[key]
Expand All @@ -442,8 +445,8 @@ def get(self, key):
else:
if config.DEBUG_CACHE:
print(
"[cache] size: {0:d}, loading artifact, key: {1:s}".format(
len(self._lookup), str(key)
"[{0:s}] size: {1:d}, loading artifact, key: {2:s}".format(
self._name, len(self._lookup), str(key)
)
)
node = self._lookup[key]
Expand All @@ -464,8 +467,8 @@ def put(self, key, value):
if key in self._lookup:
if config.DEBUG_CACHE:
print(
"[cache] size: {0:d}, storing artifact, key: {1:s}".format(
len(self._lookup), str(key)
"[{0:s}] size: {1:d}, storing artifact, key: {2:s}".format(
self._name, len(self._lookup), str(key)
)
)
self._lookup[key].value = value
Expand All @@ -480,8 +483,9 @@ def put(self, key, value):
if self._cache_file:
if config.DEBUG_CACHE:
print(
"[cache] size: {0:d}, pickling the LRU item, "
"key: {1:s}, indexed at {2:s}.".format(
"[{0:s}] size: {1:d}, pickling the LRU item, "
"key: {2:s}, indexed at {3:s}.".format(
self._name,
len(self._lookup),
str(self._head.key),
self._cache_file._index_path,
Expand All @@ -496,8 +500,8 @@ def put(self, key, value):
self._lookup.pop(self._head.key)
if config.DEBUG_CACHE:
print(
"[cache] size: {0:d}, capacity exceeded, evicted".format(
len(self._lookup)
"[{0:s}] size: {1:d}, capacity exceeded, evicted".format(
self._name, len(self._lookup)
),
self._head.key,
)
Expand All @@ -509,7 +513,7 @@ def put(self, key, value):
self._append_tail(new_node)
if config.DEBUG_CACHE:
print(
"[cache] size: {0:d}, saved artifact, key: {1:s}".format(
len(self._lookup), str(key)
"[{0:s}] size: {1:d}, saved artifact, key: {2:s}".format(
self._name, len(self._lookup), str(key)
)
)
4 changes: 2 additions & 2 deletions numba_dpex/core/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def compile_with_dpex(
return_type,
target_context,
typing_context,
debug=None,
debug=False,
is_kernel=True,
extra_compile_flags=None,
):
Expand Down Expand Up @@ -256,7 +256,7 @@ def compile_with_dpex(
flags.no_cpython_wrapper = True
flags.nrt = False

if debug is not None:
if debug:
flags.debuginfo = debug

# Run compilation pipeline
Expand Down
8 changes: 6 additions & 2 deletions numba_dpex/core/kernel_interface/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def __init__(
self._cache = NullCache()
elif enable_cache:
self._cache = LRUCache(
capacity=config.CACHE_SIZE, pyfunc=self.pyfunc
name="SPIRVKernelCache",
capacity=config.CACHE_SIZE,
pyfunc=self.pyfunc,
)
else:
self._cache = NullCache()
Expand Down Expand Up @@ -118,7 +120,9 @@ def __init__(
if specialization_sigs:
self._has_specializations = True
self._specialization_cache = LRUCache(
capacity=config.CACHE_SIZE, pyfunc=self.pyfunc
name="SPIRVKernelSpecializationCache",
capacity=config.CACHE_SIZE,
pyfunc=self.pyfunc,
)
for sig in specialization_sigs:
self._specialize(sig)
Expand Down
Loading