warp icon indicating copy to clipboard operation
warp copied to clipboard

Functions passed to wp.tile_map are not hashed: kernels differing only in a captured constant reuse stale code

Open adenzler-nvidia opened this issue 2 weeks ago • 1 comments

Bug Description

A user wp.func passed as an argument to a builtin such as wp.tile_map is not part of the kernel hash. When the function captures a Python constant, two kernels that differ only in that constant hash the same, and the second one silently runs the code compiled for the first. This happens within one process and across processes through the kernel cache.

import warp as wp


def make_tile_map_kernel(k: int):
    @wp.func
    def add_k(x: int) -> int:
        return x + k

    @wp.kernel(module="unique")
    def kernel(a: wp.array[int], out: wp.array[int]):
        wp.tile_store(out, wp.tile_map(add_k, wp.tile_load(a, shape=4)))

    return kernel


def make_call_kernel(k: int):
    @wp.func
    def add_k(x: int) -> int:
        return x + k

    @wp.kernel(module="unique")
    def kernel(a: wp.array[int], out: wp.array[int]):
        i = wp.tid()
        out[i] = add_k(a[i])

    return kernel


a = wp.array([0, 1, 2, 3], dtype=int)
for k in (5, 7):
    out = wp.zeros(4, dtype=int)
    wp.launch(make_call_kernel(k), dim=4, inputs=[a], outputs=[out])
    print(f"direct call, k={k}: {out.numpy()}  expected {[i + k for i in range(4)]}")
for k in (5, 7):
    out = wp.zeros(4, dtype=int)
    wp.launch_tiled(make_tile_map_kernel(k), dim=1, inputs=[a], outputs=[out], block_dim=32)
    print(f"tile_map,    k={k}: {out.numpy()}  expected {[i + k for i in range(4)]}")

Output with an empty kernel cache:

direct call, k=5: [5 6 7 8]  expected [5, 6, 7, 8]
direct call, k=7: [ 7  8  9 10]  expected [7, 8, 9, 10]
tile_map,    k=5: [5 6 7 8]  expected [5, 6, 7, 8]
tile_map,    k=7: [5 6 7 8]  expected [7, 8, 9, 10]

The direct call is hashed correctly; the tile_map kernel for k=7 reuses the code for k=5. Running the tile_map part in two processes with k=5 and then k=7 against the same WARP_CACHE_PATH gives the same wrong result for the second process.

The cause seems to be in Adjoint.get_references() (warp/_src/codegen.py): user functions passed as arguments are added to the referenced functions only when the callee is itself a user function, through iter_call_callable_arg_targets(), which returns immediately for builtin callees. So the function passed to wp.tile_map (or any other builtin that takes a function argument) never reaches ModuleHasher.hash_function(), and its captured constants are not hashed. Module-level functions without captures are unaffected in practice, since their source is the same either way.

Application / Workflow Impact

Silent wrong results. In MuJoCo Warp a kernel factory mapped a nested wp.func that captured a size over a tile with wp.tile_map; a process that had compiled the kernel for one model reused it for another, which left part of the solver's output unwritten. We worked around it by passing the value as a wp.tile_full tile to a module-level function.

Warp Version

1.18.0 and 1.19.0; the code path is unchanged on main (b19c734879).

System Information

Linux, Python 3.13.7, NVIDIA RTX PRO 6000 Blackwell Workstation Edition, driver 615.71.09.

adenzler-nvidia avatar Nov 13 '26 10:11 adenzler-nvidia

reproduced on main (8fd03787) on the cpu device with the script above: the tile_map kernel for k=7 returns [5 6 7 8], and wp.tile_reduce has the same problem. the cause is as you describe: iter_call_callable_arg_targets() returns early for builtin callees, so a function passed to a builtin's Callable parameter never reaches the hasher. i have a fix ready: collect those function arguments for builtin calls too (in get_references() and the module dependency scan), with a regression test in test_module_hashing.py that fails on main. i hit this while working on the captured-constant hashing fix in #2339. could i take this?

burrows99 avatar Nov 24 '26 15:11 burrows99