IR Module update_func given an error

Hi All,

I am trying to update an IRModule with a new function, and I find out update_func from here: https://tvm.apache.org/docs/api/python/ir.html#tvm.ir.IRModule.update_func

I have written a simple test, and I am getting an error. So, the problem is either my understanding of udpate_func is wrong OR I am not using update_func properly. Any suggestions?

Here is my example



import tvm
import tvm.relay as relay



def module_update():
        x0 = relay.var('x0', shape=(20, 1))
        y0 = relay.var('y0', shape=(20, 1))
        sub0 = relay.op.subtract(x0, y0)
        f1 = relay.Function([], sub0)
        print(f1)

        mod = tvm.IRModule({"func1": f1})
        print(mod)

        x1 = relay.var('x1', shape=(20, 1))
        y1 = relay.var('y1', shape=(20, 1))
        add1 = relay.op.add(x1, y1)
        f2 = relay.Function([], add1)
        print(f2)

        a = tvm.ir.GlobalVar('func2')

        # update the module
        mod.update_func(a, f2)
        print(mod)


if __name__ == "__main__":
    print("Testing Custom Sequential Pass")
    s =  module_update()


Here is the error

<class 'tvm.relay.function.Function'>
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\pydevd.py", line 1438, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/repos/tvm23/tvm/graph_opt/IRModule/IRModuleUpdate.py", line 35, in <module>
  File "C:/repos/tvm23/tvm/graph_opt/IRModule/IRModuleUpdate.py", line 29, in module_update
    
  File "C:\repos\tvm23\tvm\python\tvm\ir\module.py", line 135, in update_func
    return _ffi_api.Module_UpdateFunction(self, var, func)
  File "C:\repos\tvm23\tvm\python\tvm\_ffi\_ctypes\packed_func.py", line 237, in __call__
    raise get_last_ffi_error()
tvm._ffi.base.TVMError: Traceback (most recent call last):
  File "C:\repos\tvm23\tvm\src\ir\module.cc", line 186
TVMError: 
---------------------------------------------------------------
An internal invariant was violated during the execution of TVM.
Please read TVM's error reporting guidelines.
More details can be found here: https://discuss.tvm.ai/t/error-reporting/7793.
---------------------------------------------------------------
  Check failed: fv.size() == 0 (2 vs. 0) : There are free variables: [Var(x1, ty=TensorType([20, 1], float32)), Var(y1, ty=TensorType([20, 1], float32))]

The error message is saying that the function you passed to the module has free variables (variables that are used without being defined). Namely, f2's body is x1 + y1, but x1 and y1 are local vars without definitions. I think you won’t have this error if you make x1 and y1 args to f2 (e.g., relay.Function([x1, y1], relay.op.add(x1, y1)))

It seems like a bug that your first module did not trigger an error for also having free variables. I think the module initialization does not do the same check for some reason.

Thanks, @slyubomirsky.

That works. BTW, is this a bug in my code or in the TVM?

I meant a possible TVM bug; I should have been more specific. There might be a reason why module initialization does fewer checks than module updates but it certainly seems like unexpected behavior.

1 Like