Commit 001e7b2a authored by You Jiacheng's avatar You Jiacheng Committed by LeiWang1999
Browse files

[Refactor] Move compilation outside critical section (#260)



* move compilation outside critical section

* lint fix

---------
Co-authored-by: default avatarLeiWang1999 <leiwang1999@outlook.com>
parent f47b43c5
...@@ -38,7 +38,6 @@ class KernelCache: ...@@ -38,7 +38,6 @@ class KernelCache:
with cls._lock: with cls._lock:
if cls._instance is None: if cls._instance is None:
cls._instance = super(KernelCache, cls).__new__(cls) cls._instance = super(KernelCache, cls).__new__(cls)
cls._instance._cache = {} # In-memory cache
cls._instance.cache_dir = cache_dir # Cache directory cls._instance.cache_dir = cache_dir # Cache directory
os.makedirs(cls._instance.cache_dir, exist_ok=True) # Ensure cache directory exists os.makedirs(cls._instance.cache_dir, exist_ok=True) # Ensure cache directory exists
cls._instance.logger = logging.getLogger(__name__) # Initialize logger cls._instance.logger = logging.getLogger(__name__) # Initialize logger
...@@ -91,40 +90,38 @@ class KernelCache: ...@@ -91,40 +90,38 @@ class KernelCache:
JITKernel: The compiled kernel, either freshly compiled or from cache JITKernel: The compiled kernel, either freshly compiled or from cache
""" """
key = self._generate_key(func, out_idx, execution_backend, args, target, target_host) key = self._generate_key(func, out_idx, execution_backend, args, target, target_host)
with self._lock: # Thread-safe access to cache with self._lock: # TODO: use filelock
if key in self._cache:
return self._cache[key]
# Attempt to load from disk # Attempt to load from disk
kernel = self._load_kernel_from_disk(key, target, target_host, out_idx, kernel = self._load_kernel_from_disk(key, target, target_host, out_idx,
execution_backend, pass_configs, func) execution_backend, pass_configs, func)
if kernel: if kernel is not None:
self._cache[key] = kernel # Load to in-memory cache
return kernel return kernel
# Compile kernel if cache miss # Compile kernel if cache miss; leave critical section
kernel = JITKernel( kernel = JITKernel(
func, func,
out_idx=out_idx, out_idx=out_idx,
execution_backend=execution_backend, execution_backend=execution_backend,
target=target, target=target,
target_host=target_host, target_host=target_host,
verbose=verbose, verbose=verbose,
pass_configs=pass_configs, pass_configs=pass_configs,
) )
self._cache[key] = kernel # Store in in-memory cache if execution_backend == "dlpack":
if execution_backend == "dlpack": self.logger.warning("DLPack backend does not support cache saving to disk.")
self.logger.warning("DLPack backend does not support cache saving to disk.") else:
else: with self._lock: # enter critical section again to check and update disk cache
self._save_kernel_to_disk(key, kernel, func) disk_kernel = self._load_kernel_from_disk(key, target, target_host, out_idx,
return kernel execution_backend, pass_configs, func)
if disk_kernel is None:
self._save_kernel_to_disk(key, kernel, func)
return kernel
def clear_cache(self): def clear_cache(self):
""" """
Clears the entire kernel cache, including both in-memory and disk cache. Clears the entire kernel cache, including both in-memory and disk cache.
""" """
with self._lock: # Thread-safe operation with self._lock:
self._cache.clear() # Clear in-memory cache
self._clear_disk_cache() # Clear disk cache self._clear_disk_cache() # Clear disk cache
def _get_cache_path(self, key: str) -> str: def _get_cache_path(self, key: str) -> str:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment