From 30bc1e0237668a3c3738e78233ee833076463243 Mon Sep 17 00:00:00 2001 From: "jinzhen.ljz" Date: Fri, 9 Oct 2026 10:41:03 +0800 Subject: [PATCH] fix(jit): widen kernel IDs and preserve full cache hashes Signed-off-by: jinzhen.ljz Co-authored-by: Codex (GPT-6) --- humming/csrc/launcher/launcher.cpp | 3 +-- humming/csrc/launcher/process_input.h | 3 +-- humming/csrc/launcher/utils.h | 38 ++++++++++++++++----------- humming/utils/jit.py | 2 +- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/humming/csrc/launcher/launcher.cpp b/humming/csrc/launcher/launcher.cpp index 18b685a6..3d7b3991 100644 --- a/humming/csrc/launcher/launcher.cpp +++ b/humming/csrc/launcher/launcher.cpp @@ -339,8 +339,7 @@ std::tuple register_kernel(const std::string &cubin_path) auto path_it = g_path_ids.find(cubin_path); if (path_it != g_path_ids.end()) return path_it->second; - int64_t hash_id = manual_crc32(cubin_path); - hash_id = (hash_id << 30) + manual_crc32(kernel_name); + int64_t hash_id = get_kernel_registration_id(cubin_path, kernel_name); auto kernel_it = g_registered_kernels.find(hash_id); bool no_collision = kernel_it == g_registered_kernels.end() || kernel_it->second.cubin_path == cubin_path; ASSERT_CHECK(no_collision, "kernel id collision for ", cubin_path); diff --git a/humming/csrc/launcher/process_input.h b/humming/csrc/launcher/process_input.h index ddca18f3..fc47b6f1 100644 --- a/humming/csrc/launcher/process_input.h +++ b/humming/csrc/launcher/process_input.h @@ -152,8 +152,7 @@ inline std::tuple register_process_input_kernel(const std: } ASSERT_CHECK(!kernel_name.empty(), "no process-input kernel found in ", cubin_path); - int64_t kernel_id = manual_crc32(cubin_path); - kernel_id = (kernel_id << 30) + manual_crc32(kernel_name); + int64_t kernel_id = get_kernel_registration_id(cubin_path, kernel_name); ProcessInputKernelData metadata = { reader.getUint32("NUM_THREADS"), reader.getUint32("SOURCE_DTYPE_ID"), diff --git a/humming/csrc/launcher/utils.h b/humming/csrc/launcher/utils.h index ccec21b9..b3907ea6 100644 --- a/humming/csrc/launcher/utils.h +++ b/humming/csrc/launcher/utils.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -69,27 +71,33 @@ inline CUstream get_current_cuda_stream(int64_t dev) { #endif } -uint32_t manual_crc32(const std::string &data) { - static uint32_t table[256]; - static bool table_computed = false; - - if (!table_computed) { - for (uint32_t i = 0; i < 256; i++) { - uint32_t c = i; +uint64_t manual_crc64(const std::string &data) { + // CRC-64/ECMA-182: non-reflected, with zero initial value and final XOR. + static const auto table = [] { + std::array values{}; + for (uint64_t i = 0; i < values.size(); i++) { + uint64_t crc = i << 56; for (int j = 0; j < 8; j++) { - if (c & 1) c = 0xEDB88320L ^ (c >> 1); - else c = c >> 1; + if (crc & (1ULL << 63)) crc = (crc << 1) ^ 0x42F0E1EBA9EA3693ULL; + else crc <<= 1; } - table[i] = c; + values[i] = crc; } - table_computed = true; - } + return values; + }(); - uint32_t crc = 0xFFFFFFFFL; + uint64_t crc = 0; for (unsigned char b : data) { - crc = table[(crc ^ b) & 0xFF] ^ (crc >> 8); + crc = table[(crc >> 56) ^ b] ^ (crc << 8); } - return crc ^ 0xFFFFFFFFL; + return crc; +} + +int64_t get_kernel_registration_id(const std::string &cubin_path, const std::string &kernel_name) { + std::string key = cubin_path; + key.push_back('\0'); + key.append(kernel_name); + return static_cast(manual_crc64(key) & 0x7FFFFFFFFFFFFFFFULL); } uint32_t get_dtype_num_bits(uint32_t dtype_id) { diff --git a/humming/utils/jit.py b/humming/utils/jit.py index 45849bdd..bdee5253 100644 --- a/humming/utils/jit.py +++ b/humming/utils/jit.py @@ -63,7 +63,7 @@ def popen_and_reap(cmd, **kwargs): def hash_to_hex(s: str) -> str: md5 = hashlib.md5() md5.update(s.encode("utf-8")) - return md5.hexdigest()[0:16] + return md5.hexdigest() @functools.lru_cache(maxsize=1)