NVIDIA / warp

A Python framework for high performance GPU simulation and graphics
https://nvidia.github.io/warp/
Other
4.23k stars 240 forks source link

[BUG] error when reassigning loop variables #331

Open g1n0st opened 1 week ago

g1n0st commented 1 week ago

Bug Description

When I re-assign a local variable from a constant loop like for j in range(3): to the loop that range is a non-constant value like for j in range(dim), the warp compiler will raise an error from 1.4.0. Here is the minimal reproducible code:

import warp as wp

wp.init()

@wp.kernel
def test():
    V = wp.mat33d()
    for i in range(3):
        for j in range(3):
            V[i, j] = wp.float64(0.0)

    dim = 2
    for i in range(dim+1):
        for j in range(dim+1):
            V[i, j] = wp.float64(1.0)

    print(V)

wp.launch(test, dim=1)

wp.synchronize()

Result:

Traceback (most recent call last):
  File "/home/chang/bug.py", line 19, in <module>
    wp.launch(test, dim=1)
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/context.py", line 5003, in launch
    module_exec = kernel.module.load(device)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/context.py", line 1949, in load
    builder = ModuleBuilder(self, self.options, hasher=self.hasher)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/context.py", line 1549, in __init__
    self.build_kernel(kernel)
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/context.py", line 1578, in build_kernel
    kernel.adj.build(self)
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 970, in build
    raise e
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 960, in build
    adj.eval(adj.tree.body[0])
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 2628, in eval
    return emit_node(adj, node)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 1548, in emit_FunctionDef
    adj.eval(f)
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 2628, in eval
    return emit_node(adj, node)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 2057, in emit_For
    adj.materialize_redefinitions(adj.loop_symbols[-1])
  File "/home/chang/miniconda3/lib/python3.12/site-packages/warp/codegen.py", line 1868, in materialize_redefinitions
    raise WarpCodegenError(
warp.codegen.WarpCodegenError: Error while parsing function "test" at /home/chang/bug.py:15:
            V[i, j] = wp.float64(1.0)
;Error mutating a constant j inside a dynamic loop, use the following syntax: pi = float(3.141) to declare a dynamic variable

System Information

Warp 1.4.0 initialized: CUDA Toolkit 12.6, Driver 12.2

eric-heiden commented 1 week ago

Hi @g1n0st,

Thank you for reporting this bug! We will have a fix for this in the next release.