Captured int and float constants are hashed at 32-bit precision, so kernels reuse code built for another value
Bug Description
A kernel that captures a Python int outside the int32 range, or a Python float that differs from another only beyond single precision, can silently run code compiled for a different value of that constant. This happens within one process and across processes through the kernel cache.
ModuleHasher.get_constant_bytes() (warp/_src/context.py) hashes captured constants as bytes(ctypes.c_int(value)) for int and bytes(ctypes.c_float(value)) for float, so only the low 32 bits of an integer and the float32 rounding of a float reach the module hash. Codegen, however, emits the full value: 1 << 33 becomes 8589934592ll and 100_000_001.0 becomes the double literal 100000001.0. Two kernels whose constants agree in those 32 bits get the same hash, and the second one launches the binary built for the first. The same path hashes wp.static() results, so it is affected too.
Minimal reproducer (the documented kernel-closure pattern, CPU device):
import warp as wp
wp.config.log_level = wp.LOG_WARNING
def make_int64_kernel(value):
@wp.kernel
def k(out: wp.array[wp.int64]):
out[0] = wp.int64(value)
return k
def make_float64_kernel(value):
@wp.kernel
def k(out: wp.array[wp.float64]):
out[0] = wp.float64(value)
return k
cases = [
(make_int64_kernel, wp.int64, 5),
(make_int64_kernel, wp.int64, 1 << 32),
(make_int64_kernel, wp.int64, 1 << 33),
(make_int64_kernel, wp.int64, 0xFFFFFFFF),
(make_int64_kernel, wp.int64, -1),
(make_float64_kernel, wp.float64, 100_000_000.0),
(make_float64_kernel, wp.float64, 100_000_001.0),
]
for make, dtype, value in cases:
out = wp.zeros(1, dtype=dtype, device="cpu")
wp.launch(make(value), dim=1, outputs=[out], device="cpu")
got = out.numpy()[0].item()
print(f"captured {value!r:>13} kernel returned {got!r:>13} {'ok' if got == value else 'WRONG'}")
Actual output on main:
captured 5 kernel returned 5 ok
captured 4294967296 kernel returned 4294967296 ok
captured 8589934592 kernel returned 4294967296 WRONG
captured 4294967295 kernel returned 4294967295 ok
captured -1 kernel returned 4294967295 WRONG
captured 100000000.0 kernel returned 100000000.0 ok
captured 100000001.0 kernel returned 100000000.0 WRONG
Expected: every kernel returns the constant it captured. Each WRONG row shares its low 32 bits (or its float32 rounding) with the row above it. There is no error or warning.
The stale binary also persists across runs. A script with a module-level FLAG = 1 << 32 and a kernel computing bits[i] | wp.int64(FLAG) prints 0x100000000; changing it to FLAG = 1 << 33 and running again against the same kernel cache still prints 0x100000000, until the cache is cleared.
This is separate from #2317 and #2327. Those are about functions that never reach the hasher (a wp.func passed to a builtin such as wp.tile_map, and a user overload attached to a builtin). Here the constant is collected and hashed, but encoded at 32-bit width; the reproducer uses no wp.func, tile op or builtin overload, and small constants such as 5 and 7 hash correctly.
I have a fix ready on a branch: hash Python ints by their exact decimal value and Python floats as ctypes.c_double, matching what codegen emits, with a regression test in warp/tests/test_module_hashing.py that fails on main and passes with the change. I will open a pull request that references this issue.
Application / Workflow Impact
I found this through isolated testing of kernel closures on the CPU backend, not while working on a specific project. The pattern it breaks is the documented one in "Kernel Closures, Constants" (code generation guide): kernel factories specialised by a captured value, and module-level constants. Cases that hit it in practice:
- 64-bit integer constants: bit flags above bit 31, masks such as
0xFFFFFFFFnext to-1, 64-bit hash multipliers or seeds, large index offsets. - Double-precision kernels specialised on a large-magnitude or finely-resolved
floatconstant, for example an offset of100_000_001.0next to100_000_000.0, where the values differ only below float32 resolution.
The result is a silently wrong value rather than an error, and editing the constant and re-running does not help because the cached binary is reused.
Warp Version
1.20.0.dev0, built from source at 703efa7e (main) with python build_lib.py --no-cuda
System Information
Python 3.12.13, macOS 27.0.1 (arm64), NumPy 2.5.3. Reproduced on the cpu device. I have no NVIDIA GPU to test with; the hashing code is shared by CPU and CUDA modules, so CUDA is expected to be affected the same way.