feat: add kernel preload
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include "kernel_runtime.hpp"
|
||||
#include "../utils/system.hpp"
|
||||
|
||||
namespace deep_gemm {
|
||||
|
||||
@@ -24,6 +25,48 @@ public:
|
||||
return cache[dir_path] = std::make_shared<KernelRuntime>(dir_path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Preload all cached kernels from disk into memory
|
||||
void preload_all(const std::filesystem::path& cache_dir) {
|
||||
const auto kernel_cache_dir = cache_dir / "cache";
|
||||
|
||||
// If cache directory doesn't exist, return early
|
||||
if (!std::filesystem::exists(kernel_cache_dir))
|
||||
return;
|
||||
|
||||
int loaded_count = 0;
|
||||
int total_count = 0;
|
||||
|
||||
// Iterate through all kernel subdirectories in the cache
|
||||
for (const auto& entry : std::filesystem::directory_iterator(kernel_cache_dir)) {
|
||||
if (!entry.is_directory())
|
||||
continue;
|
||||
|
||||
total_count++;
|
||||
const auto& dir_path = entry.path();
|
||||
|
||||
// Skip if already in memory cache
|
||||
if (cache.find(dir_path) != cache.end())
|
||||
continue;
|
||||
|
||||
// Check validity and load the kernel
|
||||
if (KernelRuntime::check_validity(dir_path)) {
|
||||
try {
|
||||
cache[dir_path] = std::make_shared<KernelRuntime>(dir_path);
|
||||
loaded_count++;
|
||||
} catch (const std::exception& e) {
|
||||
// Skip failed loads
|
||||
if (get_env<int>("DG_JIT_DEBUG"))
|
||||
printf("Warning: Failed to preload kernel from %s: %s\n",
|
||||
dir_path.c_str(), e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (get_env<int>("DG_JIT_DEBUG") || loaded_count > 0)
|
||||
printf("DeepGEMM: Preloaded %d/%d cached kernels from %s\n",
|
||||
loaded_count, total_count, kernel_cache_dir.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
static auto kernel_runtime_cache = std::make_shared<KernelRuntimeCache>();
|
||||
|
||||
@@ -42,6 +42,13 @@ public:
|
||||
Compiler::library_version = get_library_version();
|
||||
}
|
||||
|
||||
static std::filesystem::path get_cache_dir() {
|
||||
std::filesystem::path cache_dir = std::filesystem::path(get_env<std::string>("HOME")) / ".deep_gemm";
|
||||
if (const auto& env_cache_dir = get_env<std::string>("DG_JIT_CACHE_DIR"); !env_cache_dir.empty())
|
||||
cache_dir = env_cache_dir;
|
||||
return cache_dir;
|
||||
}
|
||||
|
||||
std::string signature, flags;
|
||||
std::filesystem::path cache_dir_path;
|
||||
|
||||
|
||||
@@ -137,3 +137,14 @@ _ensure_initialized()
|
||||
|
||||
if __debug__:
|
||||
_verify_ops_loaded()
|
||||
|
||||
# Preload cached kernels if enabled via environment variable
|
||||
if os.environ.get('DG_PRELOAD_KERNELS', '0') == '1':
|
||||
try:
|
||||
deep_gemm_cpp.preload_kernels()
|
||||
except Exception as e:
|
||||
# Preloading failure should not block initialization
|
||||
import warnings
|
||||
warnings.warn(f"Failed to preload DeepGEMM kernels: {e}")
|
||||
|
||||
__version__ = '2.1.2'
|
||||
Reference in New Issue
Block a user