warp icon indicating copy to clipboard operation
warp copied to clipboard

A kernel declared after its source file changed compiles another function's code

Open ooctipus opened this issue 2 weeks ago • 1 comments

Bug Description

Warp reads a function's source from disk when the kernel is declared, not when the module is imported. If the file changes in between and the function's def is no longer at the line it was compiled from, Warp compiles another function's source under this function's name. No error is raised.

import os
import sys
import tempfile

import warp as wp

BEFORE = """\
import warp as wp

def fill_one(a: wp.array(dtype=float)):
    a[wp.tid()] = 1.0

def fill_two(a: wp.array(dtype=float)):
    a[wp.tid()] = 2.0
"""

# the same two functions in the other order, e.g. after a `git checkout` or an editor save
AFTER = """\
import warp as wp

def fill_two(a: wp.array(dtype=float)):
    a[wp.tid()] = 2.0

def fill_one(a: wp.array(dtype=float)):
    a[wp.tid()] = 1.0
"""

tmp = tempfile.mkdtemp()
path = os.path.join(tmp, "shifting.py")
with open(path, "w") as f:
    f.write(BEFORE)
sys.path.insert(0, tmp)
import shifting  # noqa: E402

with open(path, "w") as f:
    f.write(AFTER)

kernel = wp.kernel(shifting.fill_two)  # declared after the file changed
print(kernel.adj.source.splitlines()[0])  # expected "def fill_two(...)"
a = wp.zeros(4, dtype=float)
wp.launch(kernel, dim=4, inputs=[a])
print(a.numpy())  # expected [2. 2. 2. 2.]

Output:

def fill_one(a: wp.array(dtype=float)):
[1. 1. 1. 1.]

fill_two was compiled at line 6. After the rewrite, line 6 holds def fill_one. Adjoint.extract_function_source in warp/_src/codegen.py rejects its fast slice there (the parsed name is not fill_two) and falls back to inspect.getsourcelines(). The fallback's result is used without checking that it starts at co_firstlineno and defines co_name.

inspect.getsourcelines() reads the file as it is now:

  • On Python 3.11 and 3.12, inspect.findsource walks back from co_firstlineno to the nearest line that looks like a def and doesn't check its name. When the old line falls inside another function's body, the whole enclosing function comes back.
  • On Python 3.13, it returns the block that starts at co_firstlineno, which may be part of another function's body.

Application / Workflow Impact

Silent wrong results: the kernel runs another function's code. This happens in long-running processes that declare kernels lazily (kernel factories, nested functions, wp.kernel(f) at run time) while the source tree changes, for example a test session that is still running when a branch is checked out in the same working tree. Workaround: don't change source files while such a process runs.

Warp Version

1.20.0.dev0 (main at 3480a0c0). The same fallback is in 1.19.0 (code read, not run there).

System Information

Linux, RTX 5090, driver 580.178.04, CUDA toolkit 13.1, Python 3.12.12. Reproduces on CPU too.

ooctipus avatar Nov 19 '26 14:11 ooctipus

reproduced on main (8fd03787) on cpu with python 3.12: the kernel prints def fill_one and fills ones. adding two comment lines at the top of the file instead of swapping the functions hits it too. i have a fix ready: check the inspect.getsourcelines() fallback in extract_function_source() the same way as the fast path (body[0] must be a def named co_name) and raise a RuntimeError saying the file may have changed since import, instead of compiling the other function, with a regression test in test_codegen.py that fails on main. one behaviour change: a function renamed after import now raises instead of compiling the renamed copy. could i take this, or would you rather it look the function up by name in the current file?

burrows99 avatar Nov 24 '26 15:11 burrows99